当前位置: 首页 > wzjs >正文

即墨网站建设公司宁波seo软件免费课程

即墨网站建设公司,宁波seo软件免费课程,东莞视频网站制作,杭州做网点卖服装在那个网站文章目录 在 Spring Boot 2.7.x 中引入 Kafka-0.9 的实践一、下载 Kafka-0.9二、启动 Zookeeper 和 Kafka三、创建 Spring Boot 项目四、引入 kafka 依赖五、移除 Kafka 自动配置六、编写 Kafka 生产者6.1 Kafka配置类6.2 生产者监听类 七、编写Controller发送Kafka八、验证消费…

文章目录

  • 在 Spring Boot 2.7.x 中引入 Kafka-0.9 的实践
    • 一、下载 Kafka-0.9
    • 二、启动 Zookeeper 和 Kafka
    • 三、创建 Spring Boot 项目
    • 四、引入 kafka 依赖
    • 五、移除 Kafka 自动配置
    • 六、编写 Kafka 生产者
      • 6.1 Kafka配置类
      • 6.2 生产者监听类
    • 七、编写Controller发送Kafka
    • 八、验证消费者
    • Other. Spring Boot 引入 Kafka-0.11

在 Spring Boot 2.7.x 中引入 Kafka-0.9 的实践

一、下载 Kafka-0.9

# 1. 下载
wget https://archive.apache.org/dist/kafka/0.11.0.0/kafka_2.11-0.11.0.0.tgz# 2. 解压
tar -zxvf kafka_2.11-0.11.0.0.tgz# 3. 进入kafka目录
cd kafka_2.11-0.11.0.0

二、启动 Zookeeper 和 Kafka

# 1. 启动 zk 
bin/zookeeper-server-start.sh config/zookeeper.properties# 2. 启动 kafka
bin/kafka-server-start.sh config/server.properties# 3. 创建topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic user_topic# 4. 消费
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic user_topic --from-beginning

三、创建 Spring Boot 项目

https://blog.csdn.net/Agan__/article/details/136109762
https://start.spring.io/

四、引入 kafka 依赖

<dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId><version>1.0.5.RELEASE</version>
</dependency><dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId><version>0.9.0.0</version>
</dependency>

五、移除 Kafka 自动配置

@SpringBootApplication(exclude = {KafkaAutoConfiguration.class})

六、编写 Kafka 生产者

6.1 Kafka配置类

package com.chenjiacheng.samples.kafka.config;import com.chenjiacheng.samples.kafka.kafka.UserProducerListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.support.ProducerListener;import java.util.HashMap;
import java.util.Map;/*** Created by chenjiacheng on 2025/2/27 01:18** @author chenjiacheng* @since 1.0.0*/
@Slf4j
@Configuration
public class KafkaProducerConfig {@Autowiredprivate UserProducerListener userProducerListener;// 配置 User Topic 的 KafkaTemplate 和 ProducerListener@Bean(name = "userProducerFactory")public ProducerFactory<String, String> userProducerFactory() {Map<String, Object> configProps = new HashMap<>();configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:19092");configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);return new DefaultKafkaProducerFactory<>(configProps);}@Bean(name = "userKafkaTemplate")public KafkaTemplate<String, String> userKafkaTemplate() {KafkaTemplate<String, String> kafkaTemplate = new KafkaTemplate<>(userProducerFactory());kafkaTemplate.setProducerListener(userProducerListener);kafkaTemplate.setDefaultTopic("user");return kafkaTemplate;}
}

6.2 生产者监听类

package com.chenjiacheng.samples.kafka.kafka;import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.springframework.kafka.support.ProducerListener;
import org.springframework.stereotype.Component;@Slf4j
@Component
public class UserProducerListener implements ProducerListener<String, String> {@Overridepublic void onSuccess(String topic, Integer partition, String key, String value, RecordMetadata recordMetadata) {log.info("User message sent successfully: {}", value);}@Overridepublic void onError(String topic, Integer partition, String key, String value, Exception exception) {log.error("Failed to send user message: {}", value, exception);}@Overridepublic boolean isInterestedInSuccess() {return true;}
}

七、编写Controller发送Kafka

package com.chenjiacheng.samples.kafka.controller;import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class KafkaController {@Autowiredprivate KafkaTemplate<String, String> orderKafkaTemplate;@GetMapping("/send/user")public String sendUserMessage(@RequestParam("message") String message) {orderKafkaTemplate.sendDefault(message);return "User message sent: " + message;}
}

八、验证消费者

curl --location --globoff 'http://localhost:8081/send/user?message=hello'

在这里插入图片描述

Other. Spring Boot 引入 Kafka-0.11

其他操作同上, 仅修改依赖版本号. 也可直接使用 SpingBoot 依赖管理的 spring-kafka.

<dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId><version>1.3.11.RELEASE</version>
</dependency><dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId><version>0.11.0.0</version>
</dependency>
http://www.dtcms.com/wzjs/399083.html

相关文章:

  • 网站定制页面调整至居中平台推广计划
  • 做网站设计哈尔滨seo
  • seo优化网站快速排名长沙seo网络优化
  • 如何设计营销 网站建设谷歌seo服务
  • 搜企业信息的网站泰安优化关键词排名哪家合适
  • 网络推广网站排名百度云盘网页登录入口
  • 音乐网站开发代码今日重大国际新闻
  • 怎样做免费网站建设产品营销方案案例范文
  • 外贸网站域名网络的推广方式有哪些
  • 河北省建设机械会网站首页成都新一轮疫情
  • 昆明网站建设介绍中国搜索引擎排名2021
  • 网站建设单位是什么意思湖北网站seo设计
  • wordpress自适应 the7优化关键词的作用
  • 企业网站初始期如何优化网站优化推广服务
  • 网站页面做营销型企业网站建设的内容
  • 命令行连接wordpressseo博客教程
  • 常用的广州网站建设优化关键词的步骤
  • 做众筹网站要什么资质网络营销十大成功案例
  • 做b2b_b2c型的混合网站参考网是合法网站吗?
  • 中山市建设工程 交易中心网站百度关键字排名软件
  • 如何在大网站做外链搜狗seo优化
  • 金融投资管理公司网站源码如何做谷歌优化
  • 武鸣网站建设百度灰色关键词排名代做
  • 做公众号好还是网站好网络营销就业方向和前景
  • 网站建设方案策划seo图片优化
  • 沧州1 1 网站建设今日新闻最新
  • 运城做网站军事最新消息
  • 在什么网站能找到做外贸的邮箱长沙本地推广平台
  • 建设农产品网站的背景杭州seo运营
  • 个人备案的网站做企业站百度收录在线提交