Flume + Kafka+ Log4j 采集实时日志信息到HDFS中 (日志监控系统搭建)

1.Hadoop集群搭建的配置

 主机(kafka  zookeeper  三台同时安装启动)

     master:  192.168.28.129

     slave0:    192.168.28.130

     slave1:    192.168.28.131

1.1搭建步骤如下链接:

https://blog.csdn.net/qq_31987649/article/details/85037833

1.2 启动hadoop

   只需要启动Master 主节点上的hadoop

   hadoop安装路径: / hadoop-2.6.5 /sbin

      ./start-all.sh  启动

      ./stop-all.sh 关闭    

   启动成功如下:
   Master:

slave0:

slave1:

2.下载并解压 flume

     apache-flume-1.7.0-bin.tar.gz

   2.1   解压目录入下 命令 tar -zxvf /app/soft/apache-flume-1.7.0-bin.tar.gz -C /app/install/
   

  

2.2修改配置文件 进入conf目录下

mv flume-env.sh.template   flume-env.sh 

vi  flume-env.sh

2.3 在conf目录下创建 hdfs-avro.conf 配置文件

   命令: vi  flume-kafka-hdfs.conf 增加内容:

    

a1.sources = r1
#数据沉淀,可以多个,中间用空格分隔
a1.sinks = k1
#传输管道,一般只有一个,可以多个
a1.channels = c1


#配置source 消息生产者
#exec可以通过指定的操作对日志进行读取,使用exec时需要指定shell命令,对日志进行读取
#a1.sources.r1.type = exec
#a1.sources.r1.command = tail -F /app/logs/webProvider.log
a1.sources.r1.channels = memoryChannel
a1.sources.r1.type=org.apache.flume.source.kafka.KafkaSource
a1.sources.r1.zookeeperConnect=192.168.28.130:2181
a1.sources.r1.topic=log4j-topic
#a1.sources.r1.groupId=flume
a1.sources.r1.kafka.consumer.timeout.ms=100



# 配置channel,将buffer事件放在内存中,相当于数据缓冲区,接收source数据发送给sink
#传输管道的参数,类型是内存传输
a1.channels.c1.type = memory
#存储在通道中的最大事件数。
a1.channels.c1.capacity = 1000
#收集端的sink会在收集到了1000条以后再去提交事务(即发送到下一个目的地)
a1.channels.c1.transactionCapacity = 100



# 配置sink 接收器
#沉淀的方式是控制台打印,还可以有其他方式,如上传到hdfs。
a1.sinks.k1.type = hdfs
#设置hdfs接收器
a1.sinks.k1.hdfs.path = hdfs://192.168.28.129:9000/flume/testLog.log
#设置
a1.sinks.k1.hdfs.filePrefix = test
#a1.sinks.k1.hdfs.fileSuffix = .log
a1.sinks.k1.hdfs.fileType=DataStream


# 把source和sink绑定在channel上
#指定传输管道,
a1.sources.r1.channels = c1
#设置数据沉淀接收的管道 
a1.sinks.k1.channel = c1

2.下载并解压 Kafka

(kafka 三台同时安装启动)

    kafka_2.11-2.0.0.tgz

    解压目录

    

   3.下载并解压 zookeeper

          zookeeper-3.4.11.tar.gz

       (zookeeper  三台同时安装启动)

          解压目录

         

4.集群搭建

  4.1zookeeper集群搭建

  进入zookeeper conf目录(三台服务器都要修改)

复制zoo_sample.cfg 重命名为 zoo.conf 修改如下:

分别在三台服务器上新建 myid

cd /app/data/zookeeper/myid

master:

slave0:

slave1:

4.2 kafka集群搭建(三台服务器都要修改)

进入kafka config 目录

修改server.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.

# see kafka.server.KafkaConfig for additional details and defaults

############################# Server Basics #############################

# The id of the broker. This must be set to a unique integer for each broker.
broker.id=0

############################# Socket Server Settings #############################

# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092
listeners=PLAINTEXT://192.168.28.129:9092
# Hostname and port the broker will advertise to producers and consumers. If not set, 
# it uses the value for "listeners" if configured.  Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092
advertised.listeners=PLAINTEXT://192.168.28.129:9092

# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
#listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL

# The number of threads that the server uses for receiving requests from the network and sending responses to the network
num.network.threads=3

# The number of threads that the server uses for processing requests, which may include disk I/O
num.io.threads=8

# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=102400

# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=102400

# The maximum size of a request that the socket server will accept (protection against OOM)
socket.request.max.bytes=104857600


############################# Log Basics #############################

# A comma separated list of directories under which to store log files
log.dirs=/app/data/kafka/kafka-logs

# The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
num.partitions=1

# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
# This value is recommended to be increased for installations with data dirs located in RAID array.
num.recovery.threads.per.data.dir=1

############################# Internal Topic Settings  #############################
# The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
# For anything other than development testing, a value greater than 1 is recommended for to ensure availability such as 3.
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1

############################# Log Flush Policy #############################

# Messages are immediately written to the filesystem but by default we only fsync() to sync
# the OS cache lazily. The following configurations control the flush of data to disk.
# There are a few important trade-offs here:
#    1. Durability: Unflushed data may be lost if you are not using replication.
#    2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
#    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks.
# The settings below allow one to configure the flush policy to flush data after a period of time or
# every N messages (or both). This can be done globally and overridden on a per-topic basis.

# The number of messages to accept before forcing a flush of data to disk
#log.flush.interval.messages=10000

# The maximum amount of time a message can sit in a log before we force a flush
#log.flush.interval.ms=1000

############################# Log Retention Policy #############################

# The following configurations control the disposal of log segments. The policy can
# be set to delete segments after a period of time, or after a given size has accumulated.
# A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
# from the end of the log.

# The minimum age of a log file to be eligible for deletion due to age
log.retention.hours=168

# A size-based retention policy for logs. Segments are pruned from the log unless the remaining
# segments drop below log.retention.bytes. Functions independently of log.retention.hours.
#log.retention.bytes=1073741824

# The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=1073741824

# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=300000

############################# Zookeeper #############################

# Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the
# root directory for all kafka znodes.
#zookeeper.connect=192.168.28.129:2181,192.168.28.130:2181,192.168.28.131:2181
 zookeeper.connect=master:2181,slave0:2181,slave1:2181
# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=6000


############################# Group Coordinator Settings #############################

# The following configuration specifies the time, in milliseconds, that the GroupCoordinator will delay the initial consumer rebalance.
# The rebalance will be further delayed by the value of group.initial.rebalance.delay.ms as new members join the group, up to a maximum of max.poll.interval.ms.
# The default value for this is 3 seconds.
# We override this to 0 here as it makes for a better out-of-the-box experience for development and testing.
# However, in production environments the default value of 3 seconds is more suitable as this will help to avoid unnecessary, and potentially expensive, rebalances during application startup.
group.initial.rebalance.delay.ms=0

5.项目构建

 

5.1 pom依赖

 <!-- 单元测试 -->
	<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    
     <!-- 导入servlet支持包 -->
    <dependency>
    <groupId>javax.servlet</groupId>
	    <artifactId>javax.servlet-api</artifactId>
	    <version>4.0.0-b01</version>
	</dependency>
    
	
	<!-- 导入日志支持包 -->
