hystrix-dashboard的精简实现,仪表盘显示,附源码

1.首先创建eureka server,hystrix仪表盘的显示需要注册到eureka上,此处省略了,源码下载地址:https://download.csdn.net/download/qq_35449098/12514397

2.创建service-hystrix,pom.xml添加如下依赖:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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>

application.yml修改为:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8766
spring:
  application:
    name: service-lxy
ServieHystrixApplication修改如下:
@SpringBootApplication
@RestController
@EnableEurekaClient
/**开启断路器 打开仪表盘 */
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServieHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServieHystrixApplication.class, args);
    }
    //添加servlet 仪表盘插入点  http://localhost:8766/hystrix.stream
    @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;
    }

    @RequestMapping("/test")
    @HystrixCommand(fallbackMethod = "hiError")//开启断路器
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi:" + name;
    }

    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }

}

3.分别启动eureka、service-hystrix即可,

浏览器访问:http://localhost:8766/test?name=test,出现如图所示:

然后访问仪表盘:http://localhost:8766/hystrix  

最后,出现如上图所示,即为配置成功啦~

tip:如果出现loading 1.检查插入点是否正确 2.记得先请求一次服务http://localhost:8766/test?name=test,仪表盘才会显示

       3.如果还是loading,且http://localhost:8766/hystrix.stream,有数据返回,那就是boot和cloud的版本不一致导致的错误,版本对应可参考https://blog.csdn.net/qq_35449098/article/details/106633994

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值