java调用kafka

pom.xml
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>2.3.1</version>
        </dependency>

注意调试通过后要添加log-back.xml并设置日志级别到info及以上,否则debug日志实在是多啊……

///ProducerTest.java

package com.whq.demo;
import java.util.Properties;
import java.util.Random;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
public class ProducerTest {public static String topic = "whq_test";//定义主题public static void main(String[] args) throws InterruptedException {Properties p = new Properties();p.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.81.145:9092,192.168.81.145:9093,192.168.81.145:9094");//kafka地址,多个地址用逗号分割p.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);p.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(p);try {while (true) {String msg = "Hello,你好," + new Random().nextInt(100);ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, msg);kafkaProducer.send(record);System.out.println("消息发送成功:" + msg);Thread.sleep(500);}} finally {kafkaProducer.close();}}
}


///ConsumerTest.java

package com.whq.demo;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;import java.util.Collections;
import java.util.Properties;public class ConsumerTest {public static void main(String[] args) {Properties p = new Properties();p.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.81.145:9092,192.168.81.145:9093,192.168.81.145:9094");p.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);p.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);p.put(ConsumerConfig.GROUP_ID_CONFIG, "3");//组编号,不同的组提交偏移不同p.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,true);//自动提交,提交后数据在同组无法再次获取,否则同组另外消费者接入还会收到同样的消息p.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");/*** AUTO_OFFSET_RESET_CONFIG 重置偏移设置* earliest* 当各分区下有已提交的offset时,从提交的offset开始消费;无提交的offset时,从头开始消费* latest* 当各分区下有已提交的offset时,从提交的offset开始消费;无提交的offset时,消费新产生的该分区下的数据* none* topic各分区都存在已提交的offset时,从offset后开始消费;只要有一个分区不存在已提交的offset,则抛出异常*/KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<String, String>(p);kafkaConsumer.subscribe(Collections.singletonList(ProducerTest.topic));// 订阅消息while (true) {ConsumerRecords<String, String> records = kafkaConsumer.poll(100);for (ConsumerRecord<String, String> record : records) {System.out.println(String.format("topic:%s,offset:%d,消息:%s", //record.topic(), record.offset(), record.value()));}}}
}

//出现问题:生产者第二次发消息时连接到了127.0.0.1:9093,后来发现是kafka服务器配置问题:
vi config/server.properties
修改

listeners=PLAINTEXT://192.168.81.145:9092    #注意,这里一定要有客户端可访问的ip,否则非本机连接调用第二次会异常

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/416797.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[Noi2015]软件包管理器

来自FallDream的博客&#xff0c;未经允许&#xff0c;请勿转载&#xff0c;谢谢。 Linux用户和OSX用户一定对软件包管理器不会陌生。通过软件包管理器&#xff0c;你可以通过一行命令安装某一个软件包&#xff0c;然后软件包管理器会帮助你从软件源下载软件包&#xff0c;同时…

ELK+Kafka部署

目录 1.背景 2.ELK的配置 2.1.下载 2.2.关闭防火墙 2.3.安装elasticsearch 2.4.安装Logstash 2.5.安装Kibana 2.6.Java日志输出到Logstash 2.7.OSS版本 3.Kafka的配置 3.1.zookeeper搭建 3.2.kafka搭建 4.整合 1.背景 高日志压力情况下&#xff0c;为了避免Logsta…

Allegro改动shape网络节点

使用Allegro时改动shape的网络节点方法&#xff1a; ①选择shape->Select Shape or Void/Cavity ②选择要改动的shape ③点击&#xff08;...&#xff09;改动网络节点的名字 ④改动完毕 转载于:https://www.cnblogs.com/claireyuancy/p/6804046.html

ELK套件FileBeat部署

目录 1.简介 2.下载 3.直接输出到ElasticSearch 4.输出到Logstash 5.更改nginx日志路径 6.logstash负载均衡 7.日志文件直接作为输入 1.简介 FileBeat用于文件数据采集并输出到ElasticSearch或Logstash中。 ELK搭建过程参见&#xff1a; ELK搭建及Java程序接入 2.下载…

python基础之数据类型

python基础之数据类型 字符串(string) 用引号括起的都是字符串,其中的引号可以是单引号, 也可以是双引号1.使用方法修改字符串的大小写 例&#xff1a; >>> name "ada lovelace" >>> print name.title() Ada Lovelace >>> print(name.up…

MetricBeat(win/linux)部署 系统CPU内存等资源情况监控

目录 1.下载 2.linux系统监控 ​3.启用模块 4.windows系统监控 1.下载 下载MetricBeat的linux和windows版本 https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-oss-7.2.1-linux-x86_64.tar.gz https://artifacts.elastic.co/downloads/beats/metricbea…

android CoordinatorLayout使用

一、CoordinatorLayout有什么作用 CoordinatorLayout作为“super-powered FrameLayout”基本实现两个功能&#xff1a; 1、作为顶层布局 2、调度协调子布局 CoordinatorLayout使用新的思路通过协调调度子布局的形式实现触摸影响布局的形式产生动画效果。CoordinatorLayout通过…

deepin15.11系统体验

目录 1.下载 2.安装 3.装好后的体验 4.命令情况 5.windows共享 6.总结 听说华为笔记本都预装deepin&#xff0c;下载下来体验下 vmware创建linux系统&#xff0c;版本选择Other Linux 4.x or later kernel 64-bit 1.下载 官网 https://www.deepin.org/ 从官网下载iso真心…

带视觉差的轮播图

最终结果&#xff1a; 代码&#xff1a; <!DOCTYPE html> <html> <head lang"en"><meta charset"UTF-8"><title></title><style>html {box-sizing: border-box;font-family: Open Sans, sans-serif;}*, *:befor…

深度终端:ubuntu等linux下好用的远程终端软件

终端好不好用&#xff0c;直接上图&#xff0c;有图有真相—— 这终端不错啊&#xff0c;可以添加远程链接信息&#xff0c;像xshell似的&#xff0c;比ubuntu那些的终端强多了&#xff0c;每次都得一步步连。 怎么装&#xff1f; sudo apt install -y deepin-terminal PS&…

大数据产品的备份及恢复

Hbase Distcp方式整体下载上传方式CopyTable备份Export工具elasticsearch 建立备份快照数据挂载点建立快照仓储repository建立snapshot快照备份恢复snapshot快照数据 原集群恢复新集群恢复HDFSHbase的备份恢复 hbase数据备份策略有两类&#xff1a; 离线备份&#xff08;关闭Hb…

centos7 greenplum6.1开源版本编译

greenplum开源版本 https://greenplum.org/ 其官方手册 https://greenplum.org/documentation/ 其下载介质地址 https://github.com/greenplum-db/gpdb/releases 本次下载src-full https://github.com/greenplum-db/gpdb/releases/download/6.1.0/6.1.0-src-full.zip 编译参…

Centos7 Greenplum6.1开源版本集群部署

目录 1.前言 1.1参照文档 1.2部署包 1.3服务器环境 2 准备工作 2.1 Linux用户 2.2 主机名和hosts配置 2.3 防火墙 2.4 系统资源配置 2.5 暂时启用gpadmin sudo 2.6 复制配置文件到所有节点上 3 安装Greenplum DB 3.1 在Master节点上安装Greenplum DB 3.2 在Master…

转 C#对多个集合和数组的操作(合并,去重,判断)

在开发过程中.数组和集合的处理是最让我们担心.一般会用for or foreach 来处理一些操作.这里介绍一些常用的集合跟数组的操作函数. 首先举例2个集合A,B. List<int> listA new List<int> {1,2,3,5,7,9}; List<int> listB new List<int> {13,4,17,29…

centos7 postgresql9和postgis2.1插件编译部署

目录 依赖安装 下载编译libgeos 下载编译proj4 编译Postgresql9 编译PostGIS2 启动postgresql服务 开通外部网络访问 数据库开启PostGIS扩展 查看PostGIS版本 升级PostGIS版本 依赖安装 这个命令里面安装的包可能会多&#xff0c;由于是编译GreenPlum用的&#xff0…

三国人物共现网络

三国部分人物共现图 转载于:https://www.cnblogs.com/jzssuanfa/p/6814865.html

Spark单独集群模式部署

目录 网络配置 SSH 免密码登录 部署 执行测试 网络配置 192.168.81.157 node1 master 192.168.81.158 node2 slave1 192.168.81.159 node3 slave2 相同的配置先在一个节点上配置&#xff0c;配置完成后复制到其它节点上。 vi /etc/hosts 192.168.81.157 node1 192.168.…

flutter网络权限申请

在此文件&#xff08;android/src/main/AndroidManifest.xml&#xff09;中的manifest节点下添加如下代码&#xff1a; 注意&#xff0c;不是profile文件夹下的。 <uses-permission android:name"android.permission.READ_PHONE_STATE" /> <uses-permissio…