spring cloud sleuth 服务跟踪

一般sleuth是指服务跟踪的客户端,再每个应用引入相关的包就可以进行服务跟踪。简单的服务跟踪组成部分可分为:客户端,服务端,存储器。复杂的可以分成:客户端收集跟踪信息发送到消息中间件(rabbitmq,kafka等),然后服务端从消息中间件取数据,然后持久化存储。

客户端只要引入相关的jar包,配置好采样器的采样频率以及消息中心服务的URL即可。

本文主要讲解zipkin作为服务收集跟踪信息。存储采用mysql进行。以及将Zipkin注册到eureka。

虽然说官方不提倡我们自己编译,但是为了高可用与定制化,还是自己配置一个吧

如下配置:

server:
 port: 9400
 tomcat: 
   uri-encoding: UTF-8
 use-forward-headers: true
 compression:  
    enabled: true
    # compresses any response over min-response-size (default is 2KiB)
    # Includes dynamic json content and large static assets from zipkin-ui
    mime-types: application/json,application/javascript,text/css,image/svg
    
spring:
  application: 
   name: xxh-cloud-zipkin
  jmx:
     # reduce startup time by excluding unexposed JMX service
     enabled: false
  mvc:
    favicon:
      # zipkin has its own favicon
      enabled: false
  autoconfigure:
    exclude:
      # otherwise we might initialize even when not needed (ex when storage type is cassandra)
      - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
  

STORAGE_TYPE: mysql
HTTP_COLLECTOR_ENABLED: true
MYSQL_JDBC_URL: jdbc:mysql://192.16.70.51:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
MYSQL_USER: xxh
MYSQL_PASS: xxh
MYSQL_DB: test
MYSQL_MAX_CONNECTIONS: 50
MYSQL_USE_SSL: false


zipkin:
  self-tracing:
    # Set to true to enable self-tracing.
    enabled: ${SELF_TRACING_ENABLED:false}
    # percentage to self-traces to retain
    sample-rate: ${SELF_TRACING_SAMPLE_RATE:1.0}
    # Timeout in seconds to flush self-tracing data to storage.
    message-timeout: ${SELF_TRACING_FLUSH_INTERVAL:1}
  collector:
    # percentage to traces to retain
    sample-rate: ${COLLECTOR_SAMPLE_RATE:1.0}
    http:
      # Set to false to disable creation of spans via HTTP collector API
      enabled: ${HTTP_COLLECTOR_ENABLED:true}
      
  query:
    enabled: ${QUERY_ENABLED:true}
    # 1 day in millis
    lookback: ${QUERY_LOOKBACK:86400000}
    # The Cache-Control max-age (seconds) for /api/v2/services and /api/v2/spans
    names-max-age: 300
    # CORS allowed-origins.
    allowed-origins: "*"

  storage:
    strict-trace-id: ${STRICT_TRACE_ID:true}
    search-enabled: ${SEARCH_ENABLED:true}
    autocomplete-keys: ${AUTOCOMPLETE_KEYS:}
    autocomplete-ttl: ${AUTOCOMPLETE_TTL:3600000}
    autocomplete-cardinality: 20000
    type: ${STORAGE_TYPE:mem}
    mem:
      # Maximum number of spans to keep in memory.  When exceeded, oldest traces (and their spans) will be purged.
      # A safe estimate is 1K of memory per span (each span with 2 annotations + 1 binary annotation), plus
      # 100 MB for a safety buffer.  You'll need to verify in your own environment.
      # Experimentally, it works with: max-spans of 500000 with JRE argument -Xmx600m.
      max-spans: 500000
    mysql:
      jdbc-url: ${MYSQL_JDBC_URL:}
      host: ${MYSQL_HOST:localhost}
      port: ${MYSQL_TCP_PORT:3306}
      username: ${MYSQL_USER:}
      password: ${MYSQL_PASS:}
      db: ${MYSQL_DB:zipkin}
      max-active: ${MYSQL_MAX_CONNECTIONS:10}
      use-ssl: ${MYSQL_USE_SSL:false}
  ui:
    enabled: ${QUERY_ENABLED:true}
    ## Values below here are mapped to ZipkinUiProperties, served as /config.json
    # Default limit for Find Traces
    query-limit: 10
    # The value here becomes a label in the top-right corner
    environment:
    # Default duration to look back when finding traces.
    # Affects the "Start time" element in the UI. 1 hour in millis
    default-lookback: 3600000
    # When false, disables the "find a trace" screen
    search-enabled: ${SEARCH_ENABLED:true}
    # Which sites this Zipkin UI covers. Regex syntax. (e.g. http:\/\/example.com\/.*)
    # Multiple sites can be specified, e.g.
    # - .*example1.com
    # - .*example2.com
    # Default is "match all websites"
    instrumented: .*
    # URL placed into the <base> tag in the HTML
    base-path: /zipkin
    # When false, disables the "Try Lens UI" button in the navigation page
    suggest-lens: true

