一.软件版本

  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. 微信小程序之图片base64解码

    不知道大家在做微信小程序的时候遇到base64解码的问题,我之前在做微信小程序的时候遇到base64解析图片一直有问题,所以在这里把遇到的问题和解决方案在这里记录一下: 在平时的项目中我们是直接用ba ...

  2. 006-CSS引入外部字体

    我们制作页面css,设置字体,可能会需要加入一些比较特殊的字体,这时候就需要引入这些字体. 方法如下: @font-face { font-family: KuTang;/*定义字体的名字*/ src ...

  3. 解决:启用多线程调用webBrowsers函数报错:指定的转换无效

    这里就需要委托. 定义一个 委托.加载之后给他绑定一个方法Callback,也就是所说的回掉函数. 然后写一个线程,线程需要一个object 的参数.将你定义的委托当作参数传进线程中的方法. 在线程中 ...

  4. Python记录wsgi

    类实现wsgi app from wsgiref.util import setup_testing_defaults from wsgiref.simple_server import make_s ...

  5. Object类的wait方法带参数和notifyAll方法

    相当于sleep( 5000 ) , 效果一样:

  6. VMware centos7 如何配置静态ip并且可上网

    注:此贴使用NAT方式配置上网模式. 1.设置虚拟机的网络适配器为NAT模式: 2.修改VMware的“虚拟网络编辑器”: 打开后选择NAT模式,勾选,然后点击NAT设置: NAT默认设置了188网段 ...

  7. linux常用英文单词记录

    1.skip 跳过忽略 2.next 下一步3.hostname 主机名4.password 密码5.complete 完成6.network 网络7.conf config configuratio ...

  8. 《Java程序设计》课程实验要求

    目录 <Java程序设计>课程实验要求 注册实验楼账号 实验一 Java开发环境的熟悉 实验二<Java面向对象程序设计> 实验三 <敏捷开发与XP实践> 实验四 ...

  9. Spring HATEOAS的简单认识

    HATEOAS: 超媒体作为应用程序状态引擎(HATEOAS)是REST应用程序体系结构的一个组件,它将其与其他网络应用程序体系结构区分开来. 使用HATEOAS,客户端与网络应用程序交互,其应用程序 ...

  10. VS2110。VC++编译错误"error LNK2005: 已经在 XXX.obj 中定义的问题"

    有时候我们会在头文件当中定义一些全局变量或者全局函数,这种做法会比较方便,但有时候会出现“编译错误"error LNK2005: 已经在 XXX.obj 中定义的问题"的链接问题. ...