Spring Cloud Hystrix的使用

小明 2025-05-02 17:13:58 5

Spring Cloud Hystrix的使用

  • Spring Cloud Hystrix
    • 在OpenFeign中使用Hystrix
      • 编写(创建)方配置
      • 调用(使用)方配置
      • 使用熔断器(Hystrix)
      • 常���配置

        Spring Cloud Hystrix

        熔断器(断路器)用于保护微服务不雪崩的方法

        • 当有服务调用的情况下才会出现服务器雪崩,所以Hystrix常和OpenFeign,Ribbon一起出现

          在OpenFeign中使用Hystrix

          编写两个模块

          • 模块1:customer-service-01:用于调用模块2的方法
          • 模块2:rent-car-service-02用于编写方法

            模块1所需依赖:springweb、eureka-client、openfeign

            模块2所需依赖:springweb、eureka-client

            模块依赖如上方所示,我就不示例创建项目了

            编写(创建)方配置

            即模块2:rent-car-service-02

            yml:

            server:
              port: 8080
            spring:
              application:
                name: rent-car-service
            eureka:
              client:
                service-url:
                  defaultZone: eureka远程地址
              instance:
                hostname: localhost
                instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
            

            启动类:

            开启eurekaClient服务

            @SpringBootApplication
            @EnableEurekaClient
            

            编写方法

            创建文件夹controller–>创建方法类RentCarController

            @RestController
            public class RentCarController {
                @GetMapping("rent")
                public String rent(){
                    return "租车....";
                }
            }
            

            上方就配置好了方法编写模块

            调用(使用)方配置

            即模块2:rent-car-service-02

            yml:

            • feign.hystrix.enabled:true用于开启Hystrix的
              server:
                port: 8081
              spring:
                application:
                  name: customer-service
              eureka:
                client:
                  service-url:
                    defaultZone: eureka远程地址
                instance:
                  hostname: localhost
                  instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
              feign:
                hystrix:
                  enabled: true #在cloudF版前是默认开启的
              

              启动类:

              开启eurekaClient、feignClient服务

              @SpringBootApplication
              @EnableEurekaClient
              @EnableFeignClients
              

              编写Feign接口

              创建文件夹feign–>创建接口CustomerRentFeign

              然后编写指定服务中的方法接口(方法名一致)

              • value:指定调用服务名
                @FeignClient(value = "rent-car-service")
                public interface CustomerRentFeign {
                    @GetMapping("rent")
                    public String rent();
                }
                

                编写方法

                创建文件夹controller–>创建方法类CustomerController

                @RestController
                public class CustomerController {
                    @Autowired
                    private CustomerRentFeign customerRentFeign;
                    @GetMapping("customerRent")
                    public String CustomerRent(){
                        System.out.println("客户来了");
                        return customerRentFeign.rent();
                    }
                }
                

                这样即可实现调用编写方的接口了~

                使用熔断器(Hystrix)

                解决编写方停了导致服务器报错

                在调用方新增依赖

                        
                            org.springframework.cloud
                            spring-cloud-starter-netflix-hystrix
                        
                

                在feign文件夹下创建hystrix文件夹并编写文件CustomeRentFeignHystrix

                继承编写的feign并重写它的方法

                @Component
                public class CustomeRentFeignHystrix implements CustomerRentFeign {
                    @Override
                    public String rent() {
                        return "服务器雪崩备选方案~";
                    }
                }
                

                再在feign中指向备选方案

                fallback:表示服务挂了指向的备选接口

                @FeignClient(value = "rent-car-service",fallback = CustomeRentFeignHystrix.class)
                public interface CustomerRentFeign {
                    @GetMapping("rent")
                    public String rent();
                }
                

                这样即可实现即使服务挂了也不会报错,也能有备选方案~✌

                常用配置

The End
微信