zoukankan      html  css  js  c++  java
  • Spring Cloud 之 Ribbon集成Hystrix基本使用(九)

    Spring Cloud Hystrix 是基于 Netflix 的开源框架 Hystrix 实现的,它具有断路器、线路隔离等一系列服务保护功能。Hystrix 具备服务降级、服务熔断、请求缓存、请求合并以及服务监控等强大功能。

    Hystrix工作原理(官方文档翻译)

    1、本文主要讲Hystrix和Ribbon的基本集成使用,还是老样子,先把Eureka集群和服务提供者(x-demo-service)集群起来

    2、x-demo-service-ribbon服务消费者改造

    因为Hystrix是为了保护服务端,熔断客户端,相当于服务端和客户端之间的保险丝,所以我们只需要改造x-demo-service-ribbon即可。

    build.gradle增加Hystrix依赖

    1 dependencies {
    2     compile("org.springframework.cloud:spring-cloud-starter-netflix-ribbon")
    3     compile("org.springframework.cloud:spring-cloud-starter-netflix-hystrix")
    4 }

     3、Controller改造

    在service方法上加上@HystrixCommand注解,fallbackMethod指向回调方法。注意第12行和22行方法名要一致。

     1 @RestController
     2 @RequestMapping("ribbon")
     3 @Slf4j
     4 public class RibbonDemoController {
     5 
     6     @Autowired
     7     RestTemplate restTemplate;
     8 
     9     @Autowired
    10     LoadBalancerClient loadBalancerClient;
    11 
    12     @HystrixCommand(fallbackMethod = "ribbonFallback")
    13     @RequestMapping("service")
    14     public String service() {
    15         log.info("[ribbon]谁在撩我?");
    16 
    17         //随机访问策略
    18         this.loadBalancerClient.choose("x-demo-service");
    19         return restTemplate.getForObject("http://X-DEMO-SERVICE/demo/service", String.class);
    20     }
    21 
    22     private String ribbonFallback() {
    23         return "我是Ribbon,服务端大概率挂了!";
    24     }
    25 }

    4、启动类改造

    启动类上只需要加上@EnableHystrix注解,启动Hystrix。

     1 /**
     2  * @author Leo
     3  */
     4 @SpringBootApplication
     5 @EnableEurekaClient 7 @EnableHystrix
     8 public class RibbonServerApplication {
     9 
    10     public static void main(String[] args) {
    11         SpringApplication.run(RibbonServerApplication.class, args);
    12     }
    13 
    14     @Bean
    15     @LoadBalanced
    16     RestTemplate restTemplate() {
    17         return new RestTemplate();
    18     }
    19 
    20     @Bean
    21     public IRule ribbonRule() {
    22         return new RandomRule();
    23     }
    24 }

    网上有些帖子在启动类上既加了@EnableHystrix注解,又加了@EnableCircuitBreaker注解,其实大可不必。只需要加@EnableHystrix就行。

    查看源码可以看到

    1 @Target(ElementType.TYPE)
    2 @Retention(RetentionPolicy.RUNTIME)
    3 @Documented
    4 @Inherited
    5 @EnableCircuitBreaker
    6 public @interface EnableHystrix {
    7 
    8 }

    5、启动RibbonServerApplication

    浏览器中输入:http://localhost:8091/ribbon/service,多次刷新结果如下:

    x-demo-service hello.8081

    x-demo-service hello.8082

    x-demo-service hello.8083

    我们把服务提供者x-demo-service Kill之后,再次刷新http://localhost:8091/ribbon/service,结果如下:

  • 相关阅读:
    深入理解Mysql——锁、事务与并发控制
    深入理解Mysql——锁、事务与并发控制
    vs code中Vue代码格式化的问题
    Vue中的父子传值问题
    用画布canvas画安卓logo
    用画布canvas画安卓logo
    用画布canvas画安卓logo
    用画布canvas画安卓logo
    软中断
    软中断
  • 原文地址:https://www.cnblogs.com/shileibrave/p/14439670.html
Copyright © 2011-2022 走看看