How to integrate RabbitMQ

1. Add spring-boot-starter-amqp

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
Copy the code

2. Add the configuration

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.dynamic=true
spring.rabbitmq.cache.connection.mode=channel
Copy the code

3. Inject queues

@Configuration
public class RabbitConfig {
    @Bean
    public Queue Queue() {
        return new Queue("hello"); }}Copy the code

Create a Repository object for the operation data

interface CityRepository extends Repository<City, Long> {

	Page<City> findAll(Pageable pageable);

	Page<City> findByNameContainingAndCountryContainingAllIgnoringCase(String name,
			String country, Pageable pageable);

	City findByNameAndCountryAllIgnoringCase(String name, String country);

}
Copy the code

5. Create consumers

@Component
public class RabbitConsumer {
    @RabbitHandler
    @RabbitListener(queues = "hello")
    public void process(@Payload String foo) {
        System.out.println(new Date() + ":"+ foo); }}Copy the code

6. Start the main class

@SpringBootApplication @EnableScheduling public class AmqpApplication { public static void main(String[] args) { SpringApplication.run(AmqpApplication.class, args); }}Copy the code

Console output:

Sun Sep 30 16:30:35 CST 2018: hello
Copy the code

At this point, a simple SpringBoot2.0 integration with RabbitMQ is complete. Those familiar with RabbitMQ will know that RabbitMQ adds the concept of ExChange to the normal queue. There are four types of ExChange: Direct, Topic, Headers and Fanout. Headers is actually rarely used, and Direct is simpler. The next section details how to use Topic and Fanout.

Topic Exchange

1. Configure Topic rules

@Configuration
public class TopicRabbitConfig {

    @Bean
    public Queue queueMessage1() {
        return new Queue(MQConst.TOPIC_QUEUENAME1);
    }

    @Bean
    public Queue queueMessage2() {
        return new Queue(MQConst.TOPIC_QUEUENAME2);
    }

    @Bean
    TopicExchange exchange() {
        returnnew TopicExchange(MQConst.TOPIC_EXCHANGE); } @bean Binding bindingExchangeMessage(Queue queueMessage1, TopicExchange exchange) {// Bind Queue 1 to A routingKey named topickey. AreturnBindingBuilder.bind(queueMessage1).to(exchange).with(MQConst.TOPIC_KEY1); } @bean Binding bindingExchangeMessages(Queue queueMessage2, TopicExchange exchange) {// Bind Queue 2 to all TopicKeys. At the beginning of routingKeyreturnBindingBuilder.bind(queueMessage2).to(exchange).with(MQConst.TOPIC_KEYS); }}Copy the code

2. Configure consumers

@Component
public class TopicConsumer {

    @RabbitListener(queues = MQConst.TOPIC_QUEUENAME1)
    @RabbitHandler
    public void process1(String message) {
        System.out.println("queue:topic.message1,message:" + message);
    }

    @RabbitListener(queues = MQConst.TOPIC_QUEUENAME2)
    @RabbitHandler
    public void process2(String message) {
        System.out.println("queue:topic.message2,message:"+ message); }}Copy the code

3. Production news

Add the following to the Producer class:

        // Topic
        rabbitTemplate.convertAndSend(MQConst.TOPIC_EXCHANGE, MQConst.TOPIC_KEYS, "from keys");
        rabbitTemplate.convertAndSend(MQConst.TOPIC_EXCHANGE, MQConst.TOPIC_KEY1, "from key1");
Copy the code

Starting the main class again, the console output:

queue:topic.message2,message:from keys
queue:topic.message1,message:from key1
queue:topic.message2,message:from key1
Copy the code

Fanout Exchange

1. Configure the Fanout rule

@Configuration
public class FanoutRabbitConfig {
    @Bean
    public Queue MessageA() {
        return new Queue(MQConst.FANOUT_QUEUENAME1);
    }

    @Bean
    public Queue MessageB() {
        return new Queue(MQConst.FANOUT_QUEUENAME2);
    }

    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange(MQConst.FANOUT_EXCHANGE);
    }

    @Bean
    Binding bindingExchangeA(Queue MessageA, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(MessageA).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeB(Queue MessageB, FanoutExchange fanoutExchange) {
        returnBindingBuilder.bind(MessageB).to(fanoutExchange); }}Copy the code

2. Configure consumers

@Component
public class FanoutConsumer {

    @RabbitListener(queues = MQConst.FANOUT_QUEUENAME1)
    @RabbitHandler
    public void process1(String message) {
        System.out.println("queue:fanout.message1,message:" + message);
    }

    @RabbitListener(queues = MQConst.FANOUT_QUEUENAME2)
    @RabbitHandler
    public void process2(String message) {
        System.out.println("queue:fanout.message2,message:"+ message); }}Copy the code

3. Production news

Add the following to the Producer class:

        // FanOut
        rabbitTemplate.convertAndSend(MQConst.FANOUT_EXCHANGE, ""."fanout"); 
Copy the code

Starting the main class again, the console output:

queue:fanout.message2,message:fanout
queue:fanout.message1,message:fanout
Copy the code

Source address: GitHub


This is the end of this article, if you feel that there is a harvest after reading, welcome to like, follow, add the public account [level 2 natural disaster], see more wonderful history!!