Files
spring-cloud-static/Dalston.SR5/multi/multi__span_data_as_messages.html
2017-12-22 20:14:47 -05:00

69 lines
10 KiB
HTML

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>54.&nbsp;Span Data as Messages</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="multi_spring-cloud.html" title="Spring Cloud"><link rel="up" href="multi__spring_cloud_sleuth.html" title="Part&nbsp;VII.&nbsp;Spring Cloud Sleuth"><link rel="prev" href="multi__customizations.html" title="53.&nbsp;Customizations"><link rel="next" href="multi__metrics.html" title="55.&nbsp;Metrics"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">54.&nbsp;Span Data as Messages</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__customizations.html">Prev</a>&nbsp;</td><th width="60%" align="center">Part&nbsp;VII.&nbsp;Spring Cloud Sleuth</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="multi__metrics.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="_span_data_as_messages" href="#_span_data_as_messages"></a>54.&nbsp;Span Data as Messages</h2></div></div></div><p>You can accumulate and send span data over
<a class="link" href="http://cloud.spring.io/spring-cloud-stream" target="_top">Spring Cloud Stream</a> by
including the <code class="literal">spring-cloud-sleuth-stream</code> jar as a dependency, and
adding a Channel Binder implementation
(e.g. <code class="literal">spring-cloud-starter-stream-rabbit</code> for RabbitMQ or
<code class="literal">spring-cloud-starter-stream-kafka</code> for Kafka). This will
automatically turn your app into a producer of messages with payload
type <code class="literal">Spans</code>. The channel name to which the spans will be sent
is called <code class="literal">sleuth</code>.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_zipkin_consumer" href="#_zipkin_consumer"></a>54.1&nbsp;Zipkin Consumer</h2></div></div></div><p>There is a special convenience annotation for setting up a message consumer
for the Span data and pushing it into a Zipkin <code class="literal">SpanStore</code>. This application</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@SpringBootApplication</span></em>
<em><span class="hl-annotation" style="color: gray">@EnableZipkinStreamServer</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> Consumer {
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">static</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> main(String[] args) {
SpringApplication.run(Consumer.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>, args);
}
}</pre><p>will listen for the Span data on whatever transport you provide via a
Spring Cloud Stream <code class="literal">Binder</code> (e.g. include
<code class="literal">spring-cloud-starter-stream-rabbit</code> for RabbitMQ, and similar
starters exist for Redis and Kafka). If you add the following UI dependency</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;groupId&gt;</span>io.zipkin.java<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/groupId&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;artifactId&gt;</span>zipkin-autoconfigure-ui<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/artifactId&gt;</span></pre><p>Then you&#8217;ll have your app a
<a class="link" href="https://github.com/openzipkin/zipkin" target="_top">Zipkin server</a>, which hosts
the UI and api on port 9411.</p><p>The default <code class="literal">SpanStore</code> is in-memory (good for demos and getting
started quickly). For a more robust solution you can add MySQL and
<code class="literal">spring-boot-starter-jdbc</code> to your classpath and enable the JDBC
<code class="literal">SpanStore</code> via configuration, e.g.:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">spring</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> rabbitmq</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> host</span>: ${RABBIT_HOST:localhost<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">}</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> datasource</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> schema</span>: classpath:/mysql.sql
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> url</span>: jdbc:mysql://${MYSQL_HOST:localhost}/test
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> username</span>: root
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> password</span>: root
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-comment"># Switch this on to create the schema on startup:</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> initialize</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">true</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> continueOnError</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">true</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> sleuth</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> enabled</span>: <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">false</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute">zipkin</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> storage</span>:
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-attribute"> type</span>: mysql</pre><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>The <code class="literal">@EnableZipkinStreamServer</code> is also annotated with
<code class="literal">@EnableZipkinServer</code> so the process will also expose the standard
Zipkin server endpoints for collecting spans over HTTP, and for
querying in the Zipkin Web UI.</p></td></tr></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_custom_consumer" href="#_custom_consumer"></a>54.2&nbsp;Custom Consumer</h2></div></div></div><p>A custom consumer can also easily be implemented using
<code class="literal">spring-cloud-sleuth-stream</code> and binding to the <code class="literal">SleuthSink</code>. Example:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@EnableBinding(SleuthSink.class)</span></em>
<em><span class="hl-annotation" style="color: gray">@SpringBootApplication(exclude = SleuthStreamAutoConfiguration.class)</span></em>
<em><span class="hl-annotation" style="color: gray">@MessageEndpoint</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> Consumer {
<em><span class="hl-annotation" style="color: gray">@ServiceActivator(inputChannel = SleuthSink.INPUT)</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> sink(Spans input) <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">throws</span> Exception {
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-comment">// ... process spans</span>
}
}</pre><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td align="left" valign="top"><p>the sample consumer application above explicitly excludes
<code class="literal">SleuthStreamAutoConfiguration</code> so it doesn&#8217;t send messages to itself,
but this is optional (you might actually want to trace requests into
the consumer app).</p></td></tr></table></div><p>In order to customize the polling mechanism you can create a bean of <code class="literal">PollerMetadata</code> type
with name equal to <code class="literal">StreamSpanReporter.POLLER</code>. Here you can find an example of such a configuration.</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Configuration</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">static</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span> CustomPollerConfiguration {
<em><span class="hl-annotation" style="color: gray">@Bean(name = StreamSpanReporter.POLLER)</span></em>
PollerMetadata customPoller() {
PollerMetadata poller = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> PollerMetadata();
poller.setMaxMessagesPerPoll(<span class="hl-number">500</span>);
poller.setTrigger(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">new</span> PeriodicTrigger(<span class="hl-number">5000L</span>));
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> poller;
}
}</pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__customizations.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="multi__spring_cloud_sleuth.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="multi__metrics.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">53.&nbsp;Customizations&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;55.&nbsp;Metrics</td></tr></table></div></body></html>