springboot整合rabbitmq
1.创建一个springboot项目
2.导入rabbitmq依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
3.再加入另外一个依赖(这个依赖可省略,主要是用来简化代码)
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.0.2</version> </dependency>
4.新增实体类
package com.example.rabbitmq.bean; public class UserName { private String username; private String password; private String age; private String host; private String datatime; public String getDatatime() { return datatime; } public void setDatatime(String datatime) { this.datatime = datatime; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } @Override public String toString() { return "UserName{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", age='" + age + '\'' + ", host='" + host + '\'' + ", datatime='" + datatime + '\'' + '}'; } }
5.添加rabbitmq配置类
package com.example.rabbitmq.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.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMQConfig { @Value("${spring.rabbitmq.host}") private String host; @Value("${spring.rabbitmq.port}") private int port; @Value("${spring.rabbitmq.username}") private String username; @Value("${spring.rabbitmq.password}") private String password; @Value("${spring.rabbitmq.virtual-host}") private String virtualhost; public final static String QUEUE_NAME = "spring-boot-queue"; public final static String EXCHANGE_NAME = "spring-boot-exchange"; public final static String ROUTING_KEY = "spring-boot-key"; // 创建队列 @Bean public Queue queue() { return new Queue(QUEUE_NAME); } // 创建一个 topic 类型的交换器 @Bean public TopicExchange exchange() { return new TopicExchange(EXCHANGE_NAME); } // 使用路由键(routingKey)把队列(Queue)绑定到交换器(Exchange) @Bean public Binding binding(Queue queue, TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY); } @Bean public ConnectionFactory connectionFactory() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port); connectionFactory.setUsername(username); connectionFactory.setPassword(password); connectionFactory.setVirtualHost(virtualhost); return connectionFactory; } @Bean public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { return new RabbitTemplate(connectionFactory); } }
6.生产者
package com.example.rabbitmq.ctl; import cn.hutool.core.date.DateTime; import cn.hutool.core.lang.Console; import com.example.rabbitmq.bean.UserName; import com.example.rabbitmq.config.RabbitMQConfig; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ProducerController { @Autowired private RabbitTemplate rabbitTemplate; @Autowired private Consumer consumer; @GetMapping("/sendMessage") public Object sendMessage() { new Thread(() -> { for (int i = 0; i < 100; i++) { String value = new DateTime().toString("yyyy-MM-dd HH:mm:ss"); UserName userName=new UserName(); userName.setAge("12"); userName.setHost("10.10.40.20"); userName.setPassword("admin"); userName.setUsername("admin"); userName.setDatatime(value); Console.log("send message {}", value); rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY, userName.toString()); } }).start(); return "ok"; } }
7.消费者
package com.example.rabbitmq.ctl; import cn.hutool.core.lang.Console; import com.example.rabbitmq.bean.UserName; import com.example.rabbitmq.config.RabbitMQConfig; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class Consumer { @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME) public void consumeMessage(String message) { Console.log("consume message {}", message); } }
8.配置文件
server: port: 8084 spring: rabbitmq: host: 10.10.40.20 port: 5672 username: admin password: admin virtual-host: my_vhost
启动项目执行 localhost:8084/sendMessage
说明rabbit配置成功