Skip to content

Commit

Permalink
1. 优化部分代码 (#354)
Browse files Browse the repository at this point in the history
* Optimize the code for assigning tasks.

* Adds prefix to the input string if it doesn't already have it.#308

* Fix .#305

* Fix cyclic dependency code.

* Refactoring the Nacos Sync to Consul Logic.

* 1. 重新设计全量 Nacos 同步 Nacos
2. 修复Nacos Instance equals无效导致出现无法注册成功问题
3. 升级Nacos Sync JDK/Spring Boot版本
4. 保底同步从改成并发同步
5. 增加部分注释

* 1. 优化部分代码
  • Loading branch information
paderlol authored Jun 10, 2024
1 parent 5789d4d commit 49abb7c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
@Service
public class SpecialSyncEventBus {
private ConcurrentHashMap<String, SpecialSyncEvent> specialSyncEventRegistry = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, SpecialSyncEvent> specialSyncEventRegistry = new ConcurrentHashMap<>();

public void subscribe(TaskDO taskDO, Consumer<TaskDO> syncAction) {
SpecialSyncEvent specialSyncEvent = new SpecialSyncEvent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
Expand All @@ -34,9 +33,12 @@
@Service
@Slf4j
public class SpecialSyncEventListener {
@Autowired
private EventBus eventBus;

private final EventBus eventBus;

public SpecialSyncEventListener(EventBus eventBus) {
this.eventBus = eventBus;
}

@PostConstruct
public void init() {
eventBus.register(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
*/
@Slf4j
public abstract class AbstractServerHolderImpl<T> implements Holder<T> {

protected final Map<String, T> serviceMap = new ConcurrentHashMap<>();
private final Map<String, T> serviceMap = new ConcurrentHashMap<>();

@Autowired
protected SkyWalkerCacheServices skyWalkerCacheServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ public static void batchOperation(List<TaskDO> items, Consumer<TaskDO> operation

log.debug("Total sync tasks: {}, Execution time: {} ms", items.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS));
}



/**
* 将一个List均分成n个list, 主要通过偏移量来实现的
*
* @param source 源集合
* @param limit 最大值
* @return 均分后的列表
* // Divide a list into n sublists, mainly implemented by offset
* @param source collection to be divided
* @param limit maximum value
* @return list after division
* @param <T> object type
*/
private static <T> List<Tuple<Integer, List<T>>> averageAssign(List<T> source, int limit) {
if (CollectionUtils.isEmpty(source)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import com.alibaba.nacossync.pojo.request.TaskAddRequest;
import com.google.common.base.Joiner;

import java.io.UnsupportedEncodingException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;
Expand All @@ -37,33 +37,27 @@
* @version $Id: SkyWalkerUtil.java, v 0.1 2018-09-26 AM12:10 NacosSync Exp $$
*/
public class SkyWalkerUtil {


private static final String SEPARATOR = ":";

/**
*
* Gets the string md5
* @param value
* @return
* @param value The string to be encrypted
* @return The encrypted string
*/
public static String StringToMd5(String value) {
{
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(value.getBytes("UTF-8"));
byte[] encryption = md5.digest();
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < encryption.length; i++) {
if (Integer.toHexString(0xff & encryption[i]).length() == 1) {
strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
} else {
strBuf.append(Integer.toHexString(0xff & encryption[i]));
}
}
return strBuf.toString();
} catch (NoSuchAlgorithmException e) {
return "";
} catch (UnsupportedEncodingException e) {
return "";
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(value.getBytes(StandardCharsets.UTF_8));
byte[] encryption = md5.digest();
StringBuilder strBuf = new StringBuilder();
for (byte b : encryption) {
strBuf.append(String.format("%02x", b));
}
return strBuf.toString();
} catch (NoSuchAlgorithmException e) {
return "";
}
}

Expand Down Expand Up @@ -141,7 +135,7 @@ public static String getLocalIp() throws Exception {

public static String generateSyncKey(ClusterTypeEnum sourceClusterType, ClusterTypeEnum destClusterType) {

return Joiner.on(":").join(sourceClusterType.getCode(), destClusterType.getCode());
return Joiner.on(SEPARATOR).join(sourceClusterType.getCode(), destClusterType.getCode());
}

public static String getOperationId(TaskDO taskDO) {
Expand All @@ -153,4 +147,8 @@ public static String generateOperationId() {

return UUID.randomUUID().toString();
}




}

0 comments on commit 49abb7c

Please sign in to comment.