Spring Boot2.X-整合kafka

目录

1.linux安装kafka

2.安装kafka界面工具CMAK

3.spring boot集成kafka

4.测试

5.源码下载


1.linux安装kafka

源码下载:https://download.csdn.net/download/adam_zs/24829887

参考;http://kafka.apache.org/quickstart
备注:现在安装kafka不需要安装ZooKeeper
常用命令:
cd /usr/local/kafka_2.13-3.0.0/
nohup bin/zookeeper-server-start.sh config/zookeeper.properties & #启动zookeeper
nohup bin/kafka-server-start.sh config/server.properties & #启动kafka
bin/kafka-topics.sh --create --topic quickstart-events --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1 #创建主题名quickstart-events
bin/kafka-console-producer.sh --topic quickstart-events --bootstrap-server localhost:9092 #启动kafka生产者
bin/kafka-console-consumer.sh --topic quickstart-events --from-beginning --bootstrap-server localhost:9092 #启动kafka消费者

2.安装kafka界面工具CMAK

参考:https://www.jianshu.com/p/c3c8f55310f9
备注:需要jdk11版本
常用命令:
cd /usr/local/cmak-3.0.0.5
nohup bin/cmak & #启动cmak

3.spring boot集成kafka

Spring Boot2.X-整合kafka_第1张图片

application.yml

spring:
  kafka:
    #生产者
    producer:
      bootstrap-servers: 10.18.xx.xx:9092
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
    #消费者
    consumer:
      bootstrap-servers: 10.18.xx.xx:9092
      group-id: foo
      auto-offset-reset: earliest
    

 pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.5.5
		 
	
	com.wzs.springboot
	springboot.activemq
	1.0.0
	spring-boot-kafka
	Spring Boot kafka
	
		1.8
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.kafka
			spring-kafka
		
		
			org.springframework.boot
			spring-boot-devtools
			runtime
			true
		
		
			org.projectlombok
			lombok
			true
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
				
					
						
							org.projectlombok
							lombok
						
					
				
			
		
	


package com.wzs.springboot.kafka;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @ClassName ConsumerListener
 * @Description:
 * @author wangzs
 * @date 2021年9月26日上午9:38:41
 * @Copyright:
 */
@Component
public class ConsumerListener {
	@KafkaListener(topics = { "topic-1" })
	public void listener(String data) {
		System.out.println(data);
	}
}
package com.wzs.springboot.kafka;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName ProducerController
 * @Description:
 * @author wangzs
 * @date 2021年9月26日上午9:38:46
 * @Copyright:
 */
@RestController
public class ProducerController {
	@Autowired
	private KafkaTemplate kafkaTemplate;

	@PostMapping("/kafka/send")
	public String send(@RequestBody String str) {
		kafkaTemplate.send("topic-1", "topic-1:" + str);
		return str;
	}
}
package com.wzs.springboot.kafka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @ClassName SpringBootKafkaApplication
 * @Description:
 * @author wangzs
 * @date 2021年9月26日上午9:38:51
 * @Copyright:
 */
@SpringBootApplication
public class SpringBootKafkaApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootKafkaApplication.class, args);
	}

}

4.测试

Spring Boot2.X-整合kafka_第2张图片

 

5.源码下载

 https://download.csdn.net/download/adam_zs/24829887

你可能感兴趣的:(Spring,Boot,2.x,kafka,zookeeper,linux,spring,boot)