zoukankan      html  css  js  c++  java
  • spring boot 2.0 Feign的客户端

    1.pom.xml

    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
                <version>2.0.2.RELEASE</version>
            </dependency>

    2.UserConsumerDemoApplication.java

    @EnableFeignClients

    3.UserClient.java

    package cn.itcast.user.client;
    
    import cn.itcast.user.pojo.User;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    
    @FeignClient("user-service")
    public interface UserClient {
        @GetMapping("{id}")
        User getUserQueryInfo(@PathVariable("id") Long id);
    }

    4.UserFController.java

    package cn.itcast.user.controller;
    
    import cn.itcast.user.client.UserClient;
    import cn.itcast.user.pojo.User;
    import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    @RequestMapping("consumerF")
    @DefaultProperties(defaultFallback = "queryUserByIdFallback")
    public class UserFController {
        @Autowired
        private UserClient userClient;
    
        @GetMapping("{id}")
        public User queryUserById(@PathVariable("id") Long id){
            return userClient.getUserQueryInfo(id);
        }
    
        public String queryUserByIdFallback(){
            return "用户信息查询出现异常!";
        }
    }
  • 相关阅读:
    题目3:爬楼梯
    题目1:删除排序数组中的重复数字
    最近目标
    软件工程----个人总结
    软件工程第二次作业——结对编程
    软件工程第一次作业补充
    爬楼梯
    买卖股票的最佳时机
    删除排序数组中的重复数字
    思考题
  • 原文地址:https://www.cnblogs.com/wuxiang12580/p/10787406.html
Copyright © 2011-2022 走看看