使用Spring Cloud Stream将RabbitMQ使用者绑定到RabbitMQ生产者

时间:2018-10-29 12:55:01

标签: spring spring-boot rabbitmq spring-cloud-stream

我有两种微服务,一种用于从内部FTP服务器收集XML文件,将其转换为DTO对象,然后将它们作为字节发布到RabbitMQ中,另一种用于反序列化从RabbitMQ到DTO对象的传入字节,将它们映射到JPA实体并将它们保留到数据库中。

我想在这两个微服务之间配置RabbitMQ代理,如下所示:

1)对于收集XML文件的微服务,我在application.properties中进行了如下编辑:

spring.cloud.stream.bindings.output.destination=TOPIC
spring.cloud.stream.bindings.output.group=proactive-policy

2)对于持久保留传入DTO进阶的微服务,我在application.properties中配置如下:

spring.cloud.stream.bindings.input.destination=TOPIC
spring.cloud.stream.bindings.input.group=proactive-policy 

为了接收来自RabbitMQ的传入字节,我正在使用第二个微服务作为接收器:

@EnableJpaAuditing
@EnableBinding(Sink.class)
@SpringBootApplication(scanBasePackages = { "org.proactive.policy.data.cache" })
@RefreshScope
public class ProactivePolicyDataCacheApplication {
    private static Logger logger = LoggerFactory.getLogger(ProactivePolicyDataCacheApplication.class);

    @Autowired
    PolicyService policyService;

    public static void main(String[] args) {
        SpringApplication.run(ProactivePolicyDataCacheApplication.class, args);
    }

    @StreamListener(Sink.INPUT)
    public void input(Message<byte[]> message) throws Exception {
        if (Objects.isNull(message) || Objects.isNull(message.getPayload())) {
            logger.error("the message is null ");
            throw new IllegalArgumentException("`message` and `message.payload` cannot be null");
        }
        byte[] data = message.getPayload();
        if (data.length == 0) {
            logger.warn("Received empty message");
            return;
        }
        logger.info("Got data from policy-collector = " + new String(data, "UTF-8"));
        PolicyListDto policyListDto = (PolicyListDto) SerializationUtils.deserialize(data);
        logger.info("Policies.xml from policy-collector = " + policyListDto.getPolicy().toString());

        policyService.save(policyListDto);
    }

}

但是当我打开RabbitMQ控制台查看交易所时,队列 TOPIC.proactive-policy 中没有收到任何信息,但是传入的消息是在另一个队列中接收的我尚未将其配置为 FTPSTREAM.proactive-policy-collector

是否有解决此问题的建议

1 个答案:

答案 0 :(得分:2)

点对点: 1.输出绑定没有“组”之类的东西。消费者组是消费者属性。这是javadocs的片段。

/**
 * Unique name that the binding belongs to (applies to consumers only). Multiple
 * consumers within the same group share the subscription. A null or empty String
 * value indicates an anonymous group that is not shared.
 * @see org.springframework.cloud.stream.binder.Binder#bindConsumer(java.lang.String,
 * java.lang.String, java.lang.Object,
 * org.springframework.cloud.stream.binder.ConsumerProperties)
 */
private String group;

2。 “ FTPSTREAM.proactive-policy-collector”这个名称绝对不是spring-cloud-stream生成的,因此请考虑查看您的配置并查看错过的内容。

它告诉我,您有一个消费者,该消费者的“目的地”为 FTPSTREAM 和其“组”为 proactive-policy-collector 。它还告诉我,您的生产者将消息发送到 FTPSTREAM 交换。

相关问题