首页 > 解决方案 > 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,我没有。

标签: javaspring-bootspring-cloudspring-cloud-streamspring-rabbit

解决方案


几天前我遇到了同样的问题,结果证明这是因为originService不正确。传入context.getId()asoriginService不起作用。

简短的回答:使用org.springframework.cloud.bus.BusProperties#id. 您可以注入BusProperties您的组件。或者您可以按照文档中的说明配置自己的 spring cloud bus id 。

我不是 100% 确定这是正确的方法。也许我错过了文档中的某些内容。它只是基于我从org.springframework.cloud.bus.BusAutoConfiguration方法acceptLocal的源代码中读到的内容。

希望对你有效。


推荐阅读