spring boot 整合rabitmq:

博客主要介绍了RabbitMQ的安装,提及了生产者乙方和消费者乙方,还阐述了配置参数以及配置连接参数等内容,聚焦于信息技术领域的消息队列相关操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.rabiimq 安装:

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</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>

 

生产者乙方:

 @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMsg(String msg) {

        Map<String,Object> sendContext = new HashMap<>();
        sendContext.put("name","张三");
        sendContext.put("sex","男");
        sendContext.put("msg",msg);
        rabbitTemplate.convertAndSend("tulingDirectExchange","tuling.directkey",sendContext);
    }

    /**
     * 发送到扇形交换机上
     * @param msg
     */
    public void sendMsg2Fanout(String msg) {

        rabbitTemplate.convertAndSend("tulingFanoutExchange","aaaabbdd",msg);

    }

消费者乙方:

@Component
public class TulingConsumer {

    @RabbitListener(queues ="DirectQueue")
    public void consumerMsg(Message message) {

        System.out.println("消费消息:"+message.getPayload().toString());
    }

 

 

3.配置参数:

@Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }


    /**
     * 直接交换机
     * @return
     */
    @Bean
    public DirectExchange tulingDirectExchange() {
        //参数 durable:表示消息是否可持久化
        //autoDelete:表示若没有队列和此交换机绑定 就直接删除该交换机
        return new DirectExchange("tulingDirectExchange",true,false);
    }


    @Bean
    public Queue tulingDirectQueue() {
        return new Queue("tulingDirectQueue",true,false,false);
    }

    @Bean
    public Binding tulingDq2De(){
        return BindingBuilder.bind(tulingDirectQueue()).to(tulingDirectExchange()).with("tuling.directkey");
    }

    /**
     * 扇形交换机
     * @return
     */
    @Bean
    public FanoutExchange tulingFanoutExchange() {
        return new FanoutExchange("tulingFanoutExchange",true,false);
    }

    @Bean
    public Queue tulingfanoutQueue1() {
        return new Queue("tulingFanoutQueue1",true,false,false);
    }

    @Bean
    public Queue tulingfanoutQueue2() {
        return new Queue("tulingFanoutQueue2",true,false,false);
    }

    @Bean
    public Binding tulingBind1() {
        return BindingBuilder.bind(tulingfanoutQueue1()).to(tulingFanoutExchange());

    }

    @Bean
    public Binding tulingBind2() {
        return BindingBuilder.bind(tulingfanoutQueue2()).to(tulingFanoutExchange());
    }

    /**
     * 主题交换机
     */
    @Bean
    public TopicExchange tulingTopicExchange() {
        return new TopicExchange("tulingTopicExchange",true,false);
    }

    @Bean
    public Queue tulingTopicQueue() {
        return new Queue("tulingTopicQueue",true,false,false);
    }

    @Bean
    public Queue tulingTopicQueue2() {
        return new Queue("tulingTopicQueue2",true,false,false);
    }

    @Bean
    public Binding topicBind1(){
        return BindingBuilder.bind(tulingTopicQueue()).to(tulingTopicExchange()).with("topic.key.#");

    }

    @Bean
    public Binding topicBind2(){
        return BindingBuilder.bind(tulingTopicQueue2()).to(tulingTopicExchange()).with("#.key");

    }

4.配置连接参数:

spring:
  rabbitmq:
    host: 47.104.128.12
    port: 5672
    virtual-host: tulingVip
    username: guest
    password: guest
    connection-timeout: 10000
    template:
      mandatory: true

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迅捷的软件产品制作专家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值