我正在尝试向安装在ubuntuvm中的kafka发送消息。
有3个kafka代理已经在vm中启动,一个消费者也在vm中收听这个主题。这一切都很好。
在Windows7上的intellij中,我为一位Kafka制作人编写了一个小的演示应用程序,
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;
public class KafkaProducerApp {
public static void main(String [] args){
// Create a properties dictionary for the required/optional Producer config settings:
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9091,localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
//--> props.put("config.setting", "value");
//:: http://kafka.apache.org/documentation.html#producerconfigs
System.out.println("program start");
KafkaProducer<String, String> myProducer = new KafkaProducer<String, String>(props);
try{
for (int i = 0; i < 150; i++){
myProducer.send(new ProducerRecord<String, String>("my-topic", Integer.toString(i), "MyMessage: " + Integer.toString(i)));
}
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("program close");
myProducer.close();
}
System.out.println("program end");
}
}
当我尝试运行代码时,虽然它没有连接到消息代理,但它似乎进入了一个无限循环,只有当我单击stop时才会停止。下面是intellij终端窗口的输出。我明白了
连接被拒绝
例外如下。有人知道我怎么解决这个问题吗?
我是否可以检查从windows到ubuntu vm端口号的连接?我想知道是否有潜在的防火墙问题阻止连接。
"C:\Program Files\Java\jdk1.8.0_131\bin\java" -Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.1.3\lib\idea_rt.jar=62649:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;C:\Dev\kafka-poc\target\classes;C:\Users\rstannard\.m2\repository\org\apache\kafka\kafka-clients\0.10.2.1\kafka-clients-0.10.2.1.jar;C:\Users\rstannard\.m2\repository\net\jpountz\lz4\lz4\1.3.0\lz4-1.3.0.jar;C:\Users\rstannard\.m2\repository\org\xerial\snappy\snappy-java\1.1.2.6\snappy-java-1.1.2.6.jar;C:\Users\rstannard\.m2\repository\org\slf4j\slf4j-api\1.7.21\slf4j-api-1.7.21.jar;C:\Users\rstannard\.m2\repository\org\slf4j\slf4j-simple\1.7.21\slf4j-simple-1.7.21.jar" com.riskcare.kafkapoc.KafkaProducerApp
program start
[main] INFO org.apache.kafka.clients.producer.ProducerConfig - ProducerConfig values:
acks = 1
batch.size = 16384
block.on.buffer.full = false
bootstrap.servers = [localhost:9091, localhost:9092]
buffer.memory = 33554432
client.id =
compression.type = none
connections.max.idle.ms = 540000
interceptor.classes = null
key.serializer = class org.apache.kafka.common.serialization.StringSerializer
linger.ms = 0
max.block.ms = 60000
max.in.flight.requests.per.connection = 5
max.request.size = 1048576
metadata.fetch.timeout.ms = 60000
metadata.max.age.ms = 300000
metric.reporters = []
metrics.num.samples = 2
metrics.sample.window.ms = 30000
partitioner.class = class org.apache.kafka.clients.producer.internals.DefaultPartitioner
receive.buffer.bytes = 32768
reconnect.backoff.ms = 50
request.timeout.ms = 30000
retries = 0
retry.backoff.ms = 100
sasl.jaas.config = null
sasl.kerberos.kinit.cmd = /usr/bin/kinit
sasl.kerberos.min.time.before.relogin = 60000
sasl.kerberos.service.name = null
sasl.kerberos.ticket.renew.jitter = 0.05
sasl.kerberos.ticket.renew.window.factor = 0.8
sasl.mechanism = GSSAPI
security.protocol = PLAINTEXT
send.buffer.bytes = 131072
ssl.cipher.suites = null
ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
ssl.endpoint.identification.algorithm = null
ssl.key.password = null
ssl.keymanager.algorithm = SunX509
ssl.keystore.location = null
ssl.keystore.password = null
ssl.keystore.type = JKS
ssl.protocol = TLS
ssl.provider = null
ssl.secure.random.implementation = null
ssl.trustmanager.algorithm = PKIX
ssl.truststore.location = null
ssl.truststore.password = null
ssl.truststore.type = JKS
timeout.ms = 30000
value.serializer = class org.apache.kafka.common.serialization.StringSerializer
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name bufferpool-wait-time
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name buffer-exhausted-records
[main] DEBUG org.apache.kafka.clients.Metadata - Updated cluster metadata version 1 to Cluster(id = null, nodes = [localhost:9092 (id: -2 rack: null), localhost:9091 (id: -1 rack: null)], partitions = [])
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name connections-closed:
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name connections-created:
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name bytes-sent-received:
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name bytes-sent:
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name bytes-received:
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name select-time:
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name io-time:
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name batch-size
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name compression-rate
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name queue-time
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name request-time
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name produce-throttle-time
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name records-per-request
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name record-retries
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name errors
[main] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name record-size-max
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.clients.producer.internals.Sender - Starting Kafka producer I/O thread.
[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka version : 0.10.2.1
[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka commitId : e89bffd6b2eff799
[main] DEBUG org.apache.kafka.clients.producer.KafkaProducer - Kafka producer started
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.clients.NetworkClient - Initialize connection to node -1 for sending metadata request
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.clients.NetworkClient - Initiating connection to node -1 at localhost:9091.
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name node--1.bytes-sent
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name node--1.bytes-received
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name node--1.latency
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.common.network.Selector - Connection with localhost/127.0.0.1 disconnected
java.net.ConnectException: Connection refused: no further information
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at org.apache.kafka.common.network.PlaintextTransportLayer.finishConnect(PlaintextTransportLayer.java:51)
at org.apache.kafka.common.network.KafkaChannel.finishConnect(KafkaChannel.java:81)
at org.apache.kafka.common.network.Selector.pollSelectionKeys(Selector.java:335)
at org.apache.kafka.common.network.Selector.poll(Selector.java:303)
at org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:349)
at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:225)
at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:126)
at java.lang.Thread.run(Thread.java:748)
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.clients.NetworkClient - Node -1 disconnected.
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.clients.NetworkClient - Initialize connection to node -2 for sending metadata request
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.clients.NetworkClient - Initiating connection to node -2 at localhost:9092.
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name node--2.bytes-sent
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name node--2.bytes-received
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.common.metrics.Metrics - Added sensor with name node--2.latency
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.common.network.Selector - Connection with localhost/127.0.0.1 disconnected
java.net.ConnectException: Connection refused: no further information
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at org.apache.kafka.common.network.PlaintextTransportLayer.finishConnect(PlaintextTransportLayer.java:51)
at org.apache.kafka.common.network.KafkaChannel.finishConnect(KafkaChannel.java:81)
at org.apache.kafka.common.network.Selector.pollSelectionKeys(Selector.java:335)
at org.apache.kafka.common.network.Selector.poll(Selector.java:303)
at org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:349)
at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:225)
at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:126)
at java.lang.Thread.run(Thread.java:748)
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.clients.NetworkClient - Node -2 disconnected.
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.clients.NetworkClient - Initialize connection to node -1 for sending metadata request
[kafka-producer-network-thread | producer-1] DEBUG org.apache.kafka.clients.NetworkClient - Initiating connection to node -1 at localhost:9091.
Process finished with exit code 1
下面是ubuntu的一些输出
从ppatierno更新到答案(我不能在评论中添加图片,所以在这里添加更新)
谢谢你的评论。下面是来自,
1/我的windows终端使用telnet尝试连接到我的虚拟机会话
2/我的ubuntuvm会话截图,显示localhost 127.0.0.1上打开的端口。
3/ipconfig显示来自主机的ip地址
4/ifconfig显示来自我的ubuntu vm(guest)的ip地址。
我遗漏了什么吗?
更新
我已经设法得到ip和端口识别,下面的链接帮助我配置我的虚拟机,
https://www.howtogeek.com/122641/how-to-forward-ports-to-a-virtual-machine-and-use-it-as-a-server/
但现在,当我尝试运行java producer应用程序时,遇到了另一个问题。日志似乎表明,它能够识别3个代理,但由于某种原因,程序无法与代理连接。
[kafka producer network thread | producer-1]调试org.apache.kafka.clients.networkclient-初始化到节点-1的连接以发送元数据请求[kafka producer network thread | producer-1]调试org.apache.kafka.clients.networkclient-在192.168.56.101:9091启动到节点-1的连接[kafka producer network thread | producer-1]调试org.apache.kafka.common.metrics.metrics-已添加名为node的传感器--1.bytes-已发送[kafka producer network thread | producer-1]调试org.apache.kafka.common.metrics.metrics-已添加名为node的传感器--1.bytes-已接收[kafka producer network thread | producerorg.apache.kafka.common.metrics.metrics-添加了名为node的传感器--1.latency[kafka producer network thread | producer-1]debug org.apache.kafka.common.network.selector-创建了soïrcvbuf=32768、soïsndbuf=131072的套接字,所以| timeout=0到节点-1[kafka producer network thread | producer-1]debug org.apache.kafka.clients.networkclient-已完成到节点-1的连接。正在获取api版本[kafka producer network thread | producer-1]调试org.apache.kafka.clients.networkclient-从节点-1获取初始api版本[kafka producer network thread | producer-1]debug org.apache.kafka.clients.networkclient-为节点1录制的api版本:(producer(0):0到2[可用:2],fetch(1):0到3[可用:3],offset(2):0到1[可用:1],metadata(3):0到2[可用:2],leaderandisr(4):0[可用:0],stopplica(5):0[可用:0],updatemetadata(6):0到3[usable:3],controlledshutdown(7):1[usable:1],offsetcommit(8):0到2[usable:2],offsetfetch(9):0到2[usable:2],groupcoordinator(10):0[usable:0],joingroup(11):0到1[usable:1],heartbeat(12):0[usable:0],leavegroup(13):0[usable:0],syncgroup(14):0[usable:0],describegroups(15):0[usable:0],listgroups(16):0[usable:0],saslhandshake(17):0[usable:0],apiversions(18):0[usable:0],createtopics(19):0到1[usable:1],deletetopics(20):0[usable:0])[kafka producer network thread | producer-1]debug org.apache.kafka.clients.networkclient-发送元数据请求(type=metadatarequest,topics=my topic)到节点-1[kafka producer network thread | producer-1]debug org.apache.kafka.clients.metadata-更新了集群元数据版本2到集群(id=qzei79xctmc8qj48k1jwlw,nodes=[c0287.vm.xx)。com:9092 (id:2机架:空),c0287.vm.xx。com:9090 (id:0机架:空),c0287.vm.xx。com:9091 (id:1机架:空)],分区=[partition(topic=my topic,partition=0,leader=1,replicas=[0,1,2],isr=[1,2,0]),分区(topic=my topic,partition=1,leader=2,replicas=[0,1,2],isr=[2,0,1]),分区(topic=my topic,partition=2,leader=0,replicas=[0,1,2],isr=[0,1],2] )])[kafka producer network thread | producer-1]debug org.apache.kafka.clients.networkclient-开始连接到位于c0287.vm.的节点0。com:9090. 程序关闭[main]info org.apache.kafka.clients.producer.kafkaproducer-关闭超时毫秒为9223372036854775807 ms的Kafka生产者。[kafka生产者网络线程|生产者-1]调试org.apache.kafka.clients.networkclient-连接到c0287.vm.xx处的节点0时出错。com:9090:java.io.ioexception:无法解析地址:c0287.vm.。com:9090 at org.apache.kafka.common.network.selector.connect(选择器。java:182)在org.apache.kafka.clients.networkclient.initiateconnect(networkclient。java:629)在org.apache.kafka.clients.networkclient.ready(networkclient。java:186)在org.apache.kafka.clients.producer.internals.sender.run(sender。java:184)在org.apache.kafka.clients.producer.internals.sender.run(sender。java:126)在java.lang.thread.run(线程。java:748)原因:java.nio.channels.unsolvedAddressException at sun.nio.ch.net.checkaddress(net。java:101)在sun.nio.ch.socketchannelimpl.connect(socketchannelimpl。java:622)在org.apache.kafka.common.network.selector.connect(选择器。java:179) ... 5更多[kafka producer network thread | producer-1]debug org.apache.kafka.clients.producer.internals.sender-开始关闭kafka producer i/o线程,发送剩余记录[kafka producer network thread | producer-1]debug org.apache.kafka.clients.networkclient-开始连接到位于c0287.vm.xx的节点1。com:9091. [kafka producer network thread | producer-1]debug org.apache.kafka.clients.networkclient-连接到位于c0287.vm.xx的节点1时出错。com:9091:java.io.ioexception:无法解析地址:c0287.vm.xx。com:9091
5条答案
按热度按时间l7mqbcuq1#
我在ubuntu16.04和docker上也遇到了同样的问题。对我来说,sudhesh rajan提出了一个解决方案。在/etc/hosts中,我更改了dnsMap。只需确保您没有试图Map端口。
bjp0bcyl2#
确保所有三个kafka代理都配置了播发侦听器,其ip地址和端口可从运行在vm外部的应用程序访问。
请参见此处文档中的Adverted.listeners代理参数
https://kafka.apache.org/documentation/#brokerconfigs
r7knjye23#
改变你的想法
conf/server.properties
文件如下:或
ujv3wf0j4#
从你所说的,在我看来,生产者是运行在主机上,而不是在虚拟机(作为消费者)内部。您正尝试使用以下方式从主机连接:
localhost:9091,localhost:9092
您应该使用虚拟机ip地址或允许虚拟机共享主机网络。
ykejflvf5#
请尝试在主机上的主机文件(windows主机名文件)中提供dnsMap。您可以在system32/etc文件夹下找到它。
那也许会有帮助。