springBoot集成rabbitmq 之发布/订阅模式模式(Publish/Subscribe)

springBoot集成rabbitmq之发布/订阅模式模式(Publish/Subscribe)

springBoot整合rabbitmq的例子: https://blog.csdn.net/weixin_45730866/article/details/128971917,建议先看。
comsumer服务

配置类

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 
@Configuration
public class RabbitPublishSubscribeConfig { 
    /**
     * Queue 可以有4个参数
     *      1.name           队列名
     *      2.durable       持久化消息队列 ,rabbitmq重启的时候不需要创建新的队列 默认true
     *      3.auto-delete   表示消息队列没有在使用时将被自动删除 默认是false
     *      4.exclusive     表示该消息队列是否只在当前connection生效,默认是false
     */
    @Bean
    public Queue createPublishSubscribeQueueA() {
        return new Queue("PublishSubscribeQueueA",true);
    }
    @Bean
    public Queue createPublishSubscribeQueueB() {
        return new Queue("PublishSubscribeQueueB",true);
    }
    @Bean
    public Queue createPublishSubscribeQueueC() {
        return new Queue("PublishSubscribeQueueC",true);
    }

    @Bean
    public FanoutExchange  publishSubscribeExchange() {
        //配置广播路由器
        return new FanoutExchange("PublishSubscribeExchange");
    }

    @Bean
    public Binding bingQueueAToPublishSubscribeExchange() {
        return BindingBuilder.bind(createPublishSubscribeQueueA()).to(publishSubscribeExchange());
    }

    @Bean
    public Binding bingQueueBToPublishSubscribeExchange() {
        return BindingBuilder.bind(createPublishSubscribeQueueB()).to(publishSubscribeExchange());
    } 
    @Bean
    public Binding bingQueueCToPublishSubscribeExchange() {
        return BindingBuilder.bind(createPublishSubscribeQueueC()).to(publishSubscribeExchange());
    } 
}

接收者A B C

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component; 
import java.util.Date; 
@Component
@RabbitListener(queues = "PublishSubscribeQueueA")
public class ReceiverA {
    @RabbitHandler
    public void process(String message) {
        System.out.println("接收者A : " + message +","+ new Date());
    }
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Date; 
 
@Component
@RabbitListener(queues = "PublishSubscribeQueueB")
public class ReceiverB {
    @RabbitHandler
    public void process(String message) {
        System.out.println("接收者B : " + message +","+ new Date());
    }
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Date; 
@Component
@RabbitListener(queues = "PublishSubscribeQueueC")
public class ReceiverC {
    @RabbitHandler
    public void process(String message) {
        System.out.println("接收者C : " + message +","+ new Date());
    }
}

producer


发送者


import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

@Component
public class PublishSubscribeSender {
	@Autowired
	private RabbitTemplate rabbitTemplate;
	public void send(int i) {
		CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
		String msg = "消息是"+i;
		//消息发送,使用void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;
		//但不指定routingKey。因为FanoutExchange类型的交换机,routingKey不起作用,它向所有的队列发送广播,只要队列绑定到该交换机即接受消息。
		rabbitTemplate.convertAndSend("PublishSubscribeExchange","",msg,correlationData);
		System.out.println("发送成功 "+new Date()+" "+msg);
	}
}

测试类

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.RestController;

@RequestMapping
@RestController
public class PublishSubscribeTestsController { 
    @Autowired
    private PublishSubscribeSender publishSubscribeSender; 
    @GetMapping("publishSubscribe")
    public String publishSubscribe() {
        publishSubscribeSender.send(5201314);
        return "publishSubscribeOk";
    } 
}

然后请求http://127.0.0.1:8080/publishSubscribe,consumer服务控制台如下:

接收者B : 消息是5201314,Mon Feb 13 10:37:30 CST 2023
接收者C : 消息是5201314,Mon Feb 13 10:37:30 CST 2023
接收者A : 消息是5201314,Mon Feb 13 10:37:30 CST 2023 

produce控制如下:

发送成功 Mon Feb 13 10:37:30 CST 2023 消息是5201314

你可能感兴趣的:(springboot,java-rabbitmq,rabbitmq,spring,boot)