Spring Boot & Spring Cloud Stream: Publishing & Consuming Messages Using RabbitMQ | ChargeAhead LLC | Skillshare

Spring Boot & Spring Cloud Stream: Publishing & Consuming Messages Using RabbitMQ

ChargeAhead LLC, Technologist

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
8 Lessons (17m)
    • 1. 1 Overview

    • 2. 2 Audience

    • 3. 3 Tools

    • 4. 4 Prerequisites

    • 5. 5 InstallRabbitMQ

    • 6. 6 Publishing

    • 7. 7 Consuming

    • 8. 8 Summary


About This Class

In this class we will see how to publish messages to RabbitMQ using a publisher service. The Publisher service exposed a REST API which the client calls to post a JSON message. We will then create a Consumer Service which grabs the message from RabbitMQ and processes it downstream. RabbitMQ serves as a messaging broker and helps us decouple the publisher from the consumer. So even if the consumer service is down or having performance problems, our messages are not lost, they are held with RabbitMQ until they are retrieved by the Consumer Service. We will also see how to install RabbitMQ.


1. 1 Overview: we want to create arrest a p I for our customers to send us a data. The rest API. I wouldn't send data to a messaging broker like rabbit. MQ. A consumer service will then pick up the message from revenue MQ and process the downstream using a messaging broker like Reppert MQ decouple the publisher from the Consumer Service. Making the services is eagerness. Our clients do not have to wait for the downstream systems to complete processing. And so that's a murder data and quickly going their own way even if it were downstream. System or service is suffering performance problems or is down. Our data or messages are not lost and staying rabbit and Q until they retrieved by the downstream service sprinklered stream along with spring boot makes it very easy for us to create these services with just a few annotations. It can wire itself for epidemic you and do all the heavy lifting for us. Hello, My name is Bunker Jane and welcome to the scores on spring boot and Spring Cloud Stream. Publishing and consuming messages using rabbit and Q in this course will first start off talking about who will be the right audience for the scores, tools needed to follow along and prerequisites to be most successful with this course, Rev it and Q is one of the most popular, messed incurring software or broker. It allows for asynchronous transfer of messages from producers to consumers supporting most popular messaging protocols like AM Q P Stomp extra. We will see Hope, install rabbit MQ on Olynyk server enable and access its management console and create an admin user. Next, it will create a publisher service, which is arrest FBI exposed to work lines. The client will some under Jason Data to the c p i. R S t p. I would then use sprinklered stream and send it to rev it mq. Next, we will see how to create a consumer service, which will grab the message from revenue in Q and process it downstream. 2. 2 Audience: this course will be relevant for our developers, vendor bloopers and software architects. 3. 3 Tools: we will have a lot of demos in the scores, as I believe that is the best way to learn the tools I would use in. The scores are off course. JAMA and feel free to use Java are open. Judy Kaye version eight or about, and most of the examples should work. Alexe Print Will Suite, which is a flavor of a clips but is highly customized and optimized for spring development . I would be using Version four. It is open source and free and comes with a lot of bells and whistles, which I think would like. However, if you're familiar with and relate to use some other I d Ys like intelligent etcetera, please feel free to do so. I would be using Rabbit and Q as a messaging broker. What are the prerequisites you need to most successful? Let's also that question next 4. 4 Prerequisites: There are not a whole lot of prerequisites for this course. The only thing you need is some basic Java knowledge, some basic spring concepts and a basic understanding of messaging. Even if you do not have a lot of family already with these, don't worry. I will try to explain the concepts along the way, and you should be able to follow along without much difficulty. Let's next see how didn't stall rabbit MQ. 5. 5 InstallRabbitMQ: here. I have been duly next running first running us his route. Let us update our system. Using pseudo ppd hyphen, Get update Labour MQ requires or Lang So that is installer next by using pseudo a bt hyphen get install or lang? Yes. You want to install it? This might take a white Next. Let us install rabbit ankle server by typing pseudo a pretty happen Get install rabbit. Thank you. Hyphen server. Yes. For installation, let us now enable rabbit ankles service for typing pseudo. So some CDL enable rabbit empty hyphen server Let's no started by typing pseudo system CDL start rabbit Thank you Hyphen Server The management plug in Freud's A nice dashboard for rabbit mq which allows us to see queues exchanges except tre. Let's animal it by using pseudo rabbit MQ hyphen plug ins enable rev it. Thank you on the school management. It's done. So now let's going to go look at it. Open a browser and type issue TB, local host or your server name and then poured 15 6 72 for the management console by the fall. It creates a user name, guest and password guest. And here's a nice U Y, which shows connections, channels except tra. Now you want to create your own user and grant appropriate privileges to it. So let's on the admin user of a typing rabbit and Q CDL What is the command and control for rapid MQ? Add underscore user user name, admin and password admin. Oh, we need to run it as rude. So let me pseudo and run the same command again so the user is created next. That is set user tags for the admin toe administrator. The second school. Use it in a score. Banks Command. Let's rabbit, and you know that this user is an administrator off course. We missed the Rabbit and QC deal in front. Let's try it again. Finally there said the permissions for this user by using pseudo Robert M que CDL settling the score promotions, hyphen B permissions on this machine and getting into the slash, and we give full permissions to configure, read and write on all resources on this machine. Okay, done. Let's get back to the management console long out and long back in. But they were newly created user admin admin, and we can see the console again 6. 6 Publishing: all right here I have Spring Tool suite running on my machine, which is a flavor of eclipse. Let's create a new project, but using file New Spring Starter Project, give the name publisher service Click. Next. Let's choose from cloud messaging, Cloud Stream, Search for Rabbit, Jews from Integration Rabbit and Q. And since we will be exposing arrest FBI, let's also choose Web click Finish. Now let's open the Pond XML file in the dependencies. We also need to include the aim Cubic lined, mentioning the wars in hair. We can pass string messages to revert MQ from the publisher service, but more realistically, you were passing an object. Let's say we have an e commerce app and you want to pass the transaction. Let's go to the package. Right click. She was new and then class. Give it the name transaction. Let's include feels and already indict a mighty in order quantity and a string region that's right. Click Jews source and then generate gutters and sitter's select all and then generate. Next, Let's right click Jews source and then general constructor using fields religion rate. Let's create a publisher class by right clicking and choosing new class and colored Missett publisher Click Finish. There is an outdated as arrest controller, Let's argue notation enable binding. What's the core of the Stream Cloud application, which triggered the spring integration with Robert and Q messaging Broker Greeting exchange . Except it does the sprinkler stream free work to find the source is Outward Channel with the underlying middleware, which is evident que in our case, let's import these. Let's get right import for the source class, which is from stream messaging. Now let's hard wire the source. Create a post mapping with slash transaction dinos to the metal which were done. The string college Send message. Which Dixon The request body, which is of type String. Let's call it the Lord to believe this convergence song or XML in the payload. Let's go to the Bomber XML file and put the dependency for Jackson uniform. Arctic Summer. We can help us mapped incoming disown or XML pillow to our object. That's right. Click on our project Goto maven and choose a great product. No, never controller. Let's create an object mapper that is created connection readable and using object. Don't read Value map over. Pay low to the transaction class. Let's send a message. Rabbit MQ. Using our sources are poor channel using message builder with payload but order in action in the Gaylord. We can also send custom headers in the message. But, said Header, let us specify a key, my header and a value my hair value. We can use this headed in our consumer service to perform ordinary actions based on its value. Also known that the conversion of the string payload to the transaction class could have also been done in the consumer service. Let's call him build. Let's return success toward calling client. Let's unknown, but try catch the exception initialize or transaction Variable. Now let's go to source Main Resources and then application or properties file. Let's specify our server port as 80 81. For the Rabbit and Q broker, you are a role as TCP 1 $27.0.1 or local host AC Port, 56 72. Enter the debit mq user name and password using spring dark cloud or stream dot bindings. Or put your destination as my destination. So this will be the exchange name created for us and never them que spring book and Spring Cloud will see these sittings and automatically configure and vier us to Robert M Que Let's turn the logging level to debug. Let's start over app by right clicking and choosing run as spring boot app. Now let's go to Rabbit MQ dashboard and we always your exchange created over hair with the name my destination. There is no cure yet now, to make post calls, I will use a muzzle and on called Restrict Lined, you can use postman or any of the tool you like. It's added to Firefox. I forgot to add a Newark constructor door transaction class, so let me do that. Also, let me add a system that out toward controller mattered to make sure it is called Let's relaunch your arm. Now let's open a restrict lined extension. Change the matter to post Ural as issue DP Local host 80 81 slash connection at the header Content type as application slash Jason that you based that Jason Here where we have the feel of the transaction object and their values. Let's click Send request response code 200 indicates success. Back in our project, we see that our matter was called based on the out port in the console. Let's make a few more calls. Goto a rabbit, MQ dashboard, Click mine destination and here with your messages coming through. 7. 7 Consuming: now let's critical Zoomer service. To consume these messages, let's create a new project. Spring Starter Project. Call it consumer service. Click next select Bloodstream. However them qs dependencies. Click next and then finish. Now let's open the former XML file in the dependencies. We also need to include the aim Cubic lined, mentioning the wars in hair. Let's right click on our project Goto Maven and choose update product, since you will you consuming the transaction object. Sending the message. Let's create a induction class. Let us copy it from the publisher service and paste it in our project. Next, let's right. Click on the project and choose new glass and call it message. Consumer click Finish. Let's first start by adding the animal binding a notation refering it to the burden sink. Dark class The nimble. Binding In addition, is the core off a sprinkler application, which triggers a spring integration with the rabbit and cumin sitting broker grouping Exchange etcetera. Let's import it now. Let's put the stream. Listen in addition and minded the building sink dot in poor channel that's pretty consuming mattered Public Void Log, which receives a transaction from the evident que message. Let's imported to prove that it received a message just fine. Let's put a system or message containing order i d. As message don't get already now let's go to source many resources and then application, not properties. File. Let's specify our server port as a deity do for the rabbit. MQ broker You Earl as TCP 1 $27.0.1 Our local host at Port 56 72. Enter the David MQ user name and password using Spring Dark Cloud. God streamed on bindings dot input your destination as my destination, so this will be the exchange name. It will read from revenue. MQ Spring Boot and Spring Cloud will see these settings and automatically configure and virus revenue in. Q. Let's turn the Loughton level to debug. Fix the type of hair loss Sport is guest. That's right. Click and start over application. So here's our exchange. Let's go to the Q tab, and here we see the Consumer Service has established cue for us. Let's go to our post client and make a few post calls Tour publisher service. The authority in the call is one back now. Our spring project we've seen the console. A message containing order ideas one. So our publisher service published the transaction to ever them que our consumer service connects to it, grabs the message and the transaction and was able to consume it. Let's change in the pillow order ideas four submitted and we see it was successively retrieved by the consumer service in the rebel GameCube console received receiving the message. 8. 8 Summary: in this course, we saw 100. Create a publisher service which exposes arrest a p A to our clients, which calls it and posts the message to a rabbit. MQ exchange. Next, we created a consumer service. We're just able to successfully connect to rev it. Um, que and retrieve the messages from it. Thanks for watching.