java - springcloud总线自定义消息无法通过rabbitmq发送
问题描述
使用springcloud总线时,会创建一条自定义消息并rabbitmq
通过rabbitmq
. 当您尝试时call /actuator/bus-refresh
,您可以看到从 rabbitmq 的控制台页面发出的总线消息。
我试图启动一个微服务来注册一个自定义事件监听器,但没有收到它。但是,如果发送者自己注册了一个监听器,他可以接收它但不会从rabbitmq
.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
关于@RemoteApplicationEventScan
注释,我都在同一个包下编码,所以我应该能够扫描到TestEvent
. 我也尝试指定basepackage
.
@SpringBootApplication
@RemoteApplicationEventScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@RestController
@RequestMapping("test")
public class TestController {
@Autowired
private ApplicationContext context;
@RequestMapping("test")
public String test() {
final TestEvent testEvent = new TestEvent(this, context.getId(), null,"test");
context.publishEvent(testEvent);
return "success";
}
}
@Data
public class TestEvent extends RemoteApplicationEvent {
private String action;
public TestEvent(Object source, String originService, String destinationService, String action) {
super(source, originService, destinationService);
this.action = action;
}
}
当我打电话时,http://localhost:8080/actuator/bus-refresh
我可以在rabbitmq
.
{" type ":" AckRemoteApplicationEvent ", "timestamp" : 1554350325406, "originService" : "application: 0: b3461fbec3536203a7020ff9d24bb11b", "destinationService" : "* *", "id" : "e6b875bd - 2402-494 - f - a870 - 4917324 d2c5c ackId ", "" :" af93075e - 55 d2-41 f8 - ba27 - e3c80cf19eea ", "ackDestinationService" : "* *", "the event" is: "org. Springframework. Cloud. Bus. Event. RefreshRemoteApplicationEvent"}.
但是当我打电话给 时http://localhost:8080/test/test
,我没有。
解决方案
几天前我遇到了同样的问题,结果证明这是因为originService
不正确。传入context.getId()
asoriginService
不起作用。
简短的回答:使用org.springframework.cloud.bus.BusProperties#id
. 您可以注入BusProperties
您的组件。或者您可以按照文档中的说明配置自己的 spring cloud bus id 。
我不是 100% 确定这是正确的方法。也许我错过了文档中的某些内容。它只是基于我从org.springframework.cloud.bus.BusAutoConfiguration
方法acceptLocal的源代码中读到的内容。
希望对你有效。
推荐阅读
- r - 如果不符合某些条件,则将多维数组中的所有值设置为 0
- python - 最后一个子进程调用在 FFMPEG 的串联代码中不起作用。我应该如何解决这个问题?
- django - 如何在 Django Admin 中对外部字段启用列排序
- java - 带有按钮的 Swing JPanel 无法重绘
- python - sys.exc_info() 的目的是什么?
- macos - 在 macOS Catalina 上无法识别 Conda 命令
- javascript - 如何在 Vue 中调用可重用组件上的方法
- angular - 配置我的应用程序以阻止它重写 URL?
- r - 我没有成功使用我想要的 LOCF 方法
- reactjs - 带有样式的材料 UI 与反应测试库抛出警告