<!-- 	<dependency>
	    <groupId>log4j</groupId>
	    <artifactId>log4j</artifactId>
	    <version>1.2.17</version>
	</dependency> -->
	
	<!-- 日志系统支持(slf4j-log4j12:链接slf4j-api和log4j中间的适配器。它实现了slf4j-apiz中StaticLoggerBinder接口,
			从而使得在编译时绑定的是slf4j-log4j12的getSingleton()方法) -->
	<!-- <dependency>
	    <groupId>org.slf4j</groupId>
	    <artifactId>slf4j-log4j12</artifactId>
	    <version>1.7.21</version>
	</dependency> -->
	
  
  	<!-- spring4相关jar包导入 -->
	
	<!-- 提供在基础IOC(控制反转)功能上的扩展服务,此外还提供许多企业级服务的支持,有邮件服务、
			任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的支持。 -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>4.3.2.RELEASE</version>
	</dependency>
	
	<!-- spring核心工具包,其它包依赖此包 -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-core</artifactId>
	    <version>4.3.2.RELEASE</version>
	</dependency>
	
	<!-- spring对jdbc的简单封闭包 -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-jdbc</artifactId>
	    <version>4.3.2.RELEASE</version>
	</dependency>
	
	<!-- spring用于支持web开发时所需的Spring框架的核心类,包括自动载入WebApplicationContext特性的类、
		 Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-web</artifactId>
	    <version>4.3.2.RELEASE</version>
	</dependency>
	
	<!-- 用于对spring AOP(面向切面)的支持jar包 -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-aop</artifactId>
	    <version>4.3.2.RELEASE</version>
	</dependency>
	
	<!-- 扩展包:spring context 的扩展支持,用于MVC方面-->
	<dependency>
    <groupId>org.springframework</groupId>
	    <artifactId>spring-context-support</artifactId>
	    <version>4.3.2.RELEASE</version>
	</dependency>
	
	<!-- spring bean管理包(包括配置文件的访问和创建bean管理bean)是Spring IOC的基础实现-->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-beans</artifactId>
	    <version>4.3.2.RELEASE</version>
	</dependency>
	
	<!-- spring提供对事物的支持,事务的相关处理以及实现类就在这个jar包中 -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-tx</artifactId>
	    <version>4.3.2.RELEASE</version>
	</dependency>
	
	<!-- 提供对AspectJ的支持,以便可以方便的将面向方面的功能集成进IDE中,比如Eclipse AJDT。
		 外部依赖。 -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-aspects</artifactId>
	    <version>4.3.2.RELEASE</version>
	</dependency>
	
	<!-- springMvc4对json的支持 -->
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-annotations</artifactId>
	    <version>2.5.0</version>
	</dependency>
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-core</artifactId>
	    <version>2.5.0</version>
	</dependency>
	<dependency>
	    <groupId>com.fasterxml.jackson.core</groupId>
	    <artifactId>jackson-databind</artifactId>
	    <version>2.5.0</version>
	</dependency>
	
	<!-- 这个jar 文件包含Spring MVC 框架相关的所有类。
			包括框架的Servlets,Web MVC框架,控制器和视图支持。当然,如果你的应用使用了独立的MVC 框架,则无需这个JAR 文件里的任何类。
			外部依赖spring-web, (spring-support,Tiles,iText,POI)。 -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>4.3.2.RELEASE</version>
	</dependency> 
	
	<!--Spring数据库访问中另外一大模块就是ORM,ORM即对象/关系映射。
		Spring支持大多数ORM框架,比如Hibernate,JPA,JDO,TopLink和iBatis
		(Spring2支持iBatis2,现MyBatis3的Spring支持由MyBatis社区开发,并非Spring)。  -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-orm</artifactId>
	    <version>4.0.2.RELEASE</version>
	</dependency>

	<dependency>
		<groupId>org.springframework.security</groupId>
		<artifactId>spring-security-config</artifactId>
		<version>4.0.2.RELEASE</version>
	</dependency>
	
	<dependency>
		<groupId>org.springframework.security</groupId>
		<artifactId>spring-security-web</artifactId>
		<version>4.0.2.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.security</groupId>
		<artifactId>spring-security-core</artifactId>
		<version>4.0.2.RELEASE</version>
	</dependency>
 	<dependency>
  		<groupId>org.springframework</groupId> 
  		<artifactId>spring-test</artifactId> 
  		<version>4.1.6.RELEASE</version> 
  		<scope>test</scope> 
  	</dependency>
  	<dependency>
		<groupId>com.sun.mail</groupId>
		<artifactId>javax.mail</artifactId>
		<version>1.5.2</version>
	</dependency>
  	<dependency>
		<groupId>org.aspectj</groupId>
		<artifactId>aspectjweaver</artifactId>
		<version>1.8.7</version>
	</dependency>
  	<dependency>
		<groupId>org.apache.tomcat</groupId>
		<artifactId>tomcat-servlet-api</artifactId>
		<version>8.0.26</version>
		<scope>provided</scope>
	</dependency>	
	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-rt-frontend-jaxrs</artifactId>
		<version>3.1.2</version>
	</dependency>
	<dependency>
		<groupId>org.codehaus.jackson</groupId>
		<artifactId>jackson-jaxrs</artifactId>
		<version>1.9.2</version>
	</dependency>
	<dependency>
		<groupId>commons-net</groupId>
		<artifactId>commons-net</artifactId>
		<version>1.4.1</version>
	</dependency>
	
	<dependency>
	    <groupId>org.apache.commons</groupId>
	    <artifactId>commons-lang3</artifactId>
	    <version>3.0</version>
	</dependency>
	
	 <!-- 导入servlet支持包 -->
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>javax.servlet-api</artifactId>
		<version>3.0.1</version>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>javax.servlet.jsp</groupId>
		<artifactId>jsp-api</artifactId>
		<version>2.1</version>
		<scope>provided</scope>
	</dependency>

    
    <!-- 导入jstl支持包 -->
    <dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>jstl</artifactId>
	    <version>1.2</version>
	</dependency>

    <!-- kafka客户端支持包 -->
   <dependency>
	    <groupId>org.apache.kafka</groupId>
	    <artifactId>kafka-clients</artifactId>
	    <version>0.11.0.1</version>
	</dependency>
	
	<!-- spring支持kafka -->
	<dependency>
	    <groupId>org.springframework.kafka</groupId>
	    <artifactId>spring-kafka</artifactId>
	    <version>2.0.4.RELEASE</version>
	</dependency>
	<!-- kafka 集成 log4j 日志包 -->
	<dependency>
	    <groupId>org.apache.kafka</groupId>
	    <artifactId>kafka-log4j-appender</artifactId>
	    <version>1.1.0</version>
	</dependency>
	
	<!--spring 集成 redis -->
	<!-- <dependency>
	    <groupId>org.springframework.data</groupId>
	    <artifactId>spring-data-redis</artifactId>
	    <version>2.0.7.RELEASE</version>
	</dependency> -->
	
