目录
一、Zipkin服务器搭建
二、Sleuth服务调用链的搭建
1、基础服务搭建
2、sleuth-provider-provider模块搭建
1)、启动类添加@EnableEurekaClient
2)、为方便,直接在根目录下创建Controller和Service
3)、使用两个Spring Profile启动两台服务(直接将bootstrap.properties中的配置放到application.properties中)
3、sleuth-provider模块搭建
1)、启动类添加@EnableEurekaClient
2)、为方便,直接在根目录下创建Controller和Service
3)、使用两个Spring Profile启动两台服务(直接将bootstrap.properties中的配置放到application.properties中)
4、sleuth-consumer模块搭建
1)、启动类添加@EnableEurekaClient
2)、为方便,直接在根目录下创建Controller和Service
3)、application.properties(直接将bootstrap.properties中的配置放到application.properties中)
5、进行服务调用,查看效果
目地址为: https://github.com/kevin-lihongmin/spring-cloud-project-kevin/tree/master/zipkin-demo,其中Zipkin服务不可用,所以可以使用单独的包进行部署。zipkin服务器包地址: https://pan.baidu.com/s/1LYpoDUJxrHoJ3Z2Z4L0yTw ,密码: ee3x。为了能够测试服务的调用链路,所以使用了三个服务 sleuth-consumer -> sleuth-provider -> sleuth-provider-provider。之前的Spring Cloud Robbin搭建中有说明,ribbon有在本地配置中添加服务端列表的方式和在注册中心或者服务者列表两种调用方式,现在把三个服务都注册到注册中心,动态进行调用,ribbon-demo详见Spring Cloud Ribbon负载均衡实现。调用大致链路如下:
一、Zipkin服务器搭建
Zipkin直接实现上面的地址下载:.jar包,直接在本地或者上传服务器后,使用 java -jar zipkin-server-2.12.9-exec.jar 进行启动,如下:
D:\Zipkin>java -jar zipkin-server-2.12.9-exec.jar
********
** **
* *
** **
** **
** **
** **
********
****
****
**** ****
****** **** ***
****************************************************************************
******* **** ***
**** ****
**
**
***** ** ***** ** ** ** ** **
** ** ** * *** ** **** **
** ** ***** **** ** ** ***
****** ** ** ** ** ** ** **
:: Powered by Spring Boot :: (v2.1.4.RELEASE)
2019-06-05 20:43:12.554 INFO 9292 --- [ main] z.s.ZipkinServer : Starting ZipkinServer on 266WOQ68SAE7ZX4 with PID 9292 (D:\Zipkin\zipkin-server-2.12.9-exec.jar started by Administrator in D:\Zipkin)
2019-06-05 20:43:12.560 INFO 9292 --- [ main] z.s.ZipkinServer : The following profiles are active: shared
2019-06-05 20:43:13.676 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.verboseExceptions: false (default)
2019-06-05 20:43:13.677 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.verboseSocketExceptions: false (default)
2019-06-05 20:43:13.678 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.verboseResponses: false (default)
2019-06-05 20:43:13.681 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.useEpoll: false (default)
2019-06-05 20:43:15.741 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.useOpenSsl: true (default)
2019-06-05 20:43:15.742 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.maxNumConnections: 2147483647 (default)
2019-06-05 20:43:15.742 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.numCommonWorkers: 24 (default)
2019-06-05 20:43:15.743 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.numCommonBlockingTaskThreads: 200 (default)
2019-06-05 20:43:15.745 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultMaxRequestLength: 10485760 (default)
2019-06-05 20:43:15.746 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultMaxResponseLength: 10485760 (default)
2019-06-05 20:43:15.746 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultRequestTimeoutMillis: 10000 (default)
2019-06-05 20:43:15.747 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultResponseTimeoutMillis: 15000 (default)
2019-06-05 20:43:15.747 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultConnectTimeoutMillis: 3200 (default)
2019-06-05 20:43:15.748 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultServerIdleTimeoutMillis: 15000 (default)
2019-06-05 20:43:15.748 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultClientIdleTimeoutMillis: 10000 (default)
2019-06-05 20:43:15.749 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultHttp2InitialConnectionWindowSize: 1048576 (default)
2019-06-05 20:43:15.749 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultHttp2InitialStreamWindowSize: 1048576 (default)
2019-06-05 20:43:15.750 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultHttp2MaxFrameSize: 16384 (default)
2019-06-05 20:43:15.750 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultHttp2MaxStreamsPerConnection: 2147483647 (default)
2019-06-05 20:43:15.751 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultHttp2MaxHeaderListSize: 8192 (default)
2019-06-05 20:43:15.751 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultHttp1MaxInitialLineLength: 4096 (default)
2019-06-05 20:43:15.752 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultHttp1MaxHeaderSize: 8192 (default)
2019-06-05 20:43:15.753 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultHttp1MaxChunkSize: 8192 (default)
2019-06-05 20:43:15.754 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultUseHttp2Preface: true (default)
2019-06-05 20:43:15.754 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultUseHttp1Pipelining: false (default)
2019-06-05 20:43:15.755 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultBackoffSpec: exponential=200:10000,jitter=0.2 (default)
2019-06-05 20:43:15.755 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.defaultMaxTotalAttempts: 10 (default)
2019-06-05 20:43:15.756 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.routeCache: maximumSize=4096 (default)
2019-06-05 20:43:15.757 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.compositeServiceCache: maximumSize=256 (default)
2019-06-05 20:43:15.758 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.parsedPathCache: maximumSize=4096 (default)
2019-06-05 20:43:15.758 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.headerValueCache: maximumSize=4096 (default)
2019-06-05 20:43:15.759 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.cachedHeaders: :authority,:scheme,:method,accept-encoding,content-type (default)
2019-06-05 20:43:15.760 INFO 9292 --- [ main] c.l.a.c.Flags : com.linecorp.armeria.annotatedServiceExceptionVerbosity: unhandled (default)
2019-06-05 20:43:15.761 INFO 9292 --- [ main] c.l.a.c.Flags : /dev/epoll not available: java.lang.IllegalStateException: Only supported on Linux
2019-06-05 20:43:15.761 INFO 9292 --- [ main] c.l.a.c.Flags : Using OpenSSL: BoringSSL, 0x1010007f
2019-06-05 20:43:15.852 INFO 9292 --- [ main] c.l.a.c.u.SystemInfo : Hostname: 266woq68sae7zx4 (from 'hostname' command)
2019-06-05 20:43:16.393 INFO 9292 --- [oss-http-*:9411] c.l.a.s.Server : Serving HTTP at /0:0:0:0:0:0:0:0:9411 - http://127.0.0.1:9411/
2019-06-05 20:43:16.395 INFO 9292 --- [ main] c.l.a.s.ArmeriaAutoConfiguration : Armeria server started at ports: {/0:0:0:0:0:0:0:0:9411=ServerPort(/0:0:0:0:0:0:0:0:9411, [http])}
2019-06-05 20:43:16.420 INFO 9292 --- [ main] c.d.d.core : DataStax Java driver 3.7.1 for Apache Cassandra
2019-06-05 20:43:16.428 INFO 9292 --- [ main] c.d.d.c.GuavaCompatibility : Detected Guava >= 19 in the classpath, using modern compatibility layer
2019-06-05 20:43:16.564 INFO 9292 --- [ main] c.d.d.c.Native : Could not load JNR C Library, native system calls through this library will not be available (set this logger level to DEBUG to see the full stack trace).
2019-06-05 20:43:16.566 INFO 9292 --- [ main] c.d.d.c.ClockFactory : Using java.lang.System clock to generate timestamps.
2019-06-05 20:43:16.660 INFO 9292 --- [ main] z.s.ZipkinServer : Started ZipkinServer in 4.523 seconds (JVM running for 5.041)
启动后默认端口为 9411,则访问地址为: http://localhost:9411/ , 在没上上报数据前方问界面:
二、Sleuth服务调用链的搭建
1、基础服务搭建
在spring.start.io中添加Web、Actuator模块,然后在该项目下创建三个子模块,每个模块pom中都添加如下配置:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
2、sleuth-provider-provider模块搭建
1)、启动类添加@EnableEurekaClient
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* 配置服务器端,启动
*
* @author kevin
* @date 2019/5/15 10:42
* @since
*/
@EnableEurekaClient
@SpringBootApplication
public class SleuthProviderProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SleuthProviderProviderApplication.class, args);
}
}
2)、为方便,直接在根目录下创建Controller和Service
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 用户 Controller
*
* @author kevin
* @date 2019/5/16 9:50
*/
@RestController
public class UserProviderProviderController {
/**
* logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger(UserProviderProviderController.class);
@Autowired
private UserProviderProviderService userProviderProviderService;
@GetMapping("userInfo")
public String getUserInfo() throws InterruptedException {
LOGGER.info("get userInfo");
return userProviderProviderService.getUser();
}
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
/**
*
* @author kevin
* @date 2019/5/16 10:23
*/
@Service
public class UserProviderProviderService {
/**
* logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger(UserProviderProviderService.class);
public String getUser() throws InterruptedException {
LOGGER.info("user Server , get User Info!");
Thread.sleep(30L);
return "{'id': 1}";
}
}
3)、使用两个Spring Profile启动两台服务(直接将bootstrap.properties中的配置放到application.properties中)
application-sleuth-provider-provider-1.properties配置如下:
# 配置服务器端口
server.port = 8901
# 配置服务器名称
spring.application.name = sleuth-provider-provider
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
spring.cloud.loadbalancer.retry.enabled = true
eureka.client.healthcheck.enabled = true
eureka.client.serviceUrl.defaultZone = http://127.0.0.1:8761/eureka/,\
http://127.0.0.1:8760/eureka/,http://127.0.0.1:8759/eureka/
# 上报Zipkin服务器开关
spring.zipkin.enabled=true
# sleuth默认为上报为false, 现设置上报zipkin的服务地址
spring.zipkin.base-url=http://127.0.0.1:9411/
# span的采样率,默认为 0.1
spring.sleuth.sampler.probability = 1
# 为了使用速率限制采样器,请设置spring.sleuth.sampler.rate属性以选择每秒间隔接受的trace量,
# 最小数字为0,最大值为2,147,483,647(最大int)
spring.sleuth.sampler.rate = 10000
application-sleuth-provider-provider-2.properties配置如下:
# 配置服务器端口
server.port = 8902
# 配置服务器名称
spring.application.name = sleuth-provider-provider
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
spring.cloud.loadbalancer.retry.enabled = true
eureka.client.healthcheck.enabled = true
eureka.client.serviceUrl.defaultZone = http://127.0.0.1:8761/eureka/,\
http://127.0.0.1:8760/eureka/,http://127.0.0.1:8759/eureka/
# 开启Zipkin上报
spring.zipkin.enabled=true
# sleuth默认为上报为false, 现设置上报zipkin的服务地址
spring.zipkin.baseUrl = http://127.0.0.1:9411/
# span的采样率,默认为 0.1
spring.sleuth.sampler.probability = 1
# 为了使用速率限制采样器,请设置spring.sleuth.sampler.rate属性以选择每秒间隔接受的trace量,
# 最小数字为0,最大值为2,147,483,647(最大int)
spring.sleuth.sampler.rate = 10000
3、sleuth-provider模块搭建
1)、启动类添加@EnableEurekaClient
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* 配置服务器端,启动
*
* @author kevin
* @date 2019/5/15 10:42
* @since
*/
@EnableEurekaClient
@SpringBootApplication
@RibbonClients({@RibbonClient(name = "sleuth-provider-provider")})
public class SleuthProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SleuthProviderApplication.class, args);
}
/**
* 将{@link RestTemplate} 注解成一个{@code Bean}
*
* @return {@link RestTemplate }
*/
@Bean
@LoadBalanced
public RestTemplate initRestTemplate() {
return new RestTemplate(new HttpComponentsClientHttpRequestFactory());
}
}
2)、为方便,直接在根目录下创建Controller和Service
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 用户 Controller
*
* @author kevin
* @date 2019/5/16 9:50
*/
@RestController
public class UserProviderController {
/**
* logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger(UserProviderController.class);
@Autowired
private UserProviderService userProviderService;
@GetMapping("userInfo")
public String getUserInfo() throws InterruptedException {
LOGGER.info("get userInfo");
return userProviderService.getUser();
}
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
*
* @author kevin
* @date 2019/5/16 10:23
*/
@Service
public class UserProviderService {
/**
* logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger(UserProviderService.class);
/**
* 获取配置文件中的服务url
*/
@Value(value = "${config.user.provider.provider}")
private String configUserProvider;
@Autowired
private RestTemplate restTemplate;
public String getUser() throws InterruptedException {
LOGGER.info("user Server , get User Info!");
Thread.sleep(30L);
return restTemplate.getForObject(configUserProvider, String.class);
}
}
3)、使用两个Spring Profile启动两台服务(直接将bootstrap.properties中的配置放到application.properties中)
application-sleuth-provider-1.properties配置如下:
# 配置服务器端口
server.port = 8903
# 配置服务器名称
spring.application.name = sleuth-provider
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
spring.cloud.loadbalancer.retry.enabled = true
eureka.client.healthcheck.enabled = true
eureka.client.serviceUrl.defaultZone = http://127.0.0.1:8761/eureka/,\
http://127.0.0.1:8760/eureka/,http://127.0.0.1:8759/eureka/
config.user.provider.provider=http://sleuth-provider-provider/userInfo
spring.zipkin.enabled=true
# sleuth默认为上报为false, 现设置上报zipkin的服务地址
spring.zipkin.base-url=http://127.0.0.1:9411/
# span的采样率,默认为 0.1
spring.sleuth.sampler.probability = 1
# 为了使用速率限制采样器,请设置spring.sleuth.sampler.rate属性以选择每秒间隔接受的trace量,
# 最小数字为0,最大值为2,147,483,647(最大int)
spring.sleuth.sampler.rate = 10000
application-sleuth-provider-2.properties配置如下:
# 配置服务器端口
server.port = 8904
# 配置服务器名称
spring.application.name = sleuth-provider
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
spring.cloud.loadbalancer.retry.enabled = true
eureka.client.healthcheck.enabled = true
eureka.client.serviceUrl.defaultZone = http://127.0.0.1:8761/eureka/,\
http://127.0.0.1:8760/eureka/,http://127.0.0.1:8759/eureka/
config.user.provider.provider=http://sleuth-provider-provider/userInfo
spring.zipkin.enabled=true
# sleuth默认为上报为false, 现设置上报zipkin的服务地址
spring.zipkin.base-url=http://127.0.0.1:9411/
# span的采样率,默认为 0.1
spring.sleuth.sampler.probability = 1
# 为了使用速率限制采样器,请设置spring.sleuth.sampler.rate属性以选择每秒间隔接受的trace量,
# 最小数字为0,最大值为2,147,483,647(最大int)
spring.sleuth.sampler.rate = 10000
4、sleuth-consumer模块搭建
1)、启动类添加@EnableEurekaClient
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* 服务调用链为 :
* config-client-consumer -> config-client-provider -> config-client-provider-provider
*
* 首先所有的服务都注册到 Eureka-Server集中中心, 当所有服务都配置了maven spring-cloud-starter-sleuth
* 则默认 Spring Cloud Sleuth 已经与日志进行管理,在没有调用时,日志格式为:
* 2019-05-16 13:14:26.232 INFO [config-client-provider-provider,,,] 10768 --- [trap-executor-0] 。。。
*
* 当服务开始从web 使用链接 http://localhost:8905/userInfo 进行调用时,每个服务的日志如下:
* config-client-consumer:
* 2019-05-16 13:05:48.980 INFO [config-client-consumer,39d9e7cd26628cd0,39d9e7cd26628cd0,false] 7228 --- [nio-8905-exec-4] 。。。
* config-client-provider:
* 2019-05-16 13:05:49.015 INFO [config-client-provider,39d9e7cd26628cd0,5c9eadd20f58fd77,false] 9404 --- [nio-8903-exec-8] 。。。
* config-client-provider-provider:
* 2019-05-16 13:05:49.052 INFO [config-client-provider-provider,39d9e7cd26628cd0,45f564eb361448b3,false] 10768 --- [nio-8901-exec-4] 。。。
*
* @author kevin
* @date 2019/5/15 10:42
* @since
*/
@EnableEurekaClient
@SpringBootApplication
@RibbonClients({@RibbonClient(name = "sleuth-provider")})
public class SleuthConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SleuthConsumerApplication.class, args);
}
/**
* 将{@link RestTemplate} 注解成一个{@code Bean}
*
* @return {@link RestTemplate }
*/
@Bean
@LoadBalanced
public RestTemplate initRestTemplate() {
return new RestTemplate(new HttpComponentsClientHttpRequestFactory());
}
}
2)、为方便,直接在根目录下创建Controller和Service
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 用户 Controller
*
* @author kevin
* @date 2019/5/16 9:50
*/
@RestController
public class UserConsumerController {
/**
* logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger(UserConsumerController.class);
@Autowired
private UserConsumerService userConsumerService;
@GetMapping("userInfo")
public String getUserInfo() throws InterruptedException {
LOGGER.info("get userInfo");
return userConsumerService.getUser();
}
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class UserConsumerService {
/**
* logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger(UserConsumerService.class);
@Autowired
private RestTemplate restTemplate;
/**
* 获取配置文件中的服务url
*/
@Value(value = "${config.user.provider}")
private String configUserProvider;
public String getUser() throws InterruptedException {
LOGGER.info("user Server , get User Info!");
Thread.sleep(30L);
return restTemplate.getForObject(configUserProvider, String.class);
}
}
3)、application.properties(直接将bootstrap.properties中的配置放到application.properties中)
# 配置服务器端口
server.port = 8905
# 配置服务器名称
spring.application.name = sleuth-consumer
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
spring.cloud.loadbalancer.retry.enabled = true
eureka.client.healthcheck.enabled = true
eureka.client.serviceUrl.defaultZone = http://127.0.0.1:8761/eureka/,\
http://127.0.0.1:8760/eureka/,http://127.0.0.1:8759/eureka/
config.user.provider=http://sleuth-provider/userInfo
spring.zipkin.enabled=true
# sleuth默认为上报为false, 现设置上报zipkin的服务地址
spring.zipkin.base-url=http://127.0.0.1:9411/
# span的采样率,默认为 0.1
spring.sleuth.sampler.probability = 1
# 为了使用速率限制采样器,请设置spring.sleuth.sampler.rate属性以选择每秒间隔接受的trace量,
# 最小数字为0,最大值为2,147,483,647(最大int)
spring.sleuth.sampler.rate = 10000
5、进行服务调用,查看效果
启用服务后,在注册中看到的注册服务列表如下:
在没有进行服务调用的时候,打印日志如下:
2019-06-05 21:24:52.123 INFO [sleuth-consumer,,,] 1040 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8905
其中 INFO [sleuth-consumer,,,] 内四个参数分别为:[服务名称,traceId, spanId, boolean类型的是否开启上报]
当在浏览器中输入 http://localhost:8905/userInfo 调用sleuth-consumer服务后,调用服务链日志如下:
sleuth-consumer日志:
2019-06-05 21:36:29.432 INFO [sleuth-consumer,c4586f2a32f2b4c6,c4586f2a32f2b4c6,true] 1040 --- [nio-8905-exec-1] c.k.s.consumer.UserConsumerController : get userInfo
2019-06-05 21:36:29.433 INFO [sleuth-consumer,c4586f2a32f2b4c6,c4586f2a32f2b4c6,true] 1040 --- [nio-8905-exec-1] c.k.sleuth.consumer.UserConsumerService : user Server , get User Info!
2019-06-05 21:36:29.588 INFO [sleuth-consumer,c4586f2a32f2b4c6,11bc3ea8ab71d083,true] 1040 --- [nio-8905-exec-1] c.netflix.config.ChainedDynamicProperty : Flipping property: sleuth-provider.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-06-05 21:36:29.607 INFO [sleuth-consumer,c4586f2a32f2b4c6,11bc3ea8ab71d083,true] 1040 --- [nio-8905-exec-1] c.n.u.concurrent.ShutdownEnabledTimer : Shutdown hook installed for: NFLoadBalancer-PingTimer-sleuth-provider
2019-06-05 21:36:29.608 INFO [sleuth-consumer,c4586f2a32f2b4c6,11bc3ea8ab71d083,true] 1040 --- [nio-8905-exec-1] c.netflix.loadbalancer.BaseLoadBalancer : Client: sleuth-provider instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=sleuth-provider,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2019-06-05 21:36:29.612 INFO [sleuth-consumer,c4586f2a32f2b4c6,11bc3ea8ab71d083,true] 1040 --- [nio-8905-exec-1] c.n.l.DynamicServerListLoadBalancer : Using serverListUpdater PollingServerListUpdater
2019-06-05 21:36:29.626 INFO [sleuth-consumer,c4586f2a32f2b4c6,11bc3ea8ab71d083,true] 1040 --- [nio-8905-exec-1] c.netflix.config.ChainedDynamicProperty : Flipping property: sleuth-provider.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-06-05 21:36:29.627 INFO [sleuth-consumer,c4586f2a32f2b4c6,11bc3ea8ab71d083,true] 1040 --- [nio-8905-exec-1] c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client sleuth-provider initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=sleuth-provider,current list of Servers=[localhost:8903, localhost:8904],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone; Instance count:2; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;]
},Server stats: [[Server:localhost:8903; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
, [Server:localhost:8904; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@301f2cc6
sleuth-provider日志:
2019-06-05 21:36:29.713 INFO [sleuth-provider,c4586f2a32f2b4c6,11bc3ea8ab71d083,true] 9644 --- [nio-8903-exec-1] c.k.s.provider.UserProviderController : get userInfo
2019-06-05 21:36:29.713 INFO [sleuth-provider,c4586f2a32f2b4c6,11bc3ea8ab71d083,true] 9644 --- [nio-8903-exec-1] c.k.sleuth.provider.UserProviderService : user Server , get User Info!
2019-06-05 21:36:29.963 INFO [sleuth-provider,c4586f2a32f2b4c6,a528f9b7295f4de4,true] 9644 --- [nio-8903-exec-1] c.netflix.config.ChainedDynamicProperty : Flipping property: sleuth-provider-provider.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-06-05 21:36:29.981 INFO [sleuth-provider,c4586f2a32f2b4c6,a528f9b7295f4de4,true] 9644 --- [nio-8903-exec-1] c.n.u.concurrent.ShutdownEnabledTimer : Shutdown hook installed for: NFLoadBalancer-PingTimer-sleuth-provider-provider
2019-06-05 21:36:29.982 INFO [sleuth-provider,c4586f2a32f2b4c6,a528f9b7295f4de4,true] 9644 --- [nio-8903-exec-1] c.netflix.loadbalancer.BaseLoadBalancer : Client: sleuth-provider-provider instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=sleuth-provider-provider,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2019-06-05 21:36:29.985 INFO [sleuth-provider,c4586f2a32f2b4c6,a528f9b7295f4de4,true] 9644 --- [nio-8903-exec-1] c.n.l.DynamicServerListLoadBalancer : Using serverListUpdater PollingServerListUpdater
2019-06-05 21:36:29.998 INFO [sleuth-provider,c4586f2a32f2b4c6,a528f9b7295f4de4,true] 9644 --- [nio-8903-exec-1] c.netflix.config.ChainedDynamicProperty : Flipping property: sleuth-provider-provider.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-06-05 21:36:29.999 INFO [sleuth-provider,c4586f2a32f2b4c6,a528f9b7295f4de4,true] 9644 --- [nio-8903-exec-1] c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client sleuth-provider-provider initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=sleuth-provider-provider,current list of Servers=[localhost:8901, localhost:8902],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone; Instance count:2; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;]
},Server stats: [[Server:localhost:8901; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
, [Server:localhost:8902; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@3aaae4ed
sleuth-provider-provider日志:
2019-06-05 21:36:30.075 INFO [sleuth-provider-provider,c4586f2a32f2b4c6,a528f9b7295f4de4,true] 8856 --- [nio-8901-exec-1] c.k.s.p.p.UserProviderProviderController : get userInfo
2019-06-05 21:36:30.075 INFO [sleuth-provider-provider,c4586f2a32f2b4c6,a528f9b7295f4de4,true] 8856 --- [nio-8901-exec-1] c.k.s.p.p.UserProviderProviderService : user Server , get User Info!
可以很清晰的看见本次调用的traceId为:c4586f2a32f2b4c6, 在不同的服务调用阶段的spanid分别为:
c4586f2a32f2b4c6 -> 11bc3ea8ab71d083 -> a528f9b7295f4de4
在看看zipKin服务端收集到的数据图表: