目录
每天一练:Springboot连接rabbitmq
每天一练:Springboot连接rabbitmq
- 目录
- 一、部署Rabbitmq?
- 二、增加maven依赖
- 三、连接RabbitMq
- 四、发布和订阅消息
- 总结
一、部署Rabbitmq?
这里rabbitmq采用docker安装部署。
拉取docker镜像
[root@192 ~]# docker pull rabbitmq
Using default tag: latest
latest: Pulling from library/rabbitmq
7b1a6ab2e44d: Pull complete
37f453d83d8f: Pull complete
e64e769bc4fd: Pull complete
c288a913222f: Pull complete
12addf9c8bf9: Pull complete
eaeb088e057d: Pull complete
b63d48599313: Pull complete
05c99d3d2a57: Pull complete
43665bfbc3f9: Pull complete
Digest: sha256:884146137011519524d506a12687127f3d2c7c37c2cc11206dc72c59bedea5e2
Status: Downloaded newer image for rabbitmq:latest
docker.io/library/rabbitmq:latest
启动容器
docker run -itd --hostname my-rabbit --name rabbit -p 15672:15672 -p 5672:5672 rabbitmq
进入容器内激活rabbitmq的控制台界面,其中175cee03809cc67fd679d9525e3633b3ace4a02cb582533e247c445da6b7a0da 是容器id。
激活管理界面的命令是rabbitmq-plugins enable rabbitmq_management
[root@192 ~]# docker exec -it 175cee03809cc67fd679d9525e3633b3ace4a02cb582533e247c445da6b7a0da /bin/bash
root@my-rabbit:/# rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@my-rabbit:
rabbitmq_management
The following plugins have been configured:
rabbitmq_management
rabbitmq_management_agent
rabbitmq_prometheus
rabbitmq_web_dispatch
Applying plugin configuration to rabbit@my-rabbit...
The following plugins have been enabled:
rabbitmq_management
started 1 plugins.
访问控制台:http://192.168.1.5:15672/,用默认的guest/guest登录,然后重新创建admin/admin用户,并指定tags为administrator
进入docker,启用channel的查看,不配置的话在管理台查看channel会报错:
Stats in management UI are disabled on this node
在docker容器内执行:
root@my-rabbit:/etc/rabbitmq/conf.d# echo management_agent.disable_metrics_collector = false > management_agent.disable_metrics_collector.conf
然后重启docker容器
到这里,rabbitmq安装完成
二、增加maven依赖
在springboot项目的pom.xml中增加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
工程的创建、maven的安装和配置可以参考我之前的文章,都是练习的内容,很简单
用SpringBoot实现post和get请求
三、连接RabbitMq
在Controller中增加方法
@RequestMapping(value="/mq/send/{message}",method=RequestMethod.GET)
public String sendRabbitMQ(@PathVariable("message") String message) {
producer.send(message);
return "0";
}
增加一个常量类定义队列名称,方便练习
package net.csdn.blog.xiaoma.example.springboot.controllerdemo.controllerdemo.services.rabbitmq;
public class RabbitConsts {
public static final String QUEUE_NAME="TEST";
}
在service层增加配置类:
```java
package net.csdn.blog.xiaoma.example.springboot.controllerdemo.controllerdemo.services.rabbitmq;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitmqConfig {
@Bean
public RabbitTemplate rabbitTemplate(CachingConnectionFactory cachingConnectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory);
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
return rabbitTemplate;
}
@Bean
public Queue Queue() {
return new Queue(RabbitConsts.QUEUE_NAME);
}
}
启动服务
四、发布和订阅消息
通过浏览器访问url,发送消息
http://127.0.0.1:8080/demo/mq/send/test2
其中test2模拟的是发送消息。
执行后在rabbitmq中可以看到新增的消息
总结
因为是练习帖,所以代码逻辑和风格不好,以后再完善