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

相关文章

Java Kafka消费者实现过程

《JavaKafka消费者实现过程》Kafka消费者通过KafkaConsumer类实现,核心机制包括偏移量管理、消费者组协调、批量拉取消息及多线程处理,手动提交offset确保数据可靠性,自动提交... 目录基础KafkaConsumer类分析关键代码与核心算法2.1 订阅与分区分配2.2 拉取消息2.3

SpringBoot集成XXL-JOB实现任务管理全流程

《SpringBoot集成XXL-JOB实现任务管理全流程》XXL-JOB是一款轻量级分布式任务调度平台,功能丰富、界面简洁、易于扩展,本文介绍如何通过SpringBoot项目,使用RestTempl... 目录一、前言二、项目结构简述三、Maven 依赖四、Controller 代码详解五、Service

springboot2.1.3 hystrix集成及hystrix-dashboard监控详解

《springboot2.1.3hystrix集成及hystrix-dashboard监控详解》Hystrix是Netflix开源的微服务容错工具,通过线程池隔离和熔断机制防止服务崩溃,支持降级、监... 目录Hystrix是Netflix开源技术www.chinasem.cn栈中的又一员猛将Hystrix熔

Python利用PySpark和Kafka实现流处理引擎构建指南

《Python利用PySpark和Kafka实现流处理引擎构建指南》本文将深入解剖基于Python的实时处理黄金组合:Kafka(分布式消息队列)与PySpark(分布式计算引擎)的化学反应,并构建一... 目录引言:数据洪流时代的生存法则第一章 Kafka:数据世界的中央神经系统消息引擎核心设计哲学高吞吐

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制

SpringBoot集成P6Spy的实现示例

《SpringBoot集成P6Spy的实现示例》本文主要介绍了SpringBoot集成P6Spy的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录本节目标P6Spy简介抛出问题集成P6Spy1. SpringBoot三板斧之加入依赖2. 修改

springboot项目中集成shiro+jwt完整实例代码

《springboot项目中集成shiro+jwt完整实例代码》本文详细介绍如何在项目中集成Shiro和JWT,实现用户登录校验、token携带及接口权限管理,涉及自定义Realm、ModularRe... 目录简介目的需要的jar集成过程1.配置shiro2.创建自定义Realm2.1 LoginReal

SpringBoot集成Shiro+JWT(Hutool)完整代码示例

《SpringBoot集成Shiro+JWT(Hutool)完整代码示例》ApacheShiro是一个强大且易用的Java安全框架,提供了认证、授权、加密和会话管理功能,在现代应用开发中,Shiro因... 目录一、背景介绍1.1 为什么使用Shiro?1.2 为什么需要双Token?二、技术栈组成三、环境

Java 与 LibreOffice 集成开发指南(环境搭建及代码示例)

《Java与LibreOffice集成开发指南(环境搭建及代码示例)》本文介绍Java与LibreOffice的集成方法,涵盖环境配置、API调用、文档转换、UNO桥接及REST接口等技术,提供... 目录1. 引言2. 环境搭建2.1 安装 LibreOffice2.2 配置 Java 开发环境2.3 配

SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南

《SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南》本文将基于开源项目springboot-easyexcel-batch进行解析与扩展,手把手教大家如何在SpringBo... 目录项目结构概览核心依赖百万级导出实战场景核心代码效果百万级导入实战场景监听器和Service(核心