Spring Cloud Hystrix理解與實踐(一):搭建簡單監控集羣

 

 前言

  在分佈式架構中,所謂的斷路器模式是指當某個服務發生故障以後,經過斷路器的故障監控,向調用方返回一個錯誤響應,這樣就不會使得線程因調用故障服務被長時間佔用不釋放,避免故障的繼續蔓延Spring Cloud Hystrix實現了斷路器,線程隔離等一系列服務保護功能,它是基於Netflix的開源框架Hystrix實現的。java

  目的不是介紹Hystrix的與原理、及其使用等(有時間也要記錄啊),而是經過實戰搭建一個簡單的監控集羣,使用Hystrix Dashboard儀表盤動態監控展現以此來加深對Hystrix的認識與理解,爲何要記錄呢?這是由於網上資料甚少(或版本太低,不適用),同時加之書中的Spring Cloud版本與如今Spring Boot 2.x差距明顯。web

  本文主要參考《Spring Cloud 微服務實戰》(PDF電子版,須要的朋友能夠私聊或評論)spring

 


 

1、Hystrix 儀表盤

一、認識Hystrix儀表盤

  HystrixCommand與HystrixObserableCommand實例執行過程當中記錄的重要信息稱之爲Hystrix儀表盤,以供內部或者外部進行查詢使用。Spring Cloud整合儀表盤組件Hystrix Dashboard,主要用來實時監控Hystrix的各項指標信息,能夠幫咱們快速發現系統中存在的問題,從而及時地採起應對措施。express

  1)加入依賴瀏覽器

  特別注意Spring Boot 2.x版本引入的hystrix-dashboard依賴,否則可能訪問不了http://localhost:port/hystrix儀表盤頁面,註解@EnableHsytrixDashboard也可能找不到緩存

 <!-- hystrix 容錯機制 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>${spring-cloud-eureka.version}</version>
        </dependency>
        <!-- actuator監控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- Spring Boot 2.x以上版本 spring-cloud-starter-netflix-hystrix-dashboard 儀表盤,
        如下版本則須要spring-cloud-starter-hystrix-dashboard-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
            <version>${spring-cloud-eureka.version}</version>
        </dependency> 

  2)添加配置服務器

# 應用實例
spring:
  application:
    name: hystrix-dashboard

server:
  port: 8000

# actuator開放全部端點,Spring Boot 2.x與1.x不一樣,具體請查詢
management:
  endpoints:
    web:
      exposure:
        include: "*"

  3)增長註解:應用主類加上@EnableHsytrixDashboard,啓用Hystrix Dashboard功能。網絡

@EnableHystrixDashboard // 開啓Hystrix儀表盤
@SpringBootApplication
public class HystrixMonitorApplication {

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

}

  4)訪問http://localhost:8000/hystrix界面以下:架構

          

二、監控頁面介紹

從界面中咱們就能夠看到Hystrix Dashboard支持不一樣的三種監控方式:app

  1) 默認的集羣監控:經過URL http://turbine-hostname:port/turbine.stream

  2) 指定的集羣監控:經過URL http://turbine-hostname:port/turbine.stream?cluster=[clusterName]開啓

  3) 單體應用的監控:URL http://hystrix-app:port/hystrix.stream開啓,實現對具體某個服務實例的監控

   前二者關於集羣的監控須要整合turbine才能實現,而對於單體實例節點須要訪問實例的/hystrix.stream接口實現,咱們天然須要爲服務實例添加端點。只須要添加acutator與hystrix依賴,應用主程序類開啓斷路器@EnableCircuitBreaker註解與@EnableHystrixDashboard註解便可。

其中的參數:

  1)Delay:用來控制服務器上輪詢監控信息的延遲時間,默認爲2000ms。能夠經過該配置該屬性下降客戶端的網絡和CPU消耗。

  2)Ttile:對應進入監控後的的標題,如Hystrix,則進入監控頁面後以下圖紅框標題

此外,咱們在URL框輸入咱們須要監聽的某個服務實例/hystrix.stream接口,如http://localhost:8081/hystrix.stream,就能夠進入監控頁面

 

 

                      

監控頁面參數介紹:

1) 實心圓與曲線的含義

  實心圓顏色:健康度從綠色、黃色、橙色、紅色遞減

  實心圓大小:會根絕實例的請求流量發生變化,流量越大實心圓就越大。

  曲線:用來記錄2分鐘內流量的相對變化,能夠經過它來觀察流量的上升與降低。

