栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

SpringBoot集成RabbitMQ

SpringBoot集成RabbitMQ

1.添加依赖:

		
		
			org.springframework.boot
			spring-boot-starter-amqp
		

2.修改配置文件:

  # RabbitMQ
  rabbitmq:
    # 服务器
    host: 192.168.124.130
    #用户名
    username: guest
    #密码
    password: guest
    # 虚拟主机
    virtual-host: /
    #端口
    port: 5672
    listener:
      simple:
        #消费者最小数量
        concurrency: 10
        #消费者最大数量
        max-concurrency: 10
        #限制消费者每次只处理一条消息,处理完再继续下一条消息
        prefetch: 1
        #启动时是否默认启动容器,默认true
        auto-startup: true
        #被拒绝时重新进入队列
        default-requeue-rejected: true
    template:
      retry:
        #发布重试,默认false
        enabled: true
        #重试时间,默认1000ms
        initial-interval: 1000ms
        #重试最大次数,默认3次
        max-attempts: 3
        #重试最大间隔时间,默认10000ms
        max-interval: 10000ms
        #重试的间隔乘数。比如配2.0,第一次就等10s,第二次就等20s,第三次就等40s
        multiplier: 1

3.创建配置类

package com.xxxx.seckill.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    @Bean
    public Queue queue(){
        return new Queue("queue",true);
    }
}

4.创建消息发送者和消息接受者

package com.xxxx.seckill.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class MQSender {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    public void send(Object msg){
        log.info("发送消息:"+msg);
        rabbitTemplate.convertAndSend("queue",msg);
    }
}

package com.xxxx.seckill.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class MQReceiver {
    @RabbitListener(queues = "queue")
    public void receive(Object msg){
        log.info("接受消息:"+msg);
    }
}

5.测试:

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
	private MQSender mqSender;


	
	@RequestMapping("/info")
	@ResponseBody
	public RespBean info(User user) {
		return RespBean.success(user);
	}
	// 
	// @RequestMapping("/mq")
	// @ResponseBody
	// public void mq() {
	// 	mqSender.send("Hello");
	// }
	//
	//
	// 
	// @RequestMapping("/mq/fanout")
	// @ResponseBody
	// public void mq01() {
	// 	mqSender.send("Hello");
	// }
	//
	//
	// 
	// @RequestMapping("/mq/direct01")
	// @ResponseBody
	// public void mq02() {
	// 	mqSender.send01("Hello,Red");
	// }
	//
	// 
	// @RequestMapping("/mq/direct02")
	// @ResponseBody
	// public void mq03() {
	// 	mqSender.send02("Hello,Green");
	// }
	//
	//
	// 
	// @RequestMapping("/mq/topic01")
	// @ResponseBody
	// public void mq04() {
	// 	mqSender.send03("Hello,Red");
	// }
	//
	//
	// 
	// @RequestMapping("/mq/topic02")
	// @ResponseBody
	// public void mq05() {
	// 	mqSender.send04("Hello,Green");
	// }
	//
	//
	// 
	// @RequestMapping("/mq/header01")
	// @ResponseBody
	// public void mq06() {
	// 	mqSender.send05("Hello,Header01");
	// }
	//
	// 
	// @RequestMapping("/mq/header02")
	// @ResponseBody
	// public void mq07() {
	// 	mqSender.send06("Hello,Header02");
	// }
	@Autowired
	private MQSender mqSender;
	@RequestMapping("/mq")
	@ResponseBody
	public void mq(){
		mqSender.send("hellomqsender");
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/604545.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号