Spring Cloud Stream-无法配置kafka的经纪人地址

时间:2018-12-10 19:44:35

标签: java apache-kafka spring-cloud-stream spring-kafka

我正在尝试使用Spring Cloud Stream手动注册kafka侦听器,但是在尝试连接到代理时遇到一些问题:

[Consumer clientId=consumer-1, groupId=h2r] Initialize connection to node localhost:9092 (id: -1 rack: null) for sending metadata request
[Consumer clientId=consumer-1, groupId=h2r] Initiating connection to node localhost:9092 (id: -1 rack: null)
[Consumer clientId=consumer-1, groupId=h2r] Node -1 disconnected.
[Consumer clientId=consumer-1, groupId=h2r] Connection to node -1 could not be established. Broker may not be available.
[Consumer clientId=consumer-1, groupId=h2r] Give up sending metadata request since no node is available

它正在尝试在localhost:9092中进行连接,但是我的服务器在另一台计算机(192.168.1.200:9092)中,在此配置中我在做什么错了:

@Service
public class TenantMessageConsumer {

private final String defaultEnterpriseSchema;
private final MailService mailService;
private final KafkaListenerContainerFactory containerFactory;
private final KafkaListenerEndpointRegistry registry;

public TenantMessageConsumer(String defaultEnterpriseSchema, MailService mailService, KafkaListenerContainerFactory containerFactory, KafkaListenerEndpointRegistry registry) {
    this.defaultEnterpriseSchema = defaultEnterpriseSchema;
    this.mailService = mailService;
    this.containerFactory = containerFactory;
    this.registry = registry;
    listen();
}


public void listen() {
    TenantMessageConsumer that=this;
    AbstractKafkaListenerEndpoint endpoint=new AbstractKafkaListenerEndpoint<String, Object>() {
        @Override
        protected MessagingMessageListenerAdapter createMessageListener(MessageListenerContainer container, MessageConverter messageConverter) {
            try {
                return new RecordMessagingMessageListenerAdapter(that,TenantMessageConsumer.class.getMethod("process",Object.class));
            } catch (NoSuchMethodException e) {
                return null;
            }
        }
    };
    endpoint.setId("tenant");
    endpoint.setTopics(defaultEnterpriseSchema);
    endpoint.setGroupId("h2r");
    registry.registerListenerContainer(endpoint,containerFactory);
}

public void process(Object message){
    if (message instanceof SimpleEmailMessage) {
        SimpleEmailMessage emailMessage = (SimpleEmailMessage) message;
        if (emailMessage.getContent().equals("reset-password"))
            mailService.sendPasswordResetMail(emailMessage);
    }
}
}

应该获得此配置:

spring:
    cloud:
        stream:
            kafka:
                binder:
                    brokers: 192.168.1.200

因此,我需要一种获取配置的代理地址并将其设置在端点对象中的方法。

重要

由于主题名称是动态的,因此不能使用@StreamListener之类的注释。

1 个答案:

答案 0 :(得分:0)

您没有描述问题,也没有提供任何相关信息,例如堆栈跟踪,日志等。 (请以后再做),但我会尽力的。

Spring Cloud Stream对dynamic destinations的支持,绝对可以使用@StreamListener和其他注释。

请仔细阅读以上部分,让我们知道您是否仍需要帮助。