• Hystrix 服务熔断


    书籍,人,借阅服务之间相互调用, 高度耦合, 一旦一个服务故障, 其他服务会雪崩, 和多米诺骨牌一样

    Hystrix

    熔断器, 保险丝

    服务降级

    提供补救措施发给请求者, 服务可用, 能力下降了

    borrow-service 导入依赖

       <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
                 <version>2.2.10.RELEASEversion>
        dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    @SpringBootApplication
    @EnableHystrix   //启用Hystrix
    public class BorrowApplication {
        public static void main(String[] args) {
            SpringApplication.run(BorrowApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    @RestController
    public class BorrowController {
    
        @Resource
        BorrowService service;
    
        @HystrixCommand(fallbackMethod = "onError")    //使用@HystrixCommand来指定备选方案
        @RequestMapping("/borrow/{uid}")
        UserBorrowDetail findUserBorrows(@PathVariable("uid") int uid){
            return service.getUserBorrowDetailByUid(uid);
        }
            
          //备选方案,这里直接返回空列表了
          //注意参数和返回值要和上面的一致
        UserBorrowDetail onError(int uid){
            return new UserBorrowDetail(null, Collections.emptyList());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    关闭user-service, 运行备选方案

    服务熔断

    OpenFeign 降级

    Hystrix 配合 Feign降级

    定义UserFallbackClient类实现接口

    @Component   //注意,需要将其注册为Bean,Feign才能自动注入
    public class UserFallbackClient implements UserClient{
        @Override
        public User getUserById(int uid) {   //这里我们自行对其进行实现,并返回我们的替代方案
            User user = new User();
            user.setName("我是替代方案");
            return user;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    UserClient指定fallback

    @FeignClient(value = "user-service", fallback = UserFallbackClient.class)
    public interface UserClient {
    
        //路径保证和其他微服务提供的一致即可
        @RequestMapping("/user/{uid}")
        User getUserById(@PathVariable("uid") int uid);  //参数和返回值也保持一致
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    配置文件开启熔断支持

    feign:
      circuitbreaker:
        enabled: true
    
    • 1
    • 2
    • 3

    +++

    部署监控页面

    创建新项目

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>
        <version>2.2.10.RELEASEversion>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    server:
      port: 8900
    hystrix:
      dashboard:
        # 将localhost添加到白名单,默认是不允许的
        proxy-stream-allow-list: "localhost"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    主类加注解 @EnableHystrixDashboard

    @SpringBootApplication
    @EnableHystrixDashboard
    public class HystrixDashBoardApplication {
        public static void main(String[] args) {
            SpringApplication.run(HystrixDashBoardApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    需要监控的服务添加actuator[译:监视器]依赖

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-actuatorartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    borrow-service项目加yml配置

    management:
      endpoints:
        web:
          exposure:
            include: '*'
    
    • 1
    • 2
    • 3
    • 4
    • 5

    测试

    http://localhost:8301/actuator/hystrix.stream

    访问borrow book user的接口

    1/actuator/hystrix.stream

    [外链图片转存中…(img-kSWA9MDR-1685256243053)]

    访问borrow book user的接口

    [外链图片转存中…(img-NVYNERCp-1685256243054)]

  • 相关阅读:
    当老板一定要学会花钱去赚钱
    高校教室预约使用管理系统(PHP+Mysql)毕业论文+项目源码+数据库sql文件
    时间在情绪周期视角来看也是不存在的
    揭秘华为云GaussDB(for Influx)最佳实践:hint查询
    UE5 打包安卓报错LogPlayLevel: UAT: at org.codehaus.groovy.vmplugin.v7.Java7
    HTML5 新元素
    人机交互——自然语言生成
    CocoaPods 官宣进入维护模式,不在积极开发新功能,未来将是 Swift Package Manager 的时代
    图像分割 - 分水岭算法
    数据清洗 - 案例实战 - 上集
  • 原文地址:https://blog.csdn.net/weixin_55845798/article/details/130912692