Skip to content

Commit

Permalink
Merge pull request #231 from nacos-group/develop
Browse files Browse the repository at this point in the history
0.4.6
  • Loading branch information
paderlol committed May 16, 2021
2 parents ef4398d + dbc54c0 commit e377b38
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 59 deletions.
2 changes: 1 addition & 1 deletion nacossync-console/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<parent>
<artifactId>nacossync-parent</artifactId>
<groupId>com.alibaba.nacossync</groupId>
<version>0.4.5</version>
<version>0.4.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion nacossync-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>nacossync-parent</artifactId>
<groupId>com.alibaba.nacossync</groupId>
<version>0.4.5</version>
<version>0.4.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
Expand Down
4 changes: 2 additions & 2 deletions nacossync-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<parent>
<artifactId>nacossync-parent</artifactId>
<groupId>com.alibaba.nacossync</groupId>
<version>0.4.5</version>
<version>0.4.6</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand All @@ -39,7 +39,7 @@
<dependency>
<groupId>com.alibaba.nacossync</groupId>
<artifactId>nacossync-worker</artifactId>
<version>0.4.5</version>
<version>0.4.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
55 changes: 41 additions & 14 deletions nacossync-worker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
<parent>
<artifactId>nacossync-parent</artifactId>
<groupId>com.alibaba.nacossync</groupId>
<version>0.4.5</version>
<version>0.4.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacossync-worker</artifactId>
<version>0.4.5</version>
<version>0.4.6</version>
<properties>
<zookeeper.version>3.4.9</zookeeper.version>
<curator.version>4.1.0</curator.version>
<cloud.version>Finchley.SR2</cloud.version>
<cloud.version>2020.0.2</cloud.version>
<mockito.version>1.10.19</mockito.version>
<nacos.client.verison>1.3.1</nacos.client.verison>
<nacos.client.verison>2.0.1</nacos.client.verison>
</properties>
<dependencyManagement>
<dependencies>
Expand All @@ -52,10 +52,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -85,11 +81,7 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>

