spring cloud stream

背景

主要解决不同消息中间件切换问题。实现不同中间件的代码解耦。
链接: 支持的中间件

spring cloud stream_第1张图片
后文使用kafka测试。

引入依赖

     <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-streamartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-stream-kafkaartifactId>
        dependency>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>2021.0.3version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

编写配置文件

配置文件

参考说明

spring:
  application:
    name: streamDemo
  cloud:
    function:
      definition: streamDemo
    stream:
      bindings:
        streamDemo-out-0:
          destination: streamDemo
          group: ${spring.application.name}
        streamDemo-in-0:
          destination: streamDemo
          group: ${spring.application.name}
      kafka:
        binder:
          auto-create-topics: true
          replication-factor: 1
          brokers: kafkaServer:9092

生产者代码

@Service
@Slf4j
public class MiddleMessageProducer {
    private static final String pvRecordChannel  =  "streamDemo-out-0";

    @Resource
    private StreamBridge streamBridge;


    public void saveLogInfo(String msg) {
        streamBridge.send( pvRecordChannel, MessageBuilder.withPayload(msg).build());
    }

}

消费者代码

@Slf4j
@Component
public class MessageConsumer{
    @Bean
    public Consumer<String> streamDemo() {
        return request -> {
            log.info("收到消息:{}",request);
        };
    }
}

验证

你可能感兴趣的:(spring,kafka,spring,cloud)