rabbitmq--整合springboot使用mq各种模式

1.生产者fallout模式(发布-订阅模式)

创建消费者,生产者模块

加入rabbitmq依赖↓

配置application.properties

编写下述案例

convertAndSend编写一个生成者去发送消息

创建配置类,声明交换机、队列,以及交换机和队列的绑定

 

测试短信发送消息

执行contextloads方法

显示绑定成功↓: 

编写消费者的短信、email邮件、sms队列模块

下图,在类名上面加一个@Service

启动消费者sprinbootapplication,然后再producer测试类类测试调用contextloads方法(上文有)

2.direct路由模式

跟上述差不多,需要注意的是配置类在生产者和消费者都要有,而且一摸一样;下方是配置类,要有routingkey

生产者service层编写发送消息方法 

在消费者编写 接受信息并在控制台中打印信息

 生产者test类 编写发送信息方法

然后测试上述方法就行↑

总结:生产者和消费者最好都要有同样的配置类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot整合MQ消息队列)可以使用Spring Boot集成的JMS(Java消息服务)或AMQP(高级消息队列协议)实现。下面是使用AMQP的步骤: 1. 添加AMQP依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 2. 配置连接信息 在application.properties文件中添加以下配置: ```properties spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest ``` 3. 创建消息发送者 ```java import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MessageSender { @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(String message) { rabbitTemplate.convertAndSend("my-exchange", "my-routing-key", message); } } ``` 4. 创建消息接收者 ```java import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class MessageReceiver { @RabbitListener(queues = "my-queue") public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ``` 5. 配置消息队列 ```java import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMQConfig { @Bean public Queue myQueue() { return new Queue("my-queue"); } @Bean public TopicExchange myExchange() { return new TopicExchange("my-exchange"); } @Bean public Binding binding(Queue myQueue, TopicExchange myExchange) { return BindingBuilder.bind(myQueue).to(myExchange).with("my-routing-key"); } } ``` 6. 测试 创建一个Spring Boot应用程序,然后使用MessageSender发送消息,MessageReceiver将接收到该消息。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application implements CommandLineRunner { @Autowired private MessageSender messageSender; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { messageSender.sendMessage("Hello, world!"); } } ``` 可以在控制台看到以下输出: ``` Received message: Hello, world! ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值