SpringCloud框架hystrix和actuator的使用

服务消费者

  pom文件

  导入hystrix场景启动器

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

</dependency>

yml文件  配置hystrix超时时间

enabled: true  #feign作为客户端使用hystrix必须开启,服务端没有feign所以没有这个

hystrix:

  command:

    default:

      execution:

        isolation:

          thread:

            timeoutInMilliseconds: 3000  #毫秒为单位的超时时间

主启动类注解

@EnableHystrix  开启hystrix注解

编写代码实现OpenFeignService接口   实现消费者端服务熔断

服务提供者

  pom文件

  导入hystrix场景启动器、actuator依赖

<!--hystrix-->

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

</dependency>

<!-- 引入Hystrix dashboard依赖和 actuator依赖 -->

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>

</dependency>

yml文件  配置hystrix监控

management:

  endpoints:

    web:

      exposure:

        include: hystrix.stream

主启动类

/**

   *此配置是为了服务监控而配置,与服务降级熔断本身无关,springcloud升级后的坑

   *ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream",

   *只要在自己的项目里配置上下面的servlet就可以了

   */

@Bean

public ServletRegistrationBean getServlet() {

    HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();

    ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);

    registrationBean.setLoadOnStartup(1);

    registrationBean.addUrlMappings("/hystrix.stream");

    registrationBean.setName("HystrixMetricsStreamServlet");

    return registrationBean;

}

控制层

默认熔断

    编写熔断后的方法

public Result myFallback(Integer id){

    return new Result(200,"findPaymentById输入的id为: "+ id);

}

在需要熔断后方法体上加注解

@HystrixCommand(fallbackMethod = "myFallback")  fallbackMethod处理熔断服务时执行的方法

自定义熔断

编写熔断后的方法

public Result circuitFallback(@RequestParam(value="id")Integer id){

    return new Result<Object>(200,"delete熔断"+id);

}

在需要熔断后方法体上加注解

@HystrixCommand(fallbackMethod = "circuitFallback",commandProperties = {

        @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),// 是否开启断路器

        @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 请求次数

        @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), // 时间窗口期

        @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50")// 失败率达到多少后跳闸

})

创建仪表盘子项目

在pom文件中导入

<dependencies>

   <!-- 引入Hystrix dashboard依赖和 actuator依赖 -->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-actuator</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-devtools</artifactId>

        <scope>runtime</scope>

        <optional>true</optional>

    </dependency>

    <dependency>

        <groupId>org.projectlombok</groupId>

        <artifactId>lombok</artifactId>

        <optional>true</optional>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-test</artifactId>

        <scope>test</scope>

    </dependency>

</dependencies>

在yml配置相关设置

server:

  port: 9000

  

management:

  enabled: false

  endpoints:

    web:

      exposure:

        include: hystrix.stream

      base-path: /

主启动类中注解

@SpringBootApplication

@EnableHystrixDashboard

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值