Apache kafka Kafka:通过公共ip从windows访问vm上的Kafka服务器

Apache kafka Kafka:通过公共ip从windows访问vm上的Kafka服务器,apache-kafka,centos,remote-access,Apache Kafka,Centos,Remote Access,我有个小问题。我想从windows Java producer脚本连接到vm centos上的kafka服务器 在config/server.properties中,我有一行: listeners=PLAINTEXT://0.0.0.0:9092 advertised.listeners=PLAINTEXT://<public ip>:9092 listeners=PLAINTEXT://0.0.0.0:9092 播发。侦听器=纯文本://:9092 (例如:advised.l

我有个小问题。我想从windows Java producer脚本连接到vm centos上的kafka服务器

config/server.properties中,我有一行:

listeners=PLAINTEXT://0.0.0.0:9092

advertised.listeners=PLAINTEXT://<public ip>:9092
listeners=PLAINTEXT://0.0.0.0:9092
播发。侦听器=纯文本://:9092
(例如:advised.listeners=PLAINTEXT://192.239.83.27:9092)

我仍然无法从windows上的producer生成消息。在虚拟机上,我在网络设置中设置了“允许所有”选项

无法运行kafka服务器,在持续运行时出现警告: 无法建立到节点0的[controller id=0,targetBrokerId=0]连接。代理可能不可用(org.apache.kafka.clients.NetworkClient)

请帮忙:p

以及我在windows上的Java代码:

public class TwitterProducer {

Logger logger = LoggerFactory.getLogger(TwitterProducer.class.getName());

String consumerKey = "xxx";
String consumerSecret = "xxx";
String token = "xxx";
String secret = "xxx";

public TwitterProducer() {}

public static void main(String[] args) {
    new TwitterProducer().run();
}

public void run()
{
    logger.info("Setup");
    /** Set up your blocking queues: Be sure to size these properly based on expected TPS of your stream */
    BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>(1000);

    // create a twitter client
    Client client = createTwitterClient(msgQueue);
    // Attempts to establish a connection.
    client.connect();

    // create a kafka producer
    KafkaProducer<String, String> producer = createKafkaProducer();

    // on a different thread, or multiple different threads....
    while (!client.isDone()) {
        String msg = null;
        try {
            msg = msgQueue.poll(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
            client.stop();
        }
        if(msg != null){
            logger.info(msg);
            producer.send(new ProducerRecord<String, String>("twitter_tweets", null, msg), new Callback() {
                public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                    if (e != null){
                        logger.error("Something bad happened", e);
                    }
                }
            });
        }
    }
    logger.info("End of application");
}

public Client createTwitterClient(BlockingQueue<String> msgQueue)
{
    /** Declare the host you want to connect to, the endpoint, and authentication (basic auth or oauth) */
    Hosts hosebirdHosts = new HttpHosts(Constants.STREAM_HOST);
    StatusesFilterEndpoint hosebirdEndpoint = new StatusesFilterEndpoint();
    // Optional: set up some followings and track terms

    List<String> terms = Lists.newArrayList("bitcoin");
    hosebirdEndpoint.trackTerms(terms);


    // These secrets should be read from a config file
    Authentication hosebirdAuth = new OAuth1(consumerKey, consumerSecret, token, secret);

    ClientBuilder builder = new ClientBuilder()
            .name("Hosebird-Client-01")                              // optional: mainly for the logs
            .hosts(hosebirdHosts)
            .authentication(hosebirdAuth)
            .endpoint(hosebirdEndpoint)
            .processor(new StringDelimitedProcessor(msgQueue))
             ;                          // optional: use this if you want to process client events

    Client hosebirdClient = builder.build();
    return hosebirdClient;
}

public KafkaProducer<String, String> createKafkaProducer()
{

    String bootstrapServers = "193.239.83.27:9092";



    // create the producer properties
    Properties properties = new Properties();
    properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
    properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

    // create the producer
    KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);
    return producer;
}
公共类推特制作人{
Logger Logger=LoggerFactory.getLogger(TwitterProducer.class.getName());
字符串consumerKey=“xxx”;
字符串consumerSecret=“xxx”;
字符串标记=“xxx”;
字符串secret=“xxx”;
公共TwitterProducer(){}
公共静态void main(字符串[]args){
新建TwitterProducer().run();
}
公开募捐
{
logger.info(“设置”);
/**设置阻塞队列:确保根据流的预期TPS适当调整这些队列的大小*/
BlockingQueue msgQueue=新的LinkedBlockingQueue(1000);
//创建一个twitter客户端
Client Client=createTwitterClient(msgQueue);
//尝试建立连接。
client.connect();
//创建卡夫卡制作人
KafkaProducer producer=createKafkaProducer();
//在不同的线程上,或在多个不同的线程上。。。。
而(!client.isDone()){
字符串msg=null;
试一试{
msg=msgQueue.poll(5,时间单位:秒);
}捕捉(中断异常e){
e、 printStackTrace();
client.stop();
}
如果(msg!=null){
logger.info(msg);
send(newproducerrecord(“twitter_tweets”,null,msg),newcallback(){
完成时的公共void(RecordMetadata RecordMetadata,异常e){
如果(e!=null){
logger.error(“发生了不好的事情”,e);
}
}
});
}
}
logger.info(“应用程序结束”);
}
公共客户端createTwitterClient(BlockingQueue msgQueue)
{
/**声明要连接到的主机、端点和身份验证(基本身份验证或oauth)*/
Hosts hosebirdHosts=新的HttpHosts(Constants.STREAM_HOST);
StatusesFilterEndpoint HoseBindPoint=新的StatusesFilterEndpoint();
//可选:设置以下内容和跟踪术语
列表术语=Lists.newArrayList(“比特币”);
hosebirdEndpoint.trackTerms(术语);
//这些秘密应该从配置文件中读取
身份验证hosebirdAuth=new OAuth1(consumerKey、consumerSecret、token、secret);
ClientBuilder=newclientbuilder()
.name(“Hosebird-Client-01”)//可选:主要用于日志
.hosts(hosebirdHosts)
.身份验证(hosebirdAuth)
.终点(hosebirdEndpoint)
.processor(新的StringDelimitedProcessor(msgQueue))
;//可选:如果要处理客户端事件,请使用此选项
客户端hosebirdClient=builder.build();
返回医院客户;
}
公共卡夫卡制作人创建卡夫卡制作人()
{
字符串bootstrapserver=“193.239.83.27:9092”;
//创建生产者属性
属性=新属性();
setProperty(ProducerConfig.BOOTSTRAP\u SERVERS\u CONFIG,bootstrapserver);
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,StringSerializer.CLASS.getName());
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.CLASS.getName());
//创建制作人
卡夫卡制作人=新卡夫卡制作人(属性);
回报生产者;
}

}你说你有
监听器=纯文本:<0.0.0.0:9092

但图像显示,这已被注释掉

日志还说卡夫卡正在关闭,所以请确保brokerId是正数,并确保Zookeeper正在运行


顺便说一句,有一个Kafka Connect的推特来源,它似乎做了你想做的事

是的,我评论了这句话。是否应该取消注释??您说要将Broker_id=0更改为Broker_id=positive number,例如1?我是否应该在etc/hosts中将主机名从localhost更改为另一个?天哪,卡夫卡病得太厉害了。。我无法应付这件事。我只是想拥有远程弹性公共ip访问权,如果有评论,就不会加载任何属性。默认情况下不在外部侦听2)永远不要使用etc/hosts来解决此问题3)我真的不知道你的代理为什么要停止。我想你已经读过了