2) 其它的指標參數:鼠標停留會顯示相應的說明

          

 

2、簡單監控架構

一、監控單實例的架構

  (1)架構圖

 

         

 

  (2)過程說明

    • 服務提供者:HELLO-SERVICE,提供一個接口如:http:/HELLO-SERVER/hello,讓消費者經過restTemplate(封裝好的HTTP)調用消費
    • 服務消費者:RIBBON-CONSUMER,會有ribbon承擔負載均衡的做用,分別輪詢訪問HELLO-SERVER-1與HELLO-SERVICE-2
    • 註冊中心:Spring Cloud Eureka,主要負責服務治理:服務的註冊、續約、剔除(更新)等
    • Hystrix儀盤表:經過/hystrix.stream接口監控某個服務實例,動態展現儀表盤數據。

  然而如今只針對一個實例來監控,而分佈式系統中每每有不少實例,咱們就須要利用Turbine和Hystrix Dashboard配置實現對集羣的監控

 

二、監控聚合服務

  須要經過Turbine來聚合RIBBON-CONSUMER-1與服務RIBBON-CONSUMER-2成一個服務展現監控信息,並輸出到Hystrix Dashboard中,只顯示一張監控圖,可是注意Hosts的數量爲2

                

 

  (1)架構圖

        

  (2)過程說明

    同上述「單實例監控」,不一樣的是此次服務消費者有RIBBON-CONSUMER-1與RIBBON-CONSUMER-2兩個,經過/turbine.stream接口聚合兩個服務實例(實則就是同一個服務不一樣實例)成一個服務,共同動態展現整個集羣的動態數據。對於集羣來講關注的是服務集羣的高可用性,因此Turbine會將相同服務做爲總體看待。

 

3、代碼實踐

一、實踐前的準備

  首先本示例使用的是Idea+Maven構造的項目工程的,因此先熟悉下idea如何構建一個簡單的Spring Boot項目

  1)新建項目:File->New->Project

  

  2)選擇Spring Initializr,選擇默認的https://start.spring.io(須要聯網),點擊Next

  

  3)填寫項目信息

  

  4)選擇依賴,這裏咱們只須要選擇web依賴便可,以後再加入相關Spring Cloud Hystrix的依賴,這是由於Spring Boot版本與Spring Cloud版本有相對應的關係,否則會衝突項目處處都是坑

  

  5)查看Spring Boot與Spring Cloud的版本對應關係從官網(https://spring.io/projects/spring-cloud)中查看,這裏使用的是Spring Boot 2.0.6.RELEASE與Spring Cloud Fincley.SR1

 

   

  6)咱們須要搭建如下的架構

            

 

  從圖中咱們知道咱們須要:

  1)兩個Eureaka Server提供高可用的註冊中心:

    分別對應工程eureka-server與eureke-slave-server,配置文件中register-with-eureka與fetch-registry保持默認true,相互註冊進行同步維護服務實例列表。

  2)兩個服務提供者實例提供HELLO-SERVICE/hello服務:

    對應工程hello-service,打包成jar包。

    經過命令 java -jar hello-service-0.0.1-SNAPSHOT.jar --server.port=8081 開啓實例HELLO-SERVICE-1

    經過命令 java -jar hello-service-0.0.1-SNAPSHOT.jar --server.port=8082 開啓實例HELLO-SERVICE-2

  3)兩個服務消費者實例消費HELLO-SERVICE/hello服務

    對應工程ribbon-consumer,使用ribbon開啓負載均衡,使用hystrix開啓斷路器功能,以後打包jar包。

    經過命令 java -jar ribbon-consumer-0.0.1-SNAPSHOT.jar --server.port=8083 開啓實例RIBBON-CONSUMER-1

    經過命令 java -jar ribbon-consumer-0.0.1-SNAPSHOT.jar --server.port=8084 開啓實例RIBBON-CONSUMER-2

  4)開啓Spring Cloud Circuit Breaker 斷路器

    引入相關依賴,應用程序中開啓註解便可,具體請看下面示例。

  5)消費者開啓負載均衡器

    服務消費者直接經過調用被@LoadBalanced註解修飾過的RestTemplate來實現面向服務的接口調用

  6)開啓Hystrix Dashboard儀表盤

    引入hystrix dashboard、turbine等相關依賴,應用程序中開啓註解便可,具體請看下面示例。

 

二、代碼示例 

1)服務治理工程:eureka-service與eureka-slave-service

