kafka安装使用
版本:kafka_2.11-0.10.1.0 (之前安装2.10-0.10.0.0,一直出问题)
安装
- 下载并解压代码
wget http://mirrors.cnnic.cn/apache/kafka/0.10.0.0/kafka_2.10-0.10.0.0.tgz
#http://kafka.apache.org/downloads tar -zxvf kafka_2.10-0.10.0.0.tgzcd kafka_2.-0.10.0.0
- 修改每个broker安装目录下的配置文件
# $targetID默认是0,每个broker的broker.id必须要唯一
broker.id=$targetID #默认是注释的,$IP改成当前节点的IP即可。如果不改该配置项,在节点通过命令行可以收发消息,而在其他机器是无法通过IP去访问队列的
#在之前的版本不叫listeners,而是advertised.host.name和host.name
listeners=PLAINTEXT://$IP:9092 - 启动服务
#kafka依赖于zookeeper
#如果没有的话,可以通过kafka提供的脚本快速创建一个单节点zookeeper实例:
#bin/zookeeper-server-start.sh config/zookeeper.properties #确认zookeeper服务已经启动后,启动kafka服务
nohup bin/kafka-server-start.sh config/server.properties & - 创建一个名为test,有一份备份,一个分区的topic
bin/kafka-topics.sh --create --zookeeper localhost: --replication-factor --partitions --topic test
#查看所有topic bin/kafka-topics.sh --list --zookeeper localhost: - 发送消息
bin/kafka-console-producer.sh --broker-list localhost: --topic test
- 开启一个消费者接收消息
bin/kafka-console-consumer.sh --bootstrap-server localhost: --topic test --from-beginning
- 查看topic信息
bin/kafka-topics.sh --describe --zookeeper localhost: --topic test
Springboot结合Kafka的使用
1.在pom文件添加依赖
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
2.在application.properties中添加配置
# APACHE KAFKA (KafkaProperties)
spring.kafka.bootstrap-servers=192.168.0.155:,192.168.0.156:
spring.kafka.client-id=K1
spring.kafka.consumer.auto-offset-reset= earliest
spring.kafka.consumer.enable-auto-commit= true
spring.kafka.consumer.group-id= test-consumer-group
spring.kafka.producer.batch-size=
spring.kafka.producer.bootstrap-servers= 192.168.0.155:,192.168.0.156:
spring.kafka.producer.client-id= P1
spring.kafka.producer.retries=
spring.kafka.template.default-topic= test
3.创建消费者类(订阅消息的对象)
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component; @Component
public class ListenerBean { @KafkaListener(topics = "myTopic")
public void processMessage(String content) {
System.out.println("you have a new message:" + content);
// ...
}
}
4.创建生产者类(发布消息的对象)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @Component
@RestController
@RequestMapping("/send")
@EnableAutoConfiguration
public class SendMsgBean {
private final KafkaTemplate<String,String> kafkaTemplate; @Autowired
public SendMsgBean(KafkaTemplate<String,String> kafkaTemplate) {
this.kafkaTemplate = kafkaTemplate;
} @RequestMapping(path="/{msg}",method=RequestMethod.GET)
public String send(@PathVariable("msg") String msg) {
System.out.println("==sending msg:" + msg);
kafkaTemplate.send("test","test-"+msg);
return "message has been sent!";
}
}
只需这4步,就可以在springboot中使用kafka了,现在我们访问 http://localhost:8080/send/mymessage 就可以在控制台看到信息了。
参考:
kafka安装使用的更多相关文章
- hadoop 之 kafka 安装与 flume -> kafka 整合
62-kafka 安装 : flume 整合 kafka 一.kafka 安装 1.下载 http://kafka.apache.org/downloads.html 2. 解压 tar -zxvf ...
- Kafka安装及部署
安装及部署 一.环境配置 操作系统:Cent OS 7 Kafka版本:0.9.0.0 Kafka官网下载:请点击 JDK版本:1.7.0_51 SSH Secure Shell版本:XShell 5 ...
- [Kafka] - Kafka 安装介绍
Kafka是由LinkedIn公司开发的,之后贡献给Apache基金会,成为Apache的一个顶级项目,开发语言为Scala.提供了各种不同语言的API,具体参考Kafka的cwiki页面: Kafk ...
- Kafka 安装配置 windows 下
Kafka 安装配置 windows 下 标签(空格分隔): Kafka Kafka 内核部分需要安装jdk, zookeeper. 安装JDK 安装JDK就不需要讲解了,安装完配置下JAVA_HOM ...
- kafka安装教程
今天需要在新机器上安装一个kafka集群,其实kafka我已经装了十个不止了,但是没有一个是为生产考虑的,因此比较汗颜,今天好好地把kafka的安装以及配置梳理一下: 1,kafka版本选取: 现在我 ...
- Kafka安装及开启SASL_PLAINTEXT认证(用户名和密码认证)
前些日子要封装一个kafka的客户端驱动,配置了下kafka环境,发现配置复杂度完爆rabbitmq很多倍啊,而且发布订阅模式使用起来也很麻烦,可能就胜在分布式了吧. kafka需要java环境,自行 ...
- Kafka安装与配置(windows)
作者:灬花儿灬 出处:http://www.cnblogs.com/flower1990/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...
- kafka安装步骤
kafka 安装内存会报不够 https://stackoverflow.com/questions/9350437/incompatible-initial-and-maximum-heap-siz ...
- kafka安装与简单使用
一.kafka安装 安装是非常简单的,现在推荐安装0.8的版本,这个版本是非常稳定的,而且公司里面也多用此版本. 简单的安装: 这个是我使用的版本,kafka_2.11-0.8.2.2.tgz 直接t ...
- kafka系列一、kafka安装及部署、集群搭建
一.环境准备 操作系统:Cent OS 7 Kafka版本:kafka_2.10 Kafka官网下载:请点击 JDK版本:1.8.0_171 zookeeper-3.4.10 二.kafka安装配置 ...
随机推荐
- SharePoint服务器端对象模型 之 访问网站和列表数据(Part 1)
本节将会介绍SharePoint中最为常用的一些对象模型,以及如何使用这些对象模型来访问和操作网站中的数据.几乎所有的SharePoint服务器端开发都会涉及到这些内容,因此应着重掌握本节中所介绍的基 ...
- jquery 操作动态添加的元素
动态添加的元素,无法侦听到事件,写法如下: 使用函数.on 格式为: $(父元素).on('event','selector',function(){ //do something }) 例如 < ...
- 【python】-- Ajax
Ajax AJAX,Asynchronous JavaScript and XML (异步的JavaScript和XML),一种创建交互式网页应用的网页开发技术方案. 异步的JavaScript:使用 ...
- ASP非法赋值
Microsoft VBScript 运行时错误 错误 '800a01f5' 非法赋值: 'isCloudSpeedupMz' /records/config/class-records.asp,行 ...
- 数据库之MySQL(四)
数据库中的范式: 第一范式(1NF): 数据表中的每一列(字段),必须是不可拆分的最小单元,也就是确保每一列的原子性. 例如: userInfo: '山东省烟台市 1318162008' 依照第一范式 ...
- Spring学习笔记3—声明式事务
1 理解事务 事务:在软件开发领域,全有或全无的操作被称为事务.事务允许我们将几个操作组合成一个要么全部发生要么全部不发生的工作单元. 事务的特性: 原子性:事务是由一个或多个活动所组成的一个工作单元 ...
- sqlserver整理的实用资料
1 --- 创建 备份数据的 device 2 3 USE DB_ZJ 4 EXEC sp_addumpdevice 'disk', 'testBack', 'c:\MyNwind_1.dat' 5 ...
- C#将图片白色背景设置为透明
Image image = System.Drawing.Image.FromFile(@"C:\A.JPG"); Bitmap pbitmap = new Bitmap(imag ...
- 搭建backup服务器基本流程
守护进程实现,将daemon配置在backup服务器,因为这样其他服务器就能通过服务推即可. 服务端配置流程: 前提两台服务41为backup服务 31是其他服务器即客户端 在41服务器中配置 ...
- 'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator.
let newStr = String(str[..<index]) // = str.substring(to: index) In Swift 3 let newStr = String(s ...