Skip to content

Commit 7af4ab4

Browse files
committed
rabbitmq-bus
Signed-off-by: laoye-java <[email protected]>
1 parent 4b65210 commit 7af4ab4

6 files changed

Lines changed: 103 additions & 4 deletions

File tree

aConfigServerRabbitMQ/pom.xml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,35 @@
4545
<groupId>org.springframework.boot</groupId>
4646
<artifactId>spring-boot-starter-web</artifactId>
4747
</dependency>
48+
<!--用于测试的,本例可省略-->
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-test</artifactId>
52+
<version>2.0.4.RELEASE</version>
53+
<scope>test</scope>
54+
</dependency>
4855
<!-- 配置中心BUS消息总线kafka实现
4956
<dependency>
5057
<groupId>org.springframework.cloud</groupId>
5158
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
5259
<version>1.3.2.RELEASE</version>
5360
</dependency>
5461
-->
55-
<!--用于测试的,本例可省略-->
62+
<!--
5663
<dependency>
5764
<groupId>org.springframework.boot</groupId>
58-
<artifactId>spring-boot-starter-test</artifactId>
59-
<scope>test</scope>
65+
<artifactId>spring-boot-starter-amqp</artifactId>
66+
</dependency>
67+
-->
68+
<!--消息总线-->
69+
<dependency>
70+
<groupId>org.springframework.cloud</groupId>
71+
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
72+
</dependency>
73+
<!--config-->
74+
<dependency>
75+
<groupId>org.springframework.cloud</groupId>
76+
<artifactId>spring-cloud-config-monitor</artifactId>
6077
</dependency>
6178
</dependencies>
6279

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.sca.core.rabbitmq;
2+
3+
import org.springframework.amqp.core.Queue;
4+
import org.springframework.context.annotation.Bean;
5+
6+
/**
7+
* RabbitMQ的配置类,用来配队列、交换器、路由等高级信息
8+
*/
9+
public class RabbitConfig {
10+
@Bean
11+
public Queue helloConfig(){
12+
return new Queue("hello");
13+
}
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.sca.core.rabbitmq;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
6+
import org.springframework.amqp.rabbit.annotation.RabbitListener;
7+
import org.springframework.stereotype.Component;
8+
9+
/**
10+
* 消息消费者Receiver 使用@RabbitListener注解定义该类对hello队列的监听, 并用@RabbitHandler 注解来指定对消息的处理方法
11+
*
12+
*/
13+
@Component
14+
@RabbitListener(queues = "hello")
15+
public class Receiver {
16+
private final Logger logger = LoggerFactory.getLogger(Receiver.class);
17+
18+
@RabbitHandler
19+
public void receiver(String hello){
20+
logger.info("接收消息=====》》》》》{}",hello);
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.sca.core.rabbitmq;
2+
3+
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.amqp.core.AmqpTemplate;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Component;
9+
10+
import java.util.Date;
11+
12+
/*
13+
*消息生产者Sender使用AmqpTemplate接口的实例来实现消息的发送
14+
*/
15+
@Component
16+
public class Sender {
17+
18+
private final Logger logger = LoggerFactory.getLogger(Sender.class);
19+
@Autowired
20+
private AmqpTemplate amqpTemplate;
21+
22+
public void sender(){
23+
String context = "wqh say hello " + new Date();
24+
logger.info("发送消息=========》》》》{}",context);
25+
this.amqpTemplate.convertAndSend("hello",context);
26+
}
27+
}

aConfigServerRabbitMQ/src/main/resources/bootstrap.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ eureka:
2626
enabled: true ## 开启健康检查(需要spring-boot-starter-actuator依赖)
2727

2828

29+
2930

3031
spring:
3132
application:
@@ -42,7 +43,13 @@ spring:
4243
native:
4344
# searchLocations: classpath:/config/ #此路径无需配置权限放行
4445
searchLocations: F:/cloud-config #此路径需要配置权限放行
45-
46+
# 配置rabbitmq
47+
rabbitmq:
48+
host: 192.168.124.120 # rabbitmq的主机
49+
port: 5672 # 访问端口
50+
username: liujf #安装RabbitMQ时配置的用户名
51+
password: liujf #安装RabbitMQ时配置的密码
52+
4653
# 是否需要权限拉取,默认是true,如果不设置false就不允许你去拉取配置中心Server更新的内容,
4754
# 同时客户端服务无法执行/refresh接口刷新配置,
4855
# 此配置如果需要BUS消息总线的话,客户端和配置中心都需要配置;不加入BUS消息总线的话只需配置客户端
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//import org.springframework.beans.factory.annotation.Autowired;
2+
//
3+
//@RunWith(SpringRunner.class)
4+
//@SpringBootTest
5+
//public class RabbitmqApplicationTests {
6+
// @Autowired
7+
// private Sender sender;
8+
// @Test
9+
// public void hello(){
10+
// sender.sender();
11+
// }
12+
//}

0 commit comments

Comments
 (0)