1. 程式人生 > >微服務-springCloud快速實踐2:服務監控、熔斷器監控及zipkin呼叫鏈

微服務-springCloud快速實踐2:服務監控、熔斷器監控及zipkin呼叫鏈

springCloud快速實踐2:服務監控、熔斷器監控及zipkin呼叫鏈

完整程式碼下載連結:

https://github.com/2010yhh/springCloud-demos.git

環境

idea2018,jdk1.8,

springboot版本:springboot2.0.3.RELEASE,

springcloud版本:Finchley.SR1(2.0.1.RELEASE,)

1.服務監控

1.1服務端配置

pom:

 <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui</artifactId>
            <version>2.0.2</version>
        </dependency>

admin-web配置

server:
  port: 8760
spring:
  application:
    name: admin-server
  security:
    user:
      name: 'admin'
      password: 'admin'
  boot:
    admin:
      client:
          prefer-ip: true  #解決window下無法識別主機名的問題
      turbine:
          location: admin-server #管理所有的監控
          clusters: default
turbine:
  clusterNameExpression: new String("default")
  appConfig: cloud-feign #監控的應用,逗號隔開
  instanceUrlSuffix: hystrix.stream
eureka:
  client:
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

1.2客戶端配置

pom:

 <!--admin客戶端只需引入這個jar即可-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!--監控更多資訊-->
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--jmx bean管理需要-->
        <dependency>
        <groupId>org.jolokia</groupId>
        <artifactId>jolokia-core</artifactId>
        </dependency>

admin client配置,在eureka註冊中心註冊

# 如果被監控的服務沒有註冊到服務中心,需要增加admin的地址
# spring.boot.admin.url=http://localhost:8760

依次啟動各服務:

訪問服務監控介面:http://localhost:8760/
在這裡插入圖片描述

在這裡插入圖片描述
在這裡插入圖片描述

2.熔斷器監控

2.1配置

pom依賴:

hystrix:

<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>

turbine:

        <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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-turbine</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>

cloud-feign cloud-feign2的配置檔案示例:

spring.application.name=cloud-feign
server.port=8765
#eureka註冊地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#開啟hystrix熔斷器
feign.hystrix.enabled=true
management.endpoints.web.exposure.include = '*'
#Zipkin伺服器地址
spring.zipkin.baseUrl=http://192.168.159.142:9411
#便於測試
spring.sleuth.sampler.percentage=1
spring.application.name=cloud-feign2
server.port=8865
#eureka註冊地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#開啟hystrix熔斷器
feign.hystrix.enabled=true
#turbine配置
turbine.app-config=cloud-feign,cloud-feign2
#叢集(cluster)(turbine聚集資料的粒度)
turbine.aggregator.cluster-config=default
turbine.cluster-name-expression=new String("default")
management.endpoints.web.exposure.include = '*'
#更改turbine連線的預設uri ,預設為/actuator/hystrix.stream
turbine.instanceUrlSuffix = hystrix.stream
#Zipkin伺服器地址
spring.zipkin.baseUrl=http://192.168.159.142:9411
#便於測試
spring.sleuth.sampler.percentage=1


2.2測試

1.服務監控中啟動各程式後,訪問hystrix介面:http://localhost:8865/hystrix

以cloud-feign2作為turbine聚合客戶端,在介面中輸入
http://localhost:8765/hystrix.stream
http://localhost:8865/hystrix.stream
http://localhost:8865/turbine.stream
或者直接請求上述http url

cloud-feign2作為turbine聚合客戶端,採集資訊
在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述

3.zipkin呼叫鏈跟蹤

3.1配置

pom依賴:

<!-- 配置zipkin服務鏈路追蹤 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>

配置檔案:

#Zipkin伺服器地址
spring.zipkin.baseUrl=http://192.168.159.142:9411
#修改採集頻率,便於測試
spring.sleuth.sampler.percentage=1
#其它配置根據需求新增

3.2測試

zipkin使用參考:https://zipkin.io/pages/quickstart.html

單獨啟動zipkin server步驟:

#下載
wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec'
#啟動zipkin server
nohup java -jar zipkin.jar >>zipkin.out 2>&1 &

訪問:http://ip:9411

1.Trace:表示對一次請求的跟蹤,包括多個span,Trace就是樹結構的Span集合

2.Span:一個服務的處理

3.traceId:標記一次請求的跟蹤,相關的Spans都有相同的traceId

spanId:span的名稱,一般是介面方法的名稱

parentId:可選的id,當前Span的父Span id,通過parentId來保證Span之間的依賴關係,如果沒有parentId,表示當前Span為根Span;

測試:

1依次啟動zipkin服務,eureka各服務

2.訪問:http://localhost:8765/test?name=zyq http://localhost:8865/test?name=zyq

3.檢視zipkin介面:

實際專案中根據需要進行改造,如zipkin資料儲存由記憶體改為db或者els,採集上報由http改為mq等

在這裡插入圖片描述

在這裡插入圖片描述

在這裡插入圖片描述