SpringCloud通过hystrix进行熔断以及HystrixDashboard看监控图小记

SpringCloud各服务之间通过balancer、feign、ribbon通信,相互调用小记(二)
1、添加服务service-ribbon-hystrix并引入相关包。

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

2、启动类中添加相关注解以及初始化RestTemplate。

package com.example.serviceribbonhystrix;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableHystrix
@EnableEurekaClient
@SpringBootApplication
public class ServiceRibbonHystrixApplication {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    public static void main(String[] args) {
       SpringApplication.run(ServiceRibbonHystrixApplication.class, args);
    }
}

3、添加RibbonHystrixService类以及相关方法,“@HystrixCommand(fallbackMethod = “getUserNameByError”)”注解代表启用熔断,在出现错误时调用“getUserNameByError”方法。

package com.example.serviceribbonhystrix.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class RibbonHystrixService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "getUserNameByError")
    public String getUsesrName() {
        return  restTemplate.getForObject("http://service-user/getUserName", String.class);
    }

    public String getUserNameByError() {
        return "请求超时";
    }
}

4、添加ServiceRibbonHystrixController类以及相关方法。

package com.example.serviceribbonhystrix.web;

import com.example.serviceribbonhystrix.service.RibbonHystrixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ServiceRibbonHystrixController {
    @Autowired
    RibbonHystrixService ribbonHystrixService;

    @GetMapping(value = "/getUserName")
    public String getUserName() {
        String userName = ribbonHystrixService.getUsesrName();
        return "RibbonHystrix: " + userName;
    }
}

5、启动注册中心eureka-server以及server-user、service-ribbon-hystrix服务。
6、启动后第一次访问service-ribbon-hystrix服务中的“getUserName”方法得到返回结果“张三琪”,然后卸载server-user,再次访问“getUserName”方法,这是会触发熔断机制返回“请求超时”。
7、添加服务hystrix-dashborard,引入相关包

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <!--想要用spring-cloud-netflix-hystrix-dashboard
            但是@enablehystrixdashboard引入不进去
            属于springBot版本问题,使用以下包,begin-->
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
            <!-- 默认使用的版本是 2.2.2.RELEASE-->
            <version>2.2.2.RELEASE</version>
        </dependency>
        <!--@enablehystrixdashboard包引入End-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

8、在service-ribbon-hystrix服务的pom中添加“spring-boot-starter-actuator”包,在配置文件中添加“management.endpoints.web.exposure.include=*”,在启动类中添加“@EnableCircuitBreaker”注解
9、启动服务访问hystrix-dashborard服务的访问地址“http://127.0.0.1:8085/hystrix”即可看到一个小熊监控主页。
在这里插入图片描述
10、输入service-ribbon-hystrix服务的访问地址“http://127.0.0.1:8083/actuator/hystrix.stream”,然后再次访问service-ribbon-hystrix服务的“getUserName”方法,即可看到一个统计图。
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值