pom.xml代碼:java
<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> <groupId>com.joyce</groupId> <artifactId>joyce-component-zipkin</artifactId> <version>1.0</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.20.RELEASE</version> <relativePath /> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Spring Cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- zipkin --> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-storage-elasticsearch-http</artifactId> <version>2.8.4</version> <optional>true</optional> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin</artifactId> </dependency> <!-- zipkin --> <!-- 測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <!-- <version>3.8.1</version> --> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
application.properties配置文件代碼:web
server.port=8740
spring.application.name=joyce-component-zipkin
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
eureka.instance.instance-id=${spring.application.name}:${server.port}
eureka.instance.prefer-ip-address=true
#接受zipkin追蹤,追蹤數據會傳導給turbine實時監控(若是存在)
spring.zipkin.enabled=true
spring.sleuth.feign.enabled=true
spring.sleuth.hystrix.strategy.enabled=true
spring.sleuth.sampler.percentage=1.0
spring.zipkin.base-url=http://joyce-component-zipkin
############################## ES單點鏈接 ######################
#zipkin.storage.type=elasticsearch
#zipkin.storage.elasticsearch.hosts=192.168.10.140:9200
#zipkin.storage.elasticsearch.max-requests=100
#zipkin.storage.elasticsearch.index=zipkin
#zipkin.storage.elasticsearch.index-shards=0
#zipkin.storage.elasticsearch.index-replicas=0
############################## ES集羣鏈接,使用用戶名和密碼 ######################
zipkin.storage.type=elasticsearch //使用elasticsearch存儲zipkin追蹤數據
zipkin.storage.elasticsearch.cluster=joyce-elasticsearch //集羣的cluster name
zipkin.storage.elasticsearch.hosts=192.168.10.110:9200,192.168.10.120:9200,192.168.10.130:9200
zipkin.storage.elasticsearch.max-requests=100
zipkin.storage.elasticsearch.timeout=1000
zipkin.storage.elasticsearch.index=zipkin //存儲zipkin數據時,ES的數據索引以zipkin爲前綴,示例:zipkin-2019-06-09
zipkin.storage.elasticsearch.date-separator=- //ES索引數據的日期分隔符,默認爲「-」
zipkin.storage.elasticsearch.index-shards=6 //集羣的分片個數
zipkin.storage.elasticsearch.index-replicas=0 //集羣的備(從)個數
zipkin.storage.elasticsearch.username=elastic //用戶名
zipkin.storage.elasticsearch.password=1234567 //密碼
zipkin.storage.type前綴注入到zipkin-autoconfigure-storage-elasticsearch-http-2.8.4.jar包裏的TracingZipkinElasticsearchHttpStorageAutoConfiguration類。spring
@ConditionalOnBean(Tracing.class) @ConditionalOnProperty(name = "zipkin.storage.type", havingValue = "elasticsearch") @Configuration class TracingZipkinElasticsearchHttpStorageAutoConfiguration {
zipkin.storage.elasticsearch前綴注入到zipkin-autoconfigure-storage-elasticsearch-http-2.8.4.jar包裏的ZipkinElasticsearchHttpStorageProperties類。apache
@ConfigurationProperties("zipkin.storage.elasticsearch") class ZipkinElasticsearchHttpStorageProperties implements Serializable {
SpringCloud啓動類代碼:app
package com.joyce.zipkin; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import zipkin.server.internal.EnableZipkinServer; @SpringBootApplication @EnableZipkinServer @EnableHystrix @EnableHystrixDashboard @EnableEurekaClient public class ComponentZipkinApplication { private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(ComponentZipkinApplication.class); public static void main(String[] args) { SpringApplication.run(ComponentZipkinApplication.class, args); } }