Spring Cloud配置熔断器

Spring Cloud配置熔断器


在Spring Cloud中使用了Hystrix 来实现断路器的功能。Hystrix是Netflix开源的微服务框架套件之一,该框架目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。

以下分别在ribbon以及feign中配置断路器Hystrix

一、feign中配置Hystrix

feign是自带断路器功能的,并且默认打开,如果你要关闭的话,需要加上这个配置:


  1. feign:

  2. hystrix:

  3. enabled: false

以之前创建的消费者 service-consumer-feign为例子配置:

1) 这里只需要在@FeignClient注解上加上fallback就可以了,如下:


  1. package com.sam.service.consumer.feign;


  2. import org.springframework.cloud.netflix.feign.FeignClient;

  3. import org.springframework.stereotype.Component;

  4. import org.springframework.web.bind.annotation.RequestMapping;

  5. import org.springframework.web.bind.annotation.RequestParam;


  6. /**

  7. * @ClassName: ServiceAFeignClient

  8. * @Description: 服务客户端实现

  9. * @author sam

  10. * @date 2018年8月10日 下午4:00:42

  11. */

  12. @Component

  13. @FeignClient(value = "service-producer", fallback = ServiceAFeignClientFallback.class) // 这里的name对应调用服务的spring.applicatoin.name

  14. public interface ServiceAFeignClient {


  15. @RequestMapping(value = "/hi")

  16. String hi(@RequestParam("id") String id);


  17. }

2) 同时创建熔断处理类:ServiceAFeignClientFallback


  1. package com.sam.service.consumer.feign;


  2. import org.springframework.stereotype.Component;


  3. /**

  4. * @ClassName: ServiceAFeignClientFallback

  5. * @Description: 熔断回调类

  6. * @author sam

  7. * @date 2018年8月9日 下午5:24:52

  8. */

  9. @Component

  10. public class ServiceAFeignClientFallback implements ServiceAFeignClient {


  11. @Override

  12. public String hi(String id) {

  13. return "hi, " + id + ", error!";

  14. }


  15. }

配置之后,访问消费者:service-consumer-feign http://localhost:8911/hi?id=123 可以查看信息:

Spring Cloud配置熔断器


把生产者 service-producer-A等都停止,再刷新访问,看到熔断提示:

Spring Cloud配置熔断器


二、ribbon中配置Hystrix

以之前创建的消费者 service-consumer-ribbon为例子配置:

1)pom.xml中添加


  1. <dependency>

  2. <groupId>org.springframework.cloud</groupId>

  3. <artifactId>spring-cloud-starter-hystrix</artifactId>

  4. </dependency>

2)在启动类ClientApplicationRibbon中添加@EnableHystrix开启hystrix功能,如下:


  1. package com.sam.service.consumer.ribbon;


  2. import org.springframework.boot.SpringApplication;

  3. import org.springframework.boot.autoconfigure.SpringBootApplication;

  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;

  6. import org.springframework.cloud.netflix.hystrix.EnableHystrix;

  7. import org.springframework.context.annotation.Bean;

  8. import org.springframework.web.client.RestTemplate;


  9. /**

  10. * @ClassName: ClientApplicationRibbon

  11. * @Description: 消费者服务(使用Ribbon实现)

  12. * @author sam

  13. * @date 2018年8月10日 下午4:01:21

  14. */

  15. @SpringBootApplication

  16. @EnableDiscoveryClient

  17. @EnableHystrix

  18. public class ClientApplicationRibbon {


  19. public static void main(String[] args) {

  20. SpringApplication.run(ClientApplicationRibbon.class, args);

  21. }


  22. @Bean

  23. @LoadBalanced

  24. RestTemplate restTemplate() {

  25. return new RestTemplate();

  26. }

  27. }

3)Controller中, 使用@HystrixCommand指定回调方法,如下:


  1. package com.sam.service.consumer.ribbon;


  2. import org.springframework.beans.factory.annotation.Autowired;

  3. import org.springframework.web.bind.annotation.RequestMapping;

  4. import org.springframework.web.bind.annotation.RequestParam;

  5. import org.springframework.web.bind.annotation.RestController;

  6. import org.springframework.web.client.RestTemplate;


  7. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;


  8. /**

  9. * @ClassName: TestController

  10. * @Description: Ribbon+RestTemplate方式

  11. * @author sam

  12. * @date 2018年8月9日 下午4:17:10

  13. */

  14. @RestController

  15. public class TestController {


  16. @Autowired

  17. RestTemplate restTemplate;


  18. @RequestMapping("/hi")

  19. @HystrixCommand(fallbackMethod = "hiFallback")

  20. public String hi(@RequestParam String id) {

  21. return restTemplate.getForObject("http://service-producer/hi?id=" + id, String.class);

  22. }


  23. // 断路器回调方法

  24. public String hiFallback(String id) {

  25. return "hi, " + id + ", error!";

  26. }

  27. }

以上配置完成后,停止生产者,亦可以实现和fergn一样的熔断器效果!

如果你现在在JAVA这条路上挣扎,也想在IT行业拿高薪,可以参加我们的训练营课程,选择最适合自己的课程学习,技术大牛亲授,7个月后,进入名企拿高薪。我们的课程内容有:Java工程化、高性能及分布式、高性能、深入浅出。高架构。性能调优、Spring,MyBatis,Netty源码分析和大数据等多个知识点。如果你想拿高薪的,想学习的,想就业前景好的,想跟别人竞争能取得优势的,想进阿里面试但担心面试不过的,你都可以来,q群号为:835638062

注:加群要求

1、具有1-5工作经验的,面对目前流行的技术不知从何下手,需要突破技术瓶颈的可以加。

2、在公司待久了,过得很安逸,但跳槽时面试碰壁。需要在短时间内进修、跳槽拿高薪的可以加。

3、如果没有工作经验,但基础非常扎实,对java工作机制,常用设计思想,常用java开发框架掌握熟练的,可以加。

4、觉得自己很牛B,一般需求都能搞定。但是所学的知识点没有系统化,很难在技术领域继续突破的可以加。

5.阿里Java高级大牛直播讲解知识点,分享知识,多年工作经验的梳理和总结,带着大家全面、科学地建立自己的技术体系和技术认知!

猜你喜欢

转载自blog.51cto.com/13904503/2161343
今日推荐