kafka+windows+java+springboot中的配置
1.百度kafka+zookeeper+windows配置
1.1 zookeeper配置
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=
# disable the per-ip limit on the number of connections since this is a non-production config
maxClientCnxns=
1.2 kafka server.properties配置
advertised.host.name=IP log.dirs=D:/kafka_2.-1.0./log zookeeper.connect=IP:
1.3 windows hosts配置
IP localhost
2.maven构建springboot项目
2.1 intellij idea 新建kafka项目
2.2 kafka配置pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tangxin.kafka</groupId>
<artifactId>kafka</artifactId>
<version>1.0</version>
<name>kafka</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.29</version>
</dependency> <dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.11</artifactId>
<version>0.10.0.1</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>1.2.0.RELEASE</version>
</dependency> </dependencies> <build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<!--<excludes>-->
<!--<exclude>*</exclude>-->
<!--</excludes>-->
</resource>
</resources>
<plugins> <plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding> <compilerArguments>
<extdirs>lib</extdirs>
</compilerArguments>
</configuration>
<version>2.3.2</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
</configuration>
<version>2.4.3</version>
</plugin>
</plugins>
</build>
</project>
2.3 新建springboot启动类Application
package com.tangxin.kafka; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }
2.4 新建springboot项目中resources目录的配置文件
application.yml
server:
display-name: kafka
port: 8888
contextPath: /kafka spring:
profiles:
active: dev
application-dev.properties
kafka.bootstrap-servers=x.x.x.x:9092
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" dest="/data/logs/work/log.log">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout>
<charset>UTF-8</charset>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
</PatternLayout>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>
<RollingFile name="RollingFile" fileName="/data/logs/work/work.log"
filePattern="/data/logs/work/work-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<charset>UTF-8</charset>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="1000 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile> <RollingFile name="ErrorFile" fileName="/data/logs/work/error.log" filePattern="/data/logs/work/error.%d{yyyy-MM-dd}.%i.log">
<PatternLayout>
<charset>UTF-8</charset>
<Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%M%n</Pattern>
</PatternLayout>
<Filters>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<DefaultRolloverStrategy fileIndex="min" max="100"/>
</RollingFile>
</appenders>
<loggers>
<Root level="info">
<appender-ref ref="Console"/>
<appender-ref ref="RollingFile" level="info"/>
</Root>
<Logger name="com.tangxin.kafka">
<appender-ref ref="ErrorFile" />
</Logger>
</loggers>
</Configuration>
2.5 kafka配置类
package com.tangxin.kafka.service; import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory; import java.util.HashMap;
import java.util.Map; @Configuration
@EnableKafka
public class KafkaProducerConfig { @Value("${kafka.bootstrap-servers}")
private String bootstrapServers; public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return props;
} public ProducerFactory<String, String> producerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfigs());
} @Bean
public KafkaTemplate<String, String> kafkaTemplate() {
return new KafkaTemplate(producerFactory());
}
}
2.6 controller层调用kafka发送
package com.tangxin.kafka.web; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; @RestController
public class KafkaController { @Autowired
private KafkaTemplate kafkaTemplate; @RequestMapping(value = "/send", method = { RequestMethod.GET, RequestMethod.POST })
public String callFeedInfo() throws Exception {
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(() -> {
try {
kafkaTemplate.send("feed-info","1000");
} catch (Exception e) {
e.printStackTrace();
}
});
return "send done!";
} }
3.windows启动zookeeper和kafka
4.遇到的问题
2017-11-27 17:55:38.484 [kafka-producer-network-thread | producer-1] ERROR org.springframework.kafka.support.LoggingProducerListener - Exception thrown when sending a message with key='null' and payload='1' to topic mytopic:
org.apache.kafka.common.errors.TimeoutException: Batch containing 1 record(s) expired due to timeout while requesting metadata from brokers for mytopic-1
之所以写这个随笔就是因为这个问题,本地访问没有问题因为本机localhost和ip映射估计没问题,如果你两台电脑,一台想做server,一台想做开发就可能会遇到这样的问题,开始可能你各种官方各种百度可能都无法解决这个问题,这个问题涉及hostname主机名,说实话网络这块确实不熟悉,之前弄hadoop和spark入门时也遇到类似问题纠结很久。总结下可能存在的。
1. 防火墙是否关闭
2.windows下是否安装了vmware软件,控制面板\网络和 Internet\网络连接 禁用vmware network adapter
3.kafka配置
advertised.host.name=IP log.dirs=D:/kafka_2.11-1.0.0/log zookeeper.connect=IP:2181 windows hosts配置
IP localhost
kafka+windows+java+springboot中的配置的更多相关文章
- (二)Redis在Mac下的安装与SpringBoot中的配置
1 下载Redis 官网下载,下载 stable 版本,稳定版本. 2 本地安装 解压:tar zxvf redis-6.0.1.tar.gz 移动到: sudo mv redis-6.0.1 /us ...
- SpringBoot中yaml配置对象
转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...
- springboot中xml配置之@ImportResource
springboot中进行相关的配置往往有java配置和xml配置两种方式. 使用java的方式配置只需要使用@configuration注解即可,而使用xml的方式配置的话需要使用@ImportRe ...
- Springboot中以配置类方式自定义Mybatis的配置规则(如开启驼峰映射等)
什么是自定义Mybatis的配置规则? 答:即原来在mybatis配置文件中中我们配置到<settings>标签中的内容,如下第6-10行内容: 1 <?xml version=&q ...
- Java进阶(十)java tomcat中context配置
Tomcat中Context的配置 问题: java tomcat中<context> docBase属性是什么意思? 元素的属性: path:指定访问该Web应用的URL入口. doc ...
- springboot 中yml配置
springboot 中各种配置项纪录 1. @Value 最早获取配置文件中的配置的时候,使用的就是这个注解,SpEL表达式语言. // 使用起来很简单 @Value("${config. ...
- 案例源码解读及思路:RabbitMQ在springboot中的配置
程序员的高级之处不是什么都会,而是对自己不会的进行抽象,然后完成自己的工作.比如对于RabbitMQ,按照字面理解,就将其看成Message Queue,也就是用来容纳对象的集合.很多功能都拆分给一个 ...
- Java SpringBoot中使用sqljdbc4注意事项 java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
因项目需要,需要在Java项目中访问 MSSQLServer 数据库,本地开发的时候,没有问题,可以正常链接数据库,通过Jenkins部署到服务器上时候,报数据库驱动未找到. java.lang.Cl ...
- SpringBoot中日志配置
背景 由于一些框架中还使用log4j-1.x系列陈旧的日志框架,调试过程中有一些错误信息不能在控制台显示,增加了调试成本.以下配置方法 将帮助你获得log4j-1.x日志在控制台显示. 解决方法: 使 ...
随机推荐
- SpringBoot学习10:springboot整合mybatis
需求:通过使用 SpringBoot+SpringMVC+MyBatis 整合实现一个对数据库中的 t_user 表的 CRUD 的操作 1.创建maven项目,添加项目所需依赖 <!--spr ...
- C编程经验总结4
{}体里的语句不管在一行还是在多行,之间都是要有: for与for之间可以是独立的,也可以是相互嵌套的 For( ; i<5; )=for( ;i<=4; ) 一般都是在循环里面进行判断 ...
- 【iOS】史上最全的iOS持续集成教程 (下)
:first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...
- 百度站长针对SEO人员关系的问题的一些解答
自然排名是全部由机器完成还是存在人工干预? 夫唯:第一个就是说经常好不容易找到了一些新的想法,用我们这些草根的话讲找到了百度的漏洞,好不容易排名上去了,过两天就会波动.有些人就怀疑说在百度的整体算法里 ...
- asp.net高并发网站解决方案【未完成版本】
场景:假设现在是一个电商网站,今天要举办活动,有10个商品低价销售,但是会来抢购的人会特别多,最后只有十个人可以成功的买到商品 明确2个问题 1.访问量:抢票时间断用户访问量 2.并发:1秒内请求 ...
- 【PHP】Maximum execution time of 30 seconds exceeded解决办法
Maximum execution time of 30 seconds exceeded,今天把这个错误的解决方案总结一下: 简单总结一下解决办法: 报错一:内存超限,具体报错语句忘了,简单说一下解 ...
- 阿里云Linux服务器,挂载硬盘并将系统盘数据迁移到数据盘
因为之前用宝塔上线,宝塔只挂载了系统盘50G,打开阿里云云盘列表发现系统盘无法直接升级,故另买一块数据盘挂载到Linux服务器下,下面根据网上教程再结合我实际情况讲解一下实际操作,其实非常easy l ...
- Hive的数据库和表
本文介绍一下Hive中的数据库(Database/Schema)和表(Table)的基础知识,由于篇幅原因,这里只是一些常用的.基础的. Hive的数据库和表 先看一张草图: Hive结构 从图上可以 ...
- python flask豆瓣微信小程序案例
项目步骤 定义首页模板index.html <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- Linux平台下安装MySQL
1.下载RPM包 http://dev.mysql.com/downloads/mysql/5.5.html#downloads 选择[Red Hat & Oracle Enterprise ...