spring boot2 kafka
一.软件版本
1.linux:centos6
2.zookeeper:zookeeper-3.4.1
3.kafka:kafka_2.12-2.2.0
4.jdk:1.8
5.instelliJ Idea
二.环境准备
1.关闭防火墙:service iptables stop
2.zookeeper:复制conf目录下的zoo_sample.cfg配置文件并改名为zoo.cfg
3.kafka:修改配置文件conf/server.properties中
①broker.id=123(broker.id每个kafka的都不一样,必须唯一)
②advertised.listeners=PLAINTEXT://192.168.56.101:9092(对应自己的linux的ip地址)
③zookeeper.connect=127.0.0.1:2181(本机默认端口是2181,有需要到zookeeper的zoo.cfg文件中修改)
三.软件setup&start
1.zookeeper:zookeeper-3.4.14/bin目录下执行
①./zkServer.sh start ../conf/zoo.cfg
2.kafka:kafka_2.12-2.2.0/bin目录下执行
①./kafka-server-start.sh ../config/server.properties
四.创建topic&发送消息&消费消息
1.创建topic “test”,kafka_2.12-2.2.0/bin目录下执行
①./kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
②验证是否创建成功:./kafka-topics.sh --list --bootstrap-server localhost:9092

③开启消息生产者的console,kafka_2.12-2.2.0/bin目录下执行,并发送几条信息
./kafka-console-producer.sh --broker-list localhost:9092 --topic test

④开启消息消费者的console,kafka_2.12-2.2.0/bin目录下执行
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test

以上步骤没出现问题表示kafka服务器端以及搭建完毕。
五.Spring boot2 kafka通信
1.创建topic:TopicConfiguration
package com.aaron.kafka.springboot.configuration; import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaAdmin;
import org.springframework.util.StringUtils; import java.util.HashMap;
import java.util.Map; @Configuration
public class TopicConfiguration {
@Bean
public KafkaAdmin admin() {
Map<String, Object> configs = new HashMap<>();
configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,
StringUtils.arrayToCommaDelimitedString(new String[]{"192.168.56.101:9092"}));
return new KafkaAdmin(configs);
} @Bean
public NewTopic topic1() {
return new NewTopic("test", 1, (short) 2);
} }
2.spring boot2中建立kafka的消息生产者:KafkaProducerConfiguration
package com.aaron.kafka.springboot.configuration; import com.aaron.kafka.springboot.listener.MessageListenerImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.listener.KafkaMessageListenerContainer; import java.util.HashMap;
import java.util.Map; @Configuration
public class kafkaConsumerConfiguration { @Bean
public MessageListenerImpl messageListener(){
return new MessageListenerImpl();
} @Bean
public KafkaMessageListenerContainer kafkaMessageListenerContainer(){
ContainerProperties containerProps = new ContainerProperties("test");
containerProps.setMessageListener(messageListener());
DefaultKafkaConsumerFactory<Integer, String> cf =
new DefaultKafkaConsumerFactory<Integer, String>(consumerProps());
KafkaMessageListenerContainer<Integer, String> container =
new KafkaMessageListenerContainer<>(cf, containerProps);
return container;
} /**
* 设置consumer的properties
* @return
*/
private Map<String,Object> consumerProps(){
Map<String, Object> consumerProps = new HashMap<>();
consumerProps.put("bootstrap.servers","192.168.56.101:9092");
consumerProps.put("group.id","123");
consumerProps.put("enable.auto.commit","true");
consumerProps.put("auto.commit.interval.ms","1000");
consumerProps.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
consumerProps.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
return consumerProps;
}
}
3.建立测试代码:KafkaTest
package com.aaron.kafka.springboot; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @RunWith(SpringRunner.class)
@SpringBootTest
public class KafkaTest { @Resource
private KafkaTemplate kafkaTemplate; @Test
public void sendMsg(){
kafkaTemplate.send("test","hello","world");
}
}
运行测试结果如下:

4.Spring boot2中建立消费者和消息监听器:kafkaConsumerConfiguration和MessageListenerImpl
package com.aaron.kafka.springboot.configuration; import com.aaron.kafka.springboot.listener.MessageListenerImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.listener.KafkaMessageListenerContainer; import java.util.HashMap;
import java.util.Map; @Configuration
public class kafkaConsumerConfiguration { @Bean
public MessageListenerImpl messageListener(){
return new MessageListenerImpl();
} @Bean
public KafkaMessageListenerContainer kafkaMessageListenerContainer(){
ContainerProperties containerProps = new ContainerProperties("test");
containerProps.setMessageListener(messageListener());
DefaultKafkaConsumerFactory<Integer, String> cf =
new DefaultKafkaConsumerFactory<Integer, String>(consumerProps());
KafkaMessageListenerContainer<Integer, String> container =
new KafkaMessageListenerContainer<>(cf, containerProps);
return container;
} /**
* 设置consumer的properties
* @return
*/
private Map<String,Object> consumerProps(){
Map<String, Object> consumerProps = new HashMap<>();
consumerProps.put("bootstrap.servers","192.168.56.101:9092");
consumerProps.put("group.id","123");
consumerProps.put("enable.auto.commit","true");
consumerProps.put("auto.commit.interval.ms","1000");
consumerProps.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
consumerProps.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer");
return consumerProps;
}
}
package com.aaron.kafka.springboot.listener;
import org.springframework.kafka.listener.MessageListener; public class MessageListenerImpl implements MessageListener {
@Override
public void onMessage(Object o) {
System.out.println(o.toString());
}
}
关闭服务器上的消费者然后启动springBoot
在服务器上的producer中输入文本 Hello World
Idea 中的console显示如下:

