我正在尝试使用Spring Cloud Stream Kafka Binding来发送和接收本地zookeeper和kafka服务器的消息。但是,在启动Spring MVC服务器时,我看到以下异常:
Caused by: org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:154) ~[spring-integration-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:121) ~[spring-integration-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77) ~[spring-integration-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]
... 28 common frames omitted
服务器非常简单,一个类和一个spring应用程序属性文件:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.util.Map;
@RestController
@EnableBinding(ProducerChannels.class)
@SpringBootApplication
public class ProducerApplication {
private final MessageChannel consumer;
public ProducerApplication(ProducerChannels channels){
this.consumer = channels.consumer();
}
@PostMapping("/greet/{name}")
public void publish(@RequestBody Map<String, String> name){
String greeting = "Hello, " + name + "!";
Message<String> msg = MessageBuilder.withPayload(greeting).build();
consumer.send(msg);
}
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
interface ProducerChannels {
@Output
MessageChannel consumer();
}
块引用 spring.cloud.stream.kafka.bindings.consumer.destination = consumer server.port = 8080
配置Spring Cloud Stream还需要做些什么?
答案 0 :(得分:1)
您使用的是哪个版本?我只是将你的代码粘贴到一个启动1.4.2应用程序(1.1.0.RELEASE为流启动器),它对我来说很好。
答案 1 :(得分:0)
消费者是一个特殊的关键字。你不应该使用它。我认为,如果您按照以下说明进行修复,将会发生这种情况。
if ! [[ -d "$AEM_SEGMENTSTORE_LOCATION" && -d "$AEM_SEGMENTSTORE_LOCATION_AZURE" ]]; then
echo "not found"
fi
application.yml文件
public interface ProducerChannels {
String OUTPUT = "example-topic";
@Output(OUTPUT)
MessageChannel exampleTopic();
}
或
spring:
cloud:
stream:
kafka:
binder:
brokers: localhost:9092
bindings:
example-topic:
destination: example-topic
contentType: application/json