feat(sync-gather): 实现数据同步功能
- 新增数据同步方法,支持增量同步 -增加数据校验和处理逻辑 - 集成消息队列服务,支持异步消息处理 - 优化 Redis 缓存操作 - 添加参数验证注解和切面dev
parent
6bfef02b9b
commit
edbb311f00
@ -0,0 +1,25 @@
|
||||
package com.lyr.common.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Params {
|
||||
|
||||
Valid[] value();
|
||||
|
||||
|
||||
@Target(ElementType.ANNOTATION_TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface Valid {
|
||||
|
||||
String value();
|
||||
|
||||
String message();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package com.lyr.framework.aspectj;
|
||||
|
||||
import com.lyr.common.annotation.Params;
|
||||
import com.lyr.common.utils.AssertUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Validator;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
@Slf4j
|
||||
public class ParameterValidationAspect {
|
||||
|
||||
@Before("@annotation(params)")
|
||||
public void validateParameters(JoinPoint joinPoint, Params params) {
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
Object[] args = joinPoint.getArgs();
|
||||
|
||||
for (Params.Valid valid : params.value()) {
|
||||
Parameter[] parameters = method.getParameters();
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
Parameter parameter = parameters[i];
|
||||
if (parameter.getName().equals(valid.value())) {
|
||||
ParameterValidator parameterValidator = getValidator(parameter.getType());
|
||||
parameterValidator.validate(args[i], valid.message());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface ParameterValidator {
|
||||
void validate(Object arg, String message);
|
||||
}
|
||||
|
||||
@Resource
|
||||
private Validator validator;
|
||||
|
||||
private ParameterValidator getValidator(Class<?> clazz) {
|
||||
|
||||
if (String.class.isAssignableFrom(clazz)) {
|
||||
log.debug("String 类型参数非空判断");
|
||||
return (arg, message) -> AssertUtil.notEmpty(String.valueOf(arg), message);
|
||||
} else if (Number.class.isAssignableFrom(clazz)) {
|
||||
log.debug("Number 类型参数非空判断");
|
||||
return (arg, message) -> {
|
||||
|
||||
};
|
||||
} else if (Boolean.class.isAssignableFrom(clazz)) {
|
||||
log.debug("Boolean 类型参数非空判断");
|
||||
return (arg, message) -> {
|
||||
|
||||
};
|
||||
} else if (clazz.isEnum()) {
|
||||
log.debug("Enum 类型参数非空判断");
|
||||
return (arg, message) -> {
|
||||
|
||||
};
|
||||
} else if (clazz.isArray()) {
|
||||
log.debug("数组类型参数非空判断");
|
||||
return (arg, message) -> {
|
||||
|
||||
};
|
||||
} else {
|
||||
// 默认的验证器,处理未列出的类型
|
||||
return (arg, message) -> {
|
||||
log.warn("Unsupported parameter type: {}", clazz.getName());
|
||||
// 可以在这里添加通用的验证逻辑
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.lyr.mq.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author liyc
|
||||
* @date 2024/11/11
|
||||
* @description TODO
|
||||
**/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "rss.mq")
|
||||
public class RssMqProperties {
|
||||
|
||||
private String topicName;
|
||||
private String confirmTopicName;
|
||||
private String subscriberChannelName;
|
||||
private String host;
|
||||
private Integer port;
|
||||
private Integer database;
|
||||
private String password;
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.lyr.mq.consumer;
|
||||
|
||||
import com.lyr.mq.config.RssMqProperties;
|
||||
import com.lyr.mq.service.RTopicSubscriberService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RTopic;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.api.listener.MessageListener;
|
||||
|
||||
/**
|
||||
* @author liyc
|
||||
* @date 2024/11/11
|
||||
* @description TODO
|
||||
**/
|
||||
@Slf4j
|
||||
public class RTopicSubscribe {
|
||||
|
||||
|
||||
private RedissonClient redissonClient;
|
||||
private RssMqProperties properties;
|
||||
private RTopicSubscriberService RTopicSubscriberService;
|
||||
|
||||
public void setRedissonClient(RedissonClient redissonClient) {
|
||||
this.redissonClient = redissonClient;
|
||||
}
|
||||
|
||||
public void setProperties(RssMqProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public void setSubscriberService(RTopicSubscriberService RTopicSubscriberService) {
|
||||
this.RTopicSubscriberService = RTopicSubscriberService;
|
||||
}
|
||||
|
||||
public void startListening() {
|
||||
RTopic topic = redissonClient.getTopic(properties.getTopicName());
|
||||
topic.addListener(String.class, (MessageListener<String>) (channel, msg) -> {
|
||||
RTopicSubscriberService.handleMessage(msg);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.lyr.mq.producer;
|
||||
|
||||
import com.lyr.mq.config.RssMqProperties;
|
||||
import com.lyr.mq.service.RTopicSubscriberService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RTopic;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author liyc
|
||||
* @date 2024/11/11
|
||||
* @description TODO
|
||||
**/
|
||||
@Slf4j
|
||||
@Component
|
||||
@Primary
|
||||
public class RTopicPublish {
|
||||
private RedissonClient redissonClient;
|
||||
private RssMqProperties properties;
|
||||
private RTopicSubscriberService rTopicSubscriberService;
|
||||
|
||||
public void setRedissonClient(RedissonClient redissonClient) {
|
||||
this.redissonClient = redissonClient;
|
||||
}
|
||||
|
||||
public void setProperties(RssMqProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public void setSubscriberService(RTopicSubscriberService rTopicSubscriberService) {
|
||||
this.rTopicSubscriberService = rTopicSubscriberService;
|
||||
}
|
||||
|
||||
public void publishMessage(String message) {
|
||||
RTopic topic = redissonClient.getTopic(properties.getTopicName());
|
||||
topic.publish(message);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.lyr.mq.service;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author liyc
|
||||
* @date 2024/11/11
|
||||
* @description TODO
|
||||
**/
|
||||
@Component
|
||||
public class RTopicSubscriberService {
|
||||
public void handleMessage(String message) {
|
||||
System.out.println("Received message: " + message);
|
||||
// 处理消息的逻辑
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.lyr.mq.RssMqAutoConfiguration
|
Loading…
Reference in New Issue