sring coud 2集成kafka

2024-02-25 04:18
文章标签 集成 kafka coud sring

本文主要是介绍sring coud 2集成kafka,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

安装zookeeper

docker run --privileged=true --name zookeeper -p 2181:2181  -d zookeeper

安装kafka
192.168.0.33为外网访问地址

docker run --name kafka -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=192.168.0.33:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.0.33:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -d wurstmeister/kafka

maven

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

application.yml
Spring Cloud 2 中 zk-nodes不用设置
在这里插入图片描述

spring:cloud:stream:bindings:shop_input:binder: kafka1consumer:headerMode: rawproducer:headerMode: raw#绑定的kafka topic名称destination: shop-topiccontent-type: text/plainshop_output:binder: kafka1consumer:headerMode: rawproducer:headerMode: rawdestination: shop-topiccontent-type: text/plainbinders:#可以配置多个kafkakafka1:type: kafkaenvironment:spring:cloud:stream:kafka:binder:#kafka地址brokers: http://kafka:9092auto-add-partitions: trueauto-create-topics: truemin-partition-count: 1

Source

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;/*** @author yl*/
public interface MySource {String SHOP_OUTPUT = "shop_output";@Output(MySource.SHOP_OUTPUT)MessageChannel output();}

Sink

import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;/*** @author yl*/
public interface MySink {String SHOP_INPUT = "shop_input";@Input(MySink.SHOP_INPUT)SubscribableChannel input();}

KafkaSendTemplate

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;/*** kafka消息发送模板** @author yl*/
@EnableBinding(MySource.class)
public class KafkaSendTemplate {@Autowiredprivate MySource source;public void sendMessage(String msg) {try {source.output().send(MessageBuilder.withPayload(msg).build());} catch (Exception e) {e.printStackTrace();}}
}

KafkaConsumer

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;/*** kafka消息监听** @author yl*/
@EnableBinding(MySink.class)
@Slf4j
public class KafkaConsumer {@AutowiredSpCartService spCartService;@StreamListener(MySink.SHOP_INPUT)public void onReceive(String shopJson) {log.info(shopJson);ShopKafkaDTO shopKafkaDTO = JSONObject.parseObject(shopJson, ShopKafkaDTO.class);log.info("get Kafka message:{}", shopKafkaDTO);}
}

MyController
定义一个controller发送消息

@RestController
public class MyController {@Autowiredprivate KafkaSendTemplate kafkaSendTemplate;@GetMapping("/send")public void sendMessage(@RequestParam("message") String message) {kafkaSendTemplate.sendMessage(message);}
}

这篇关于sring coud 2集成kafka的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/744367

相关文章

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

SpringBoot集成LiteFlow工作流引擎的完整指南

《SpringBoot集成LiteFlow工作流引擎的完整指南》LiteFlow作为一款国产轻量级规则引擎/流程引擎,以其零学习成本、高可扩展性和极致性能成为微服务架构下的理想选择,本文将详细讲解Sp... 目录一、LiteFlow核心优势二、SpringBoot集成实战三、高级特性应用1. 异步并行执行2

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

Java集成Onlyoffice的示例代码及场景分析

《Java集成Onlyoffice的示例代码及场景分析》:本文主要介绍Java集成Onlyoffice的示例代码及场景分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 需求场景:实现文档的在线编辑,团队协作总结:两个接口 + 前端页面 + 配置项接口1:一个接口,将o

SpringBoot实现Kafka动态反序列化的完整代码

《SpringBoot实现Kafka动态反序列化的完整代码》在分布式系统中,Kafka作为高吞吐量的消息队列,常常需要处理来自不同主题(Topic)的异构数据,不同的业务场景可能要求对同一消费者组内的... 目录引言一、问题背景1.1 动态反序列化的需求1.2 常见问题二、动态反序列化的核心方案2.1 ht

Swagger2与Springdoc集成与使用详解

《Swagger2与Springdoc集成与使用详解》:本文主要介绍Swagger2与Springdoc集成与使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1. 依赖配置2. 基础配置2.1 启用 Springdoc2.2 自定义 OpenAPI 信息3.