1. 程式人生 > >[java]微服務架構連載No4 Hystrix+Dashboard+Turbine實現斷路器(限流,超時,異常...)和服務監控

[java]微服務架構連載No4 Hystrix+Dashboard+Turbine實現斷路器(限流,超時,異常...)和服務監控

Hyxtrix:通過訪問遠端系統,服務,和第三方節點,從而對故障和延遲提供了強大的容錯能力,具備了服務降級,服務熔斷,遠端隔離,請求快取,請求合併已經服務監控等強大功能

本編文章架構圖如下


新建6個工程

spring-cloud-03-hystrix-eureka (服務發現中心,同上篇文章)

spring-cloud-03-hystrix-client (服務釋出)

spring-cloud-03-hystrix-request-a (服務請求叢集a)

spring-cloud-03-hystrix-request-b (服務請求叢集b)

spring-cloud-03-hystrix-dashboard (服務監控面板)

spring-cloud-03-hystrix-dashboard-turbine (叢集服務監控)

spring-cloud-03-hystrix-client (服務釋出)

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() throws InterruptedException {
        System.err.println("---hello --");
Thread.sleep(4000l);
return "--- hello --"
; } @GetMapping("/dashboard") public String dashboard() throws InterruptedException { System.err.println("---dashboard --"); return "--- dashboard --"; } }

spring:
  application:
    name: client-service

server:
  port: 7001
  context-path: /

eureka:
  client:
    service-url:
     defaultZone: 
http://localhost:8001/eureka/

備註:  requestAPI hello配置睡眠4秒相應時間

  dashboard無配置

 埠 7001

spring-cloud-03-hystrix-request-a (服務請求叢集a)

pom.xml匯入hyxtrix和actuator包用作斷路器

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

application.yml

spring:
  application:
    name: request-service

server:
  port: 7002
  context-path: /

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/

#啟動斷路器
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 1000

custom:
   rest:
    connect-timeout: 1000
    connection-request-timeout: 1000
    read-timeout: 30000


備註: timeoutInMilliseconds 配置斷路器超時時間1秒

custom.rest 配置服務超時時間

埠號 7002

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class RequestAApplication {

    @Bean
    @ConfigurationProperties(prefix = "custom.rest")
    public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory(){
        HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory=new HttpComponentsClientHttpRequestFactory();
return httpComponentsClientHttpRequestFactory;
}

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


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

備註: @EnableCircuitBreaker 開啟斷路器

HystrixService

@Service
public class HystrixService {

    @Autowired
private RestTemplate restTemplate;
/**
     * 超時降級策略
     *
     * @return
*/
@HystrixCommand(fallbackMethod = "helloFallback")
    public String hello() {
        return restTemplate.getForObject("http://client-service/hello", String.class);
}

    public String helloFallback() {
        return "------執行服務超時降級策略---";
}


    /**
     * 異常降級策略
     *
     * @return
*/
@HystrixCommand(fallbackMethod = "helloExceptionFallback", ignoreExceptions = {IOException.class})
    public String helloException() {
        throw new RuntimeException("helloException RuntimeException 異常了");
}

    public String helloExceptionFallback(Throwable e) {
        return "------執行服務異常降級策略---" + e.getMessage();
}

    /**
     * 高併發限流  之一: execution.isolation.strategy=THREAD執行緒隔離策略(它會獨立在一個執行緒上執行,並且它的併發量受執行緒池中的數量限制)
     *
     */
@HystrixCommand(
            commandKey = "threadKey",
commandProperties = {
                    @HystrixProperty(name = "execution.isolation.strategy", value = "HTREAD"),
@HystrixProperty(name = "coreSize", value = "10"),
@HystrixProperty(name = "maxQueueSize", value = "50"),     //最大數
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "30") // 拒絕閾值
},
fallbackMethod = "helloThreadFallback")
    public String helloThread() {
        return "------執行執行緒隔離策略 helloThread---";
}

    public String helloExceptionFallback() {
        return "------執行執行緒隔離策略 helloExceptionFallback---";
}

    /**
     * 高併發限流  之二: execution.isolation.strategy=SEMAPHORE 訊號量策略(它則實現再呼叫的執行緒上,通過訊號量的方式進行隔離,)
     */
@HystrixCommand(
            commandKey = "semaphoreKey",
commandProperties = {
                    @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"),
@HystrixProperty(name = "executiion.isolation.strategy.maxConcurrentRequests", value = "50")
            },
fallbackMethod = "helloSemaphoneFailback"
)
    public String  helloSemaphone(){
        return "------執行執行緒隔離策略 helloSemaphone---";
}


    @HystrixCommand(fallbackMethod = "dashboardFallback")
    public String dashboard() {
        return restTemplate.getForObject("http://client-service/dashboard", String.class);
}

    public String dashboardFallback() {
        return "------執行服務dashboardFallback 超時降級策略---";
}
}
@RestController
public class HystrixController {


