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

相关文章

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

Java中Springboot集成Kafka实现消息发送和接收功能

《Java中Springboot集成Kafka实现消息发送和接收功能》Kafka是一个高吞吐量的分布式发布-订阅消息系统,主要用于处理大规模数据流,它由生产者、消费者、主题、分区和代理等组件构成,Ka... 目录一、Kafka 简介二、Kafka 功能三、POM依赖四、配置文件五、生产者六、消费者一、Kaf

Kafka拦截器的神奇操作方法

《Kafka拦截器的神奇操作方法》Kafka拦截器是一种强大的机制,用于在消息发送和接收过程中插入自定义逻辑,它们可以用于消息定制、日志记录、监控、业务逻辑集成、性能统计和异常处理等,本文介绍Kafk... 目录前言拦截器的基本概念Kafka 拦截器的定义和基本原理:拦截器是 Kafka 消息传递的不可或缺

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

SpringBoot使用注解集成Redis缓存的示例代码

《SpringBoot使用注解集成Redis缓存的示例代码》:本文主要介绍在SpringBoot中使用注解集成Redis缓存的步骤,包括添加依赖、创建相关配置类、需要缓存数据的类(Tes... 目录一、创建 Caching 配置类二、创建需要缓存数据的类三、测试方法Spring Boot 熟悉后,集成一个外

Docker集成CI/CD的项目实践

《Docker集成CI/CD的项目实践》本文主要介绍了Docker集成CI/CD的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、引言1.1 什么是 CI/CD?1.2 docker 在 CI/CD 中的作用二、Docke

如何在一台服务器上使用docker运行kafka集群

《如何在一台服务器上使用docker运行kafka集群》文章详细介绍了如何在一台服务器上使用Docker运行Kafka集群,包括拉取镜像、创建网络、启动Kafka容器、检查运行状态、编写启动和关闭脚本... 目录1.拉取镜像2.创建集群之间通信的网络3.将zookeeper加入到网络中4.启动kafka集群

SpringBoot集成SOL链的详细过程

《SpringBoot集成SOL链的详细过程》Solanaj是一个用于与Solana区块链交互的Java库,它为Java开发者提供了一套功能丰富的API,使得在Java环境中可以轻松构建与Solana... 目录一、什么是solanaj?二、Pom依赖三、主要类3.1 RpcClient3.2 Public

SpringBoot3集成swagger文档的使用方法

《SpringBoot3集成swagger文档的使用方法》本文介绍了Swagger的诞生背景、主要功能以及如何在SpringBoot3中集成Swagger文档,Swagger可以帮助自动生成API文档... 目录一、前言1. API 文档自动生成2. 交互式 API 测试3. API 设计和开发协作二、使用