java代码消费kafka数据,并打成jar包,上传至服务器运行 —>>>
消费者代码:
package kafka.test;
import java.util.Collections;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.log4j.Logger;
public class KafkaTest {
public static Logger logger = Logger.getLogger(KafkaTest.class);
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("bootstrap.servers", "****:9092");//xxx是服务器集群的ip
properties.put("group.id", "***-gq-test-redlist");
// properties.put("group.id", "");
properties.put("enable.auto.commit", "true");
properties.put("auto.commit.interval.ms", "1000");
properties.put("auto.offset.reset", "latest");
properties.put("session.timeout.ms", "30000");
properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
properties
.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
//创建一个消费者客户端实例
KafkaConsumer<String,String> consumer=new KafkaConsumer<>(properties);
//订阅主题
consumer.subscribe(Collections.singletonList("topic"));
//循环消费消息
while (true){
try {
ConsumerRecords<String,String> records=consumer.poll(1000);
if (records.count()>0) {
for (ConsumerRecord<String,String> record:records){
// System.out.println("receiver a message from consumer client:"+record.value());
logger.info("jjjj");
}
}
} catch (Exception e) {
logger.error(e);
}
}
}
}
log4j.properties写法
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Set everything to be logged to the file target/unit-tests.log
### \u8BBE\u7F6E###
log4j.rootLogger=stdout,info,error,ERROR,E
### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayouts
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### \u8F93\u51FADEBUG \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/info.log ###
log4j.appender.info=org.apache.log4j.RollingFileAppender
log4j.appender.info.File=logs/info.log
log4j.appender.info.Append=true
log4j.appender.info.Threshold=INFO
log4j.appender.info.MaxFileSize=20480KB
log4j.appender.info.MaxBackupIndex=10
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### \u8F93\u51FADEBUG \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###
log4j.appender.debug=org.apache.log4j.RollingFileAppender
log4j.appender.debug.File=file:///opt/opsmgr/web/components/logs/debug.log
log4j.appender.debug.Append=true
log4j.appender.debug.Threshold=DEBUG
log4j.appender.debug.MaxFileSize=20480KB
log4j.appender.debug.MaxBackupIndex=10
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### \u8F93\u51FAERROR \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###
log4j.appender.error=org.apache.log4j.RollingFileAppender
log4j.appender.error.File=logs/error.log
log4j.appender.error.Append=true
log4j.appender.error.Threshold=ERROR
log4j.appender.error.MaxFileSize=20480KB
log4j.appender.error.MaxBackupIndex=10
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
引入的pom文件 — > > >
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.10.0.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>kafka.test.KafkaTest</mainClass> // 指定主类
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
生成jar
上传服务器执行
前台模式 java –jar XXXX.jar(注意这个命令启动时,断开服务连接后,服务就关闭了);
后台模式 nohup java -jar XXXX.jar >XX.log 2>&1&(xx.log代表日志输出文件)
要是不识别 java命令 前面跟上jdk路径 -->
/opt/opsmgr/web/components/jre18linux64.1/bin/java -jar kafka-1.0-SNAPSHOT-jar-with-dependencies.jar>gqtest.log 2>&1&
查询进程
[root@localhost gqtest]# ps aux|grep kafka-1.0-SNAPSHOT-jar-with-dependencies.jar
root 3125 2.7 0.4 36591388 607608 pts/2 Sl 19:46 0:07 /opt/opsmgr/web/components/jre18linux64.1/bin/java -jar kafka-1.0-SNAPSHOT-jar-with-dependencies.jar
root 11763 0.0 0.0 112844 1256 pts/2 S+ 19:51 0:00 grep --color=auto kafka-1.0-SNAPSHOT-jar-with-dependencies.jar
以上只是参考 自己留存~~~