使用SpringCloud开发了一个完整的项目,但是从学习SpringCloud到使用SpringCloud进行完整的项目开发,一直没有进行过系统性的学习,最近工作之余开始慢慢的学习,回顾,总结.之后会慢慢的完善一整个学习及使用过程,已及分享一些项目上使用的方式,不好的地方,请小伙伴们多多指正.刚好最近项目上使用的rabbitMq问题频发,那么第一个分享就从rabbitMq开始.
不同Ubuntu版本之下安装方式略有差异,但是基本相同.
由于rabbitMq需要erlang语言的支持,在安装rabbitMq之前需要安装erlang,执行命令:
sudo apt-get install erlang-nox
在执行该命令时遇到错误:
The package lists or status file could not be parsed or opened
执行其他安装命令也报该错误,一般情况下是不会出现该问题,应该是之前的误操作导致.
解决方式:
sudo rm /var/lib/apt/lists/* -vf sudo apt-get clean sudo apt-get update
sudo apt-get update
sudo apt-get install rabbitmq-server
启动、停止、重启、状态rabbitMq命令:
启动:sudo rabbitmq-server start
关闭: sudo rabbitmq-server stop
重启: sudo rabbitmq-server restart
查看状态:sudo rabbitmqctl status
安装之后默认启动了rabbitmq,但是并不能正常访问rabbitmq管理界面.
进入安装文件夹/usr/lib/rabbitmq/bin,修改rabbitmqctl文件.
HOME=/var/lib/rabbitmq
https://www.cnblogs.com/hongdada/p/7203589.html
略
参考文档:https://blog.csdn.net/dreamchasering/article/details/77653512
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
spring:
application:
name: pikachu
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
publisher-confirms: true
virtual-host: /
listener:
acknowledge-mode: MANUAL
@Component
public class MqReceiver {
@RabbitListener(queues = "pikachu")
@RabbitHandler
public void execute(String content){
System.out.println("content----------->>>"+content);
}
}
@RestController
@RequestMapping(value = "/sendmq")
public class TestController {
@Autowired
private AmqpTemplate amqpTemplate;
@GetMapping
public String sendmq(@RequestParam(name = "content") String content){
amqpTemplate.convertAndSend("pikachu","send content :"+content);
return content;
}
}
启动报错
解决方式:
在管理界面添加队列
应用启动成功,发送消息:
对于一个生产级的应用来说,每一次新起一个消息队列需要在管理界面添加队列名显然不太合适.
解决方式(一):
@Bean
public Queue pikachu() {
return new Queue("pikachu",true);
}
解决方式(二):
@Component
public class MqReceiver {
//@RabbitListener(queues = "pikachu")
//自动创建队列
@RabbitListener(queuesToDeclare =@Queue("pikachu"))
@RabbitHandler
public void execute(String content){
System.out.println("content----------->>>"+content);
}
}
为方便测试我们建立两组
@RabbitHandler
@RabbitListener(bindings = @QueueBinding(
value = @Queue("pikachu_q"),
key = "pikachu_1",
exchange = @Exchange("pikachu_e")
))
public void execute1(String content){
System.out.println("execute1----------->>>"+content);
}
@RabbitHandler
@RabbitListener(bindings = @QueueBinding(
value = @Queue("pikachu_q"),
key = "pikachu_2",
exchange = @Exchange("pikachu_e")
))
public void execute2(String content){
System.out.println("execute2----------->>>"+content);
}
@GetMapping(value = "/q1")
public String sendmq1(@RequestParam(name = "content") String content){
amqpTemplate.convertAndSend("pikachu_e","pikachu_1","send content :"+content);
return content;
}
@GetMapping(value = "/q2")
public String sendmq2(@RequestParam(name = "content") String content){
amqpTemplate.convertAndSend("pikachu_e","pikachu_2","send content :"+content);
return content;
}
分别访问q1/q2得到结果如下:
rabbitMq的初级使用大概就是这些,在下一篇文章中将会讲解spring cloud stream 的使用,已经rabbitMq在多实例的场景下如何消费.
不足之处请小伙伴多多之处,QAQ.
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。