一、环境描述
2个服务注册中心(服务注册中心配置方法参考Spring Cloud Eureka 部署高可用注册中心),2个微服务应用 trace-1 和 trace-2,两个微服务应用中配置了分布式服务追踪 spring cloud sleuth。
二、trace-1
1. pom.xml
4.0.0
com.sande
trace-1
0.0.1-SNAPSHOT
jar
trace-1
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
UTF-8
UTF-8
1.8
Finchley.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-eureka
1.4.4.RELEASE
org.springframework.cloud
spring-cloud-starter-sleuth
org.springframework.cloud
spring-cloud-starter-ribbon
1.4.5.RELEASE
net.logstash.logback
logstash-logback-encoder
5.2
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
2. src/main/resources/application.properties
#spring.application.name=trace-1
server.port=9101
eureka.client.serviceUrl.defaultZone=http://localhost:1112/eureka/,http://localhost:1111/eureka/
logging.level.org.springframework=DEBUG
spring.sleuth.sampler.probability=1
spring.cloud.refresh.enabled=false
3.src/main/resources/bootstrap.properties
spring.application.name=trace-1
由于 logback-spring.xml 的加载在 application.properties 之前,所以当 spring.application.name 配置在 application.properties 中时 logback-spring.xml 无法获取 spring.applicatin.name 属性,因此将该属性配置到 bootstrap.properties 配置文件中。
4.src/main/resources/logback-spring.xml 配置 sleuth 使用 logback 输出 Logstatsh 能识别的 json 格式的日志,便于与 ELK 整合。
INFO
${CONSOLE_LOG_PATTERN}
utf8
${LOG_FILE}.json
${LOG_FILE}.json.%d{yyyy-MM-dd}.gz
7
UTC
{
"severity":"%level",
"service":"${springAppName:-}",
"trace":"%X{X-B3-TraceId:-}",
"span":"%X{X-B3-SpanId:-}",
"exportable":"%X{X-Span-Export:-}",
"pid":"${PID:-}",
"thread":"%thread",
"class":"%logger{40}",
"rest":"%message"
}
配置好后,启动 trace-1 将会在 trace-1 工程目录下生成一个 build 目录,下面创建了以应用名称命名的 JSON文件。
三、微服务应用 trace-2
1. pom.xml
4.0.0
com.sande
trace-2
0.0.1-SNAPSHOT
jar
trace-2
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
UTF-8
UTF-8
1.8
Finchley.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.0.1.RELEASE
org.springframework.cloud
spring-cloud-starter-ribbon
1.4.5.RELEASE
org.springframework.cloud
spring-cloud-starter-sleuth
org.springframework.boot
spring-boot-starter-test
test
net.logstash.logback
logstash-logback-encoder
5.2
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
2.src/main/resources/application.properties
#spring.application.name=trace-2
server.port=9102
eureka.client.serviceUrl.defaultZone=http://localhost:1112/eureka/,http://localhost:1111/eureka/
logging.level.org.springframework=DEBUG
spring.sleuth.sampler.probability=1
#spring.cloud.refresh.extra-refreshable=false
spring.cloud.refresh.enabled=false
3.src/main/resources/bootstrap.properties
spring.application.name=trace-2
由于 logback-spring.xml 的加载在 application.properties 之前,所以当 spring.application.name 配置在 application.properties 中时 logback-spring.xml 无法获取 spring.applicatin.name 属性,因此将该属性配置到 bootstrap.properties 配置文件中。
4.src/main/resources/logback-spring.xml 配置 sleuth 使用 logback 输出 Logstatsh 能识别的 json 格式的日志,便于与 ELK 整合。
INFO
${CONSOLE_LOG_PATTERN}
utf8
${LOG_FILE}.json
${LOG_FILE}.json.%d{yyyy-MM-dd}.gz
7
UTC
{
"severity":"%level",
"service":"${springAppName:-}",
"trace":"%X{X-B3-TraceId:-}",
"span":"%X{X-B3-SpanId:-}",
"exportable":"%X{X-Span-Export:-}",
"pid":"${PID:-}",
"thread":"%thread",
"class":"%logger{40}",
"rest":"%message"
}
配置好后,启动 trace-2 将会在 trace-2 工程目录下生成一个 build 目录,下面创建了以应用名称命名的 JSON文件。
四、与 Zipkin 整合通过HTTP收集
1.搭建 Zipkin 服务器
1)pom.xml
4.0.0
com.sande
zipkin-server
0.0.1-SNAPSHOT
jar
zipkin-server
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
UTF-8
UTF-8
1.8
Finchley.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
io.zipkin.java
zipkin-autoconfigure-ui
2.11.6
io.zipkin.java
zipkin-server
2.11.6
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
2) src/main/resources/application.properties
spring.application.name=zipkin-server
server.port=9411
management.metrics.web.server.auto-time-requests=false
3)应用主类
package com.sande.zipkin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin2.server.internal.EnableZipkinServer;
@EnableZipkinServer
@SpringBootApplication
public class ZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinApplication.class, args);
}
}
2.为应用引入和配置 zipkin 服务
1)在 trace-1和trace-2 的 pom.xml 加入 spring-cloud-sleuth-zipkin 依赖
org.springframework.cloud
spring-cloud-sleuth-zipkin
2.0.1.RELEASE
2)在 trace-1 和 trace-2 的 application.properties 中添加 Zipkin Server 的配置信息
spring.zipkin.base-url=http://localhost:9411
3. 测试
启动服务配置中心节点、zipkin server 、tarce-1和trace-2
=======================================================================================
=====================================================================================