spring boot + kafka 使用详细步骤
来源:互联网 发布:win10网络没有本地连接 编辑:程序博客网 时间:2024/05/22 23:58
一,Kafka的安装以及配置
1.下载文件
wget http://mirror.bit.edu.cn/apache/kafka/0.11.0.0/kafka_2.11-0.11.0.0.tgz
2.安装
tar xzvf kafka_2.11-0.11.0.0.tgz -C /usr/local/
3.配置(服务器在阿里云ECS上)
vi %kafka_home%/server.properties############################# Server Basics #############################broker.id=0port=9092host.name=阿里云内网ipadvertised.host.name=阿里云外网ipdelete.topic.enable=true....其他的配置不变
二,Spring boot操作Kafka
1.Spring boot pom依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --></parent>
2.kafka依赖
<!--kafka支持--><dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId></dependency>
3.kafka配置
#kafka相关配置spring.kafka.bootstrap-servers=阿里云外网ip:9092#设置一个默认组spring.kafka.consumer.group-id=defaultGroup#key-value序列化反序列化spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializerspring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializerspring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializerspring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializerspring.kafka.producer.batch-size=65536spring.kafka.producer.buffer-memory=524288
4.kafka发送代码
@Componentpublic class KafkaSender { @Autowired private KafkaTemplate kafkaTemplate; /** * 发送消息到kafka,主题为test */ public void sendTest(){ kafkaTemplate.send("test","hello,kafka " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))); }}
5.Kafka消费代码
public class KafkaConsumer { /** * 监听test主题,有消息就读取 * @param message */ @KafkaListener(topics = {"test"}) public void consumer(String message){ log.info("test topic message : {}", message); }}
6,启动测试代码
@SpringBootApplication@EnableSchedulingpublic class ServerApplication { @Autowired private KafkaSender kafkaSender; public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); } //然后每隔1分钟执行一次 @Scheduled(fixedRate = 1000 * 60) public void testKafka() throws Exception { kafkaSender.sendTest(); }}
三,以上就是完整的spring boot整合Kafka消息队列的步骤.
阅读全文
0 0
- spring boot + kafka 使用详细步骤
- Spring boot 下使用Kafka实例
- Spring Boot集成Kafka
- Kafka集群搭建详细步骤
- Kafka集群搭建详细步骤
- Kafka集群搭建详细步骤
- spring boot与kafka集成
- Spring Boot集成kafka笔记
- spring boot与kafka集成
- Spring Boot集成kafka笔记
- spring boot之集成kafka
- spring boot 与kafka集成
- 使用spring-kafka操作kafka
- Spring boot集成shiro使用Ajax方式,最详细教程
- spring boot与kafka集成(spring boot 1.5.1版本)
- spring boot 1.5.2自动配置kafka
- spring boot 发送kafka消息队列
- Spring boot 消费kafka 写入hbase
- strcmp源码解惑
- JAVA 对象引用,以及对象赋值
- Ambari--主机管理
- 今天开通了CNSD博客
- Unity性能优化 对象延迟创建
- spring boot + kafka 使用详细步骤
- Maven学习总结(四)——Maven核心概念
- Oracle goldengate 实现mysql到kafka同步配置
- Spark Streaming消息的传输与保证及编程示例
- 【《Real-Time Rendering 3rd》 提炼总结】(一) 全书知识点总览
- 自然语言分词处理,词频统计
- 有关NSValue
- Lambda 详解
- JAVA开发的23种设计模式之 — 装饰器模式 && 代理器模式