【发布时间】:2019-04-04 04:02:43
【问题描述】:
使用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时,我没有。
【问题讨论】:
-
对不起,我的英文不是很好,请谅解,我用的是翻译,这是我第一次使用stackoverflow。请多多指教
标签: java spring-boot spring-cloud spring-cloud-stream spring-rabbit