spring boot 2.0 Feign的客户端

2019-04-29 08:51:11来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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 "用户信息查询出现异常!";
    }
}

 


原文链接:https://www.cnblogs.com/wuxiang12580/p/10787406.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Spring Boot 2.X 如何快速集成单元测试?

下一篇:Hystix熔断解决雪崩问题