pom.xml文件內容:eureka-service與eureka-slave-service都相同

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
        <spring-cloud-eureka.version>1.4.6.RELEASE</spring-cloud-eureka.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Spring Cloud Eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>${spring-cloud-eureka.version}</version>
        </dependency>
        <!-- 能夠刪除(須要同時刪除Test類),可是爲了避免麻煩就保留了 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

eureka-service的application.yml文件:

server:
  port: 5678
eureka:
  instance:
    hostname: master
  # slave註冊中心url
  client:
    service-url:
      defaultZone: http://slave:5679/eureka/
  # 關閉保護模式
  server:
    enable-self-preservation: false

eureka-slave-service的application.yml文件:

server:
  port: 5679
eureka:
  instance:
    hostname: slave
  client:
    service-url:
      defaultZone: http://master:5678/eureka/
  server:
    enable-self-preservation: false

注:須要在hosts文件中添加slave/master域名解析。

應用程序類開啓@EnableEurekaServer註解,代表是註冊中心服務器

@EnableEurekaServer
@SpringBootApplication
public class EurekaSlaveServerApplication {

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

}

2)服務提供者工程:hello-service

pom.xml文件內容:

注:這裏加入的依賴是spring-cloud-starter-eureka,不是spring-cloud-starter-eureka-server

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
        <spring-cloud-eureka.version>1.4.6.RELEASE</spring-cloud-eureka.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>${spring-cloud-eureka.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

application.yml配置文件:

spring:
  application:
    name: hello-service
# 註冊中心url
eureka:
  client:
    service-url:
      defaultZone: http://master:5678/eureka/,http://slave:5679/eureka/

  instance:
    # 定義服務失效的時間,默認爲90s。
    lease-expiration-duration-in-seconds: 60
    # 續約任務的調用時間間隔,默認爲30s
    lease-renewal-interval-in-seconds: 10

應用程序增長註解@EnableEurekaClient

@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

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

}

編寫/hello接口,提供消費者調用

@RestController
public class HelloController {


    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(){
        return "Hello World";
    }
}

3)服務消費者工程:ribbon-consumer

消費者服務工程是相對比較複雜的,須要幾個步驟:

  • 實現ribbon負載均衡功能:增長@LoadBalanced
  • 實現hystrix斷路器容錯功能:增長@EnableCircuitBreaker
  • 增長actuator監控:增長actuator/hystrix.stream端點,提供hystrix儀表盤monitor使用
  • 編寫容錯降級服務:發生異常超時等狀況以後,作相應容錯處理
  • 從新註冊restTemplate:增長@LoadBalanced實現負載均衡
  • 開啓請求緩存:對請求數據作緩存,減小壓力
  • 指定groupName與commandName

pom文件:既然要監控服務實例,天然要在該服務中添加Hystrix與actuator依賴。

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
        <spring-cloud-eureka.version>1.4.6.RELEASE</spring-cloud-eureka.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>${spring-cloud-eureka.version}</version>
        </dependency>
        <!-- ribbon 負載均衡 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>${spring-cloud-eureka.version}</version>
        </dependency>
        <!-- hystrix 容錯機制 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>${spring-cloud-eureka.version}</version>
        </dependency>
        <!-- actuator監控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

