一.软件版本

  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的更多相关文章

  1. Spring Boot2.0 整合 Kafka

    Kafka 概述 Apache Kafka 是一个分布式流处理平台,用于构建实时的数据管道和流式的应用.它可以让你发布和订阅流式的记录,可以储存流式的记录,并且有较好的容错性,可以在流式记录产生时就进 ...

  2. Spring Boot2.0 设置拦截器

    所有功能完成 配置登录认证 配置拦截器 在spring boot2.0 之后 通过继承这个WebMvcConfigurer类 就可以完成拦截 新建包com.example.interceptor; 创 ...

  3. Spring Boot2.0 静态资源被拦截问题

    在Spring Boot2.0+的版本中,只要用户自定义了拦截器,则静态资源会被拦截.但是在spring1.0+的版本中,是不会拦截静态资源的. 因此,在使用Spring Boot2.0+时,配置拦截 ...

  4. Spring Boot2.0使用Spring Security

     一.Spring Secutity简介     Spring 是一个非常流行和成功的 Java 应用开发框架.Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性 ...

  5. spring boot2.0(一 ) 基础环境搭建

    1.基础配置 开发环境:window jdk版本:1.8(spring boot2.0最低要求1.8) 开发工具:eclipse 构建方式:maven3 2.POM配置文件 <project x ...

  6. Spring Boot2.0自定义配置文件使用

    声明: spring boot 1.5 以后,ConfigurationProperties取消locations属性,因此采用PropertySource注解配合使用 根据Spring Boot2. ...

  7. Spring boot2.0 设置文件上传大小限制

    今天把Spring boot版本升级到了2.0后,发现原来的文件上传大小限制设置不起作用了,原来的application.properties设置如下: spring.http.multipart.m ...

  8. spring boot 2.0(一)权威发布spring boot2.0

    Spring Boot2.0.0.RELEASE正式发布,在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误,然后Spring ...

  9. 【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- ...

随机推荐

  1. Selenium Edge Driver Python 自动化测试

    Finally I got it. 没有自动化过edge,今天被小坑了一下,但是还好现在弄好了. 1. 查看edge 版本,我的是14.14393 2. https://developer.micro ...

  2. Linux 命令整理-ps

    ps 命令 ps -ef | grep tomcat ps -ef :以长格式(全格式)显示所有进程:“|” :是管道grep :检索tomcat :与字符tomcat有关的进程 ps[选项]-e:显 ...

  3. nodejs:导出Excel和解析导入的Excel

    用的是koa2框架,但好好处理一下,用express框架也是可以的.导出的Excel是xlsx的格式,解析导入Excel的有xlsx和csv格式.通常导入Excel是要上传的,然后获取文件的路径,这里 ...

  4. Requests卡死问题

    https://www.cnblogs.com/niansi/p/7143736.html https://blog.csdn.net/pilipala6868/article/details/807 ...

  5. python zip()函数

    描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符 ...

  6. redis知识点汇总

    1. redis是什么 2. 为什么用redis 3. redis 数据结构 4. redis中的对象类型 5. redis都能做什么?怎么实现的的? 6. redis使用过程中需要注意什么 7. 数 ...

  7. mysql_study_3

    代码: CREATE DATABASE test_01; CREATE DATABASE mysql_shiyan; use mysql_shiyan; CREATE TABLE department ...

  8. 文件下载的ie11兼容性优化

    在 http://www.cnblogs.com/sunshine6/p/8296945.html 中有说关于前后端分离时如何实现文件下载的功能,但是过完年回来,同事告诉我这个方式在ie11上存在不兼 ...

  9. 安卓recyclerView 分割线的那些事

    在这里我想记录下recyclerView 分割线遇到的一些问题,主要提供一些个人思路,代码可能不多~ 1.宽度问题 描述:我现在需要做一张卡片,卡片里面是一条条联系人,而且我们卡片外层是有阴影的,我的 ...

  10. MySql-2019-4-21-复习

    数据库对象:存储,管理和使用数据的不同结构形式,如:表.视图.存储过程.函数.触发器.事件.索引等. 数据库:存储数据库对象的容器. 数据库分两种: 系统数据库(系统自带的数据库):不能修改 info ...