18 changed files with 302 additions and 246 deletions
-
15client/pom.xml
-
26client/src/main/java/com/inscloudtech/alog/client/task/ALogConfig.java
-
9client/src/main/java/com/inscloudtech/alog/client/task/Monitor.java
-
20common/src/main/java/com/inscloudtech/alog/common/annotation/ActionLog.java
-
21common/src/main/java/com/inscloudtech/alog/common/annotation/IgnoreRecordField.java
-
2common/src/main/java/com/inscloudtech/alog/common/annotation/NeedRecordField.java
-
23common/src/main/java/com/inscloudtech/alog/common/utils/IdWorker.java
-
5example/pom.xml
-
182example/src/main/java/com/inscloudtech/alog/clientdemo/aspectj/ActionLogAspect.java
-
25example/src/main/java/com/inscloudtech/alog/clientdemo/demo/controller/DemoUserController.java
-
72example/src/main/java/com/inscloudtech/alog/clientdemo/utils/BeanCompareUtils.java
-
111example/src/main/java/com/inscloudtech/alog/clientdemo/utils/BeanCopyUtils.java
-
1example/src/main/java/com/inscloudtech/alog/clientdemo/utils/StringUtils.java
-
8example/src/main/resources/application.yml
-
10worker/pom.xml
-
4worker/src/main/java/com/inscloudtech/alog/worker/disruptor/TracerConsumer.java
-
12worker/src/main/resources/application.yml
-
2worker/src/main/resources/jlog.sql
@ -0,0 +1,26 @@ |
|||
package com.inscloudtech.alog.client.task; |
|||
|
|||
import java.util.List; |
|||
|
|||
|
|||
public class ALogConfig { |
|||
private long appId; |
|||
private List<String> workers; |
|||
|
|||
|
|||
public long getAppId() { |
|||
return appId; |
|||
} |
|||
|
|||
public void setAppId(long appId) { |
|||
this.appId = appId; |
|||
} |
|||
|
|||
public List<String> getWorkers() { |
|||
return workers; |
|||
} |
|||
|
|||
public void setWorkers(List<String> workers) { |
|||
this.workers = workers; |
|||
} |
|||
} |
@ -0,0 +1,21 @@ |
|||
package com.inscloudtech.alog.common.annotation; |
|||
|
|||
import java.lang.annotation.ElementType; |
|||
import java.lang.annotation.Retention; |
|||
import java.lang.annotation.RetentionPolicy; |
|||
import java.lang.annotation.Target; |
|||
|
|||
/** |
|||
* 忽略记录的修改值 |
|||
* |
|||
* @author inscloudtech |
|||
*/ |
|||
@Retention(RetentionPolicy.RUNTIME) |
|||
@Target(ElementType.FIELD) |
|||
//@JacksonAnnotationsInside |
|||
//@JsonSerialize(using = SensitiveJsonSerializer.class) |
|||
public @interface IgnoreRecordField { |
|||
String fieldName() default ""; |
|||
} |
|||
|
|||
|
@ -0,0 +1,72 @@ |
|||
package com.inscloudtech.alog.clientdemo.utils; |
|||
|
|||
|
|||
|
|||
import com.inscloudtech.alog.common.annotation.IgnoreRecordField; |
|||
import com.inscloudtech.alog.common.annotation.NeedRecordField; |
|||
|
|||
|
|||
import java.lang.reflect.Field; |
|||
import java.util.*; |
|||
|
|||
/** |
|||
* bean字段值比较 |
|||
* |
|||
* @author inscloudtech |
|||
*/ |
|||
|
|||
public class BeanCompareUtils { |
|||
|
|||
/** |
|||
* 排除敏感属性字段 |
|||
*/ |
|||
public static final List<String> EXCLUDE_FIELDS = new ArrayList(); |
|||
|
|||
/** |
|||
* 获取变更内容 |
|||
* @param oldBean 更改前的Bean |
|||
* @param newBean 更改后的Bean |
|||
* @param <T> |
|||
* @return |
|||
*/ |
|||
public static <T> Map getChangedFields(T oldBean, T newBean){ |
|||
Field[] fields = newBean.getClass().getDeclaredFields(); |
|||
StringBuilder beforeBuilder = new StringBuilder(); |
|||
StringBuilder afterBuilder = new StringBuilder(); |
|||
for(Field field : fields) { |
|||
field.setAccessible(true); |
|||
String fieldName = field.getName(); |
|||
if (field.isAnnotationPresent(IgnoreRecordField.class) || EXCLUDE_FIELDS.contains(fieldName)) { |
|||
continue; |
|||
} |
|||
|
|||
try { |
|||
Object oldValue = field.get(oldBean); |
|||
Object newValue = field.get(newBean); |
|||
if(null != newValue && !Objects.equals(newValue, oldValue)) { |
|||
if (field.isAnnotationPresent(NeedRecordField.class)) { |
|||
String tempFieldName = field.getAnnotation(NeedRecordField.class).fieldName(); |
|||
fieldName = StringUtils.isEmpty(tempFieldName)?fieldName:tempFieldName; |
|||
} |
|||
beforeBuilder.append(fieldName); |
|||
beforeBuilder.append(": 【更改前:"); |
|||
beforeBuilder.append(oldValue); |
|||
beforeBuilder.append("】,"); |
|||
|
|||
afterBuilder.append(fieldName); |
|||
afterBuilder.append(": 【更改后:"); |
|||
afterBuilder.append(newValue); |
|||
afterBuilder.append("】,"); |
|||
|
|||
} |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
Map<String,String> result = new HashMap<>(2); |
|||
result.put("before",beforeBuilder.toString()); |
|||
result.put("after",afterBuilder.toString()); |
|||
return result; |
|||
} |
|||
|
|||
} |
@ -1,111 +0,0 @@ |
|||
package com.inscloudtech.alog.clientdemo.utils; |
|||
|
|||
|
|||
|
|||
import com.inscloudtech.alog.common.annotation.NeedRecordField; |
|||
|
|||
|
|||
import java.lang.reflect.Field; |
|||
import java.lang.reflect.Method; |
|||
import java.util.*; |
|||
|
|||
/** |
|||
* bean深拷贝工具(基于 cglib 性能优异) |
|||
* <p> |
|||
* 重点 cglib 不支持 拷贝到链式对象 |
|||
* 例如: 源对象 拷贝到 目标(链式对象) |
|||
* 请区分好`浅拷贝`和`深拷贝`再做使用 |
|||
* |
|||
* @author inscloudtech |
|||
*/ |
|||
|
|||
public class BeanCopyUtils { |
|||
|
|||
/** |
|||
* 获取变更内容 |
|||
* @param oldBean 更改前的Bean |
|||
* @param newBean 更改后的Bean |
|||
* @param <T> |
|||
* @return |
|||
*/ |
|||
public static <T> Map getChangedFields(T oldBean, T newBean){ |
|||
Field[] fields = newBean.getClass().getDeclaredFields(); |
|||
StringBuilder beforeBuilder = new StringBuilder(); |
|||
StringBuilder afterBuilder = new StringBuilder(); |
|||
String fieldName = ""; |
|||
for(Field field : fields) { |
|||
field.setAccessible(true); |
|||
if (!field.isAnnotationPresent(NeedRecordField.class)) { |
|||
continue; |
|||
} |
|||
|
|||
try { |
|||
Object oldValue = field.get(oldBean); |
|||
Object newValue = field.get(newBean); |
|||
if(null != newValue && !Objects.equals(newValue, oldValue)) { |
|||
fieldName = field.getAnnotation(NeedRecordField.class).fieldName(); |
|||
beforeBuilder.append(fieldName); |
|||
beforeBuilder.append(": 【更改前:"); |
|||
beforeBuilder.append(oldValue); |
|||
beforeBuilder.append("】,"); |
|||
|
|||
afterBuilder.append(fieldName); |
|||
afterBuilder.append(": 【更改后:"); |
|||
afterBuilder.append(newValue); |
|||
afterBuilder.append("】,"); |
|||
|
|||
} |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
Map<String,String> result = new HashMap<>(2); |
|||
result.put("before",beforeBuilder.toString()); |
|||
result.put("after",afterBuilder.toString()); |
|||
return result; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 利用反射通过get方法获取bean中字段fieldName的值 |
|||
* @param bean |
|||
* @param fieldName |
|||
* @return |
|||
* @throws Exception |
|||
*/ |
|||
public static Object getFieldValue(Object bean, String fieldName) |
|||
throws Exception { |
|||
StringBuffer result = new StringBuffer(); |
|||
String methodName = result.append("get") |
|||
.append(fieldName.substring(0, 1).toUpperCase()) |
|||
.append(fieldName.substring(1)).toString(); |
|||
|
|||
Object rObject = null; |
|||
Method method = null; |
|||
|
|||
@SuppressWarnings("rawtypes") |
|||
Class[] classArr = new Class[0]; |
|||
method = bean.getClass().getMethod(methodName, classArr); |
|||
rObject = method.invoke(bean, new Object[0]); |
|||
|
|||
return rObject; |
|||
} |
|||
|
|||
|
|||
|
|||
private static void setFieldValue(Object bean, String fieldName, Object value) |
|||
throws Exception { |
|||
StringBuffer result = new StringBuffer(); |
|||
String methodName = result.append("set") |
|||
.append(fieldName.substring(0, 1).toUpperCase()) |
|||
.append(fieldName.substring(1)).toString(); |
|||
|
|||
/** |
|||
* 利用发射调用bean.set方法将value设置到字段 |
|||
*/ |
|||
Class[] classArr = new Class[1]; |
|||
classArr[0]="java.lang.String".getClass(); |
|||
Method method=bean.getClass().getMethod(methodName,classArr); |
|||
method.invoke(bean,value); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue