【问题标题】:Springcloud bus custom messages cannot be sent through rabbitmqspringcloud总线自定义消息无法通过rabbitmq发送
【发布时间】: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


【解决方案1】:

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

简答:使用org.springframework.cloud.bus.BusProperties#id。您可以将BusProperties 注入到您的组件中。或者您可以配置自己的spring cloud bus id,如document中所述。

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

希望它对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多