spring cloud stream kafka 处理消息

spring cloud stream kafka

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-kafka</artifactId>
        </dependency>

自定义管道

import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.stereotype.Component;

@Component
public interface MyProcessor {

    String MESSAGE_INPUT = "log_input";

    String MESSAGE_OUTPUT = "log_output";

    String LOG_FORMAT_INPUT = "log_format_input";

    String LOG_FORMAT_OUTPUT = "log_format_output";

    @Input(MESSAGE_INPUT)
    SubscribableChannel logInput();

    @Output(MESSAGE_OUTPUT)
    MessageChannel logOutput();

    @Input(LOG_FORMAT_INPUT)
    SubscribableChannel logFormatInput();

    @Output(LOG_FORMAT_OUTPUT)
    MessageChannel logFormatOutput();

}

发送,需要绑定@EnableBinding(MyProcessor.class)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;


@EnableBinding(MyProcessor.class)
public class SendService {

    @Autowired
    private MyProcessor source;

    public void sendMsg(String msg) {
        source.logOutput().send(MessageBuilder.withPayload(msg).build());
    }

    public void sendFormatMsg(String msg) {
        source.logFormatOutput().send(MessageBuilder.withPayload(msg).build());
    }
}

接收,receive方法在接收到消息后,可以直接通过@SendTo 转发到其他的消息队列中

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.SendTo;

@EnableBinding(MyProcessor.class)
@Slf4j
public class ReceiveListener {

    @StreamListener(value = MyProcessor.MESSAGE_INPUT)
    @SendTo(value = MyProcessor.LOG_FORMAT_OUTPUT)
    public String receive(String payload) {
        log.info("接收到的信息:" + payload);
        String result="格式化的消息:"+payload;
        return result;
    }

    @StreamListener(value = MyProcessor.LOG_FORMAT_INPUT)
    public void receiveFormat(String payload) {
        log.info("接收到的信息:" + payload);
    }

}

绑定了2个kafka,注意要指定默认的绑定器default-binder,不然启动报错

spring:
    cloud:
      stream:
        default-binder: kafka1
        bindings:
          #与@StreamListener注解中的value一致,是绑定的渠道名
          log_input:
            binder: kafka1
            #绑定的kafka topic名称为test
            destination: topicOne
            #消费组
          #          group: cloud-test2-group1
          #          content-type: application/json
          log_format_input:
            binder: kafka2
            #绑定的kafka topic名称为test
            destination: topicTwo
            #消费组
          # group: cloud-test2-group1
          #          content-type: application/json
          log_output:
            binder: kafka1
            destination: topicOne
          #          content-type: application/json
          log_format_output:
            binder: kafka2
            destination: topicTwo
        binders:
          kafka1:
            type: kafka
            environment:
              spring:
                cloud:
                  stream:
                    kafka:
                      binder:
                        brokers: localhost:9092
                        zkNodes: localhost:2181
          kafka2:
            type: kafka
            environment:
              spring:
                cloud:
                  stream:
                    kafka:
                      binder:
                        brokers: localhost:9093
                        zkNodes: localhost:2182
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Stream是一个基于Spring Boot的框架,用于构建可扩展和可靠的消息驱动型微服务应用程序。而Kafka是一个分布式流媒体平台,用于构建实时数据流应用程序。 在Spring Cloud Stream中集成Kafka非常简单。首先,需要在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-kafka</artifactId> </dependency> ``` 接下来,需要在项目的application.properties文件中配置Kafka的相关信息,包括Kafka服务器的地址和端口号、Topic的名称等。下面是一个示例配置: ```properties spring.cloud.stream.kafka.binder.brokers=192.168.0.1:9092 spring.cloud.stream.bindings.input.destination=my-topic spring.cloud.stream.bindings.output.destination=my-topic ``` 在上述配置中,`spring.cloud.stream.kafka.binder.brokers`指定Kafka服务器的地址和端口号,`spring.cloud.stream.bindings.input.destination`和`spring.cloud.stream.bindings.output.destination`分别指定了输入和输出的Topic名称。 然后,可以使用`@EnableBinding`注解启用绑定器并定义输入和输出的通道。例如,可以创建一个消费者类并定义一个`@Input`注解,用于接收来自Kafka Topic的消息: ```java @EnableBinding(Processor.class) public class Consumer { @StreamListener(Processor.INPUT) public void receive(String message) { System.out.println("Received message: " + message); } } ``` 类似地,可以创建一个生产者类并定义一个`@Output`注解,用于将消息发送到Kafka Topic: ```java @EnableBinding(Processor.class) public class Producer { @Autowired private MessageChannel output; public void send(String message) { output.send(MessageBuilder.withPayload(message).build()); } } ``` 以上是使用Spring Cloud Stream配置Kafka的基本步骤。通过这种方式,我们可以轻松地实现消息驱动型的微服务应用程序,并且享受到Kafka作为分布式流媒体平台的优势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值