HelloService: 實現請求處理+降級服務+命令名指定+請求緩存(http://HELLO-SERVER/hello接口並無涉及數據交互,這裏只作展現)

@Service
public class HelloService {

    private final Logger logger = LoggerFactory.getLogger(HelloService.class);

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 消費者調用服務,而且開啓斷路器指定回調函數
     * 同步執行實現
     * @return
     */
    @CacheResult // 請求命令開啓緩存(能夠指定緩存key)
    @HystrixCommand(fallbackMethod = "helloFallback", groupKey = "HelloGroup", commandKey = "HelloKey") // 指定熔斷回調函數
    public String helloService(){
        long start = System.currentTimeMillis();
        // 模擬消息服務時間, getForEntity調用HELLO-SERVER服務的hello接口
        String result = restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
        long end = System.currentTimeMillis();
        logger.info("Spend time: "+(end - start));
        return result;

    }

    /**
     * 經過@CacheKey指定緩存key
     * @param id
     * @return
     */
    @CacheResult
    @HystrixCommand
    public User getUserById(@CacheKey String id){
       User user = restTemplate.getForObject("http://HELLO-SERVICE/hello", User.class);
       return user;
    }

    /**
     * 更新數據:更新緩存(刪除失效緩存)
     * @param user
     */
    @CacheRemove(commandKey = "getUserById")
    @HystrixCommand
    public void update(@CacheKey("id") User user){
        restTemplate.postForObject("http://HELLO-SERVICE/hello", user, User.class);
    }

    /**
     * 獲取cache key
     * @param id
     * @return
     */
    public String getUserByCacheKey(String id){
        logger.info("獲取緩存key....");
        return id;
    }
    /**
     * 消費者調用服務,而且開啓斷路器指定回調函數
     * 異步執行實現
     * @return
     */
    @HystrixCommand(fallbackMethod = "helloFallback") // 指定熔斷回調函數
    public Future<String> helloAsyncService(){
        return new AsyncResult<String>(){
            @Override
            public String invoke(){
               return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
            }
        };
    }

    /**
     * 降級服務
     * @return
     */
    public String helloFallback(){
        return "error";
    }
}
View Code

ConsumerController:對外調用接口/ribbon-consumer。

@RestController
public class ConsumerController {

    @Autowired
    HelloService helloService;


    @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
    public String helloConsumer(){
        return helloService.helloService();
    }
}

應用程序類RibbonConsumerApplication:開啓斷路器,開啓服務發現,註冊restTemplate,開啓負載均衡

注:能夠用@SpringCloudApplication代替,由於@SpringCloudApplication=@EnableCircuitBreaker+@EnableDiscoveryClient+@SpringBootApplication

@EnableCircuitBreaker // 開啓斷路器,也可使用@SpringCloudApplication
@EnableDiscoveryClient // 開啓服務發現,也可使用@SpringCloudApplication
@SpringBootApplication
public class RibbonConsumerApplication {


    /**
     * 註冊RestTemplate bean
     * 並開啓負載均衡
     * @return
     */
    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

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

}

application.yml文件:

# 服務消費者
spring:
  application:
    name: ribbon-consumer

# 註冊中心地址
eureka:
  client:
    service-url:
      defaultZone: http://master:5678/eureka/,http://slave:5679/eureka/
server:
  port: 9000
# 開放全部端點,與Spring boot 1.x版本有差別
management:
  endpoints:
    web:
      exposure:
        include: "*"

 

4)儀盤表Hystrix Dashboard工程:hystrix-dashboard

pom文件:注意這裏的hystrix-dashboard依賴於Spring boot 1.x引入的版本是不一樣的,具體看註釋

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
        <spring-cloud-eureka.version>1.4.6.RELEASE</spring-cloud-eureka.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- hystrix 容錯機制 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>${spring-cloud-eureka.version}</version>
        </dependency>
        <!-- actuator監控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- Spring Boot 2.x以上版本 spring-cloud-starter-netflix-hystrix-dashboard 儀表盤,
        如下版本則須要spring-cloud-starter-hystrix-dashboard-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
            <version>${spring-cloud-eureka.version}</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

 

應用程序類:開啓儀表盤註解@EnableHystrixDashboard

@EnableHystrixDashboard // 開啓Hystrix儀表盤
@SpringBootApplication
public class HystrixMonitorMonitorApplication {

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

}

application.yml:

spring:
  application:
    name: hystrix-dashboard

server:
  port: 8000

# 開放全部端點
management:
  endpoints:
    web:
      exposure:
        include: "*"

5)turbine集羣監控工程:turbine-monitor

經過turbine來聚合RIBBON-CONSUMER服務的監控信息,並提供/turbine.stream接口輸出給Hystrix Dashboard進行展現

pom.xml文件:增長turbine依賴+actuator依賴

 <properties>
        <java.turbineversion>1.8</java.turbineversion>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
        <spring-cloud-eureka.version>1.4.6.RELEASE</spring-cloud-eureka.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- actuator監控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-turbine</artifactId>
            <version>${spring-cloud-eureka.version}</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

application.yml文件:

spring:
  application:
    name: turbine-monitor
server: port: 8001 # 註冊中心地址 eureka: client: service-url: defaultZone: http://master:5678/eureka/,http://slave:5679/eureka/ turbine: # 指定須要收集監控信息的服務名(必定是大寫,服務註冊後都是大寫) app-config: RIBBON-CONSUMER # 同一主機上的服務經過主機+端口區分(默認同一個主機上聚合成一個服務) combine-host-port: true # 當啓動多個turbine服務構建不一樣的聚合集羣,該參數能夠區分不一樣的聚合集羣 # 同時能夠在Hystrix Stream的URL中指定Cluster參數指定 cluster-name-expression: new String("default") # 指定聚合哪些集羣,多個使用","分割,默認爲default。 aggregator: cluster-config: default

