Skip to content

spring cloud 学习笔记0x11 #28

@hugeterry

Description

@hugeterry

断路器监控(Hystrix Dashboard)

  • 在笔记0x1的基础上,在工程eurekaclient 加入Hystrix Dashboard起步依赖
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashard</artifactId>
    </dependency>
  • 在启动器上加上@EnableHystrix,@EnableHystrixDashboard,@EnableCircuitBreaker断路器相关注解
    @SpringBootApplication
    @EnableEurekaClient
    @EnableDiscoveryClient
    @RestController
    @EnableHystrix
    @EnableHystrixDashboard
    @EnableCircuitBreaker
    public class EurekaclientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaclientApplication.class, args);
        }
    
        @Value("${server.port}")
        String port;
    
        @RequestMapping("/nice")
        public String home(@RequestParam(value = "name", defaultValue = "a") String name) {
            return "hello " + name + " ,port:" + port;
        }
    
        @RequestMapping("/hi")
        @HystrixCommand(fallbackMethod = "hiError")
        public String hi(@RequestParam(value = "name", defaultValue = "hugeterry") String name) {
            return "hi " + name + " ,i am from port:" + port;
        }
    
        public String hiError(String name) {
            return "hi," + name + ",sorry,error!";
        }
    
        @Bean
        public ServletRegistrationBean getServlet() {
            HystrixMetricsStreamServlet streamServlet = new   HystrixMetricsStreamServlet();
            ServletRegistrationBean registrationBean = new   ServletRegistrationBean(streamServlet);
            registrationBean.setLoadOnStartup(1);
            registrationBean.addUrlMappings("/actuator/hystrix.stream");
            registrationBean.setName("HystrixMetricsStreamServlet");
            return registrationBean;
        }
    }
    其中getServlet是在spring cloud2 hystrix没有/actuator/hystrix.stream路径解决方式
  • 访问http://localhost:8662/actuator/hystrix.stream 可看到请求数据
  • 访问http://localhost:8662/hystrix , 配上http://localhost:8662/actuator/hystrix.stream 2000 hi确认,这个时候没有看到监控数据
  • 访问http://localhost:8662/hi?name=wqw,再刷新Hystrix Dashboard界面,会看到监控到数据
  • 更多Hystrix Dashboard 的运用可查阅https://www.cnblogs.com/happyflyingpig/p/8372485.html

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions