【微服务架构 - 08 - Spring Cloud】06 使用熔断器防止服务雪崩

野性酷女 2022-03-22 19:00 250阅读 0赞

Ribbon 中使用熔断器


pom.xml 中增加依赖

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

在 Application 中增加 @EnableHystrix 注解

  1. package com.yuu.hello.spring.cloud.web.admin.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.netflix.hystrix.EnableHystrix;
  6. /**
  7. * @Classname WebAdminRibbonApplication
  8. * @Date 2019/1/29 0:04
  9. * @Created by Yuu
  10. */
  11. @SpringBootApplication
  12. @EnableDiscoveryClient
  13. @EnableHystrix
  14. public class WebAdminRibbonApplication {
  15. public static void main(String[] args) {
  16. SpringApplication.run(WebAdminRibbonApplication.class, args);
  17. }
  18. }

在 Service 中增加 @HystrixCommand 注解

在 Ribbon 调用方法上增加 @HystrixCommand注解并指定 fallbackMethod 熔断方法

  1. package com.yuu.hello.spring.cloud.web.admin.ribbon.service;
  2. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. import org.springframework.web.client.RestTemplate;
  6. /**
  7. * @Classname AdminService
  8. * @Date 2019/1/29 0:09
  9. * @Created by Yuu
  10. */
  11. @Service
  12. public class AdminService {
  13. @Autowired
  14. private RestTemplate restTemplate;
  15. @HystrixCommand(fallbackMethod = "hiError")
  16. public String sayHi(String message) {
  17. return restTemplate.getForObject("http://HELLO-SPRING-CLOUD-SERVICE-ADMIN/hi?message=" + message, String.class);
  18. }
  19. public String hiError(String message) {
  20. return "Hi, your message is :\"" + message + "\" but request error.";
  21. }
  22. }

测试熔断器

此时我们关闭服务提供者,再次请求 http://localhost:8764/hi?message=HelloRibbon 浏览器会显示:

在这里插入图片描述

Feign 中使用熔断器

Feign 是自带熔断器的,但默认是关闭的。需要在配置文件中配置打开它,在配置文件增加以下代码:

  1. feign:
  2. hystrix:
  3. enabled: true

在 Service 中增加 fallback 指定类

  1. package com.yuu.hello.spring.cloud.web.admin.feign.service;
  2. import com.yuu.hello.spring.cloud.web.admin.feign.service.hystrix.AdminServiceHystrix;
  3. import org.springframework.cloud.openfeign.FeignClient;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. /**
  8. * @Classname AdminService
  9. * @Date 2019/1/29 13:09
  10. * @Created by Yuu
  11. */
  12. @FeignClient(value = "hello-spring-cloud-service-admin", fallback = AdminServiceHystrix.class)
  13. public interface AdminService {
  14. @RequestMapping(value = "hi", method = RequestMethod.GET)
  15. public String sayHi(@RequestParam(value = "message") String message);
  16. }

创建熔断器类并实现对应的 Feign 接口

  1. package com.yuu.hello.spring.cloud.web.admin.feign.service.hystrix;
  2. import com.yuu.hello.spring.cloud.web.admin.feign.service.AdminService;
  3. import org.springframework.stereotype.Component;
  4. /**
  5. * @Classname AdminServiceHystrix
  6. * @Date 2019/1/29 14:09
  7. * @Created by Yuu
  8. */
  9. @Component
  10. public class AdminServiceHystrix implements AdminService {
  11. @Override
  12. public String sayHi(String message) {
  13. return "Hi, your message is :\"" + message + "\" but request error";
  14. }
  15. }

测试熔断器

此时我们关闭服务提供者,再次请求 http://localhost:8765/hi?message=HelloFeign 浏览器会显示:

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,250人围观)

还没有评论,来说两句吧...

相关阅读