专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

springcloud Hystrix入门

熔断配置

1、在order-service中pom文件中添加依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

2、在OrderApplication中添加hystrix激活注解@EnableCircuitBreaker

3、在OrderController中添加如下代码

/**
 * 使用注解配置熔断保护
 *     fallbackmethod : 配置熔断之后的降级方法
 */
@HystrixCommand(fallbackMethod = "orderFallBack")
@RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
public Product findById(@PathVariable Long id) {
    Product product = productFeignClient.findById(id);
    return product;
}

/**
 * 降级方法
 *  和需要收到保护的方法的返回值一致
 *  方法参数一致
 */
public Product orderFallBack(Long id) {
    Product product = new Product();
    product.setProductName("触发降级方法");
    return product;
}

4、在application.yml文件中添加如下配置

128_1.png

启动服务访问: http://localhost:9002/order/buy/1

128_2.png

这时停掉product服务再次访问 http://localhost:9002/order/buy/1

128_3.png

到这里我们的降级就基本实现了。

统一降级配置

@DefaultProperties(defaultFallback = “defaultFallBack”)*指定此接口中公共的熔断设置,如果过在@DefaultProperties指定了公共的降级方法,在@HystrixCommand**不需要单独指定了*

基于feign调用熔断配置

(1)修改order服务application.yml在Fegin中开启hystrix在Feign中已经内置了hystrix,但是默认是关闭的需要在工程的 application.yml 中开启对hystrix的支持(2)配置FeignClient接口的实现类基于Feign实现熔断降级,那么降级方法需要配置到FeignClient接口的实现类中
(3)修改FeignClient添加hystrix熔断在@FeignClient注解中添加降级方法@FeignClient注解中以fallback声明降级方法
feign:
hystrix: #在feign中开启hystrix熔断
enabled: true

128_4.png

128_5.png

重新启动order服务访问: http://localhost:9002/order/buy/1

128_6.png

hystrix监控数据

1、在order服务pom文件中添加依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2、在启动类加上注解

//激活hystrix
@EnableCircuitBreaker

3、在application.yml添加如下配置:

management:
  endpoints:
    web:
      exposure:
        include: '*'

访问: http://localhost:9002/actuator

128_7.png

在访问: http://localhost:9002/actuator/hystrix.stream

128_8.png

搭建Hystrix DashBoard监控
1、在order服务pom文件中添加依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

2、在启动类上添加注解@EnableHystrixDashboard

3、访问: http://localhost:9002/hystrix

128_9.png

128_10.png

128_11.png

断路器聚合监控Turbine

1、创建工程hystrix-turbine,引入相关依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
</dependencies>

2、配置多个微服务的hystrix监控

server:
  port: 8031
spring:
  application:
name: microservice-hystrix-turbine
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    prefer-ip-address: true
turbine:
  # 要监控的微服务列表,多个用,分隔
  appConfig: order-service
  clusterNameExpression: "'default'"

3、启动类:

@SpringBootApplication
@EnableTurbine
@EnableHystrixDashboard
public class TurbineAppcation {

    public static void main(String[] args) {
        SpringApplication.run(TurbineAppcation.class, args);
    }
}

4、访问 http://localhost:8031/hystrix/

128_12.png

128_13.png

未经允许不得转载:搜云库技术团队 » springcloud Hystrix入门

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们