当前位置:   article > 正文

Spring Cloud Hystrix(服务容错保护)(1)_springcloud中断一个服务

springcloud中断一个服务

在前面的博客中我已经将Eureka与Ribbon的使用展示了出来,但是如果将其中的某一个服务停止(Hello-Service)。你会发现Ribbon任然会去访问那个页面。

这就需要用到断路器的功能。

首先将Ribbon工程的pom文件中加入该jar包:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  4. </dependency>

再其主类工程中使用@EnableCircuitBreaker注解开启断路器功能:

  1. @EnableCircuitBreaker
  2. @EnableDiscoveryClient
  3. @SpringBootApplication
  4. public class EurekaRibbonApplication {
  5. @Bean
  6. @LoadBalanced
  7. RestTemplate restTemplate() {
  8. return new RestTemplate();
  9. }
  10. public static void main(String[] args) {
  11. SpringApplication.run(EurekaRibbonApplication.class, args);
  12. }
  13. }

也可以用@SpringCloudApplication注解来替代:

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

然后改造其服务的消费方式,新增Service层,注入RestTemplate实例。然后在其方法上添加@HystrixCommand注解来指定回调方法:

  1. @Service
  2. public class HelloService {
  3. @Autowired
  4. RestTemplate restTemplate;
  5. @HystrixCommand(fallbackMethod = "helloFallback")
  6. public String helloService() {
  7. ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://hello-service/hello", String.class);
  8. String body = responseEntity.getBody();
  9. return body;
  10. }
  11. public String helloFallback() {
  12. return "error";
  13. }
  14. }

Controller类改成如下:

  1. @RestController
  2. public class ConsumerController {
  3. @Autowired
  4. HelloService helloService;
  5. @RequestMapping("/ribbon-consumer")
  6. public String helloConsumer() {
  7. return helloService.helloService();
  8. }
  9. }

运行后:

然后中断一个服务

随后可以发现再请求http://localhost:8099/ribbon-consumer它不会再请求已经断开的服务。

除了断开服务实例来模拟节点无法访问的情况之外,我们还能模拟下服务阻塞(长时间未响应)如:

  1. @RestController
  2. public class HelloController {
  3. private final Logger logger=Logger.getLogger(getClass());
  4. @Autowired
  5. private DiscoveryClient client;
  6. @RequestMapping("/hello")
  7. public String index() throws InterruptedException {
  8. int sleepTime=new Random().nextInt(3000);
  9. logger.info("sleep:"+sleepTime);
  10. Thread.sleep(sleepTime);
  11. return "Hello";
  12. }
  13. }

修改Hello-Service中的服务提供者的代码,用Thread.sleep来进行模拟延时。

因为Hystrix默认的超时时间为1000毫秒,所以这里用0-3000的随机数进行了模拟,为了方便观察断路器的触发,在消费者调用函数也做了时间的记录:

  1. @HystrixCommand(fallbackMethod = "helloFallback")
  2. public String helloService() {
  3. long start=System.currentTimeMillis();
  4. ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://hello-service/hello", String.class);
  5. String body = responseEntity.getBody();
  6. long end=System.currentTimeMillis();
  7. logger.info("消耗时间:"+(start-end));
  8. return body;
  9. }

此时可以看到当sleep大于1000的时候它就会返回error,即服务消费者因调用服务超时从而触发熔断请求,并回调逻辑返回结果。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/843100
推荐阅读
相关标签
  

闽ICP备14008679号