**注意 - 1**:`listeners`参数不可以为默认值`listeners=PLAINTEXT://:9092` , 否则JavaApi可能连接不上。
```properties
listeners=PLAINTEXT://192.168.8.110:9092
```
**注意 - 2**:开放端口
```shell
firewall-cmd --add-port=9092/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-port
```
---
```java
public class ProducerBuilder {
public static void main(String[] args) {
Producer producer = new ProducerBuilder().build();
producer.send(new ProducerRecord("gosuncn", "123456"));
producer.close();
}
private Properties props = new Properties();
{
props.put(ProducerConfig.ACKS_CONFIG, "all");
props.put(ProducerConfig.RETRIES_CONFIG, 0);
props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
props.put(ProducerConfig.LINGER_MS_CONFIG, 1);
props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
}
public ProducerBuilder() {
this("192.168.8.110:9092");
}
public ProducerBuilder(String bootstrapServers) {
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
}
public Producer build() {
return new KafkaProducer(props);
}
}
```
```xml
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.10.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
```
> 特别注意:`ProducerConfig.BOOTSTRAP_SERVERS_CONFIG`一定一定要和`listeners`相关参数保持一致。