0% found this document useful (0 votes)
3 views5 pages

Spring Boot Microservices Guide

Uploaded by

saravanansd2102
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Spring Boot Microservices Guide

Uploaded by

saravanansd2102
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Developing Microservices with Spring Boot: Synchronous (HTTP) and Asynchronous

(ActiveMQ) Communication

In this guide, we will create two microservices using Spring Boot:

1. Synchronous Service: Communicates using HTTP.

2. Asynchronous Service: Communicates using ActiveMQ for message queuing.

Prerequisites

 Java Development Kit (JDK)

 Apache ActiveMQ (download and install from ActiveMQ)

 Spring Boot (using Spring Initializr)

Step 1: Set Up ActiveMQ

1. Start ActiveMQ: Navigate to the ActiveMQ installation directory and start it:

cd <activemq-installation-directory>

bin/activemq start

2. Access the ActiveMQ Web Console: You can access the ActiveMQ web console at
[Link] (default credentials: admin / admin).

Step 2: Create the Synchronous Microservice

1. Create a Spring Boot Project: Use Spring Initializr to create a new project with the
following dependencies:

 Spring Web

 Spring Boot DevTools

2. Configure [Link]:

server:

port: 8081

spring:

application:

name: sync-service

3. Create a REST Controller:

import [Link];

import [Link];
@RestController

public class SyncController {

@GetMapping("/sync")

public String sync() {

return "Synchronous Response from Sync Service";

Step 3: Create the Asynchronous Microservice

1. Create Another Spring Boot Project: Use Spring Initializr to create a new project with
the following dependencies:

 Spring Web

 Spring for ActiveMQ

2. Configure [Link]:

server:

port: 8082

spring:

application:

name: async-service

activemq:

broker-url: tcp://localhost:61616

user: admin

password: admin

3. Create a Message Producer:

import [Link];

import [Link];

@Service
public class MessageProducer {

private final JmsTemplate jmsTemplate;

public MessageProducer(JmsTemplate jmsTemplate) {

[Link] = jmsTemplate;

public void sendMessage(String message) {

[Link]("myQueue", message);

[Link]("Sent: " + message);

4. Create a Message Consumer:

import [Link];

import [Link];

@Component

public class MessageConsumer {

@JmsListener(destination = "myQueue")

public void receiveMessage(String message) {

[Link]("Received: " + message);

// Process the message

5. Create a REST Controller:

import [Link];

import [Link];

import [Link];
import [Link];

@RestController

public class AsyncController {

private final MessageProducer messageProducer;

@Autowired

public AsyncController(MessageProducer messageProducer) {

[Link] = messageProducer;

@PostMapping("/send")

public String sendMessage(@RequestBody String message) {

[Link](message);

return "Message sent: " + message;

Step 4: Running the Microservices

1. Run the Synchronous Service: Navigate to the synchronous service project directory
and run:

mvn spring-boot:run

2. Run the Asynchronous Service: Navigate to the asynchronous service project


directory and run:

mvn spring-boot:run

Step 5: Testing the Microservices

1. Test the Synchronous Service: Use a tool like Postman or curl to access the
synchronous service:

curl [Link]

You should receive the response: Synchronous Response from Sync Service.
2. Test the Asynchronous Service: Send a POST request to the asynchronous service:

curl -X POST [Link] -H "Content-Type: text/plain" -d "Hello ActiveMQ!"

You should see output in the console of the asynchronous service indicating that the
message was sent and received.

Conclusion

You have successfully created two microservices using Spring Boot:

 A synchronous service that responds to HTTP requests.

 An asynchronous service that uses ActiveMQ for message queuing.

You might also like