應用程序類TurbineMonitorApplication :開啓turbine監控

@SpringBootApplication
@EnableTurbine // 開啓turbine集羣監控
@EnableDiscoveryClient
public class TurbineMonitorApplication {

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

}

 

至此,全部的項目已經構建完畢,咱們如今能夠理清整個架構圖,使架構圖更加具體易懂。經過端口+IP的指定,咱們能夠清楚集羣中每一個角色的信息:

啓動項目:

第一步:啓動高可用的註冊中心,訪問註冊中心http://slave:5679或者http://master:5678

實例列表中兩個eureka服務實例已經相互註冊

而且已經相互註冊爲同步備份服務器,在http://slave:5679中(http://master:5678同理)

 

第二步:啓動服務提供者實例,開啓HELLO-SERVICE-1與HELLO-SERVICE-2服務

執行命令:

 java -jar hello-service-0.0.1-SNAPSHOT.jar --server.port=8081 開啓HELLO-SERVICE-1,保留CMD窗口

 java -jar hello-service-0.0.1-SNAPSHOT.jar --server.port=8082 開啓HELLO-SERVICE-2,保留CMD窗口

查看註冊中心是否已經註冊:

第三步:啓動服務消費者實例,開啓RIBBON-CONSUMER-1與RIBBON-CONSUMER-2服務

執行命令:

   java -jar ribbon-consumer-0.0.1-SNAPSHOT.jar --server.port=8083  開啓RIBBON-CONSUMER-1,保留CMD窗口

   java -jar ribbon-consumer-0.0.1-SNAPSHOT.jar --server.port=8084  開啓RIBBON-CONSUMER-2,保留CMD窗口

查看註冊中心是否已經註冊:

第四步:啓動turbine-monitor項目,訪問http://localhost:8001/turbine.stream

可能一開始的時候一直ping或者返回的data甚少,以下圖:

這是由於咱們尚未訪問http://localhost:8083/ribbon-consumer或者http://localhost:8084/ribbon-consumer接口,訪問後會有不少監控數據返回,以下圖:

 

第五步:啓動hystrix-dashboard項目,開啓hystrix儀表盤監控,輸入URL:http://localhost:8001/turbine.stream,點擊Monitor Stream,進入監控頁面

注:

  若一開始一直顯示Loading,緣由同上,都是由於咱們尚未訪問http://localhost:8083/ribbon-consumer或者http://localhost:8084/ribbon-consumer接口

  若一開始Hosts的數目只有1的話,說明咱們只訪問了http://localhost:8083/ribbon-consumer或者http://localhost:8084/ribbon-consumer接口其中一個,或者其中一個還沒啓用。同時啓動並訪問後就會顯示Hosts數目爲2

正常是這樣的:

                    

 

第六步:觀察監控圖、負載均衡。

 1)觀察負載均衡:

  不斷訪問http://localhost:8083/ribbon-consumer或者http://localhost:8084/ribbon-consumer,能夠看到服務提供實例不斷交替打印日誌,實際上就是輪詢負載均衡

  

 2)觀察監控圖:

   代碼中已經有模擬超時,若是超時(默認爲2000ms)則會顯示error字樣,監控圖中的超時數量會增長1

  

   增長訪問http://localhost:8083/ribbon-consumer或者http://localhost:8084/ribbon-consumer速度(不斷刷新),能夠看到監控圖中的流量圖呈上升趨勢

  

 


 

總結

  Spring Cloud的兩個重要角色Spring Cloud ribbon與Spring Cloud Hystrix能夠整合爲Feign,Feign除了提供這兩個強大的功能外,還提供了一種聲明式的Web服務端定義方式。在Spring Cloud Feign的實現下,咱們只須要建立一個接口並用註解的方式陪着它,便可完成對服務提供方的接口綁定,簡化了在使用Spring Cloud Ribbon時自行封裝服務調用客戶端的開發量。同時Spring Cloud Feign擴展了Spring MVC的註解支持。

  本文可能篇幅過長,可是每一步都很仔細,包括架構圖都是本身理解以後所畫。通過這段時間的學習,抽出差很少8小時的編寫(中間瀏覽器崩潰幾回,從頭來過三次),終於寫完了本篇博文,可是仍然有不足之處,敬請見諒,以後再慢慢摸索學習,但願本身能有所收穫!

相關文章
相關標籤/搜索