    @Autowired
private HystrixService hystrixService;
@GetMapping(value = "/hello-hystrix")
    public String hello(){
        return hystrixService.hello();
}


    @GetMapping(value = "/hello-exception")
    public String helloException(){
        return hystrixService.helloException();
}

    @GetMapping(value = "/hello-dashboard")
    public String dashboardException(){
        return hystrixService.dashboard();
}
}

備註: 分別寫了4個方法,分別是服務超時,服務異常,和高併發限流二種策略

spring-cloud-03-hystrix-request-b (服務請求叢集b)

spring:
  application:
    name: request-service

server:
  port: 7003
  context-path: /

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/

#啟動斷路器
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 1000

custom:
   rest:
    connect-timeout: 1000
    connection-request-timeout: 1000
    read-timeout: 30000


備註:埠號7003,其他同spring-cloud-03-hystrix-request-a

spring-cloud-03-hystrix-dashboard (服務監控面板)

pom.xml匯入dashboard

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>

application配置埠7004

spring:
  application:
    name: hystrix-dashboard

server:
  port: 7004
  context-path: /

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/


@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrixDashboard  //啟動斷路器監控
public class DashboardApplication {

    // 監控臺地址  http://192.168.0.102:7004/hystrix
    //檢視什麼樣的資料[監控服務] http://192.168.0.102:7002/hystrix.stream 並輸入到監控臺
public static void main(String[] args) {
        SpringApplication.run(DashboardApplication.class, args);
}
}

備註: @EnableHystrixDashboard 啟動斷路器監控

spring-cloud-03-hystrix-dashboard-turbine (叢集服務監控)

pom.xml匯入turbine

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>

spring:
  application:
    name: hystrix-turbine

server:
  port: 7005
  context-path: /

management:
prot:3001

turbine:
  app-config: request-service    #要監控的服務
cluster-name-expression: "'default'"
combine-host-port: true


eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/

備註: app-config 配置要監控的服務,spring-cloud-03-hystrix-request-a 和b

cluster-name-expression寫預設值

@SpringBootApplication
@EnableDiscoveryClient
@EnableTurbine  //啟動收集叢集斷路器
public class TruebineApplication {

    //監控臺地址  http://192.168.0.102:7004/hystrix
    //檢視什麼樣的資料[監控服務] http://192.168.0.102:7002/hystrix.stream 並輸入到監控臺
    //要監控的turbine地址[監控的hystrix-reuqest服務] http://localhost:7005/trubine.stream,輸入到dashboard
public static void main(String[] args) {
        SpringApplication.run(TruebineApplication.class, args);
}
}

備註:@EnableTurbine  開啟叢集服務監控

使用:

1:訪問http://192.168.0.102:7003/hello-hystrix ,由於服務睡眠4秒,而配置的服務超時時間custom.rest.read-timeout =3000 ,那麼服務會超時,斷路器通過fallbackMethod = "helloFallback" 最終執行 helloFallback方法,並返回  "------執行服務超時降級策略---" ,異常同理

2:訪問 http://192.168.0.102:7005/turbine.stream 獲取服務資料流

3:訪問面板路徑 http://localhost:7005/trubine.stream 資料要監控的服務地址 http://192.168.0.102:7005/turbine.stream

4:監控面板獲取實時監控資料

截圖一


截圖二


截圖三


截圖四