384.10. 示例
要启用 camel-zipkin,您需要首先配置
ZipkinTracer zipkin = new ZipkinTracer();
// Configure a reporter, which controls how often spans are sent
// (the dependency is io.zipkin.reporter2:zipkin-sender-okhttp3)
sender = OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans");
zipkin.setSpanReporter(AsyncReporter.create(sender));
// and then add zipkin to the CamelContext
zipkin.init(camelContext);
以上配置将跟踪 Camel 路由中的所有传入和传出消息。
要在 XML 中使用 ZipkinTracer,您需要做的都是定义 scribe 和 zipkin tracer Bean。Camel 将自动发现和使用它们。
<!-- configure how to reporter spans to a Zipkin collector
(the dependency is io.zipkin.reporter2:zipkin-reporter-spring-beans) -->
<bean id="http" class="zipkin2.reporter.beans.AsyncReporterFactoryBean">
<property name="sender">
<bean id="sender" class="zipkin2.reporter.beans.OkHttpSenderFactoryBean">
<property name="endpoint" value="http://localhost:9411/api/v2/spans"/>
</bean>
</property>
<!-- wait up to half a second for any in-flight spans on close -->
<property name="closeTimeout" value="500"/>
</bean>
<!-- setup zipkin tracer -->
<bean id="zipkinTracer" class="org.apache.camel.zipkin.ZipkinTracer">
<property name="serviceName" value="dude"/>
<property name="spanReporter" ref="http"/>
</bean>
384.10.1. ServiceName 复制链接链接已复制到粘贴板!
但是,如果要将 Camel 端点映射到用户友好的逻辑名称,您可以添加映射
- serviceName *
您可以配置一个全局服务名称,所有事件都将回退和使用,例如:
zipkin.setServiceName("invoices");
这将对所有传入和传出 zipkin trace 使用相同的服务名称。如果您的应用程序使用不同的服务,您应该将它们映射到更精细的客户端/服务器服务映射
384.10.2. 客户端和服务器服务映射 复制链接链接已复制到粘贴板!
- ClientServiceMappings
- ServerServiceMappings
如果您的应用程序托管了其他可以调用的服务,您可以将 Camel 路由端点映射到服务器服务映射。例如,假设您的 Camel 应用程序有以下路由:
from("activemq:queue:inbox")
.to("http:someserver/somepath");
您希望将其设置为服务器服务,您可以添加以下映射:
zipkin.addServerServiceMapping("activemq:queue:inbox", "orders");
然后,当消息使用该 inbox 队列时,它会变为 zipkin 服务器事件,其服务名称为"orders"。
现在假设对 http:someserver/somepath 的调用也是您要映射到客户端服务名称的服务,可以执行以下操作:
zipkin.addClientServiceMapping("http:someserver/somepath", "audit");
然后,在同一 Camel 应用程序中,您已将传入和传出端点映射到不同的 zipkin 服务名称。
您可以在服务映射中使用通配符。要匹配对同一 HTTP 服务器的所有传出调用,您可以:
zipkin.addClientServiceMapping("http:someserver*", "audit");