我写了一个宁静的网络服务,Post发布了Kafka消息,Get消费来自Kafka主题。我硬编码了允许我连接到Kafka的属性。我想将它移动到src / main / resources
中的yaml文件producer:
bootstrap.servers: "localhost:9092"
acks: "all"
retries: 0
batch.size: 16384
linger.ms: 1
buffer.memory: 33554432
key.serializer: "org.apache.kafka.common.serialization.StringSerializer"
value.serializer: "org.apache.kafka.common.serialization.StringSerializer"
然后我尝试在我的Kafka Producer Code
中得到这个@Service
public class SonicRestKafkaProducer {
@Value("${producer.bootstrap.servers}")
String bootStrapServers;
public void buildProducer() {
Properties props = new Properties();
if (bootStrapServers == null) {System.out.println("bootStrapServers is null");}
props.put("bootstrap.servers", bootStrapServers);
这导致空指针。
bootStrapServers is null
2016-02-15 17:09:14.941 ERROR 10846 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at java.util.Hashtable.put(Hashtable.java:459) ~[na:1.8.0_31]
at com.sonicdrivein.bdp.kafka.restproxy.kafka.api.SonicRestKafkaProducer.buildProducer(SonicRestKafkaProducer.java:59) ~[kafka-rest-proxy-0.1.0.jar!/:0.1.0]
我尝试过创建一个单独的类。我觉得这很简单,但我没有在文档中看到任何内容,或者在这里解释它。