首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kafka - spring云流

Kafka - spring云流
EN

Stack Overflow用户
提问于 2020-04-23 04:38:38
回答 2查看 55关注 0票数 0

我正在尝试使用spring-cloud-stream和kafka。以下是示例代码。但它似乎什么也做不了。它总是创建一个叫做“输出”的主题。但是这些值不会被发布。

application.yaml

代码语言:javascript
复制
spring.cloud.stream:
  function:
    definition: streamSupplier
  bindings:
    streamSupplier-out-0:
      destination: numbers

我的目标是创造价值。

代码语言:javascript
复制
@SpringBootApplication
@EnableBinding(Source.class)
public class CloudStreamDemoApplication {

    private AtomicInteger atomicInteger = new AtomicInteger();
    public static void main(String[] args) {
        SpringApplication.run(CloudStreamDemoApplication.class, args);
    }

    @Bean
    public Supplier<Integer> streamSupplier(){
        return () -> {
            System.out.println("Publishing : " + atomicInteger.incrementAndGet());
            return atomicInteger.get();
        };
    }
}

依赖关系- 2.2.6.RELEASE

代码语言:javascript
复制
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-stream</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-stream-binder-kafka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka</artifactId>
    </dependency>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-23 04:47:52

您需要从类中删除@EnableBinding(Source.class)。如果存在这种情况,函数绑定将不会发生。

票数 1
EN

Stack Overflow用户

发布于 2020-04-23 06:26:00

注释@EnableBinding导致了上述问题。

请阅读spring Docs的以下摘录:

Unlike previous versions of spring-cloud-stream which relied on @EnableBinding and @StreamListener annotations, the above example looks no different then any vanilla spring-boot application. It defines a single bean of type Function and that it is. So, how does it became spring-cloud-stream application? It becomes spring-cloud-stream application simply based on the presence of spring-cloud-stream and binder dependencies and auto-configuration classes on the classpath effectively setting the context for your boot application as spring-cloud-stream application. And in this context beans of type Supplier, Function or Consumer are treated as defacto message handlers triggering binding of to destinations exposed by the provided binder following certain naming conventions and rules to avoid extra configuration.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61374690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档