spring.main.web-application-type: none

# We are using Armeria instead of Tomcat. Have it inherit the default configuration from Spring
armeria:
  ports:
    - port: 9400
      protocols:
        - http
  gracefulShutdownQuietPeriodMillis: -1
  gracefulShutdownTimeoutMillis: -1


info:
  zipkin:
    version: "2.12.9"

logging:
  pattern:
    level: "%clr(%5p) %clr([%X{traceId}/%X{spanId}]){yellow}"
  level:
    # Silence Invalid method name: '__can__finagle__trace__v3__'
    com.facebook.swift.service.ThriftServiceProcessor: 'OFF'
#     # investigate /api/v2/dependencies
#     zipkin2.internal.DependencyLinker: 'DEBUG'
#     # log cassandra queries (DEBUG is without values)
#     com.datastax.driver.core.QueryLogger: 'TRACE'
#     # log cassandra trace propagation
#     com.datastax.driver.core.Message: 'TRACE'
#     # log reason behind http collector dropped messages
#     zipkin2.server.ZipkinHttpCollector: 'DEBUG'
#     zipkin2.collector.kafka.KafkaCollector: 'DEBUG'
#     zipkin2.collector.kafka08.KafkaCollector: 'DEBUG'
#     zipkin2.collector.rabbitmq.RabbitMQCollector: 'DEBUG'
#     zipkin2.collector.scribe.ScribeCollector: 'DEBUG'

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always
# Disabling auto time http requests since it is added in Undertow HttpHandler in Zipkin autoconfigure
# Prometheus module. In Zipkin we use different naming for the http requests duration
  metrics:
    web:
      server:
        auto-time-requests: false


xxh:
 eureka: 
  node1: 192.16.50.76
  node2: 192.16.50.77
  node3: 192.16.50.78
  port: 8886
 zipkin:
   node1: 192.16.50.76
            

eureka:
 instance:
  instance-id: ${xxh.zipkin.node1}:${spring.application.name}:${server.port}
  prefer-ip-address: true  #解决gateway转发微服务时UnknownHostException的问题
 client:
  register-with-eureka: true
  fetch-registry: true
  service-url:
   defaultZone: http://admin:admin@${xxh.eureka.node1}:${xxh.eureka.port}/eureka,http://admin:admin@${xxh.eureka.node2}:${xxh.eureka.port}/eureka,http://admin:admin@${xxh.eureka.node3}:${xxh.eureka.port}/eureka
 

      POM.XML

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.abcde.xxh</groupId>
    <artifactId>xxh-cloud-zipkin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>xxh-cloud-zipkin</name>
    <description>xxh-cloud-zipkin</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
    
        <dependency>
           <groupId>io.zipkin.java</groupId>
           <artifactId>zipkin-server</artifactId>
           <version>2.12.9</version>
        </dependency>
        <!-- 可视化查询界面 -->
        <dependency>
          <groupId>io.zipkin.java</groupId>
          <artifactId>zipkin-autoconfigure-ui</artifactId>
          <version>2.12.9</version>
        </dependency>
        <!-- 不同的存储引入不同的包即可 -->
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
            <version>2.12.9</version>
        </dependency>
        
        <!-- 集成eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </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>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
 

启动类:

package com.abcde.xxh.server.zipkin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin2.server.internal.EnableZipkinServer;

@SpringBootApplication
@EnableZipkinServer
public class XxhCloudZipkinApplication {

    private static Logger logger = LoggerFactory.getLogger(xxhCloudZipkinApplication.class);
    
    public static void main(String[] args) {
        logger.debug("xxhCloudGatewayApplication:::main:::::starting..::::");
        SpringApplication.run(xxhCloudZipkinApplication.class, args);
        logger.debug("xxhCloudGatewayApplication:::main:::::started success..::::");
    }

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值