-
引入pom
<!--rabbitmq--> <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>
-
配置 application.properties
## rabbitmq 配置(必须) spring.rabbitmq.host=192.168.63.112 spring.rabbitmq.port=5672 spring.rabbitmq.username=admin spring.rabbitmq.password=admin spring.rabbitmq.virtual-host=light_test ## 队列名称 spring.application.name=rabbitmq-provider spring.rabbitmq.exchageName=light_exchange spring.rabbitmq.queueName1=light_queue1 ## 项目端口 server.port=80
-
配置queue和exchage
import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; /** * @Classname RabbitMQConfig * @Description TODO * @Date 2020/7/7 14:23 * @Created by light */ @Configurable public class RabbitMQConfig { @Value("${spring.rabbitmq.exchageName}") private String exchangeName; @Value("${spring.rabbitmq.queueName1}") private String queueName1; /** * 配置 exchange * * @return */ @Bean public DirectExchange exchange() { return new DirectExchange(exchangeName, true, false); } /** * 配置 Queue1 * * @return */ @Bean public Queue lightQueue1() { return new Queue(queueName1, true); } /** * 绑定 指定的queue 到指定的 exchage 上 * @return */ @Bean public Binding bindingQueue1() { return BindingBuilder.bind(lightQueue1()) .to(exchange()) .with(queueName1); } }
-
配置消费者
import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; import java.util.Map; /** * @Classname RabbitMQConsumer * @Description TODO * @Date 2020/7/7 15:30 * @Created by light */ @Component @RabbitListener(queues = "light_queue1") public class RabbitMQConsumer { /** * 此方法为消费方法 * @param messageMap 参数为发送消息的 参数 */ @RabbitHandler public void process (Map messageMap) { System.out.println(messageMap.toString()); } }
-
发送消息
@Resource private RabbitTemplate rabbitTemplate; /** * 发送消息的方法 */ public void sendMq () { HashMap<String, String> map = new HashMap<>(); map.put("name","light"); map.put("id","1"); map.put("type","1"); rabbitTemplate.convertAndSend(exchateName,queueName1,map); System.out.println("发送成功"); }
springboot集成 rabbitMQ
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...