首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Springcloud bus自定义消息不能通过rabbitmq发送

Springcloud bus自定义消息不能通过rabbitmq发送
EN

Stack Overflow用户
提问于 2019-04-04 12:02:44
回答 1查看 295关注 0票数 1

当使用rabbitmq总线时,会创建一个自定义消息并通过sent发送,但消息发送后,它不会去springcloud。当您尝试call /actuator/bus-refresh时,您可以看到从rabbitmq的控制台页面发出的总线消息。

我尝试启动微服务来注册自定义事件监听器,但接收失败。但是,如果发送者自己注册了一个侦听器,他可以接收它,但不能从rabbitmq发送它。

代码语言:javascript
复制
 <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

代码语言:javascript
复制
@SpringBootApplication
@RemoteApplicationEventScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
代码语言:javascript
复制
@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";
    }
}
代码语言:javascript
复制
@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中看到信息。

代码语言:javascript
复制
{" 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的时候,我不知道。

EN

回答 1

Stack Overflow用户

发布于 2019-04-19 12:52:07

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

简而言之:使用org.springframework.cloud.bus.BusProperties#id。您可以将BusProperties注入到组件中。或者,您可以按照document中的说明配置您自己的spring cloud bus id。

我不是百分之百确定这是正确的方式。也许我在文档中漏掉了什么。它只是基于我从acceptLocal方法org.springframework.cloud.bus.BusAutoConfiguration的源代码中读到的内容。

希望它能为你工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55507650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档