(三)Hystrix服务容错处理

    技术2026-10-05  4

    Hystrix:可以简单认为是,服务的调用者(消费者)调用服务提供者提供的服务时的异常处理。

    Feign整合Hystrix服务容错

    基本环境

    参考:https://blog.csdn.net/admin_15082037343/article/details/107071725

    Fallback方式 demo-client添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> 启动类新增注解@EnableHystrix package com.demo.client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @EnableHystrix public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } } application.yml启动断路器 feign: hystrix: enabled: true 新增类UserClientHystrix package com.demo.client.fallback; import com.demo.client.api.UserClient; import com.demo.client.entity.User; import org.springframework.stereotype.Component; @Component public class UserClientHystrix implements UserClient { @Override public Object findUser(Integer pageNo, Integer size) { return "{}"; } @Override public User save(User user) { return new User(); } @Override public User findById(String id) { return new User(); } @Override public String deleteById(String id) { return "-1"; } }

    这里可以理解为调用服务失败返回默认值

    @FeignClient指定fallback @FeignClient(value = "demo-provider", path = "/user", configuration = FeignConfiguration.class, fallback = UserClientHystrix.class) 验证

    此时启动注册中心和服务消费者,不启动服务提供者,访问接口,不会报错 {},就是在UserClientHystrix 的findUser失败是返回的默认值。

    FallbackFactory方式 新增类UserClientFallbackFactory package com.demo.client.fallback; import com.demo.client.api.UserClient; import com.demo.client.entity.User; import feign.hystrix.FallbackFactory; import org.springframework.stereotype.Component; @Component public class UserClientFallbackFactory implements FallbackFactory<UserClient> { @Override public UserClient create(Throwable throwable) { throwable.printStackTrace(); return new UserClient() { @Override public Object findUser(Integer pageNo, Integer size) { return "{}"; } @Override public User save(User user) { return new User(); } @Override public User findById(String id) { return new User(); } @Override public String deleteById(String id) { return "-1"; } }; } } @FeignClient更新 @FeignClient(value = "demo-provider", path = "/user", configuration = FeignConfiguration.class, fallbackFactory = UserClientFallbackFactory.class)

    这里用fallbackFactory 而不是fallback

    Hystrix监控

    application.yml新增配置

    management: endpoints: web: exposure: include: health,info,hystrix.stream

    浏览器访问:http://127.0.0.1:8082/actuator/hystrix.stream

    整合Dashboard查看监控数据

    这里新建一个模块demo-dashboard,并添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> 启动类 package com.demo.dashboard; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @SpringBootApplication @EnableHystrixDashboard public class DashboardApplication { public static void main(String[] args) { SpringApplication.run(DashboardApplication.class, args); } } application.yml server: port: 9011 spring: application: name: demo-dashboard

    这里没有注册到注册中心

    浏览器访问:http://127.0.0.1:9011/hystrix 这里的地址为http://127.0.0.1:8082/actuator/hystrix.stream

    Turbine聚合集群数据

    demo-dashboard中添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency> 启动类新增注解 package com.demo.dashboard; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.netflix.turbine.EnableTurbine; @SpringBootApplication @EnableHystrixDashboard @EnableDiscoveryClient @EnableTurbine public class DashboardApplication { public static void main(String[] args) { SpringApplication.run(DashboardApplication.class, args); } } application.yml eureka: client: service-url: defaultZone: http://admin:123456@127.0.0.1:8761/eureka/ turbine: app-config: demo-client aggregator: cluster-config: default cluster-name-expression: new String("default")

    turbine.app-config:需要监控的应用 这里需要注册到注册中心

    启动,浏览器访问:http://127.0.0.1:9011/turbine.stream 浏览器访问: http://127.0.0.1:9011/hystrix 填写路径

    源码参考

    https://github.com/phone15082037343/demo.git

    Processed: 0.009, SQL: 9