hystrix配置dashboard

前言

Github:https://github.com/yihonglei/SpringCloud-Study

Eureka註冊中心:eureka-server

服務提供者(訂單服務):eureka-provider-order

Feign-api(服務接口抽象):eureka-feign-api

Feign客戶端消費(含hystrix和dashboard):eureka-consumer-feign-hystrix-dashboard

儀表盤:eureka-hystrix-dashboard

一 Feign、Hystrix、dashboard

Feign是一個聲明式的僞RPC的REST客戶端,基於接口的註解方式,很方便客戶端配置。

Spring Cloud集成Ribbon和Eureka以在使用Feign時提供負載均衡的http客戶端。

Hystrix基於開源框架Netflix實現了Spring Cloud Hystrix,該框架的目標在於通過控制哪些訪問遠程系統、

服務等,從而對於網絡延遲和故障提供更強大的容錯能力。

dashboard是hystrix監控儀表盤。

二 啓動eureka-server(註冊中心)

執行eureka-server項目EurekaServerApplication類的main方法。

三 啓動eureka-provider-order(服務提供者)

執行eureka-provider-order項目EurekaOrderApplication類的main方法。

四 eureka-feign-api(feign抽象接口)

打成jar包,以供別的項目使用。

五 eureka-consumer-feign-hystrix-dashboard

feign通訊,hystrix熔斷,hystrix開啓dashboard。

1、項目結構

2、pom.xml

<!-- 監控 -->
<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>

還需要引入feigin-api的jar包。

<!-- feign-api -->
<dependency>
    <groupId>com.lanhuigu</groupId>
    <artifactId>eureka-feign-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

  3、application.yml

# 註冊到eureka服務端的微服務名稱
spring:
  application:
    name: eureka-consumer-feign-hystrix-dashboard
# 服務提供端口
server:
  port: 8009
# 註冊到eureka服務端的地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  # 顯示指定微服務的名稱,默認ip:應用名稱:端口(192.168.1.7:eureka-consumer-feign-hystrix:8009)
  instance:
    instance-id: eureka-consumer-feign-hystrix-dashboard-8009
    prefer-ip-address: true
# 開啓feign支持hystrix,默認關閉
feign:
  hystrix:
    enabled: true

feign.hystrix.enabled=true開啓feign支持hystrix。 

 4、ServerConfig

package com.lanhuigu.config;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * server配置
 *
 * @auther: yihonglei
 * @date: 2019-07-08 19:10
 */
@Configuration
public class ServerConfig {
    @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;
    }
}

Spring Cloud F版本之後需要顯示配置hystrix的監控。 

5、UserController

package com.lanhuigu.controller;

import com.lanhuigu.order.OrderApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @RestController這個註解等價於spring mvc用法中的@Controller+@ResponseBody
 *
 * @auther: yihonglei
 * @date: 2019-06-17 22:30
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private OrderApi orderApi;

    @RequestMapping(value = "/queryUserInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public String queryUserInfo() {
        String orderInfo = orderApi.queryOrdersByUserId();

        return orderInfo;
    }
}

6、EurekaConsumerFeignHystrixDashboardApplication

package com.lanhuigu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @SpringBootApplication 啓動一個Spring Boot應用程序
 * @EnableDiscoveryClient 服務發現與註冊,當應用啓動時,將應用註冊到配置的註冊中心
 *
 * @auther: yihonglei
 * @date: 2019-06-19 11:27
 */
@EnableDiscoveryClient
@SpringBootApplication
@EnableCircuitBreaker
@EnableFeignClients(basePackages = "com.lanhuigu")
public class EurekaConsumerFeignHystrixDashboardApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaConsumerFeignHystrixDashboardApplication.class, args);
    }
}

六 服務說明

1、註冊中心

http://localhost:9000

看到eureka-consumer-feign-hystrix-dashboard-8009和eureka-provider-order-8001服務。

2、訪問監控

http://localhost:8009/actuator/hystrix.stream 

頁面一直打印ping:但是沒有東西,因爲還沒有對服務進行訪問。

訪問http://localhost:8009/user/queryUserInfo,觸發服務訪問。

 

當訪問服務後,再看監控臺,可以看到監控的內容,以json格式輸出,

json看起來不方便,所以引入了儀表盤,即監控的圖形界面。

七 eureka-hystrix-dashboard

1、項目結構

2、pom.xml

<!--加入依賴-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

3、application.yml

server:
  port: 9001
spring:
  application:
    name: eureka-hystrix-dashboard

4、EurekaHystrixDashboardApplication

package com.lanhuigu.hystrix.dashboard;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

/**
 * @SpringBootApplication 啓動一個Spring Boot應用程序
 * @EnableDiscoveryClient 服務發現與註冊,當應用啓動時,將應用註冊到配置的註冊中心
 *
 * @auther: yihonglei
 * @date: 2019-06-19 11:27
 */
@SpringBootApplication
@EnableHystrixDashboard
public class EurekaHystrixDashboardApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaHystrixDashboardApplication.class, args);
    }
}

@EnableHystrixDashboard開啓Hystrix-dashboard。 

5、訪問儀表盤

http://localhost:9001/hystrix/

 

6、將要監控地址配置入儀表盤

然後點擊Monitor Stream進入界面,然後就可以看到控制界面。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章