1. 程式人生 > >spring cloud 斷路器監控-Hystrix Dashboard

spring cloud 斷路器監控-Hystrix Dashboard

Hystrix Dashboard是作為斷路器狀態的一個元件,提供了資料監控和友好的圖表化介面。

修改service-hi

1、在pom工程檔案引入相應的依賴

<dependencies>
        <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-web</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>
        
    </dependencies>

2、在程式的入口ServiceHiApplication類,加上@EnableHystrix註解開啟斷路器,這個必須的,並且需要在程式中宣告斷點HystrixCommand,加上@EnableHystrixDashboard註解,開啟HystrixDashboard。

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServiceHiApplication {

    /**
     * 訪問地址 http://localhost:8762/actuator/hystrix.stream
     * @param args
     */

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

    @Value("${server.port}")
    String port;

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

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

}

4、application.yml

server:
  port: 8762
  application:
    name: service-hi

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

4、執行程式,依次開啟eureka-server和service-hi

5、開啟http://localhost:8762/actuator/hystrix.stream,可以看到一些具體的資料。

6、開啟localhost:8762/hystrix,可以看見下介面。在介面依次輸入http://localhost:8762/actuator/hystrix.stream、2000、miya點選確定。

7、在另一個視窗輸入http://localhost:8762/hi?name=xxx,重新重新整理hystrix.stream網頁。