spring boot 使用rabbitmq

配置和工作流程
server.port=8081
logging.path=D:/
logging.level.boot.spring=DEBUG
spring.application.name=spring-boot-rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

创建项目中使用的 队列,交换机,路由 并设置 路由的消息传播方式

rabbitmq 经常使用的有3种

直连模式

	@Bean
 	public DirectExchange directExchange(){
		DirectExchange directExchange=new DirectExchange("direct");
 		return directExchange;
 	}

广播模式

	@Bean
 	public FanoutExchange fanoutExchange(){
 		FanoutExchange fanoutExchange=new FanoutExchange("fanout");
 		return fanoutExchange;
 	}

主题模式

	@Bean
 	public TopicExchange topicExchange(){
		TopicExchange topicExchange=new TopicExchange("mytopic");
 		return topicExchange;
 	}

创建队列

	@Bean
    public Queue topicQueue2() {
       Queue queue=new Queue("topicqueue2");
       return queue;
    }

绑定队列和路由

	@Bean
 	public Binding bindingtopic1(){
 		Binding binding=BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("*.orange.*");//binding key
 		return binding;
 	}
 	

完整的示例

package boot.spring.config;

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;
//topic交换机模型,需要一个topic交换机,两个队列和三个binding
@Configuration
public class TopicExchangeConfig {
	@Bean
 	public TopicExchange topicExchange(){
		TopicExchange topicExchange=new TopicExchange("mytopic");
 		return topicExchange;
 	}
	
	@Bean
    public Queue topicQueue1() {
       Queue queue=new Queue("topicqueue1");
       return queue;
    }
 	
 	@Bean
    public Queue topicQueue2() {
       Queue queue=new Queue("topicqueue2");
       return queue;
    }
	
 	//3个binding将交换机和相应队列连起来
 	@Bean
 	public Binding bindingtopic1(){
 		Binding binding=BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("*.orange.*");//binding key
 		return binding;
 	}
 	
 	@Bean
 	public Binding bindingtopic2(){
 		Binding binding=BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("*.*.rabbit");
 		return binding;
 	}
 	
 	@Bean
 	public Binding bindingtopic3(){
 		Binding binding=BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("lazy.#");//#表示0个或若干个关键字,*表示一个关键字
 		return binding;
 	}
}

创建完 队列,路由,交换机 并相互绑定以后

就可以使用 rabbitTeplate 的 convertAndSend 方法相队列生产消息

@Transactional
@Service("producer")
public class ProducerImpl implements Producer{
	@Autowired
	RabbitTemplate rabbitTemplate;
	public void sendMail(String queue,Mail mail) {
		rabbitTemplate.setQueue(queue);
		rabbitTemplate.convertAndSend(queue,mail);
	}

}

同时使用 rabbitTeplate 的 convertAndSend 方法相队列消费消息

package boot.spring.service.impl;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import boot.spring.po.Mail;
import boot.spring.service.Publisher;


@Service("publisher")
public class PublisherImpl implements Publisher{
	@Autowired
	RabbitTemplate rabbitTemplate;

	public void publishMail(Mail mail) {
		rabbitTemplate.convertAndSend("fanout", "", mail);
	}

	public void senddirectMail(Mail mail, String routingkey) {
		rabbitTemplate.convertAndSend("direct", routingkey, mail);
	}

	public void sendtopicMail(Mail mail, String routingkey) {
		rabbitTemplate.convertAndSend("mytopic", routingkey, mail);
	}

	
	
}

我们可以使用前端请求的方式通知rabbitmq 生产和发送消息,如何通知消息队列去监听消息呢 可以使用 @RabbitListener

@Component
public class SubscribeListener1 {
	
	@Autowired
	LoginService loginService;
	
	@RabbitListener(queues = "queue1")
	public void subscribe(Mail mail) throws IOException {
		System.out.println("订阅者1收到消息"+mail.toString());
		loginService.sendMessage();
	}
}


@Component
public class SubscribeListener2 {
	
	@Autowired
	LoginService loginService;
	
	@RabbitListener(queues = "queue2")
	public void subscribe(Mail mail) throws IOException {
		System.out.println("订阅者2收到消息"+mail.toString());
		loginService.sendMail();
		
	}
}

使用注解
功能说明
代码实现
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot提供了RabbitMQ的集成,可以方便地使用RabbitMQ来实现消息队列的功能。 以下是使用Spring BootRabbitMQ实现消息队列的基本步骤: 1. 添加RabbitMQ依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 2. 配置RabbitMQ连接信息 在application.properties文件中添加以下配置: ``` spring.rabbitmq.host=your_host spring.rabbitmq.port=your_port spring.rabbitmq.username=your_username spring.rabbitmq.password=your_password ``` 3. 创建消息发送者 创建一个消息发送者类,通过注入RabbitTemplate来实现消息发送: ``` @Service public class MessageSender { @Autowired private RabbitTemplate rabbitTemplate; public void send(String message) { rabbitTemplate.convertAndSend("exchange_name", "routing_key", message); } } ``` 4. 创建消息接受者 创建一个消息接受者类,通过注入@RabbitListener注解来实现消息接收: ``` @Service public class MessageReceiver { @RabbitListener(queues = "queue_name") public void receive(String message) { System.out.println("Received message: " + message); } } ``` 5. 发送消息 在需要发送消息的地方,注入MessageSender并调用send方法: ``` @Autowired private MessageSender messageSender; public void sendMessage(String message) { messageSender.send(message); } ``` 6. 启动应用程序 启动应用程序后,消息发送者发送消息,消息接受者监听队列并接收消息。 以上就是使用Spring BootRabbitMQ实现消息队列的基本步骤。需要注意的是,RabbitMQ是一个分布式消息队列系统,需要正确配置连接信息和队列信息才能保证消息的正确传递。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值