学习笔记:微服务12 spring cloud Feign(Rest请求)+ hystrix(熔断)

Feign在RestTemplate的基础上对其封装,由它来帮助我们定义和实现依赖服务接口的定义。Spring Cloud Feign 基于Netflix Feign 实现的,整合了Spring Cloud Ribbon 与 Spring Cloud Hystrix,并且实现了声明式的Web服务客户端定义方式。

我的理解是Feign是一个接口,是发起rest请求的工具,它集成了ribbon负载均衡和hystrix的错误熔断机制,通过rest的方式得到类似rpc远程调用的服务,也就是根据用户(客户)的url路径参数要求,去申请其他微服务的资源(多个服务可以负载均衡比如轮询),如果某个微服务宕机,可以通过hystric熔断(躲开),并返回回退函数的值给客户。

1. 创建微服务spring start project项目,命名为microservice-Feign-Hystrix-8804

2.pom.xml  依赖

      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

3.application.properties

server.port: 8804
spring.application.name=MicroserviceFeignHystrix8804
spring.cloud.discovery.enabled=true
eureka.client.serviceUrl.defaultZone=http://admin:123@centos7:8101/eureka/,http://admin:123@microservice1:8102/eureka/
feign.hystrix.enabled=true

4. 启动类增加注解

@EnableDiscoveryClient
@EnableFeignClients
@EnableEurekaClient
@EnableCircuitBreaker
@SpringBootApplication

5. 声明@FeignClient接口


 
 
  1. // 作为消费者轮询eureka中名字为MicroserviceServerA8801的微服务,如果超时,回退执行HelloClientFallback.class
  2. @FeignClient(name = "MicroserviceServerA8801", fallback = HelloClientFallback.class)
  3. public interface HelloClient {
  4. //定义声明接口
  5. @RequestMapping(method = RequestMethod.GET, value = "/hello")
  6. public String hello();
  7. @RequestMapping(method = RequestMethod.GET, value = "/toHello")
  8. public String toHello();
  9. @RequestMapping(method = RequestMethod.GET, value = "/listusers")
  10. public String listusers();
  11. }

6. 定义回退类HelloClientFallback


 
 
  1. @Component
  2. public class HelloClientFallback implements HelloClient {
  3. //这是回退类,超时就执行这个得到返回值
  4. public String hello() {
  5. return "fallback hello";
  6. }
  7. public String toHello() {
  8. return "fallback timeout hello";
  9. }
  10. public String listusers() {
  11. return "fallback listusers";
  12. }
  13. }

7. 定义控制类FeignController  对接口声明的实现


 
 
  1. @RestController
  2. public class FeignController {
  3. @Autowired
  4. private HelloClient helloClient;
  5. //对声明接口方法的具体实现
  6. @RequestMapping(method = RequestMethod.GET, value = "/hello")
  7. public String hello() {
  8. return helloClient.hello();
  9. }
  10. @RequestMapping(method = RequestMethod.GET, value = "/listusers")
  11. public String listusers() {
  12. return helloClient.listusers();
  13. // helloclient 是一个restemplate的请求,就是去发起连接请求获取返回,不能获取就执行回退程序
  14. }
  15. @RequestMapping(method = RequestMethod.GET, value = "/toHello")
  16. public String toHello() throws InterruptedException {
  17. Thread.sleep( 500);
  18. String result = helloClient.toHello();
  19. HystrixCircuitBreaker breaker = HystrixCircuitBreaker.Factory
  20. .getInstance(HystrixCommandKey.Factory
  21. .asKey( "HelloClient#toHello()"));
  22. System. out.println( "断路器状态:" + breaker.isOpen());
  23. return result;
  24. }
  25. }

8,测试,先启动eureka服务,再启动微服务生产者MicroserviceServerA8801 我这启动两次,分别在8801,8802端口提供相同服务,最后启动本测试微服务在端口8804

可见访问同一网址多次,会负载均衡轮询多个MicroserviceServerA8801同名服务(8801,8802端口),当轮询的服务中断时,返回回退信息“fallback listusers”。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值