Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为多个实体配置ReplyingKafkatTemplate_Java_Spring Boot_Kotlin_Apache Kafka_Spring Kafka - Fatal编程技术网

Java 为多个实体配置ReplyingKafkatTemplate

Java 为多个实体配置ReplyingKafkatTemplate,java,spring-boot,kotlin,apache-kafka,spring-kafka,Java,Spring Boot,Kotlin,Apache Kafka,Spring Kafka,我有两个服务(都是kotlin上的spring boot)。将其命名为“客户端”和“服务器” 由于一些限制,我必须使用kafka的同步请求-应答模式。所以我尝试使用ReplyingKafkaTemplate。我的问题是我需要对多个实体使用。意味着创建多个回复KafkatTemplate,一个代表“FOO”,另一个代表“BAR”。因此,在我的代码中,我创建了多个Kafkanconfig类,其中设置了每个实体和一个基本配置 我还从加载中排除了KafkaAutoConfig。下面是“服务器”端的配置(

我有两个服务(都是kotlin上的spring boot)。将其命名为“客户端”和“服务器” 由于一些限制,我必须使用kafka的同步请求-应答模式。所以我尝试使用ReplyingKafkaTemplate。我的问题是我需要对多个实体使用。意味着创建多个回复KafkatTemplate,一个代表“FOO”,另一个代表“BAR”。因此,在我的代码中,我创建了多个Kafkanconfig类,其中设置了每个实体和一个基本配置

我还从加载中排除了KafkaAutoConfig。下面是“服务器”端的配置(不带Krepling kafka模板):

问题是,若我从共享消费者/生产者配置spring中删除bean命名,那个么在kafkaTEmpalte和producerFactory之间创建循环依赖项。当我将其返回时,spring没有从中看到kafka道具并抛出
,原因是:org.apache.kafka.common.config.ConfigException:bootstrap.servers中没有给出可解析的引导URL,因为无法解析引导服务器

另外,若我删除排除KafkaAutoConfiguration,那个么ContainerListener无法知道witch配置的异常抛出


使用带有一个模板的简单java spring kafka应用程序,一切都很好。

您不需要两个消费者工厂;类型擦除意味着它在运行时是无关的

引导将一个配置为

ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory(
这实际上是
(或Kotlin中的

两个侦听器可以使用相同的工厂

由于您使用的是JSON反序列化程序,因此类型由发送端设置的头确定

模板也是如此

public KafkaTemplate<?, ?> kafkaTemplate
公共卡夫卡模板卡夫卡模板

它可以用不同的泛型类型多次注入,或者作为

感谢Gary Russell提供了正确的方向。我删除了除producer config和kafkaTemplate.Autowire之外的所有配置,并使用所需的类型将其插入kafkaTemplate默认的spring引导配置

@Component
class FOOReplyingKafkaConsumer @Autowired constructor(
    private val fooService: FooService
) {
    @KafkaListener(topics = ["\${kafka.topic.request-FOO-topic}"], containerFactory = "kafkaListenerContainerFactoryForFoo", groupId = "\${spring.kafka.consumer.group-id}")
    @SendTo()
    fun cropListen(request: FOO): FOO{
        return FOO(fooService.getAllByIds(request.ids ?: mutableSetOf()).toMutableSet())
    }
}
ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
public KafkaTemplate<?, ?> kafkaTemplate