Files
spring-cloud-static/Finchley.M9/multi/multi__quick_start_2.html
2018-03-22 21:17:28 -04:00

50 lines
11 KiB
HTML

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>25.&nbsp;Quick Start</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_stream.html" title="Part&nbsp;V.&nbsp;Spring Cloud Stream"><link rel="prev" href="multi__spring_cloud_stream.html" title="Part&nbsp;V.&nbsp;Spring Cloud Stream"><link rel="next" href="multi__what_s_new_in_2_0.html" title="26.&nbsp;What&#8217;s New in 2.0?"></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">25.&nbsp;Quick Start</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__spring_cloud_stream.html">Prev</a>&nbsp;</td><th width="60%" align="center">Part&nbsp;V.&nbsp;Spring Cloud Stream</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="multi__what_s_new_in_2_0.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="_quick_start_2" href="#_quick_start_2"></a>25.&nbsp;Quick Start</h2></div></div></div><p>You can try Spring Cloud Stream in less then 5 min even before you jump into any details and the following <span class="emphasis"><em>three-step guide</em></span> will help.</p><p>We&#8217;ll create a simple Spring Cloud Stream application which receives messages coming from the messaging middleware of your choice (more on this later) and
logs received messages to the console. We&#8217;ll call it <span class="emphasis"><em>LoggingConsumer</em></span>. While not very practical it will certainly provide a good introduction to some of the main concepts
and abstractions, making it easier to digest the rest of this user guide.</p><p>So let&#8217;s get started. . .</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_step_one_create_sample_application_using_spring_initilaizer" href="#_step_one_create_sample_application_using_spring_initilaizer"></a>25.1&nbsp;Step One - Create sample Application using Spring Initilaizer</h2></div></div></div><p>Visit the <a class="link" href="https://start.spring.io" target="_top">Spring Initializr</a>. This is where we&#8217;ll generate our <span class="emphasis"><em>LoggingConsumer</em></span> application.</p><p>In the <span class="emphasis"><em>Dependencies</em></span> start typing 'stream' and <span class="emphasis"><em>Cloud Stream</em></span> option should pop up. Select it. Now start typing either 'kafka' or 'rabbit'. Basically this is where you are choosing
what messaging midleware this application will be bound to. Choose the one you have already installed and/or feel more comfortable with installing/running.
Also, as you can see from the Initilaizer screen there are few other options you can choose. For example, you can choose Gradle as your build tool instead of the default Maven.
With the <span class="emphasis"><em>Dependencies</em></span> selected the only other thing you have to identify is the application name - <span class="emphasis"><em>logging-consumer</em></span>.
Your configuration screeen should now contain the following:</p><pre class="literallayout">Dependencies: Cloud Stream, RabbitMQ (or Kafka)
Group: com.example - default
Artifact: logging-consumer
Spring Boot Version: 2.0.0 (or above) - default</pre><p>Click on <span class="emphasis"><em>Generate Project</em></span> button. This will donwload the zipped version of the generated project to your hard drive. Unzip it and you&#8217;re ready for Step Two.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_step_two_import_project_into_the_ide" href="#_step_two_import_project_into_the_ide"></a>25.2&nbsp;Step Two - Import project into the IDE</h2></div></div></div><p>Here you simply import the project into your IDE of choice.
Please keep in mind that dependening on the IDE you may need to follow a specific import procedures. For example depending on how the project was generated (Maven or Gradle)
you may need to follow specific import procedure (e.g., in Eclipse/STS: <code class="literal">File &#8594; Import &#8594; Maven &#8594; Existing Maven Project</code>).</p><p>Ones imported the project must have no errors of any kind and <code class="literal">src/main/java</code> should also contain <code class="literal">com.example.loggingconsumer.LoggingConsumerApplication</code>.</p><p>Technically at this point you can just run the application&#8217;s main class since it&#8217;s already a valid <span class="emphasis"><em>Spring Boot</em></span> application, but it does not do anything, so let&#8217;s add some code.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_step_three_add_message_handler_build_and_run" href="#_step_three_add_message_handler_build_and_run"></a>25.3&nbsp;Step Three - Add message handler, build and run</h2></div></div></div><p>Modify the <code class="literal">com.example.loggingconsumer.LoggingConsumerApplication</code> to look as follows:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@SpringBootApplication</span></em>
<em><span class="hl-annotation" style="color: gray">@EnableBinding(Sink.class)</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> LoggingConsumerApplication {
<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(LoggingConsumerApplication.<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">class</span>, args);
}
<em><span class="hl-annotation" style="color: gray">@StreamListener(Sink.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> handle(Person person) {
System.out.println(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Received: "</span> + person);
}
<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> Person {
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> String name;
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String getName() {
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> name;
}
<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> setName(String name) {
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">this</span>.name = name;
}
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> String toString() {
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">return</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">this</span>.name;
}
}
}</pre><p>As you can see from the above:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">We&#8217;ve enabled <code class="literal">Sink</code> binding (input-no-output) via <code class="literal">@EnableBinding(Sink.class)</code>. This will signal to the framework to initiate binding to the messaging middleware where
it will auto-create the destination (i.e., queue, topic) which will be bound to <code class="literal">Sink.INPUT</code> channel.</li><li class="listitem">We&#8217;ve added handler method to receive incoming Message as type <code class="literal">Person</code>. What this means is that here youcan already observe one of the core features of the framework where
it will attempt to automatically convert incoming message&#8217;s payload to type <code class="literal">Person</code>.</li></ul></div><p>This is it, we now have a fully functional Spring Cloud Stream application that does something. From here for simplicity we&#8217;ll assume RabbitMQ was selected in <span class="emphasis"><em>step one</em></span>.
Assuming you have RabbitMQ installed and running, start the application by simply running its <code class="literal">main</code> method.</p><p>You should see following output:</p><pre class="literallayout">--- [ main] c.s.b.r.p.RabbitExchangeQueueProvisioner : declaring queue for inbound: input.anonymous.CbMIwdkJSBO1ZoPDOtHtCg, bound to: input
--- [ main] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [localhost:5672]
--- [ main] o.s.a.r.c.CachingConnectionFactory : Created new connection: rabbitConnectionFactory#2a3a299:0/SimpleConnection@66c83fc8. . .
. . .
--- [ main] o.s.i.a.i.AmqpInboundChannelAdapter : started inbound.input.anonymous.CbMIwdkJSBO1ZoPDOtHtCg
. . .
--- [ main] c.e.l.LoggingConsumerApplication : Started LoggingConsumerApplication in 2.531 seconds (JVM running for 2.897)</pre><p>Go to RabbitMQ management console or any other RabbitMQ client and simply send message to <code class="literal">input.anonymous.CbMIwdkJSBO1ZoPDOtHtCg</code>
(NOTE: the <code class="literal">anonymous.CbMIwdkJSBO1ZoPDOtHtCg</code> part represents the group name and is generated and will be different in your environment. For something more
predictable you can use explicit group name via <code class="literal">spring.cloud.stream.bindings.input.group=hello</code>).</p><p>The contents of the message should be JSON representation of <code class="literal">Person</code> class, so let&#8217;s send this:</p><pre class="literallayout">{"name":"Turd Ferguson"}</pre><p>And in your console you should see:</p><pre class="literallayout">Received: Turd Ferguson</pre><p>You can also build/package your application into a boot jar (i.e., <code class="literal">./mvnw clean install</code>) and run the built JAR using <code class="literal">java -jar</code> command.</p><p>That is all!</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__spring_cloud_stream.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="multi__spring_cloud_stream.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="multi__what_s_new_in_2_0.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Part&nbsp;V.&nbsp;Spring Cloud Stream&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;26.&nbsp;What&#8217;s New in 2.0?</td></tr></table></div></body></html>