60. 将 Spans 发送到 Zipkin
默认情况下,如果将spring-cloud-starter-zipkin
作为依赖项添加到项目中,则在关闭 span 时,会通过 HTTP 将其发送到 Zipkin。通信是异步的。您可以通过设置spring.zipkin.baseUrl
property 来配置 URL,如下所示:
spring.zipkin.baseUrl: http://192.168.99.100:9411/
如果要通过服务发现找到 Zipkin,可以在 URL 中传递 Zipkin 的服务 ID,如下面的zipkinserver
示例所示zipkinserver
服务 ID:
spring.zipkin.baseUrl: http://zipkinserver/
要禁用此 feature,只需将spring.zipkin.discoveryClientEnabled
设置为`false。
启用 Discovery Client feature 后,Sleuth 使用LoadBalancerClient
查找 Zipkin 服务器的 URL。这意味着您可以设置负载平衡 configuration e.g. 通过 Ribbon。
zipkinserver:
ribbon:
ListOfServers: host1,host2
如果在 classpath 上有 web,rabbit 或 kafka,则可能需要选择将 spans 发送到 zipkin 的方法。为此,请将web
,rabbit
或kafka
设置为spring.zipkin.sender.type
property。以下 example 显示为web
设置发件人类型:
spring.zipkin.sender.type: web
要自定义通过 HTTP 将 spans 发送到 Zipkin 的RestTemplate
,您可以注册ZipkinRestTemplateCustomizer
bean。
@Configuration
class MyConfig {
@Bean ZipkinRestTemplateCustomizer myCustomizer() {
return new ZipkinRestTemplateCustomizer() {
@Override
void customize(RestTemplate restTemplate) {
// customize the RestTemplate
}
};
}
}
但是,如果要控制创建RestTemplate
object 的完整 process,则必须创建一个zipkin2.reporter.Sender
类型的 bean。
@Bean Sender myRestTemplateSender(ZipkinProperties zipkin,
ZipkinRestTemplateCustomizer zipkinRestTemplateCustomizer) {
RestTemplate restTemplate = mySuperCustomRestTemplate();
zipkinRestTemplateCustomizer.customize(restTemplate);
return myCustomSender(zipkin, restTemplate);
}