我试图通过Kafka(Jhipster / SpringBoot制作人和Python消费者)发送JSON,我已经能够以字节为单位接收消息,但是当我尝试解码它时,我总是在能够提取消息之前遇到一些问题。
我的制片人是这样的:
@RestController
@RequestMapping("/api")
public class ProducerResource{
private MessageChannel channel;
public ProducerResource(ProducerChannel channel) {
this.channel = channel.messageChannel();
}
@GetMapping("/subscribableChannel/{count}")
@Timed
@JsonInclude(Include.NON_NULL)
public void produce(@PathVariable int count) {
Map<String,String> json = new HashMap<String,String>();
json.put("message","HELL YEAH");
while(count > 0) {
channel.send(MessageBuilder.withPayload(json.toString().getBytes()).build());
count--;
}
}
}
我的Python消费者:
def stop_handler(signal, frame, consumer):
print('Arrêt...')
consumer.close()
sys.exit(0)
def read_messages(consumer):
meta = consumer.partitions_for_topic(TOPIC)
for msg in consumer:
print(msg.value)
lol = json.load(msg.value.decode("utf-8"))
print(lol)
def main():
consumer = KafkaConsumer(TOPIC, auto_offset_reset='earliest',
group_id='read', bootstrap_servers=['localhost:9092'])
signal.signal(signal.SIGINT, lambda signal, frame: stop_handler(signal, frame, consumer))
read_messages(consumer)
return 0
if __name__ == "__main__":
exit(main())
我从Python得到的警告是:
python3 scripts/simple_consumer.py
b'\xff\x01\x0bcontentType\x00\x00\x00\x0c"text/plain"{message=HELL YEAH}'
Traceback (most recent call last):
File "scripts/simple_consumer.py", line 36, in <module>
exit(main())
File "scripts/simple_consumer.py", line 31, in main
read_messages(consumer)
File "scripts/simple_consumer.py", line 23, in read_messages
lol = json.load(msg.value.decode("utf-8"))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
在我的云配置中,我只有消费者/生产者HeaderMode = raw
和kafka。任何建议都会非常感激!!
UPDATE --- 这是我对云的配置:
spring:
profiles:
active: dev
include: swagger
devtools:
restart:
enabled: true
livereload:
enabled: false # we use gulp + BrowserSync for livereload
jackson:
serialization.indent_output: true
cloud:
stream:
default:
consumer:
headerMode: raw
producer:
headerMode: raw
kafka:
binder:
brokers: localhost
zk-nodes: localhost
bindings:
messageChannel:
destination: messageChannel
content-type: application/json
subscribableChannel:
destination: subscribableChannel