<!-- 		<dependency>
		    <groupId>org.springframework.data</groupId>
		    <artifactId>spring-data-redis</artifactId>
		    <version>1.6.0.RELEASE</version>
		</dependency> -->
	
	<!-- jedis 操作 redis 客户端 -->
	<!-- <dependency>
	    <groupId>redis.clients</groupId>
	    <artifactId>jedis</artifactId>
	    <version>2.9.1</version>
	</dependency>
	 -->
	 
<!-- 	  <dependency>  
        <groupId>redis.clients</groupId>  
        <artifactId>jedis</artifactId>  
        <version>2.7.3</version>  
    </dependency>  
	<dependency>
	    <groupId>org.apache.commons</groupId>
	    <artifactId>commons-pool2</artifactId>
	    <version>2.0</version>
	</dependency>
	 -->
	
	
	<!-- redis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>1.7.2.RELEASE</version>
    </dependency>

5.2  application-kafka.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-2.1.xsd
        ">
        <!-- 加载配置文件 -->
		<context:property-placeholder ignore-unresolvable="false" location="classpath:properties/config.properties"/>
		
		<context:component-scan base-package="com.java.kafka.service" />     
	    <!-- 激活annotation功能 -->  
	    <context:annotation-config />  
		     
		<!-- 生产者 -->
		<bean id="producerFactory" class="org.springframework.kafka.core.DefaultKafkaProducerFactory">
			<constructor-arg>
				<map>
					<entry key="bootstrap.servers" value="${kafka.servers}"></entry>
					<entry key="key.serializer" value="org.apache.kafka.common.serialization.StringSerializer"></entry>
					<entry key="value.serializer" value="org.apache.kafka.common.serialization.StringSerializer"></entry>
					<entry key="key.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"></entry>
					<entry key="value.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"></entry>
				</map>
			</constructor-arg>
		</bean>
		<!-- 用于发送消息的模板 kafkaTemplate-->
        <bean id="kafkaTemplate" class="org.springframework.kafka.core.KafkaTemplate">
        	<constructor-arg ref="producerFactory"></constructor-arg>
        	<constructor-arg name="autoFlush" value="true"></constructor-arg>
        </bean>
        
        <!-- 创建消费者 -->
       <bean id="consumerFactory" class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
       		<constructor-arg>
       			<map>
       				<entry key="bootstrap.servers" value="${kafka.servers}"></entry>
       				<entry key="group.id" value="${kafka.report_data_container}"></entry>
       				<entry key="key.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"></entry>
       				<entry key="value.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"></entry>
       			</map>
       		</constructor-arg>
       </bean>
       <!-- 消息消费者监听器 用于在接收到消息时执行相关的业务代码  (自行定义的类 用户于处理相应的业务操作)-->
       <bean id="consumerListener" class="com.java.kafka.listener.KafkaConsumerListener"/>
       <!-- 页面page监听器 -->
       <bean id="reportDataListener" class="com.java.kafka.listener.ReportDataListener"></bean>
       
       <!-- 商品业务处理监听类 -->
       <bean id="commodityInfoListener" class="com.java.kafka.listener.CommodityInfoListener"></bean>
       
       <!-- 用于将监听器和个某个主题绑定在一起 -->
       <bean id="containerProperties_example" class="org.springframework.kafka.listener.config.ContainerProperties">
       		<!-- <constructor-arg value="test-topic"></constructor-arg>
       		<property name="messageListener" ref="consumerListener"></property> -->
       		<property name="messageListener" ref="reportDataListener"/> 
       		<!-- <property name="messageListener" ref="commodityInfoListener"/> -->
       		<constructor-arg>
       			<list>
       			 	<value>${kafka.topic_page}</value>
       			 	<!-- <value>${kafka.topic_click}</value> -->
       			 	<!-- <value>${kafka.topic_commod}</value> -->
       			</list>
       		</constructor-arg>
       </bean>
       <!-- 用于将消息监听器与消息消费者工厂绑定在一起 -->
       <bean id="messageListtenerContainer_example" class="org.springframework.kafka.listener.KafkaMessageListenerContainer">
       		<constructor-arg ref="containerProperties_example"></constructor-arg>
       		<constructor-arg ref="consumerFactory"></constructor-arg>
       </bean>
       
       <!-- redis  -->
       <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
            <!-- pool 最大空闲数 -->
       		<property name="maxIdle" value="8"></property>
       		<!-- 控制一个pool可分配多少个jedis实例 -->
       		<property name="maxTotal" value="10"></property>
       		<!-- 最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。 -->
       		<property name="maxWaitMillis" value="1000"></property>
       		<!-- 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 -->
       		<property name="testOnBorrow" value="true"></property>
       </bean>
       <!-- 
       		depends-on用来指定Bean初始化及销毁时的顺序。
       		depends-on用来表示一个Bean的实例化依靠另一个Bean先实例化。
       		如果在一个bean A上定义了depend-on B那么就表示:A 实例化前先实例化 B。
        -->
