SpringCloud之Turbine

前面的話】書接上文,本文的某些知識依賴個人上一篇SpringCLoud的文章:SpringCloud之Feign,若是沒有看過能夠先移步去看一下。前文提到了hystrix的應用,以及hystrix的監控,當時咱們在實際生產過程當中每每會在多個服務中或者說網關集羣中使用hystrix,這樣咱們來監控的是否再去分別查看當時的每一個應用的話,效率就會顯得很低下呢,這裏咱們就要用的上文提到的集羣監控了。java

壹、Turbine的簡介

看單個的Hystrix Dashboard的數據並無什麼多大的價值,要想看這個系統的Hystrix Dashboard數據就須要用到Hystrix Turbine。Hystrix Turbine將每一個服務Hystrix Dashboard數據進行了整合。Hystrix Turbine的使用很是簡單,只須要引入相應的依賴和加上註解和配置就能夠了。簡而言之:Turbine就是聚合監控多個Hystrix Dashboard的數據。git

貳、準備工做

新建一個feign子工程lovin-cloud-turbine,用於後面的操做。下面是主要的pom依賴:~~~pom lovincloud com.eelve.lovincloud 1.0-SNAPSHOT 4.0.0 github

<artifactId>lovin-cloud-turbine</artifactId>
    <packaging>jar</packaging>
    <name>lovincloudturbine</name>
    <version>0.0.1</version>
    <description>turbine監控</description>複製代碼
<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-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-turbine</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-turbine</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-turbine-amqp</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        -->
    </dependencies>複製代碼
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
~~~
- 這裏爲了安全,我這裏仍是添加**spring-boot-starter-security**
~~~yaml
server:
  port: 8808   # 服務端口號
spring:
  application:
    name: lovincloudturbine     # 服務名稱
  security:
    basic:
      enabled: true
    user:
      name: lovin
      password: ${REGISTRY_SERVER_PASSWORD:lovin}
eureka:
  client:
    serviceUrl:
      defaultZone: http://lovin:lovin@localhost:8881/eureka/   # 註冊到的eureka服務地址
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    metadata-map:
      user.name: lovin
      user.password: lovin
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
turbine:
  aggregator:
    clusterConfig: default   # 指定聚合哪些集羣,多個使用","分割,默認爲default。可以使用http://.../turbine.stream?cluster={clusterConfig之一}訪問
  appConfig: lovinfeignclient,lovinribbonclient  ### 配置Eureka中的serviceId列表,代表監控哪些服務
  clusterNameExpression: new String("default")
  # 1. clusterNameExpression指定集羣名稱,默認表達式appName;此時:turbine.aggregator.clusterConfig須要配置想要監控的應用名稱
  # 2. 當clusterNameExpression: default時,turbine.aggregator.clusterConfig能夠不寫,由於默認就是default
  # 3. 當clusterNameExpression: metadata['cluster']時,假設想要監控的應用配置了eureka.instance.metadata-map.cluster: ABC,則須要配置,同時turbine.aggregator.clusterConfig: ABC複製代碼

~~~web

  • 配置spring-boot-starter-security,這裏爲了方便我這裏放開全部請求
    ~~~java
    package com.eelve.lovin.config;

import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;spring

/**安全

  • @ClassName SecurityConfig
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 2019/8/16 14:13
  • @Version 1.0
    **/
    @Configuration
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll()
                .and().csrf().disable();
    }
}
~~~
- 在主類上添加**@EnableTurbine**,固然也須要註冊到註冊中心:
~~~java
package com.eelve.lovin;複製代碼

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.turbine.EnableTurbine;import org.springframework.cloud.netflix.turbine.stream.EnableTurbineStream;app

/**負載均衡

  • @ClassName LovinCloudTurbineApplication
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 2019/8/25 17:17
  • @Version 1.0
    *
    **/
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableTurbine
    public class LovinCloudTurbineApplication {
    public static void main(String[] args) {
    SpringApplication.run(LovinCloudTurbineApplication.class,args);
    }
    }
    ~~~
    • 改造lovin-feign-client,使之變成集羣,添加第二份配置文件

叄、啓動測試

  • 依次啓動eureka的服務端和兩個客戶端,以及lovin-feign-client、lovin-ribbon-client和新建的lovin-cloud-turbine
    咱們能夠看到服務已經所有啓動成功
    咱們能夠看到服務已經所有啓動成功
  • 而後訪問幾回http://localhost:8806/getHello和http://localhost:8805/hello使之產生熔斷器數據,而後訪問http://localhost:8806/hystrix按照提示選擇第一個集羣監控
    選擇聚合監控
    查看詳情

肆、消息隊列來作到異步監控

turbine服務端修改

  • 修改pom依賴
    ~~~pom


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client


    org.springframework.boot
    spring-boot-starter-security


    org.springframework.boot
    spring-boot-starter-web


    de.codecentric
    spring-boot-admin-starter-client
    2.1.6


    org.springframework.cloud
    spring-cloud-starter-turbine
    1.4.7.RELEASE


    org.springframework.cloud
    spring-cloud-netflix-turbine


    org.springframework.boot
    spring-boot-starter-actuator


    org.springframework.cloud
    spring-cloud-starter-turbine-amqp
    1.4.7.RELEASE

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
~~~
能夠看到這裏主要引入了spring-cloud-starter-turbine-amqp依賴,它實際上就是包裝了spring-cloud-starter-turbine-stream和pring-cloud-starter-stream-rabbit。
* 添加鏈接rabbitmq配置
~~~yaml
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
~~~
* 在應用主類中使用@EnableTurbineStream註解來啓用Turbine Stream的配置
~~~java
package com.eelve.lovin;複製代碼

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.turbine.EnableTurbine;import org.springframework.cloud.netflix.turbine.stream.EnableTurbineStream;異步

/**maven

  • @ClassName LovinCloudTurbineApplication
  • @Description TDO
  • @Author zhao.zhilue
  • @Date 2019/8/25 17:17
  • @Version 1.0
    *
    **/
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableTurbineStream
    public class LovinCloudTurbineApplication {
    public static void main(String[] args) {
    SpringApplication.run(LovinCloudTurbineApplication.class,args);
    }
    }
    ~~~

對服務消費者進行修改

  • 添加spring-cloud-netflix-hystrix-amqp依賴
    ~~~pom

    org.springframework.cloud
    spring-cloud-netflix-hystrix-amqp
    1.4.7.RELEASE

    ~~~
  • 添加鏈接rabbitmq配置
    ~~~yaml
    spring:
    rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
    ~~~

而後重啓服務以後,就能夠再次看到監控詳情

註冊中心聚合監控結果

伍、監控數據流圖

  • 咱們能夠看到咱們調用的服務再也不是像再上一篇文章中的直接訪問對應的服務,而是經過feign的Ribbon的負載均衡的去調用的,並且這裏說明一點,Ribbon的默認機制是輪詢。
  1. 直接使用Turbine監控

直接使用Turbine監控

  1. 使用RabbitMQ異步監控

使用RabbitMQ異步監控

  • 其中後者更能作到和業務解耦
    ---

陸、Turbine詳解

監控圖示

  • 咱們能夠在監控信息的左上部分找到兩個重要的圖形信息:一個實心圓和一條曲線。
  1. 實心圓:共有兩種含義。它經過顏色的變化表明了實例的健康程度,以下圖所示,它的健康度從綠色、黃色、橙色、紅色遞減。該實心圓除了顏色的變化以外,它的大小也會根據實例的請求流量發生變化,流量越大該實心圓就越大。因此經過該實心圓的展現,咱們就能夠在大量的實例中快速的發現故障實例和高壓力實例。
  2. 曲線:用來記錄2分鐘內流量的相對變化,咱們能夠經過它來觀察到流量的上升和降低趨勢。

---

相關文章
相關標籤/搜索