PCF Spring Boot RabbitMQ Example
As seen in previous tutorial, we learned how to develop and deploy Spring Boot application to PCF and deploy Spring Boot + MYSQL to PCF.
Now lets move ahead and learn step by step how to develop and deploy Spring Boot with Local RabbitMQ and also with Pivotal Cloud Foundry (PCF) RabbitMQ service.
To test this application at local will use Local RabbitMQ, however to test with Pivotal Cloud Foundry, will see how to create RabbitMQ service and bind.
RabbitMQ Tutorial :
- Install RabbitMQ on Windows
- Install RabbitMQ using Docker on Windows
- Spring Boot RabbitMQ Example
- Spring Boot + RabbitMQ + Error Handling Example
- Spring Cloud Stream + RabbitMQ Example
- Spring Boot + Pivotal Cloud Foundry (PCF) + RabbitMQ Example
- RabbitMQ Interview Questions
Note : The full source code for PCF spring boot rabbitMQ example can be downloaded at the end of this article.
- To begin with, first Create Spring Boot Project from Spring Initializer site https://start.spring.io/.
- Project will look as given below
- Add RabbitMq dependency in pom.xml
- Provide rabbitmq properties in application.properties
- Create manifest.yml, you can see we have provided service name as techgeeknextRabbitmqservice, which is RabbitMQ service name created in PCF.
Note: This manifest.yml file only be needed in case to deploy and test on Pivotal Cloud Foundry. To test at Local, we don't need this file. You can setup RabbitMQ at Local to test this Example. - Let's create Employee Model class
- Create Configuration class to connect to RabbitMQ.
- Create controller class to expose the endpoint to send message to RabbitMQ.
- Create Message Producer/Sender, which send messages to RabbitMQ Consumer using Direct Exchange as shown below.
- Now, test this application at local. Start local RabbitMQ and Run Spring Boot App
- Create RabbitMQ service from Pivotal Cloud Foundry
- Loging to Pivotal Cloud Foundry
- Click on Marketplace.earch for RabbitMQ, click on CloudAMQP
- Click on Select This Plan button
- Give service name, same as provided in manifest.yml file and click on Create button
- Finally, it will create the RabbitMQ service as shown below
- Deploy your application by using cf push from path where manifest.yml resides
Login to Cloud Foundry from command prompt using cf login -a api.run.pivotal.io command, and provide login credentials. Once login, push your application by using cf push command. - Once deployment process get complete, go to Pivotal Cloud Foundry Web.
- Click on Organization-> Development, you can see your Spring Boot App deployed to PCF.
- To test the application, click on route and as given below append the required attribute to call controller services.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.techgeeknext</groupId>
<artifactId>SpringBootRabbitMQEaxmple</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootRabbitMQEaxmple</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-cloud-connectors -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cloud-connectors</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
techgeeknext.rabbitmq.exchange=techgeeknext.exchange
techgeeknext.rabbitmq.queue=techgeeknext.queue
techgeeknext.rabbitmq.routingkey=techgeeknext.routingkey
name: techgeeknext-springBootRabbitMq
path: target\SpringBootRabbitMQEaxmple-0.0.1-SNAPSHOT.jar
memory: 1G
services:
- techgeeknextRabbitmqservice
package com.techgeeknext.model;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id", scope = Employee.class)
public class Employee {
private String empName;
private String empId;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
@Override
public String toString() {
return "Employee [empName=" + empName + ", empId=" + empId + "]";
}
}
package com.techgeeknext.config;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
@Value("${techgeeknext.rabbitmq.queue}")
String queueName;
@Value("${techgeeknext.rabbitmq.exchange}")
String exchange;
@Value("${techgeeknext.rabbitmq.routingkey}")
private String routingkey;
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
DirectExchange exchange() {
return new DirectExchange(exchange);
}
@Bean
Binding binding(Queue queue, DirectExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(routingkey);
}
@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(jsonMessageConverter());
return rabbitTemplate;
}
}
package com.techgeeknext.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.techgeeknext.model.Employee;
import com.techgeeknext.service.RabbitMQSender;
@RestController
@RequestMapping(value = "/techgeeknext")
public class RabbitMQWebController {
@Autowired
private RabbitMQSender rabbitMQSender;
@GetMapping(value = "/rabbitmq/receive/message")
public String consume(@RequestParam("empName") String empName,@RequestParam("empId") String empId) {
Employee emp=new Employee();
emp.setEmpId(empId);
emp.setEmpName(empName);
rabbitMQSender.send(emp);
return "Message has been sent to the RabbitMQ techgeeknext successfully";
}
}
package com.techgeeknext.service;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.techgeeknext.model.Employee;
@Service
public class RabbitMQSender {
@Autowired
private AmqpTemplate amqpTemplate;
@Value("${techgeeknext.rabbitmq.exchange}")
private String exchange;
@Value("${techgeeknext.rabbitmq.routingkey}")
private String routingkey;
public void send(Employee empName) {
amqpTemplate.convertAndSend(exchange, routingkey, empName);
System.out.println("Send msg = " + empName);
}
}
Download Source Code
The full source code for this article can be found on below.Download it here - PCF Spring Boot RabbitMQ Example