<!--        <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy" depends-on="poolConfig">
       		<constructor-arg name="poolConfig" ref="poolConfig"/>
       		<constructor-arg name="host" value="localhost"/>
       		<constructor-arg name="port" value="6379"/>
       </bean> -->
       
       
       
          <!-- 配置连接工厂 -->
	   <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" id="connectionFactory" 
	   p:pool-config-ref="poolConfig" p:password="${redis.password}" p:port="${redis.port}" p:host-name="${redis.host}"/>
	   
	   <!-- 操作redis -->
	   <bean class="org.springframework.data.redis.core.StringRedisTemplate" id="redisTemplate">
		   <property name="connectionFactory" ref="connectionFactory"/>
		   <property name="keySerializer">  
	       <bean  
	                class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
	       </property>  
	       <property name="hashKeySerializer">  
	            <bean  
	                class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
	       </property>  
	       <property name="valueSerializer">  
	            <bean  
	                class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> 
	       </property>  
	       <property name="hashValueSerializer">  
	            <bean  
	                class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
	       </property>  
	   </bean>
	   
       	<!-- springMvc4 用于将对象转换为 JSON  -->  
	    <bean id="stringConverter"  
	        class="org.springframework.http.converter.StringHttpMessageConverter">  
	        <property name="supportedMediaTypes">  
	            <list>  
	                <value>text/plain;charset=UTF-8</value>  
	            </list>  
	        </property>  
	    </bean>  
	    <bean id="jsonConverter"  
	        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>  
	    <bean  
	        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
	        <property name="messageConverters">  
	            <list>  
	                <ref bean="stringConverter" />  
	                <ref bean="jsonConverter" />  
	            </list>  
	        </property>  
	    </bean>          	
 
</beans>        

5.3  log4j.properties

#log4j.rootLogger=DEBUG,INFO, Console  
log4j.rootLogger=INFO,Console,KAFKA  
  
#Console 配置日志输出目的地 控制台输出
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.target=System.out
log4j.appender.Console.encoding=UTF-8

#配置日志信息的格式
log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n 

#配置日志信息输出目的地 为 kafka  (将kafka作为日志输出目的地)
log4j.appender.KAFKA=org.apache.kafka.log4jappender.KafkaLog4jAppender
#将日志数据发送到Kafka时的具体主题名
log4j.appender.KAFKA.topic=log4j-topic
#配置kafka服务器的地址,如果是群集则用逗号分隔
log4j.appender.KAFKA.brokerList=192.168.28.129:9092,192.168.28.130:9092,192.168.28.131:9092
#日志信息是否压缩
log4j.appender.KAFKA.compressionType=none
#表示是否同步发送
log4j.appender.KAFKA.syncSend=true
#将日志输出到Kafka时格式化
log4j.appender.KAFKA.layout=org.apache.log4j.PatternLayout  
log4j.appender.kafka.layout.ConversionPattern=%d{yyyy-MM-dd     HH:mm:ss} %-5p %c{1}:%L - %m%n

  
#log4j.logger.java.sql.ResultSet=INFO  
#log4j.logger.org.apache=INFO  
#log4j.logger.java.sql.Connection=DEBUG  
#log4j.logger.java.sql.Statement=DEBUG  
#log4j.logger.java.sql.PreparedStatement=DEBUG  
#log4j.logger.advertisement =TRACE


6.测试

  6.1启动 hdfs 如上

   6.2 启动Kafka 需要同时启动三台

     进入kafka bin目录执行命令:./kafka-server-start.sh -daemon ../config/server.properties &

    进程如下:

     master:

     

    slave0:

     

   slave1:

    

   启动zookeeper 需要同时启动三台

   进入zookeeper bin目录 执行命令: ./zkServer.sh start

6.3 在( 192.168.28.131)启动flume 

          启动命令:./flume-ng agent -c ../conf -f ../conf/flume-kafka-hdfs.conf -n a1 -Dflume.root.logger=INFO,console

    6.3 创建java测试类   KafkaLog4jTest2

package com.java.kafka.test;

import org.apache.log4j.Logger;

public class KafkaLog4jTest2 {

	private static Logger logger = Logger.getLogger(KafkaLog4jTest2.class);
	
	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		for (int i = 0; i < 5; i++) {
			logger.info("这是从 KafkaLog4jTest2 产生的消息 【"+i+"】...");
			Thread.sleep(1000);
		}
	}

}

 1.运行测试类:(kafka 作为从Log4j中获取数据)

2. hdfs 中存入的消息如下(flume 数据从kafka中获取):

执行命令 hadoop fs -cat /flume/testLog.log/*

    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值