SpringCloud 2.x学习笔记:10、断路器监控(Hystrix Dashboard)(Greenwich版本)

1、断路器监控(Hystrix Dashboard)

Hystrix 已经停止开发了,Hystrix 官方推荐替代的开源组件:Resilience4j(感觉学不动啦)
除了 Resilience4j,还有Spring Cloud Alibaba作为替代组件,首个版本Spring Cloud for Alibaba 0.2.0

现在 Spring Boot 有两条线,即 Spring Boot 1.x 和 Spring Boot 2.x,
所以 0.2.0 即是和 Spring Boot 2.x 兼容的,0.1.0 则是和 Spring Boot 1.x 兼容的

2、 hystrix-dashboard模块

2.1 pom.xml



    4.0.0
    com.cntaiping.tpa
    hystrix-dashboard
    0.0.1-SNAPSHOT
    jar
    hystrix-dashboard
    Demo project for Spring Boot

    
        com.cntaiping.tpa
        cloud
        1.0-SNAPSHOT
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            com.netflix.hystrix
            hystrix-javanica
            RELEASE
        
        
            org.springframework.cloud
            spring-cloud-netflix-hystrix-dashboard
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        
    



2.2 application.properties

#服务名称
spring.application.name=consumer-hystrix
#端口号
server.port=8505
#在注册中心中进行注册
eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/

2.3 Application类

package com.cntaiping.tpa.hystrixdashboard;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrixDashboard
@EnableHystrix
@EnableCircuitBreaker
public class HystrixDashboardApplication {

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

    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet hystrixMetricsStreamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(hystrixMetricsStreamServlet);
        servletRegistrationBean.addUrlMappings("/hystrix.stream");
        servletRegistrationBean.setName("HystrixMetricsStreamServlet");
        return servletRegistrationBean;
    }
}


2.4 控制器

package com.cntaiping.tpa.hystrixdashboard.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {
    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    @HystrixCommand(fallbackMethod = "hiError")
    public String hello(@RequestParam("name") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }

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

}

2.5 运行效果

依次运行服务注册中心register-server模块和断路器监控hystrix-dashboard模块
SpringCloud 2.x学习笔记:10、断路器监控(Hystrix Dashboard)(Greenwich版本)_第1张图片

http://localhost:8506/hi?name=chengyq
SpringCloud 2.x学习笔记:10、断路器监控(Hystrix Dashboard)(Greenwich版本)_第2张图片
http://localhost:8506/hystrix.stream
SpringCloud 2.x学习笔记:10、断路器监控(Hystrix Dashboard)(Greenwich版本)_第3张图片
http://localhost:8506/hystrix
SpringCloud 2.x学习笔记:10、断路器监控(Hystrix Dashboard)(Greenwich版本)_第4张图片
SpringCloud 2.x学习笔记:10、断路器监控(Hystrix Dashboard)(Greenwich版本)_第5张图片

你可能感兴趣的:(SpringCloud,2.x学习笔记,hystrix)