Dashboard of microservice fuse current limiting Hystrix

Posted by kickoff3pm on Sat, 16 Nov 2019 19:32:09 +0100

brief introduction

Hystrix Dashboard is a real-time monitoring tool for hystrix. Through the Hystrix Dashboard, you can directly see the request response time, request success rate and other data of each Hystrix Command.

Quick start

Engineering description

project name port Effect
eureka-server 8761 Registry Center
service-hi 8762 Service provider
service-consumer 8763 Service consumers

Core code

Eureka server project

pom.xml

<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-server</artifactId>
</dependency>
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

Startup class

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

Service hi project

pom.xml

<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>

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 probability timeout of analog interface
        Random rand = new Random();
        int randomNum = rand.nextInt(3) + 1;
        if (3 == randomNum) {
            try {
                Thread.sleep( 3000 );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "Here we go.~";
    }

}

Startup class

@SpringBootApplication
@EnableEurekaClient
public class ServiceHiApplication {

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

}

Service consumer project

pom.xml

<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-netflix-ribbon</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

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;

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

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

    /**
     * Custom degradation method
     */
    @HystrixCommand(fallbackMethod = "getFallback")
    public String ahService() {
        return restTemplate.getForObject("http://SERVICE-HI/ah" , String.class);
    }

    /**
     * Custom thread pool isolation policy
     */
    @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();
    }


}

Startup class

@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();
    }

}

Use of Hystrix Dashboard

JSON format monitoring information

Visit http://localhost:8762/hi first
Open http://localhost: 8763/actor/hystrix.stream again, and you can see some specific data:

Hystrix dashboard monitoring information

It's difficult to analyze the results simply by viewing the json data. Therefore, to view the json in the hystrix dashboard, enter the monitoring address in the hystrix dashboard to monitor:
Open the dashboard address: http://localhost:8762/hystrix

Enter: http://localhost: 8763/actor/hystrix.stream, 2000, service consumer; click OK.

Meaning of Hystrix dashboard indicators

test

Make a test script 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

Execute the test script to view the Hystrix dashboard:

It can be seen that ahService has simulated a probability timeout of 1 / 3, so it presents an error percentage of about 30% in monitoring.

Source code

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




Welcome to scan code or WeChat search public number "programmer Guo Guo" pay attention to me, pay attention to surprises ~

Topics: Java Spring curl xml JSON