189 8069 5689

微服务熔断限流Hystrix之Dashboard

简介

Hystrix Dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard可以直观地看到各Hystrix Command的请求响应时间,请求成功率等数据。

创新互联服务项目包括栖霞网站建设、栖霞网站制作、栖霞网页制作以及栖霞网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,栖霞网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到栖霞省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

快速上手

工程说明

工程名端口作用
eureka-server 8761 注册中心
service-hi 8762 服务提供者
service-consumer 8763 服务消费者

核心代码

eureka-server 工程
pom.xml

    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server
application.yml
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:/${server.port}/eureka/
spring:
  application:
    name: eureka-server
启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

service-hi 工程

pom.xml

    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client
application.yml
server:
  port: 8762

spring:
  application:
    name: service-hi
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
HelloController
@RestController
public class HelloController {

    @GetMapping("/hi")
    public String hi() {
        return "hello ~";
    }

    @GetMapping("/hey")
    public String hey() {
        return "hey ~";
    }

    @GetMapping("/oh")
    public String oh() {
        return "ah ~";
    }

    @GetMapping("/ah")
    public String ah() {
        //模拟接口1/3的概率超时
        Random rand = new Random();
        int randomNum = rand.nextInt(3) + 1;
        if (3 == randomNum) {
            try {
                Thread.sleep( 3000 );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "来了老弟~";
    }

}
启动类
@SpringBootApplication
@EnableEurekaClient
public class ServiceHiApplication {

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

}

service-consumer 工程

pom.xml

    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client


    org.springframework.cloud
    spring-cloud-starter-netflix-ribbon


    org.springframework.boot
    spring-boot-starter-actuator


    org.springframework.cloud
    spring-cloud-starter-netflix-hystrix


    org.springframework.cloud
    spring-cloud-starter-netflix-hystrix-dashboard
application.yml
server:
  port: 8763

  tomcat:
    uri-encoding: UTF-8
    max-threads: 1000
    max-connections: 20000

spring:
  application:
    name: service-consumer
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"
HelloService
@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 简单用法
     */
    @HystrixCommand
    public String hiService() {
        return restTemplate.getForObject("http://SERVICE-HI/hi" , String.class);
    }

    /**
     * 定制超时
     */
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "30000") })
    public String heyService() {
        return restTemplate.getForObject("http://SERVICE-HI/hey" , String.class);
    }

    /**
     * 定制降级方法
     */
    @HystrixCommand(fallbackMethod = "getFallback")
    public String ahService() {
        return restTemplate.getForObject("http://SERVICE-HI/ah" , String.class);
    }

    /**
     * 定制线程池隔离策略
     */
    @HystrixCommand(fallbackMethod = "getFallback",
            threadPoolKey = "studentServiceThreadPool",
            threadPoolProperties = {
                    @HystrixProperty(name="coreSize", value="30"),
                    @HystrixProperty(name="maxQueueSize", value="50")
            }
    )
    public String ohService() {
        return restTemplate.getForObject("http://SERVICE-HI/oh" , String.class);
    }

    public String getFallback() {
        return "Oh , sorry , error !";
    }

}
HelloController
@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hi")
    public String hi() {
        return helloService.hiService();
    }

    @GetMapping("/hey")
    public String hey() {
        return helloService.heyService();
    }

    @GetMapping("/oh")
    public String oh() {
        return helloService.ohService();
    }

    @GetMapping("/ah")
    public String ah() {
        return helloService.ahService();
    }

}
启动类
@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
@EnableHystrix
@EnableCircuitBreaker
public class ServiceConsumerApplication {

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

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

Hystrix Dashboard 的使用

JSON格式监控信息

先访问http://localhost:8762/hi
再打开http://localhost:8763/actuator/hystrix.stream,可以看到一些具体的数据:

微服务熔断限流Hystrix之Dashboard

Hystrix仪表盘监控信息

单纯的查看json数据,很难分析出结果,所以,要在Hystrix仪表盘中来查看这一段json,在hystrix仪表盘中输入监控地址进行监控:
打开仪表盘地址:http://localhost:8762/hystrix

微服务熔断限流Hystrix之Dashboard

在界面依次输入:http://localhost:8763/actuator/hystrix.stream 、2000 、service-consumer;点确定。

微服务熔断限流Hystrix之Dashboard

Hystrix仪表盘指标含义

微服务熔断限流Hystrix之Dashboard

测试

编一个测试脚本curl.sh

while true;
do
curl "http://localhost:8763/hi";
curl "http://localhost:8763/hey";
curl "http://localhost:8763/oh";
curl "http://localhost:8763/ah";
done

执行测试脚本,查看Hystrix仪表盘:

微服务熔断限流Hystrix之Dashboard

可以看出 ahService 因为模拟了1/3的概率超时,所以监控中呈现了30%左右的错误百分比。

源码

https://github.com/gf-huanchupk/SpringCloudLearning/tree/master/chapter17

欢迎关注我的公众号《程序员果果》,关注有惊喜~~
微服务熔断限流Hystrix之Dashboard


新闻名称:微服务熔断限流Hystrix之Dashboard
网站路径:http://cdxtjz.com/article/gojjdc.html

其他资讯