六.kafka的集群配置
1.复制配置文件server.properties改名为server-1.properties、server-2.properties
修改配置文件中
broker.id=121
log.dirs=/tmp/kafka-logs-1
advertised.listeners=PLAINTEXT://192.168.56.101:9093
(server-2.properties中的则为:
broker.id=122
log.dirs=/tmp/kafka-logs-2
advertised.listeners=PLAINTEXT://192.168.56.101:9094)
2.运行方式启动kafka,kafka_2.12-2.2.0/bin下执行
./kafka-server-start.sh ../config/server-1.properties
./kafka-server-start.sh ../config/server-2.properties
后台方式运行命令如下:
nohup ./kafka-server-start.sh ../config/server-1.properties &
nohup ./kafka-server-start.sh ../config/server-2.properties &
注意:如果出现

则配置文件(server-1.properties和server-2.properties)中打开注释并修改为:listeners=PLAINTEXT://192.168.56.101:9093(server-2.properties的为:listeners=PLAINTEXT://192.168.56.101:9094)
spring boot2 kafka的更多相关文章
- Spring Boot2.0 整合 Kafka
Kafka 概述 Apache Kafka 是一个分布式流处理平台,用于构建实时的数据管道和流式的应用.它可以让你发布和订阅流式的记录,可以储存流式的记录,并且有较好的容错性,可以在流式记录产生时就进 ...
- Spring Boot2.0 设置拦截器
所有功能完成 配置登录认证 配置拦截器 在spring boot2.0 之后 通过继承这个WebMvcConfigurer类 就可以完成拦截 新建包com.example.interceptor; 创 ...
- Spring Boot2.0 静态资源被拦截问题
在Spring Boot2.0+的版本中,只要用户自定义了拦截器,则静态资源会被拦截.但是在spring1.0+的版本中,是不会拦截静态资源的. 因此,在使用Spring Boot2.0+时,配置拦截 ...
- Spring Boot2.0使用Spring Security
一.Spring Secutity简介 Spring 是一个非常流行和成功的 Java 应用开发框架.Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性 ...
- spring boot2.0(一 ) 基础环境搭建
1.基础配置 开发环境:window jdk版本:1.8(spring boot2.0最低要求1.8) 开发工具:eclipse 构建方式:maven3 2.POM配置文件 <project x ...
- Spring Boot2.0自定义配置文件使用
声明: spring boot 1.5 以后,ConfigurationProperties取消locations属性,因此采用PropertySource注解配合使用 根据Spring Boot2. ...
- Spring boot2.0 设置文件上传大小限制
今天把Spring boot版本升级到了2.0后,发现原来的文件上传大小限制设置不起作用了,原来的application.properties设置如下: spring.http.multipart.m ...
- spring boot 2.0(一)权威发布spring boot2.0
Spring Boot2.0.0.RELEASE正式发布,在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误,然后Spring ...
- 【spring cloud】spring cloud2.X spring boot2.0.4调用feign配置Hystrix Dashboard 和 集成Turbine 【解决:Hystrix仪表盘Unable to connect to Command Metric Stream】【解决:Hystrix仪表盘Loading...】
环境: <java.version>1.8</java.version><spring-boot.version>2.0.4.RELEASE</spring- ...
随机推荐
- WatchDirService 实时监控
import config.Config; import java.io.IOException; import java.nio.file.*; import java.util.List; imp ...
- 安装完Ubuntu后要做的事情
Ubuntu 16.04安装完成后,还需要做一些配置才能愉快的使用,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java写出来的off ...
- 浅谈Linux文件与目录权限
作为一个程序员,在工作的过程中或多或少都会接触都Linux,那么对于权限这块肯定有所了解,今天有空想谈谈觉得比较绕的权限问题,即文件权限与目录权限 1.文件权限,对于文件权限这个是比较简单的,也很容易 ...
- 用Nuget部署程序包
用Nuget部署程序包 Nuget是.NET程序包管理工具(类似linux下的npm等),程序员可直接用简单的命令行(或VS)下载包.好处: (1)避免类库版本不一致带来的问题.GitHub是管理源代 ...
- new Date().getTime()和System.currentTimeMillis()的区别
在Java中,new Date().getTime()和System.currentTimeMillis()都是用来获取当前时间的,并可以用DateFormat转成对应的时间格式,代码如下. impo ...
- iOS进阶之UDP代理鉴权过程
上一篇介绍的是TCP代理的鉴权过程,这篇将介绍UDP代理的大致鉴权过程. 在UDP鉴权过程中,有几点是需要注意的.首先,UDP是一种无连接协议,不需要连接,使用广播的方式:其次,为了通过鉴权,所以需要 ...
- flutter 读取sdcard权限问题相关
https://stackoverflow.com/questions/46698751/permission-denied-at-externalstoragedirectory-access-vi ...
- 关于Java多线程的一些常考知识点
前言 Java多线程也是面试中经常会提起到的一个点.面试官会问:实现多线程的两种方式以及区别,死锁发生的4个条件以及如何避免发生死锁,死锁和活锁的区别,常见的线程池以及区别,怎么理解有界队列与无界队列 ...
- mysql批量导出单结构与结构数据表脚本
由于一个库里面不需要导出全部, 只需要导出一部分指定的数据表结构与数据 那么就写了一个比较简单而且为了能偷懒的小shell #!/bin/bash #************************* ...
- java开发工具STS的下载及安装
将下载后的压缩文件解压,在解压后的sts-bundle下的sts-3.9.1RELEASE目录中STS.exe便是可执行程序,用于启动STS,将该文件发送到桌面快捷方式,当我们想使用STS时可以快速的 ...