<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
Expand All @@ -113,6 +105,12 @@
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand All @@ -127,18 +125,46 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>${curator.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>


<!-- consul -->
<dependency>
<groupId>com.ecwid.consul</groupId>
<artifactId>consul-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
</dependencies>
<build>
<finalName>nacosSync-server.${parent.version}</finalName>
Expand All @@ -157,6 +183,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<excludes>
<exclude>application.properties</exclude>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
*/
package com.alibaba.nacossync.cache;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import com.alibaba.nacossync.constant.ClusterTypeEnum;
import com.alibaba.nacossync.dao.ClusterAccessService;
import com.alibaba.nacossync.exception.SkyWalkerException;
import com.alibaba.nacossync.pojo.FinishedTask;
import com.alibaba.nacossync.pojo.model.ClusterDO;
import com.alibaba.nacossync.pojo.model.TaskDO;
import com.alibaba.nacossync.util.SkyWalkerUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.jboss.netty.util.internal.ThreadLocalRandom;
import java.util.concurrent.ThreadLocalRandom;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
Expand All @@ -44,6 +45,9 @@ public class SkyWalkerCacheServices {
@Autowired
private ClusterAccessService clusterAccessService;

@Autowired
private ObjectMapper objectMapper;

private static Map<String, FinishedTask> finishedTaskMap = new ConcurrentHashMap<>();

public String getClusterConnectKey(String clusterId) {
Expand All @@ -52,12 +56,12 @@ public String getClusterConnectKey(String clusterId) {
return allClusterConnectKey.get(ThreadLocalRandom.current().nextInt(allClusterConnectKey.size()));
}

@SneakyThrows
public List<String> getAllClusterConnectKey(String clusterId) {
ClusterDO clusterDO = clusterAccessService.findByClusterId(clusterId);

List<String> connectKeyList = JSONObject.parseObject(clusterDO.getConnectKeyList(),
new TypeReference<List<String>>() {
});
List<String> connectKeyList = objectMapper.readerForListOf(String.class)
.readValue(clusterDO.getConnectKeyList());

if (CollectionUtils.isEmpty(connectKeyList)) {
throw new SkyWalkerException("getClusterConnectKey empty, clusterId:" + clusterId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

/**
* eureka
*
*
* @author paderlol
* @date: 2018-12-31 16:25
*/
Expand Down Expand Up @@ -121,13 +121,17 @@ private void addValidInstance(TaskDO taskDO, NamingService destNamingService, Li
}
}
}

private void deleteAllInstanceFromEureka(TaskDO taskDO, NamingService destNamingService, List<InstanceInfo> eurekaInstances)
throws NacosException {

private void deleteAllInstanceFromEureka(TaskDO taskDO, NamingService destNamingService,
List<InstanceInfo> eurekaInstances)
throws NacosException {
if (CollectionUtils.isEmpty(eurekaInstances)) {
return;
}
for (InstanceInfo instance : eurekaInstances) {
if (needSync(instance.getMetadata())) {
log.info("Delete service instance from Eureka, serviceName={}, Ip={}, port={}",
instance.getAppName(), instance.getIPAddr(), instance.getPort());
instance.getAppName(), instance.getIPAddr(), instance.getPort());
destNamingService.deregisterInstance(taskDO.getServiceName(), buildSyncInstance(instance, taskDO));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package com.alibaba.nacossync.template.processor;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacossync.constant.ClusterTypeEnum;
import com.alibaba.nacossync.dao.ClusterAccessService;
import com.alibaba.nacossync.exception.SkyWalkerException;
Expand All @@ -26,6 +25,7 @@
import com.alibaba.nacossync.pojo.result.ClusterAddResult;
import com.alibaba.nacossync.template.Processor;
import com.alibaba.nacossync.util.SkyWalkerUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -45,6 +45,9 @@ public class ClusterAddProcessor implements Processor<ClusterAddRequest, Cluster
@Autowired
private ClusterAccessService clusterAccessService;

@Autowired
private ObjectMapper objectMapper;

@Override
public void process(ClusterAddRequest clusterAddRequest, ClusterAddResult clusterAddResult,
Object... others) throws Exception {
Expand Down Expand Up @@ -76,7 +79,7 @@ public void process(ClusterAddRequest clusterAddRequest, ClusterAddResult cluste
clusterDO.setClusterId(clusterId);
clusterDO.setClusterName(clusterAddRequest.getClusterName());
clusterDO.setClusterType(clusterAddRequest.getClusterType());
clusterDO.setConnectKeyList(JSONObject.toJSONString(clusterAddRequest.getConnectKeyList()));
clusterDO.setConnectKeyList(objectMapper.writeValueAsString(clusterAddRequest.getConnectKeyList()));
clusterDO.setUserName(clusterAddRequest.getUserName());
clusterDO.setPassword(clusterAddRequest.getPassword());
clusterAccessService.insert(clusterDO);
Expand Down
1 change: 1 addition & 0 deletions nacossync-worker/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.show_sql=false

spring.cloud.discovery.enabled=false


spring.datasource.url=jdbc:mysql://127.0.0.1:3306/nacos_sync?characterEncoding=utf8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.alibaba.nacossync.extension.impl;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.anyBoolean;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacossync.cache.SkyWalkerCacheServices;
Expand All @@ -9,12 +16,14 @@
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.transport.RawResponse;
import com.ecwid.consul.transport.HttpResponse;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.health.model.HealthService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -23,19 +32,14 @@
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.List;
import java.util.Map;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

/**
* @author paderlol
* @date: 2019-01-12 20:58
* @see ConsulSyncToNacosServiceImpl
*/
@RunWith(MockitoJUnitRunner.class)
public class ConsulSyncToNacosServiceImplTest {

public static final String TEST_SOURCE_CLUSTER_ID = "test-source-cluster-id";
public static final String TEST_DEST_CLUSTER_ID = "test-dest-cluster-id";
public static final String TEST_TASK_ID = "test-task-id";
Expand Down Expand Up @@ -78,6 +82,7 @@ public void testConsulDeleteSyncToNacos() throws Exception {
public void testConsulSyncToNacosWithException() throws Exception {
Assert.assertFalse(consulSyncToNacosService.sync(null));
}

@Test(expected = Exception.class)
public void testConsulDeleteToNacosWithException() throws Exception {
Assert.assertFalse(consulSyncToNacosService.delete(null));
Expand All @@ -89,22 +94,22 @@ public void mockSync(TaskDO taskDO) throws Exception {
metadata.put(SkyWalkerConstants.SOURCE_CLUSTERID_KEY, TEST_SOURCE_CLUSTER_ID);
HealthService healthServiceUp = buildHealthService(TEST_INSTANCE_ADDRESS, 8080, Maps.newHashMap());
HealthService healthServiceDown = buildHealthService(TEST_INSTANCE_ADDRESS, 8081, metadata);
List<HealthService> healthServiceList = Lists.newArrayList(healthServiceUp,healthServiceDown);
RawResponse rawResponse = new RawResponse(200,null,null,1000L,true,100L);
Response<List<HealthService>> response = new Response<>(healthServiceList,rawResponse);
List<HealthService> healthServiceList = Lists.newArrayList(healthServiceUp, healthServiceDown);
HttpResponse rawResponse = new HttpResponse(200, null, null, 1000L, true, 100L);
Response<List<HealthService>> response = new Response<>(healthServiceList, rawResponse);
when(taskDO.getTaskId()).thenReturn(TEST_TASK_ID);
when(taskDO.getSourceClusterId()).thenReturn(TEST_SOURCE_CLUSTER_ID);
when(taskDO.getDestClusterId()).thenReturn(TEST_DEST_CLUSTER_ID);
doReturn(destNamingService).when(nacosServerHolder).get(anyString(), any());
doReturn(consulClient).when(consulServerHolder).get(anyString(), any());
doReturn(response).when(consulClient).getHealthServices(anyString(),anyBoolean(), any());
doReturn(response).when(consulClient).getHealthServices(anyString(), anyBoolean(), any());
List<Instance> allInstances = Lists.newArrayList(instance);
doReturn(allInstances).when(destNamingService).getAllInstances(anyString());
doReturn(ClusterTypeEnum.EUREKA).when(skyWalkerCacheServices).getClusterType(any());

}

private HealthService buildHealthService(String address,int port,Map<String,String> metadata) {
private HealthService buildHealthService(String address, int port, Map<String, String> metadata) {
HealthService healthService = new HealthService();
HealthService.Node node = new HealthService.Node();
node.setMeta(metadata);
Expand Down
Loading

0 comments on commit e377b38

Please sign in to comment.