-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Consul synchronization to Nacos function for issue #17
- Loading branch information
Showing
7 changed files
with
219 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ssync-worker/src/main/java/com/alibaba/nacossync/extension/holder/ConsulServerHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.alibaba.nacossync.extension.holder; | ||
|
||
import com.ecwid.consul.v1.ConsulClient; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.net.URL; | ||
|
||
/** | ||
* @author paderlol | ||
* @date: 2018-12-31 16:26 | ||
*/ | ||
@Service | ||
@Slf4j | ||
public class ConsulServerHolder extends AbstractServerHolder<ConsulClient> { | ||
|
||
public static final String HTTP = "http://"; | ||
|
||
@Override | ||
ConsulClient createServer(String serverAddress, String namespace) throws Exception { | ||
serverAddress = serverAddress.startsWith(HTTP) ? serverAddress : HTTP + serverAddress; | ||
URL url = new URL(serverAddress); | ||
return new ConsulClient(url.getHost(), url.getPort()); | ||
} | ||
|
||
} |
133 changes: 133 additions & 0 deletions
133
...sync-worker/src/main/java/com/alibaba/nacossync/extension/impl/ConsulSyncServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package com.alibaba.nacossync.extension.impl; | ||
|
||
import com.alibaba.nacos.api.naming.NamingService; | ||
import com.alibaba.nacos.api.naming.pojo.Instance; | ||
import com.alibaba.nacossync.cache.SkyWalkerCacheServices; | ||
import com.alibaba.nacossync.constant.ClusterTypeEnum; | ||
import com.alibaba.nacossync.constant.SkyWalkerConstants; | ||
import com.alibaba.nacossync.extension.SyncService; | ||
import com.alibaba.nacossync.extension.annotation.NacosSyncService; | ||
import com.alibaba.nacossync.extension.holder.ConsulServerHolder; | ||
import com.alibaba.nacossync.extension.holder.NacosServerHolder; | ||
import com.alibaba.nacossync.pojo.model.TaskDO; | ||
import com.ecwid.consul.v1.ConsulClient; | ||
import com.ecwid.consul.v1.QueryParams; | ||
import com.ecwid.consul.v1.Response; | ||
import com.ecwid.consul.v1.health.model.HealthService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* Consul 同步 Nacos | ||
* | ||
* @author paderlol | ||
* @date: 2018-12-31 16:25 | ||
*/ | ||
@Slf4j | ||
@NacosSyncService(clusterType = ClusterTypeEnum.CONSUL) | ||
public class ConsulSyncServiceImpl implements SyncService { | ||
|
||
@Autowired | ||
private ConsulServerHolder consulServerHolder; | ||
@Autowired | ||
private SkyWalkerCacheServices skyWalkerCacheServices; | ||
|
||
@Autowired | ||
private NacosServerHolder nacosServerHolder; | ||
|
||
@Override | ||
public boolean delete(TaskDO taskDO) { | ||
|
||
try { | ||
NamingService destNamingService = nacosServerHolder.get(taskDO.getDestClusterId(), null); | ||
List<Instance> allInstances = destNamingService.getAllInstances(taskDO.getServiceName()); | ||
for (Instance instance : allInstances) { | ||
if (needDelete(instance.getMetadata(), taskDO)) { | ||
destNamingService.deregisterInstance(taskDO.getServiceName(), instance.getIp(), instance.getPort()); | ||
} | ||
} | ||
|
||
} catch (Exception e) { | ||
log.error("delete task from consul to nacos was failed, taskId:{}", taskDO.getTaskId(), e); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean sync(TaskDO taskDO) { | ||
try { | ||
ConsulClient consulClient = consulServerHolder.get(taskDO.getSourceClusterId(), null); | ||
NamingService destNamingService = nacosServerHolder.get(taskDO.getDestClusterId(), null); | ||
Response<List<HealthService>> response = | ||
consulClient.getHealthServices(taskDO.getServiceName(), true, QueryParams.DEFAULT); | ||
List<HealthService> healthServiceList = response.getValue(); | ||
Set<String> instanceKeySet = new HashSet<>(); | ||
for (HealthService healthService : healthServiceList) { | ||
if (needSync(healthService.getNode().getMeta())) { | ||
|
||
destNamingService.registerInstance(taskDO.getServiceName(), | ||
buildSyncInstance(healthService, taskDO)); | ||
instanceKeySet.add(composeInstanceKey(healthService.getService().getAddress(), | ||
healthService.getService().getPort())); | ||
} | ||
} | ||
List<Instance> allInstances = destNamingService.getAllInstances(taskDO.getServiceName()); | ||
for (Instance instance : allInstances) { | ||
if (needDelete(instance.getMetadata(), taskDO) | ||
&& !instanceKeySet.contains(composeInstanceKey(instance.getIp(), instance.getPort()))) { | ||
destNamingService.deregisterInstance(taskDO.getServiceName(), instance.getIp(), instance.getPort()); | ||
} | ||
} | ||
|
||
} catch (Exception e) { | ||
log.error("sync task from consul to nacos was failed, taskId:{}", taskDO.getTaskId(), e); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* 判断当前实例数据是否源集群信息是一致的, 一致才会进行删除 | ||
* | ||
* @param destMetaData | ||
* @param taskDO | ||
* @return | ||
*/ | ||
private boolean needDelete(Map<String, String> destMetaData, TaskDO taskDO) { | ||
return StringUtils.equals(destMetaData.get(SkyWalkerConstants.SOURCE_CLUSTERID_KEY), | ||
taskDO.getSourceClusterId()); | ||
} | ||
|
||
private Instance buildSyncInstance(HealthService instance, TaskDO taskDO) { | ||
Instance temp = new Instance(); | ||
temp.setIp(instance.getService().getAddress()); | ||
temp.setPort(instance.getService().getPort()); | ||
|
||
Map<String, String> metaData = new HashMap<>(instance.getNode().getMeta()); | ||
metaData.put(SkyWalkerConstants.DEST_CLUSTERID_KEY, taskDO.getDestClusterId()); | ||
metaData.put(SkyWalkerConstants.SYNC_SOURCE_KEY, | ||
skyWalkerCacheServices.getClusterType(taskDO.getSourceClusterId()).getCode()); | ||
metaData.put(SkyWalkerConstants.SOURCE_CLUSTERID_KEY, taskDO.getSourceClusterId()); | ||
temp.setMetadata(metaData); | ||
return temp; | ||
} | ||
|
||
/** | ||
* 判断当前实例数据是否是其他地方同步过来的, 如果是则不进行同步操作 | ||
* | ||
* @param sourceMetaData | ||
* @return | ||
*/ | ||
private boolean needSync(Map<String, String> sourceMetaData) { | ||
return StringUtils.isBlank(sourceMetaData.get(SkyWalkerConstants.SOURCE_CLUSTERID_KEY)); | ||
} | ||
|
||
private String composeInstanceKey(String ip, int port) { | ||
return ip + ":" + port; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters