springcloud学习-12 Hystrix断路器【周阳springcloud2020学习笔记】

    技术2022-07-11  96

    作用:

    服务降级: 程序运行异常、超时、服务熔断触发服务降级、线程池或信号量打满也会导致服务降级服务熔断: 类比保险丝达到最大服务访问后,直接拒绝访问,拉闸限电,然后调用服务降级的方法并返回友好提示 服务的降级->进而熔断->恢复调用链路服务限流:秒杀高并发等操作,严禁一窝蜂的过来拥挤,大家排队,一秒钟N个,有序进行 接近实时的监控

    提供者hystrix-provider-payment8005

    1.新建hystrix-provider-payment8005 2.pom

    <dependencies> <!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!--eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--公共模块:通用工具包--> <dependency> <groupId>cn.chen.demo</groupId> <artifactId>api-common</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- 热部署依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>

    3.yml

    server: port: 8005 # 端口号 spring: # 应用名称 application: name: hystrix-payment-service # 服务名称 # 数据源 datasource: type: com.alibaba.druid.pool.DruidDataSource # 数据库操作类型 driver-class-name: org.gjt.mm.mysql.Driver # 数据库驱动 #url: jdbc:mysql://192.168.221.129:3306/demo2020?useUnicode=true&characterEncoding=utf-8&useSSL=false url: jdbc:mysql://localhost:3306/demo2020?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 # 是否支持热部署 devtools: restart: enabled: true eureka: client: register-with-eureka: true fetchRegistry: true service-url: defaultZone: http://localhost:7001/eureka # 单机版 #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版 instance: instance-id: payment8005 prefer-ip-address: true # eureka客户端想服务端发动心跳的时间间隔,单位为秒(默认是30秒)。开发的时候可以设置小一些,以保证服务关闭后注册中心及时剔除服务 lease-renewal-interval-in-seconds: 1 # eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒)。开发时候设置小一些 lease-expiration-duration-in-seconds: 2 mybatis: mapperLocations: classpath:mapper/*.xml #mapper文件 type-aliases-package: cn.chen.cloud.entity #所有entity别名所在包

    4.主启动

    @SpringBootApplication @EnableEurekaClient public class HystrixPaymentApplication { public static void main(String[] args) { SpringApplication.run(HystrixPaymentApplication.class,args); } }

    5.业务类 1)service

    @Service public class PaymentService { /** * @Description 模拟访问成功情况 **/ public String IsOk(){ return "线程池:"+Thread.currentThread().getName()+",IsOk。"; } /** * @Description 模拟访问失败情况 **/ public String IsTimeOut(int timeNumber){ try { TimeUnit.SECONDS.sleep(timeNumber); }catch (Exception e) { e.printStackTrace(); } return "线程池:"+Thread.currentThread().getName()+",IsTimeOut,耗时(秒)"+timeNumber; } }

    2)controller

    @RestController @Slf4j @RequestMapping("/payment") public class PaymentController { @Resource private PaymentService paymentService; @GetMapping("/hystrix/ok") public String ok(){ String result = paymentService.IsOk(); log.info("*******result:"+result); return result; } @GetMapping("/hystrix/timeout") public String timeOut(){ String result = paymentService.IsTimeOut(3); log.info("*******result:"+result); return result; } }

    6.启动7001(单机版) 启动8005(Hystrix)


    消费者hystrix-consumer-order80

    1.新建hystrix-consumer-order80 2.pom依赖(参考consumer-order80)

    <dependencies> <!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!--openfeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--公共模块--> <dependency> <groupId>cn.chen.demo</groupId> <artifactId>api-common</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- devtools --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>

    3.yml配置(参考consumer-order80)

    server: port: 80 spring: application: name: hystrix-order-consumer eureka: client: register-with-eureka: true fetchRegistry: true service-url: defaultZone: http://localhost:7001/eureka # 单机版 #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版 instance: instance-id: order80 prefer-ip-address: true payment: #url: http://localhost:8001 server: name: http://payment-service

    4.主启动类

    @SpringBootApplication @EnableFeignClients public class HystrixOrderApplication { public static void main(String[] args) { SpringApplication.run(HystrixOrderApplication.class,args); } }

    springcloud学习系列目录

    Processed: 0.011, SQL: 9