下面的代码包含我尝试使用的两种类型的 kafka 消费者。
@SpringBootApplication
@EnableBinding(MixApplication.IncomingBinding.class)
@Slf4j
public class MixApplication {
public static void main(String[] args) {
final ConfigurableApplicationContext application
= new SpringApplicationBuilder(MixApplication.class).web(WebApplicationType.SERVLET).run(args);
}
@StreamListener(IncomingBinding.INPUT)
public void listen(Object obj){
log.info("hello1");
}
@Bean("output2")
public Consumer<Object> output2() {
return obj -> {
log.info("hello2");
};
}
public interface IncomingBinding {
String INPUT = "output1";
@Input(INPUT)
SubscribableChannel input();
}
}
有配置
spring:
application:
name: MixApplication
cloud:
stream:
function:
definition: output2
kafka:
binder:
brokers: localhost:9092
bindings:
output2-in-0:
destination: test2
group: "test"
consumer:
concurrency: 10
max-attempts: 3
output1:
destination: test1
group: "test"
consumer:
concurrency: 10
max-attempts: 3
但只有带有 @StreamListener 注解的消费者才能工作。
如果删除 @StreamListener 实现,那么“output2”消费者就可以工作。是否有可能使它们同时工作? 我也看到过类似的项目,但没有答案。 Spring Cloud Function 和 Spring Cloud Stream 一起使用吗?
不,您只能使用其中之一,但由于 4.0 版本中基于注释的编程模型已被完全删除,这意味着您只能使用基于函数的编程模型。
什么是你想做而基于函数的模型无法做到的?