简体   繁体   English

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

[英]Spring Cloud Stream - Can't get configured kafka's broker address

I'm trying register manually a kafka listener using Spring Cloud Stream, however I'm facing some problems when trying to connect to broker: 我正在尝试使用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

It is trying connect in localhost:9092 but my server is in another computer (192.168.1.200:9092), what am I doing wrong in this configuration: 它正在尝试在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);
    }
}
}

It is supposed to get this configuration: 应该获得以下配置:

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

So, what I need is a way to get the configured broker address and set it in endpoint object. 因此,我需要一种获取配置的代理地址并将其设置在端点对象中的方法。

Important 重要

As the topic name is dynamic, I can't use annotations like @StreamListener. 由于主题名称是动态的,因此不能使用@StreamListener之类的注释。

You didn't describe your problem nor have you provided any relevant information such as stack trace, logs etc,. 您没有描述问题,也没有提供任何相关信息,例如堆栈跟踪,日志等。 (please do in the future), but I'll try. (请以后再做),但我会尽力的。

You absolutely can use @StreamListener and other annotations with Spring Cloud Stream's support for dynamic destinations . 您绝对可以在Spring Cloud Stream对动态目标的支持下使用@StreamListener和其他注释。

Please go through the above section and let us know if you still need help. 请仔细阅读以上部分,如果您仍然需要帮助,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM