1 资源
资源信息 | 版本号 | 备注 |
---|---|---|
springboot | 2.1.5.RELEASE |
springboot-elk-demo 源码 下载:https://download.csdn.net/download/qq_15769939/16756675
2 配置信息
2.1 pom文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.auskat.demo
springboot-elk-demo
1.0-SNAPSHOT
8
8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-log4j2
com.lmax
disruptor
3.3.4
com.alibaba
fastjson
1.2.58
collector
src/main/java
**/*.properties
**/*.xml
true
src/main/resources
org.springframework.boot
spring-boot-maven-plugin
com.auskat.demo.elk.Application
2.2 配置信息
2.2.1 application.yml
# 设置访问路径
server.servlet.context-path=/
# 设置端口号
server.port=8001
# 设置应用名称
spring.application.name=springboot-elk-demo
# 设置http编码
spring.http.encoding.charset=UTF-8
# 设置时间格式标准
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
# 设置时区
spring.jackson.time-zone=GMT+8
# 设置JSON默认格式化字段规则
spring.jackson.default-property-inclusion=NON_NULL
3.2.2 log4j2.xml
logs
elk-demo
[%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ}] [%level{length=5}] [%thread-%tid] [%logger] [%X{hostName}] [%X{ip}] [%X{applicationName}] [%F,%L,%C,%M] [%m] ## '%ex'%n
3 功能实现
3.1 messge对象
package com.auskat.demo.elk.entity;
import lombok.Data;
/**
* 类文件: AccurateWatcherMessage
*
*
* 类描述:
*
* 作 者: AusKa_T
*
* 日 期: 2021/3/28 0028
*
* 时 间: 14:07
*
*/
@Data
public class AccurateWatcherMessage {
/**
* 标题
*/
private String title;
/**
* 执行时间
*/
private String executionTime;
/**
* 业务名称
*/
private String applicationName;
/**
* 等级
*/
private String level;
/**
* 内容
*/
private String body;
}
3.2 工具类
3.2.1 NetUtil
package com.auskat.demo.elk.utils;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.SocketChannel;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 类文件: NetUtil
*
*
* 类描述:
*
* 作 者: AusKa_T
*
* 日 期: 2021/3/26 0026
*
* 时 间: 14:14
*
*/
public class NetUtil {
public static String normalizeAddress(String address){
String[] blocks = address.split("[:]");
if(blocks.length > 2){
throw new IllegalArgumentException(address + " is invalid");
}
String host = blocks[0];
int port = 80;
if(blocks.length > 1){
port = Integer.valueOf(blocks[1]);
} else {
address += ":"+port; //use default 80
}
String serverAddr = String.format("%s:%d", host, port);
return serverAddr;
}
public static String getLocalAddress(String address){
String[] blocks = address.split("[:]");
if(blocks.length != 2){
throw new IllegalArgumentException(address + " is invalid address");
}
String host = blocks[0];
int port = Integer.valueOf(blocks[1]);
if("0.0.0.0".equals(host)){
return String.format("%s:%d",NetUtil.getLocalIp(), port);
}
return address;
}
private static int matchedIndex(String ip, String[] prefix){
for(int i=0; i ]+");
try {
Pattern pattern = Pattern.compile("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+");
Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
String matchedIp = null;
int matchedIdx = -1;
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
Enumeration en = ni.getInetAddresses();
while (en.hasMoreElements()) {
InetAddress addr = en.nextElement();
String ip = addr.getHostAddress();
Matcher matcher = pattern.matcher(ip);
if (matcher.matches()) {
int idx = matchedIndex(ip, prefix);
if(idx == -1) continue;
if(matchedIdx == -1){
matchedIdx = idx;
matchedIp = ip;
} else {
if(matchedIdx>idx){
matchedIdx = idx;
matchedIp = ip;
}
}
}
}
}
if(matchedIp != null) return matchedIp;
return "127.0.0.1";
} catch (Exception e) {
return "127.0.0.1";
}
}
public static String getLocalIp() {
return getLocalIp("*>10>172>192>127");
}
public static String remoteAddress(SocketChannel channel){
SocketAddress addr = channel.socket().getRemoteSocketAddress();
String res = String.format("%s", addr);
return res;
}
public static String localAddress(SocketChannel channel){
SocketAddress addr = channel.socket().getLocalSocketAddress();
String res = String.format("%s", addr);
return addr==null? res: res.substring(1);
}
public static String getPid(){
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
String name = runtime.getName();
int index = name.indexOf("@");
if (index != -1) {
return name.substring(0, index);
}
return null;
}
public static String getLocalHostName() {
try {
return (InetAddress.getLocalHost()).getHostName();
} catch (UnknownHostException uhe) {
String host = uhe.getMessage();
if (host != null) {
int colon = host.indexOf(':');
if (colon > 0) {
return host.substring(0, colon);
}
}
return "UnknownHost";
}
}
}
3.2.2 InputMDC
package com.auskat.demo.elk.utils;
import org.jboss.logging.MDC;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
/**
* 类文件: InputMDC
*
*
* 类描述:
*
* 作 者: AusKa_T
*
* 日 期: 2021/3/26 0026
*
* 时 间: 14:13
*
*/
@Component
public class InputMDC implements EnvironmentAware {
private static Environment environment;
@Override
public void setEnvironment(Environment environment) {
InputMDC.environment = environment;
}
public static void putMDC() {
MDC.put("hostName", NetUtil.getLocalHostName());
MDC.put("ip", NetUtil.getLocalIp());
MDC.put("applicationName", environment.getProperty("spring.application.name"));
}
}
3.2.3 FastJsonConverUtil
package com.auskat.demo.elk.utils;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
/**
* 类文件: FastJsonConvertUtil
*
*
* 类描述:
*
* 作 者: AusKa_T
*
* 日 期: 2021/3/26 0026
*
* 时 间: 14:13
*
*/
@Slf4j
public class FastJsonConvertUtil {
private static final SerializerFeature[] featuresWithNullValue = {
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullStringAsEmpty
};
/**
* 方法名称:将JSON字符串转换为实体对象
* 概要说明:将JSON字符串转换为实体对象
* @author hezhuo.bai
* @since 2019年1月15日 下午4:53:49
* @param data JSON字符串
* @param clzss 转换对象
* @return T
*/
public static T convertJSONToObject(String data, Class clzss) {
try {
T t = JSON.parseObject(data, clzss);
return t;
} catch (Exception e) {
log.error("convertJSONToObject Exception", e);
return null;
}
}
/**
* 方法名称:将JSONObject对象转换为实体对象
* 概要说明:将JSONObject对象转换为实体对象
* @author hezhuo.bai
* @since 2019年1月15日 下午4:54:32
* @param data JSONObject对象
* @param clzss 转换对象
* @return T
*/
public static T convertJSONToObject(JSONObject data, Class clzss) {
try {
T t = JSONObject.toJavaObject(data, clzss);
return t;
} catch (Exception e) {
log.error("convertJSONToObject Exception", e);
return null;
}
}
/**
* 方法名称:将JSON字符串数组转为List集合对象
* 概要说明:将JSON字符串数组转为List集合对象
* @author hezhuo.bai
* @since 2019年1月15日 下午4:54:50
* @param data JSON字符串数组
* @param clzss 转换对象
* @return List集合对象
*/
public static List convertJSONToArray(String data, Class clzss) {
try {
List t = JSON.parseArray(data, clzss);
return t;
} catch (Exception e) {
log.error("convertJSONToArray Exception", e);
return null;
}
}
/**
* 方法名称:将List转为List集合对象
* 概要说明:将List转为List集合对象
* @author hezhuo.bai
* @since 2019年1月15日 下午4:55:11
* @param data List
* @param clzss 转换对象
* @return List集合对象
*/
public static List convertJSONToArray(List data, Class clzss) {
try {
List t = new ArrayList();
for (JSONObject jsonObject : data) {
t.add(convertJSONToObject(jsonObject, clzss));
}
return t;
} catch (Exception e) {
log.error("convertJSONToArray Exception", e);
return null;
}
}
/**
* 方法名称:将对象转为JSON字符串
* 概要说明:将对象转为JSON字符串
* @author hezhuo.bai
* @since 2019年1月15日 下午4:55:41
* @param obj 任意对象
* @return JSON字符串
*/
public static String convertObjectToJSON(Object obj) {
try {
String text = JSON.toJSONString(obj);
return text;
} catch (Exception e) {
log.error("convertObjectToJSON Exception", e);
return null;
}
}
/**
* 方法名称:将对象转为JSONObject对象
* 概要说明:将对象转为JSONObject对象
* @author hezhuo.bai
* @since 2019年1月15日 下午4:55:55
* @param obj 任意对象
* @return JSONObject对象
*/
public static JSONObject convertObjectToJSONObject(Object obj){
try {
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(obj);
return jsonObject;
} catch (Exception e) {
log.error("convertObjectToJSONObject Exception", e);
return null;
}
}
public static String convertObjectToJSONWithNullValue(Object obj) {
try {
String text = JSON.toJSONString(obj, featuresWithNullValue);
return text;
} catch (Exception e) {
log.error("convertObjectToJSONWithNullValue Exception", e);
return null;
}
}
}
3.3 接口
3.3.1 日志接口
package com.auskat.demo.elk.controller;
import com.auskat.demo.elk.utils.InputMDC;
import org.jboss.logging.MDC;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;
/**
* 类文件: IndexController
*
*
* 类描述:
*
* 作 者: AusKa_T
*
* 日 期: 2021/3/26 0026
*
* 时 间: 14:15
*
*/
@Slf4j
@RestController
public class IndexController {
/**
* [%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ}]
* [%level{length=5}]
* [%thread-%tid]
* [%logger]
* [%X{hostName}]
* [%X{ip}]
* [%X{applicationName}]
* [%F,%L,%C,%M] F 当前执行类, L 行数, C class ,M method
* [%m] ## '%ex'%n
* -----------------------------------------------
* [2019-09-18T14:42:51.451+08:00]
* [INFO]
* [main-1]
* [org.springframework.boot.web.embedded.tomcat.TomcatWebServer]
* []
* []
* []
* [TomcatWebServer.java,90,org.springframework.boot.web.embedded.tomcat.TomcatWebServer,initialize]
* [Tomcat initialized with port(s): 8001 (http)] ## ''
*
* ["message",
* "\[%{NOTSPACE:currentDateTime}\]
* \[%{NOTSPACE:level}\]
* \[%{NOTSPACE:thread-id}\]
* \[%{NOTSPACE:class}\]
* \[%{DATA:hostName}\]
* \[%{DATA:ip}\]
* \[%{DATA:applicationName}\]
* \[%{DATA:location}\]
* \[%{DATA:messageInfo}\]
* ## (\'\'|%{QUOTEDSTRING:throwable})"]
* @return
*/
@RequestMapping(value = "/index")
public String index() {
// MDC 当前线程绑定的局部变量
InputMDC.putMDC();
log.info("我是一条info日志");
log.warn("我是一条warn日志");
log.error("我是一条error日志");
return "idx";
}
@RequestMapping(value = "/err")
public String err() {
InputMDC.putMDC();
try {
int a = 1/0;
} catch (Exception e) {
log.error("算术异常", e);
}
return "err";
}
}
3.3.2 告警接口
package com.auskat.demo.elk.controller;
import com.alibaba.fastjson.JSON;
import com.auskat.demo.elk.entity.AccurateWatcherMessage;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 类文件: WatcherController
*
*
* 类描述:
*
* 作 者: AusKa_T
*
* 日 期: 2021/3/28 0028
*
* 时 间: 14:06
*
*/
@RestController
public class WatcherController {
@RequestMapping(value ="/accurateWatch")
public String watch(@RequestBody AccurateWatcherMessage accurateWatcherMessage) {
String ret = JSON.toJSONString(accurateWatcherMessage);
System.err.println("----告警内容----:" + ret);
return "is watched" + ret;
}
}
4 相关信息
上一篇:ELK 日志采集框架(一):架构设计
下一篇:ELK 日志采集框架(三):Filebeat安装与配置
博文不易,辛苦各位猿友点个关注和赞,感谢