Authored by liangyi.chen@yoho.cn

Add Project

Showing 28 changed files with 1220 additions and 0 deletions
HELP.md
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/build/
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>yoho-datasync-consumer</artifactId>
<groupId>com.yoho.datasync</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yoho-datasync-consumer-dal</artifactId>
</project>
\ No newline at end of file
... ...
package com.yoho.datasync.consumer.dal.entity;
import lombok.Data;
import javax.persistence.*;
@Data
@Entity
@Table(name = "test_sync")
public class TestSync {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer Id;
@Column(nullable = false)
private String city;
private Integer provinceId;
}
... ...
package com.yoho.datasync.consumer.dal.repository;
import com.yoho.datasync.consumer.dal.entity.TestSync;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TestSyncRepository extends JpaRepository<TestSync,Integer> {
}
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>yoho-datasync-consumer</artifactId>
<groupId>com.yoho.datasync</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yoho-datasync-consumer-handler</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.13.sec01</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
package com.yoho.datasync.consumer.handler.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ClassTransfer {
public Class<?> targetClass();
}
... ...
package com.yoho.datasync.consumer.handler.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldTransfer {
public Class<?> targetClass();
public String targetFieldName() default "";
}
... ...
package com.yoho.datasync.consumer.handler.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MqConsumerListerner {
String dbName();
String tableName();
}
... ...
package com.yoho.datasync.consumer.handler.config;
import com.yoho.datasync.consumer.handler.entity.TableConfig;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties
public class TableConfigLoader {
@Setter
@Getter
private List<TableConfig> tableConfigs;
private Map<String, TableConfig> tableConfigMap = new HashMap<>();
public Map<String, TableConfig> getTableConfigMap() {
return tableConfigMap;
}
@PostConstruct
private void init() {
if(!CollectionUtils.isEmpty(tableConfigs)) {
tableConfigs.forEach(tableConfig ->
tableConfigMap.put(this.getTableConfigKey(tableConfig.getDbName(), tableConfig.getTableName()),
tableConfig));
}
}
public TableConfig getTableConfigByKey(String dbName, String tableName) {
String tabelConfigKey = getTableConfigKey(dbName, tableName);
return this.tableConfigMap.get(tabelConfigKey);
}
private String getTableConfigKey(String dbName, String tableName) {
if (StringUtils.isBlank(dbName)) {
return tableName;
}
return dbName + "." + tableName;
}
}
... ...
package com.yoho.datasync.consumer.handler.entity;
import lombok.Data;
import java.util.Map;
@Data
public class MqMessageEntity {
/**
* binlog操作类型,新增,删除等
*/
private String action;
private String dbName;
private String tableName;
/**
* 版本号
*/
private long version;
private Map<String, Object> data;
public MqMessageEntity(String action, String dbName, String tableName, long version, Map<String, Object> data) {
this.action = action;
this.dbName = dbName;
this.tableName = tableName;
this.version = version;
this.data = data;
}
}
... ...
package com.yoho.datasync.consumer.handler.entity;
import lombok.Data;
@Data
public class TableConfig {
private String dbName;
private String tableName;
private String[] primaryKeys;
private int queueSize;
}
... ...
package com.yoho.datasync.consumer.handler.factory;
import com.yoho.datasync.consumer.handler.annotation.MqConsumerListerner;
import com.yoho.datasync.consumer.handler.helper.MessageHelper;
import com.yoho.datasync.consumer.handler.helper.FastJsonMessageConverter;
import com.yoho.datasync.consumer.handler.mqcomponent.MqBeansResgister;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
public class MqConsumerFactory implements ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger(MqConsumerFactory.class);
@Autowired
private MessageHelper messageHelper;
@Autowired
private FastJsonMessageConverter fastJsonMessageConverter;
@Autowired
private MqBeansResgister searchMqBeansResgister;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, Object> consumerBeans = applicationContext.getBeansWithAnnotation(MqConsumerListerner.class);
if(consumerBeans==null || consumerBeans.isEmpty()){
logger.info("can not load any rabbit consumer...");
return;
}
this.addMessageListener(consumerBeans);
}
private void addMessageListener(Map<String, Object> consumerBeans) {
for (Object consumer : consumerBeans.values()) {
MqConsumerListerner searchRmqListerner = consumer.getClass().getAnnotation(MqConsumerListerner.class);
String dbName = searchRmqListerner.dbName();
String tableName = searchRmqListerner.tableName();
List<String> queueNames = messageHelper.getTableQueueNames(dbName,tableName);
for (String queueName : queueNames) {
try {
SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer();
listenerContainer.setConnectionFactory(searchMqBeansResgister.getConnectionFactory());
listenerContainer.setConcurrentConsumers(1);
listenerContainer.setPrefetchCount(1);
//linstener
MessageListenerAdapter adapter = new MessageListenerAdapter(consumer);
adapter.setMessageConverter(fastJsonMessageConverter);
listenerContainer.setMessageListener(adapter);
listenerContainer.setQueueNames(queueName);
listenerContainer.start();
logger.info("starting rabbitmq container {} success, concurrentConsumers {}.", queueName,1);
} catch (Exception e) {
logger.error("starting rabbitmq container failed.", e);
}
}
}
}
}
... ...
package com.yoho.datasync.consumer.handler.helper;
import com.yoho.datasync.consumer.handler.annotation.ClassTransfer;
import com.yoho.datasync.consumer.handler.annotation.FieldTransfer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ClassTargetFactory {
private static final Logger logger = LoggerFactory.getLogger(ClassTargetFactory.class);
private final Map<Class, Map<Field, Field>> targetInfoFactory = new ConcurrentHashMap<>();
private Field getField(Class<?> clazz, String fieldName) {
try {
return clazz.getDeclaredField(fieldName);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}
private <T> T newInstance(Class<T> clazz) {
try {
return clazz.newInstance();
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}
private Map<Field, Field> getTargetInfoFactory(Class<?> clazz) {
//1、从缓存中获取
Map<Field, Field> targetInfo = targetInfoFactory.get(clazz);
if (targetInfo != null) {
return targetInfo;
}
targetInfo = new HashMap<>();
targetInfoFactory.put(clazz, targetInfo);
//2、解析ClassTransfer
Class<?> defaultTargetClass = null;
ClassTransfer classTransfer = clazz.getAnnotation(ClassTransfer.class);
if (classTransfer != null) {
defaultTargetClass = classTransfer.targetClass();
}
//3、解析FieldTransfer
for (Field field : clazz.getDeclaredFields()) {
Class<?> targetClass = null;
String targetFieldName = null;
FieldTransfer fieldTransfer = field.getAnnotation(FieldTransfer.class);
if (fieldTransfer != null) {
targetClass = fieldTransfer.targetClass();
targetFieldName = StringUtils.isBlank(fieldTransfer.targetFieldName()) ? field.getName() : fieldTransfer.targetFieldName();
} else if (defaultTargetClass != null) {
targetClass = defaultTargetClass;
targetFieldName = field.getName();
}
if (targetClass != null && targetFieldName != null) {
Field targetField = getField(targetClass, targetFieldName);
if (targetField != null) {
targetInfo.put(field, targetField);
}
}
}
//4、返回解析结果
return targetInfo;
}
public Map<Class, Object> getTargetObjects(Object instance) throws Exception {
//1、获取每个field对应的目标信息
Map<Field, Field> targetClassInfo = getTargetInfoFactory(instance.getClass());
//2、初始化实例
Map<Class, Object> instanceMap = new HashMap<>();
for (Map.Entry<Field, Field> entry : targetClassInfo.entrySet()) {
Field field = entry.getKey();
Field targetField = entry.getValue();
Object traget = instanceMap.computeIfAbsent(targetField.getDeclaringClass(), a -> newInstance(a));
if (traget != null) {
field.setAccessible(true);
targetField.setAccessible(true);
targetField.set(traget, field.get(instance));
}
}
return instanceMap;
}
}
... ...
package com.yoho.datasync.consumer.handler.helper;
import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.AbstractMessageConverter;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
@Component
public class FastJsonMessageConverter extends AbstractMessageConverter {
public static final String DEFAULT_CHARSET = "UTF-8";
private volatile String defaultCharset = DEFAULT_CHARSET;
public FastJsonMessageConverter() {
super();
}
@Override
public Object fromMessage(Message message) throws MessageConversionException {
return message;
}
@Override
protected Message createMessage(Object objectToConvert, MessageProperties messageProperties) throws MessageConversionException {
byte[] bytes = null;
try {
String jsonString = JSONObject.toJSONString(objectToConvert);
bytes = jsonString.getBytes(this.defaultCharset);
} catch (UnsupportedEncodingException e) {
throw new MessageConversionException("Failed to convert Message content", e);
}
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
messageProperties.setContentEncoding(this.defaultCharset);
if (bytes != null) {
messageProperties.setContentLength(bytes.length);
}
return new Message(bytes, messageProperties);
}
}
\ No newline at end of file
... ...
package com.yoho.datasync.consumer.handler.helper;
import java.util.Date;
import java.util.Map;
public class FieldTransferTest {
public static void main(String[] args) throws Exception{
long ts = System.currentTimeMillis();
Object1 object1 = new Object1();
object1.setField1(new Date());
ClassTargetFactory classTargetFactory = new ClassTargetFactory();
Map<Class,Object> targetObject = classTargetFactory.getTargetObjects(object1);
Object2 object2 = (Object2) targetObject.get(Object2.class);
System.out.println(targetObject.get(Object2.class).toString());
//System.out.println(targetObject.get(Object3.class).toString());
System.out.println("cost:"+(System.currentTimeMillis()- ts));
}
}
... ...
package com.yoho.datasync.consumer.handler.helper;
import com.yoho.datasync.consumer.handler.config.TableConfigLoader;
import com.yoho.datasync.consumer.handler.entity.MqMessageEntity;
import com.yoho.datasync.consumer.handler.entity.TableConfig;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Component
public class MessageHelper {
private static final Logger logger = LoggerFactory.getLogger(MessageHelper.class);
private static final String QUEUE_PERIFX = "yohodatasync.";
@Resource
private TableConfigLoader tableConfigLoader;
private String getQueuePerfix(String dbName, String tableName) {
StringBuilder queuePerfix = new StringBuilder(QUEUE_PERIFX);
if (StringUtils.isNotBlank(dbName)) {
queuePerfix.append(dbName);
queuePerfix.append(".");
}
if (StringUtils.isNotBlank(tableName)) {
queuePerfix.append(tableName);
}
return queuePerfix.toString();
}
/**
* 获取更新时的真实的队列名
* yohodatasync.shops.user_account
*/
public String getRealQueueName(String dbName, String tableName) {
StringBuilder queuePerfix = new StringBuilder(QUEUE_PERIFX);
if (StringUtils.isNotBlank(dbName)) {
queuePerfix.append(dbName);
queuePerfix.append(".");
}
if (StringUtils.isNotBlank(tableName)) {
queuePerfix.append(tableName);
}
return queuePerfix.toString();
}
private String getPrimaryKeysValue(TableConfig tableConfig, Map<String, Object> dataMap) {
String[] primaryKeys = tableConfig.getPrimaryKeys();
StringBuilder primaryKeysValue = new StringBuilder();
for (String primaryKey : primaryKeys) {
primaryKeysValue.append(dataMap.get(primaryKey));
}
return primaryKeysValue.toString();
}
/**
* 当前表对应的队列是否是单队列
*
* @param tableConfig
* @return
*/
private boolean isSingleQueue(TableConfig tableConfig) {
if (tableConfig == null) {
return true;
}
int queueSize = tableConfig.getQueueSize();
if (queueSize <= 1) {
return true;
}
String[] primaryKeys = tableConfig.getPrimaryKeys();
return primaryKeys == null;
}
private String genRealQueueName(String queuePerfix, int index) {
return queuePerfix + "." + index;
}
/**
* 根据表名获取队列名
*
* @param tableName
* @return
*/
public List<String> getTableQueueNames(String dbName, String tableName) {
String queuePerfix = this.getQueuePerfix(dbName, tableName);
TableConfig tableConfig = tableConfigLoader.getTableConfigByKey(dbName, tableName);
if (this.isSingleQueue(tableConfig)) {
return Arrays.asList(this.genRealQueueName(queuePerfix, 0));
}
List<String> results = new ArrayList<>();
for (int i = 0; i < tableConfig.getQueueSize(); i++) {
results.add(this.genRealQueueName(queuePerfix, i));
}
return results;
}
/**
* 获取更新时的真实的队列名
*
* @param mqMessage
* @param tableName
* @param mqMessage
* @return
*/
public String getRealQueueName(String dbName, String tableName, MqMessageEntity mqMessage) {
String queuePerfix = this.getQueuePerfix(dbName, tableName);
try {
TableConfig tableConfig = tableConfigLoader.getTableConfigByKey(dbName, tableName);
Map<String, Object> dataMap = mqMessage.getData();
if (this.isSingleQueue(tableConfig) || dataMap==null) {
return this.genRealQueueName(queuePerfix, 0);
}
String primaryKeysValue = getPrimaryKeysValue(tableConfig, dataMap);
if (StringUtils.isBlank(primaryKeysValue)) {
return this.genRealQueueName(queuePerfix, 0);
}
int queueIndex = Math.abs(primaryKeysValue.hashCode()) % tableConfig.getQueueSize();
return this.genRealQueueName(queuePerfix, queueIndex);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return this.genRealQueueName(queuePerfix, 0);
}
}
}
... ...
package com.yoho.datasync.consumer.handler.helper;
import com.yoho.datasync.consumer.handler.annotation.FieldTransfer;
import com.yoho.datasync.consumer.handler.annotation.ClassTransfer;
import java.util.Date;
@ClassTransfer(targetClass = Object2.class)
public class Object1 {
private Date field1 = new Date();
private String field2 = "field2";
@FieldTransfer(targetClass = Object2.class, targetFieldName = "field3")
private String field3 = "field3";
@FieldTransfer(targetClass = Object2.class, targetFieldName = "field2")
private String field4 = "field4";
@FieldTransfer(targetClass = Object3.class, targetFieldName = "field1")
private String field5 = "field5";
@FieldTransfer(targetClass = Object3.class, targetFieldName = "field2")
private String field6 = "field6";
@FieldTransfer(targetClass = Object3.class, targetFieldName = "field3")
private String field7 = "field7";
public Date getField1() {
return field1;
}
public void setField1(Date field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public String getField3() {
return field3;
}
public void setField3(String field3) {
this.field3 = field3;
}
public String getField4() {
return field4;
}
public void setField4(String field4) {
this.field4 = field4;
}
public String getField5() {
return field5;
}
public void setField5(String field5) {
this.field5 = field5;
}
public String getField6() {
return field6;
}
public void setField6(String field6) {
this.field6 = field6;
}
public String getField7() {
return field7;
}
public void setField7(String field7) {
this.field7 = field7;
}
}
... ...
package com.yoho.datasync.consumer.handler.helper;
import java.util.Date;
public class Object2 {
private Date field1;
private String field2;
private String field3;
private String field4;
public Date getField1() {
return field1;
}
public void setField1(Date field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public String getField3() {
return field3;
}
public void setField3(String field3) {
this.field3 = field3;
}
public String getField4() {
return field4;
}
public void setField4(String field4) {
this.field4 = field4;
}
@Override
public String toString() {
return "Object2{" +
"field1='" + field1 + '\'' +
", field2='" + field2 + '\'' +
", field3='" + field3 + '\'' +
", field4='" + field4 + '\'' +
'}';
}
}
... ...
package com.yoho.datasync.consumer.handler.helper;
public class Object3 {
private String field1;
private String field2;
private String field3;
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public String getField3() {
return field3;
}
public void setField3(String field3) {
this.field3 = field3;
}
@Override
public String toString() {
return "Object3{" +
"field1='" + field1 + '\'' +
", field2='" + field2 + '\'' +
", field3='" + field3 + '\'' +
'}';
}
}
... ...
package com.yoho.datasync.consumer.handler.mqcomponent;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class MqBeansResgister {
private static final int requestedHeartBeat = 10;
private static final int channelCacheSize = 60;
@Value("${rabbitmq.username}")
private String userName;
@Value("${rabbitmq.password}")
private String passWord;
@Value("${rabbitmq.server}")
private String server;
@Value("${rabbitmq.port}")
private String port;
@Value("${rabbitmq.server}:${rabbitmq.port}")
private String addresses;
@Value("${rabbitmq.virtualHost}")
private String virtualHost;
private ConnectionFactory connectionFactory;
@PostConstruct
void init(){
//定义connectionFactory
this.connectionFactory = createConnectionFactory();
}
private ConnectionFactory createConnectionFactory() {
CachingConnectionFactory cf = new CachingConnectionFactory(server);
cf.setUsername(userName);
cf.setPassword(passWord);
cf.setAddresses(addresses);
cf.setVirtualHost(virtualHost);
cf.setChannelCacheSize(channelCacheSize);
cf.setRequestedHeartBeat(requestedHeartBeat);
return cf ;
}
public ConnectionFactory getConnectionFactory() {
return connectionFactory;
}
}
... ...
package com.yoho.datasync.consumer.handler.utils;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CharUtils {
/**
* 将字符转换成大写,如a->A
*
* @param c
* @return
*/
public static char toUpperCase(char c) {
if (c < 97 || c > 122)
return c;
else
return (char) (c - 32);
}
/**
* 将字符转换成小写,如A->a
*
* @param c
* @return
*/
public static char toLowerCase(char c) {
if (c < 65 || c > 90)
return c;
else
return (char) (c + 32);
}
public static boolean isLowerCase(char c) {
return (c >= 'a' && c <= 'z');
}
public static boolean isUpperCase(char c) {
return (c >= 'A' && c <= 'Z');
}
public static String getLowerCaseNames(String names) {
StringBuilder sb = new StringBuilder();
char[] cc = names.toCharArray();
sb.append(toLowerCase(cc[0]));
for (int i = 1; i < cc.length; i++) {
if (isLowerCase(cc[i - 1]) && isUpperCase(cc[i])) {
sb.append("_" + toLowerCase(cc[i]));
} else {
sb.append(toLowerCase(cc[i]));
}
}
;
return sb.toString();
}
/**
* 将驼峰风格替换为下划线风格
*
* @param str
* @return
*/
public static String CamelhumpToUnderline(String str) {
Matcher matcher = Pattern.compile("[A-Z]").matcher(str);
StringBuilder builder = new StringBuilder(str);
for (int i = 0; matcher.find(); i++) {
builder.replace(matcher.start() + i, matcher.end() + i, "_" + matcher.group().toLowerCase());
}
if (builder.charAt(0) == '_') {
builder.deleteCharAt(0);
}
return builder.toString();
}
/**
* 将下划线风格替换为驼峰风格
*
* @param str
* @return
*/
public static String underlineToCamelhump(String str) {
Matcher matcher = Pattern.compile("_[a-z]").matcher(str);
StringBuilder builder = new StringBuilder(str);
for (int i = 0; matcher.find(); i++) {
builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase());
}
if (Character.isUpperCase(builder.charAt(0))) {
builder.replace(0, 1, String.valueOf(Character.toLowerCase(builder.charAt(0))));
}
return builder.toString();
}
public static String underlineToCamelhumpWithNumber(String str) {
Matcher matcher = Pattern.compile("_[a-z0-9]").matcher(str);
StringBuilder builder = new StringBuilder(str);
for (int i = 0; matcher.find(); i++) {
builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase());
}
if (Character.isUpperCase(builder.charAt(0))) {
builder.replace(0, 1, String.valueOf(Character.toLowerCase(builder.charAt(0))));
}
return builder.toString();
}
public static String getBeanName(String str) {
StringBuilder builder = new StringBuilder(str);
builder.replace(0, 1, String.valueOf(Character.toUpperCase(builder.charAt(0))));
return builder.toString();
}
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
public static String standardized(String str){
if(StringUtils.isEmpty(str)){
return str;
}
return str.trim().toLowerCase().replaceAll(" ", "");
}
public static void main(String[] args) {
System.out.println(isLowerCase('C'));
String s1 = "size_attribute";
System.out.println(underlineToCamelhump(s1));
String s2 = "hserInfo";
System.out.println(getLowerCaseNames(s2));
String s3 = "brand";
System.out.println(getBeanName(s3));
String s4 = "skn_7_days_view";
System.out.println(underlineToCamelhumpWithNumber(s4));
System.out.println(standardized("Vans 卫衣"));
}
}
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>starter</module>
<module>handler</module>
<module>dal</module>
<module>service</module>
</modules>
<groupId>com.yoho.datasync</groupId>
<artifactId>yoho-datasync-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>yoho-datasync-consumer</name>
<properties>
<java.version>1.8</java.version>
<project-version>1.0.0-SNAPSHOT</project-version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.yoho.datasync</groupId>
<artifactId>yoho-datasync-consumer-handler</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.yoho.datasync</groupId>
<artifactId>yoho-datasync-consumer-dal</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>yoho-datasync-consumer</artifactId>
<groupId>com.yoho.datasync</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yoho-datasync-consumer-service</artifactId>
<dependencies>
<dependency>
<groupId>com.yoho.datasync</groupId>
<artifactId>yoho-datasync-consumer-dal</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.dal.entity.TestSync;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class TestSyncController {
@Resource
TestSyncService testSyncService;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
TestSync testSync = new TestSync();
testSync.setCity("test");
testSync.setProvinceId(158);
testSyncService.saveTestSync(testSync);
return "hello world";
}
}
... ...
package com.yoho.datasync.consumer.service;
import com.yoho.datasync.consumer.dal.entity.TestSync;
import com.yoho.datasync.consumer.dal.repository.TestSyncRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class TestSyncService {
@Resource
TestSyncRepository syncRepository;
void saveTestSync(TestSync sync) {
syncRepository.save(sync);
}
}
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>yoho-datasync-consumer</artifactId>
<groupId>com.yoho.datasync</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yoho-datasync-consumer-starter</artifactId>
<dependencies>
<dependency>
<groupId>com.yoho.datasync</groupId>
<artifactId>yoho-datasync-consumer-handler</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.datasync</groupId>
<artifactId>yoho-datasync-consumer-dal</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.datasync</groupId>
<artifactId>yoho-datasync-consumer-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
package com.yoho.datasync.consumer.starter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication(scanBasePackages = "com.yoho.datasync")
@EnableJpaRepositories(basePackages = "com.yoho.datasync.consumer.dal.repository")
@EntityScan(basePackages = "com.yoho.datasync.consumer.dal.entity")
public class YohoDatasyncConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(YohoDatasyncConsumerApplication.class, args);
}
}
... ...
rabbitmq:
server: "192.168.102.216"
port: 5672
username: "admin1"
password: "123qwe"
exchange: "yoho_datasync"
virtualHost: "/datasync"
spring:
datasource:
url: jdbc:mysql://192.168.102.219:3306/line_shops?useUnicode=true&amp;characterEncoding=UTF-8
username: yh_test
password: yh_test
driver-class-name: com.mysql.jdbc.Driver
tableConfigs:
- dbName: "line_shops"
tableName: "test_sync"
primaryKeys:
- id
queueSize: 1
\ No newline at end of file
... ...