diff --git a/README.md b/README.md index 4e971eb8..37b10978 100644 --- a/README.md +++ b/README.md @@ -47,3 +47,104 @@ Info | +------------+ ^ - Web Console: http://127.0.0.1:8081/ - Others: TBD +# NacosSync Migration User Guide + +## Manual Goal + +- Start the NacosSync service +- Use a simple example to demonstrate how to migrate a Dubbo client registered in the Zookeeper Registry to the Nacos Registry + +## Prerequisites + +Before you begin, install the following: + +- 64bit OS: Linux/Unix/Mac/Windows supported, Linux/Unix/Mac recommended. +- 64bit JDK 1.8+: downloads, JAVA_HOME settings. +- Maven 3.2.x+: downloads, settings. +- MySql 5.6.+ + +## Download & Build From Release + +There are two ways to get NacosSync. + +- Download run package +- Download source code from Github + +``` xml + +cd nacosSync/ +mvn clean package -U + +``` + +The path to the target file: + +``` xml + +nacos-sync/nacossync-distribution/target/nacosSync.0.1.0.zip + +``` + +After extracting the installation package, the directory structure: + +``` xml + +nacosSync +├── LICENSE +├── NOTICE +├── bin +│   ├── nacosSync.sql +│   ├── shutdown.sh +│   └── startup.sh +├── conf +│   ├── application.properties +│   └── logback-spring.xml +├── logs +└── nacosSync-server.0.1.0.jar + +``` + +## Initialize The DB + +The default is Mysql database, which can support other relational databases + +- Build db schema, the default schema name nacos_Sync. +- Tables do not need to be created separately, which is conducive to hibernate's automatic table creation function. +- If the automatic table creation fails, you can build the table nacosSync.sql, the table statement is in the bin folder. + +## DB Configuration + +In the bin folder, application.properties: + +``` xml + +spring.datasource.url=jdbc:mysql://127.0.0.1:3306/nacos_sync?characterEncoding=utf8 +spring.datasource.username=root +spring.datasource.password=root + +``` + +## Start Server + +``` xml + +$ nacosSync/bin: +sh startup.sh restart + +``` + +## Admin Console + +``` xml + +http://127.0.0.1:8081/#/serviceSync + +``` + + + + + + + + diff --git a/nacossync-console/pom.xml b/nacossync-console/pom.xml index a53190ad..0a49f194 100644 --- a/nacossync-console/pom.xml +++ b/nacossync-console/pom.xml @@ -16,7 +16,7 @@ nacossync-parent com.alibaba.nacossync - 0.2.0 + 0.3.0 4.0.0 diff --git a/nacossync-distribution/pom.xml b/nacossync-distribution/pom.xml index 25486e13..537e6dfa 100644 --- a/nacossync-distribution/pom.xml +++ b/nacossync-distribution/pom.xml @@ -5,7 +5,7 @@ nacossync-parent com.alibaba.nacossync - 0.2.0 + 0.3.0 4.0.0 pom diff --git a/nacossync-test/pom.xml b/nacossync-test/pom.xml index 7705f125..795d65eb 100644 --- a/nacossync-test/pom.xml +++ b/nacossync-test/pom.xml @@ -17,7 +17,7 @@ nacossync-parent com.alibaba.nacossync - 0.2.0 + 0.3.0 ../pom.xml 4.0.0 @@ -40,12 +40,27 @@ com.alibaba.nacossync nacossync-worker + 0.3.0 - org.springframework.boot spring-boot-test - 2.0.4.RELEASE + + + + org.springframework + spring-test + 5.1.0.RC3 + + + junit + junit + + + com.ning + async-http-client + 1.7.17 + test diff --git a/nacossync-test/src/test/java/com/alibaba/nacossync/RestApiTest.java b/nacossync-test/src/test/java/com/alibaba/nacossync/RestApiTest.java new file mode 100644 index 00000000..b6935cd4 --- /dev/null +++ b/nacossync-test/src/test/java/com/alibaba/nacossync/RestApiTest.java @@ -0,0 +1,566 @@ +//package com.alibaba.nacossync; +// +//import java.util.HashMap; +//import java.util.Map; +// +//import com.alibaba.fastjson.JSON; +//import com.alibaba.fastjson.JSONArray; +//import com.alibaba.fastjson.JSONObject; +//import com.alibaba.nacossync.util.HttpClient; +//import com.alibaba.nacossync.util.HttpClient.HttpResult; +// +//import org.apache.http.HttpStatus; +//import org.junit.After; +//import org.junit.Assert; +//import org.junit.Before; +//import org.junit.Test; +//import org.junit.runner.RunWith; +//import org.springframework.boot.web.server.LocalServerPort; +//import org.springframework.test.context.junit4.SpringRunner; +// +// +///** +// * Created by mingyi.xxc +// * Date: 2019/2/23 +// * Time: 下午7:20 +// * DESC: +// * +// * @author mingyi.xxc +// * @date 2019/02/23 +// */ +//@RunWith(SpringRunner.class) +////@SpringBootTest(classes = NacosSyncMain.class, properties = {"server.servlet.context-path=/", +//// "server.port=8081"}, +//// webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +//public class RestApiTest { +// +// private String baseUrl; +// +// @LocalServerPort +// private int port; +// +// @Before +// public void setUp() throws Exception { +// this.baseUrl = String.format("http://localhost:%d", port); +// } +// +// @After +// public void cleanup() throws Exception { +// +// } +// +// @Test +// public void addCluster() { +// String url = baseUrl + "/v1/cluster/add"; +// +// JSONObject clusterJson = new JSONObject(); +// clusterJson.put("clusterName", "CI-Nacos-Test" + System.currentTimeMillis()); +// clusterJson.put("clusterType", "NACOS"); +// +// JSONArray jsonArray = new JSONArray(); +// jsonArray.add("11.11.11.11"); +// jsonArray.add("22.22.22.22"); +// clusterJson.put("connectKeyList", jsonArray); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpPost(url, clusterJson.toJSONString()); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertTrue(JSON.parseObject(result.content).getBoolean("success")); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void addClusterNotHaveClusterType() { +// String url = baseUrl + "/v1/cluster/add"; +// +// JSONObject clusterJson = new JSONObject(); +// clusterJson.put("clusterName", "CI-Nacos-Test" + System.currentTimeMillis()); +// +// JSONArray jsonArray = new JSONArray(); +// jsonArray.add("11.11.11.11"); +// jsonArray.add("22.22.22.22"); +// clusterJson.put("connectKeyList", jsonArray); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpPost(url, clusterJson.toJSONString()); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertFalse(JSON.parseObject(result.content).getBoolean("success")); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void deleteCluster() { +// String url = baseUrl + "/v1/cluster/list"; +// String clusterId = ""; +// try { +// Map paramValues = new HashMap<>(); +// paramValues.put("pageNum", "1"); +// paramValues.put("pageSize", "50"); +// +// HttpResult result = HttpClient.httpGet(url, null, paramValues); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// JSONArray clusterModels = JSON.parseObject(result.content).getJSONArray("clusterModels"); +// if (clusterModels.size() > 0) { +// for (int i=0; i(); +// paramValues.put("clusterId", clusterId); +// +// result = HttpClient.httpDelete(url, paramValues, null, "UTF-8"); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertTrue(JSON.parseObject(result.content).getBoolean("success")); +// System.out.println(result.content); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// /** +// * delete clusterId不存在 +// */ +// @Test +// public void deleteClusterByClusterId() { +// String clusterId = ""; +// try { +// String url = baseUrl + "/v1/cluster/delete"; +// Map paramValues = new HashMap<>(); +// paramValues.put("clusterId", clusterId); +// +// HttpResult result = HttpClient.httpDelete(url, paramValues, null, "UTF-8"); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertTrue(JSON.parseObject(result.content).getBoolean("success")); +// System.out.println(result.content); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void getClusterDetails() { +// addCluster(); +// +// String clusterId = ""; +// String url = baseUrl + "/v1/cluster/list"; +// try { +// Map paramValues = new HashMap<>(); +// paramValues.put("pageNum", "1"); +// paramValues.put("pageSize", "50"); +// +// HttpResult result = HttpClient.httpGet(url, null, paramValues); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// JSONArray clusterModels = JSON.parseObject(result.content).getJSONArray("clusterModels"); +// if (clusterModels.size() > 0) { +// for (int i=0; i(); +// paramValues.put("clusterId", clusterId); +// +// result = HttpClient.httpGet(url, null, paramValues); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertTrue(JSON.parseObject(result.content).getBoolean("success")); +// Assert.assertEquals(clusterId, JSON.parseObject(result.content).getJSONObject("clusterModel").getString("clusterId")); +// System.out.println(result.content); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void getClusterList() { +// String url = baseUrl + "/v1/cluster/list"; +// try { +// Map paramValues = new HashMap<>(); +// paramValues.put("pageNum", "1"); +// paramValues.put("pageSize", "10"); +// +// HttpResult result = HttpClient.httpGet(url, null, paramValues); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// System.out.println(result.content); +// Assert.assertTrue(JSON.parseObject(result.content).getJSONArray("clusterModels").size() >= 0); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void getClusterListByClusterName() { +// String url = baseUrl + "/v1/cluster/list"; +// try { +// Map paramValues = new HashMap<>(); +// paramValues.put("pageNum", "1"); +// paramValues.put("pageSize", "10"); +// paramValues.put("clusterName", "-CI"); +// +// HttpResult result = HttpClient.httpGet(url, null, paramValues); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertTrue(JSON.parseObject(result.content).getJSONArray("clusterModels").size() == 0); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void getClusterTypes() { +// String url = baseUrl + "/v1/cluster/types"; +// try { +// HttpResult result = HttpClient.httpGet(url, null, null); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// System.out.println(result.content); +// Assert.assertTrue(JSON.parseObject(result.content).getJSONArray("types").contains("NACOS")); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void addSystemConfig() { +// String url = baseUrl + "/v1/systemconfig/add"; +// +// JSONObject clusterJson = new JSONObject(); +// clusterJson.put("config_desc", "test"); +// clusterJson.put("config_key", "key"); +// clusterJson.put("config_value", "value"); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpPost(url, clusterJson.toJSONString()); +// +// System.out.println(result.content); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertTrue(JSON.parseObject(result.content).getBoolean("success")); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void deleteSystemConfig() { +// String url = baseUrl + "/v1/systemconfig/delete"; +// Map paramValues = new HashMap<>(); +// paramValues.put("config_desc", "test"); +// paramValues.put("config_key", "key"); +// paramValues.put("config_value", "value"); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpDelete(url, paramValues, null, "UTF-8"); +// //Assert.assertEquals(HttpStatus.SC_OK, result.code); +// //Assert.assertTrue(JSON.parseObject(result.content).getBoolean("success")); +// System.out.println(result.content); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void getSystemConfigList() { +// String url = baseUrl + "/v1/systemconfig/list"; +// try { +// HttpResult result = HttpClient.httpGet(url, null, null); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// System.out.println(result.content); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void addTask() { +// addCluster("EUREKA"); +// addCluster("NACOS"); +// String destClusterId = getClusterId("NACOS"); +// String sourceClusterId = getClusterId("EUREKA"); +// +// String url = baseUrl + "/v1/task/add"; +// +// JSONObject clusterJson = new JSONObject(); +// clusterJson.put("destClusterId", destClusterId); +// clusterJson.put("groupName", "eureka"); +// clusterJson.put("nameSpace", "CI-test"); +// clusterJson.put("serviceName", "CI-Nacos-Service" + System.currentTimeMillis()); +// clusterJson.put("sourceClusterId", sourceClusterId); +// clusterJson.put("version", "1.0.0"); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpPost(url, clusterJson.toJSONString()); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertTrue(JSON.parseObject(result.content).getBoolean("success")); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void deleteTask() { +// String url = baseUrl + "/v1/task/delete"; +// Map paramValues = new HashMap<>(); +// paramValues.put("taskId", getTaskId()); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpDelete(url, paramValues, null, "UTF-8"); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertTrue(JSON.parseObject(result.content).getBoolean("success")); +// System.out.println(result.content); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void deleteTaskByTaskId() { +// String url = baseUrl + "/v1/task/delete"; +// Map paramValues = new HashMap<>(); +// paramValues.put("taskId", ""); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpDelete(url, paramValues, null, "UTF-8"); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertTrue(JSON.parseObject(result.content).getBoolean("success")); +// System.out.println(result.content); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void getTaskDetail() { +// String taskId = getTaskId(); +// String url = baseUrl + "/v1/task/detail"; +// Map paramValues = new HashMap<>(); +// paramValues.put("taskId", taskId); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpGet(url, null, paramValues); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertTrue(JSON.parseObject(result.content).getBoolean("success")); +// Assert.assertEquals(taskId, JSON.parseObject(result.content).getJSONObject("taskModel").getString("taskId")); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// /** +// * taskId为空, 必须存在taskId +// */ +// @Test +// public void getTaskDetailWithTaskId() { +// String taskId = ""; +// String url = baseUrl + "/v1/task/detail"; +// Map paramValues = new HashMap<>(); +// paramValues.put("taskId", taskId); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpGet(url, null, paramValues); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// System.out.print(result.content); +// Assert.assertFalse(JSON.parseObject(result.content).getBoolean("success")); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void getTaskList() { +// String url = baseUrl + "/v1/task/list"; +// try { +// Map paramValues = new HashMap<>(); +// paramValues.put("pageNum", "1"); +// paramValues.put("pageSize", "10"); +// +// HttpResult result = HttpClient.httpGet(url, null, paramValues); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// +// Assert.assertTrue(JSON.parseObject(result.content).getJSONArray("taskModels").size() >= 0); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void updateTask() { +// String url = baseUrl + "/v1/task/update"; +// String taskId = getTaskId(); +// JSONObject clusterJson = new JSONObject(); +// clusterJson.put("taskId", taskId); +// clusterJson.put("taskStatus", "DELETE"); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpPost(url, clusterJson.toJSONString()); +// System.out.println(result.content); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertTrue(JSON.parseObject(result.content).getBoolean("success")); +// JSONObject taskDetail = getTaskDetailByTaskId(taskId); +// Assert.assertEquals("DELETE", taskDetail.getString("taskStatus")); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// @Test +// public void updateTaskWithStatusSync() { +// String url = baseUrl + "/v1/task/update"; +// JSONObject clusterJson = new JSONObject(); +// clusterJson.put("taskId", getTaskId()); +// clusterJson.put("taskStatus", "SYNC"); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpPost(url, clusterJson.toJSONString()); +// System.out.println(result.content); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertTrue(JSON.parseObject(result.content).getBoolean("success")); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// /** +// * update中必须包含taskStatus +// */ +// @Test +// public void updateTaskWithServiceName() { +// String url = baseUrl + "/v1/task/update"; +// JSONObject clusterJson = new JSONObject(); +// clusterJson.put("taskId", getTaskId()); +// String serviceName = "CI-Nacos-Service" + System.currentTimeMillis(); +// clusterJson.put("serviceName", serviceName); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpPost(url, clusterJson.toJSONString()); +// System.out.println(result.content); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertFalse(JSON.parseObject(result.content).getBoolean("success")); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// private void addCluster(String clusterType) { +// String url = baseUrl + "/v1/cluster/add"; +// +// JSONObject clusterJson = new JSONObject(); +// clusterJson.put("clusterName", "CI-Nacos-Test" + System.currentTimeMillis()); +// clusterJson.put("clusterType", clusterType); +// +// JSONArray jsonArray = new JSONArray(); +// jsonArray.add("11.11.11.11"); +// jsonArray.add("22.22.22.22"); +// clusterJson.put("connectKeyList", jsonArray); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpPost(url, clusterJson.toJSONString()); +// Assert.assertEquals(HttpStatus.SC_OK, result.code); +// Assert.assertTrue(JSON.parseObject(result.content).getBoolean("success")); +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// Assert.assertTrue("error", false); +// } +// } +// +// private String getClusterId(String clusterType) { +// String clusterId = ""; +// String url = baseUrl + "/v1/cluster/list"; +// try { +// Map paramValues = new HashMap<>(); +// paramValues.put("pageNum", "1"); +// paramValues.put("pageSize", "50"); +// +// HttpResult result = HttpClient.httpGet(url, null, paramValues); +// JSONArray clusterModels = JSON.parseObject(result.content).getJSONArray("clusterModels"); +// if (clusterModels.size() > 0) { +// for (int i=0; i paramValues = new HashMap<>(); +// paramValues.put("pageNum", "1"); +// paramValues.put("pageSize", "10"); +// +// HttpResult result = HttpClient.httpGet(url, null, paramValues); +// JSONArray taskModels = JSON.parseObject(result.content).getJSONArray("taskModels"); +// +// if (taskModels.size() > 0) { +// for (int i=0; i paramValues = new HashMap<>(); +// paramValues.put("taskId", taskId); +// +// try { +// HttpClient.HttpResult result = HttpClient.httpGet(url, null, paramValues); +// if (HttpStatus.SC_OK == result.code) { +// return JSON.parseObject(result.content).getJSONObject("taskModel"); +// } +// } catch (Exception e) { +// System.out.println(e.getMessage()); +// return null; +// } +// return null; +// } +// +//} diff --git a/nacossync-test/src/test/java/com/alibaba/nacossync/StarterTest.java b/nacossync-test/src/test/java/com/alibaba/nacossync/StarterTest.java index 0e1bf560..de97c95f 100644 --- a/nacossync-test/src/test/java/com/alibaba/nacossync/StarterTest.java +++ b/nacossync-test/src/test/java/com/alibaba/nacossync/StarterTest.java @@ -16,20 +16,14 @@ package com.alibaba.nacossync; import org.junit.Before; -import org.junit.Test; import org.junit.runner.RunWith; - -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; - /** * @author nkorange */ @RunWith(SpringRunner.class) -//@SpringBootTest(classes = com.alibaba.nacossync.NacosSyncMain.class, -// webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class StarterTest { @Before @@ -37,8 +31,5 @@ public void init() throws Exception { } - @Test - public void test() { - System.out.println("======================="); - } + } \ No newline at end of file diff --git a/nacossync-worker/pom.xml b/nacossync-worker/pom.xml index d051b1b1..9955a9a1 100644 --- a/nacossync-worker/pom.xml +++ b/nacossync-worker/pom.xml @@ -16,10 +16,11 @@ nacossync-parent com.alibaba.nacossync - 0.2.0 + 0.3.0 4.0.0 nacossync-worker + 0.3.0 3.4.9 4.1.0 @@ -38,6 +39,14 @@ + + org.springframework.boot + spring-boot-starter-actuator + + + io.micrometer + micrometer-registry-prometheus + org.springframework.boot spring-boot-starter-web @@ -45,7 +54,6 @@ org.springframework.boot spring-boot-starter-logging - junit diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/cache/SkyWalkerCacheServices.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/cache/SkyWalkerCacheServices.java index 60de231a..a1f760b8 100644 --- a/nacossync-worker/src/main/java/com/alibaba/nacossync/cache/SkyWalkerCacheServices.java +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/cache/SkyWalkerCacheServices.java @@ -84,7 +84,6 @@ public void addFinishedTask(TaskDO taskDO) { finishedTask.setOperationId(operationId); finishedTaskMap.put(operationId, finishedTask); - } public FinishedTask getFinishedTask(TaskDO taskDO) { @@ -96,7 +95,6 @@ public FinishedTask getFinishedTask(TaskDO taskDO) { } return finishedTaskMap.get(operationId); - } public Map getFinishedTaskMap() { diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/constant/MetricsStatisticsType.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/constant/MetricsStatisticsType.java new file mode 100644 index 00000000..d19db043 --- /dev/null +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/constant/MetricsStatisticsType.java @@ -0,0 +1,36 @@ +/** + * Alipay.com Inc. Copyright (c) 2004-2019 All Rights Reserved. + */ +package com.alibaba.nacossync.constant; + +/** + * @author NacosSync + * @version $Id: MetricsStatisticsType.java, v 0.1 2019年02月28日 下午2:17 NacosSync Exp $ + */ +public enum MetricsStatisticsType { + + CACHE_SIZE("nacosSync.finished.taskMap.cacheSize", "任务执行完成缓存列表数"), + + TASK_SIZE("nacosSync.task.size", "同步任务数"), + + CLUSTER_SIZE("nacosSync.cluster.size", "集群数"), + + SYNC_TASK_RT("nacosSync.add.task.rt", "同步任务执行耗时"), + + DELETE_TASK_RT("nacosSync.delete.task.rt", "同步任务执行耗时"); + + /** + * metricsName + */ + private String metricsName; + private String desc; + + MetricsStatisticsType(String code, String desc) { + this.metricsName = code; + this.desc = desc; + } + + public String getMetricsName() { + return metricsName; + } +} \ No newline at end of file diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/event/listener/EventListener.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/event/listener/EventListener.java index a444b2ee..b72e638a 100644 --- a/nacossync-worker/src/main/java/com/alibaba/nacossync/event/listener/EventListener.java +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/event/listener/EventListener.java @@ -18,6 +18,8 @@ import javax.annotation.PostConstruct; +import com.alibaba.nacossync.constant.MetricsStatisticsType; +import com.alibaba.nacossync.monitor.MetricsManager; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -39,10 +41,13 @@ public class EventListener { @Autowired - private SyncManagerService syncManagerService; + private MetricsManager metricsManager; @Autowired - private EventBus eventBus; + private SyncManagerService syncManagerService; + + @Autowired + private EventBus eventBus; @Autowired private SkyWalkerCacheServices skyWalkerCacheServices; @@ -56,10 +61,10 @@ public void register() { public void listenerSyncTaskEvent(SyncTaskEvent syncTaskEvent) { try { - + long start = System.currentTimeMillis(); syncManagerService.sync(syncTaskEvent.getTaskDO()); skyWalkerCacheServices.addFinishedTask(syncTaskEvent.getTaskDO()); - + metricsManager.record(MetricsStatisticsType.SYNC_TASK_RT, System.currentTimeMillis() - start); } catch (Exception e) { log.warn("listenerSyncTaskEvent process error", e); } @@ -70,10 +75,10 @@ public void listenerSyncTaskEvent(SyncTaskEvent syncTaskEvent) { public void listenerDeleteTaskEvent(DeleteTaskEvent deleteTaskEvent) { try { - + long start = System.currentTimeMillis(); syncManagerService.delete(deleteTaskEvent.getTaskDO()); skyWalkerCacheServices.addFinishedTask(deleteTaskEvent.getTaskDO()); - + metricsManager.record(MetricsStatisticsType.DELETE_TASK_RT, System.currentTimeMillis() - start); } catch (Exception e) { log.warn("listenerDeleteTaskEvent process error", e); } diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/extension/impl/NacosSyncToZookeeperServiceImpl.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/extension/impl/NacosSyncToZookeeperServiceImpl.java index db1863f1..0f89c6a6 100644 --- a/nacossync-worker/src/main/java/com/alibaba/nacossync/extension/impl/NacosSyncToZookeeperServiceImpl.java +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/extension/impl/NacosSyncToZookeeperServiceImpl.java @@ -16,6 +16,7 @@ import com.alibaba.nacos.api.naming.listener.EventListener; import com.alibaba.nacos.api.naming.listener.NamingEvent; import com.alibaba.nacos.api.naming.pojo.Instance; +import com.alibaba.nacos.client.naming.utils.CollectionUtils; import com.alibaba.nacossync.cache.SkyWalkerCacheServices; import com.alibaba.nacossync.constant.ClusterTypeEnum; import com.alibaba.nacossync.constant.SkyWalkerConstants; @@ -25,10 +26,8 @@ import com.alibaba.nacossync.extension.holder.ZookeeperServerHolder; import com.alibaba.nacossync.pojo.model.TaskDO; import com.alibaba.nacossync.util.DubboConstants; -import com.google.common.base.Joiner; import com.google.common.collect.Sets; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.recipes.cache.PathChildrenCache; import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; @@ -36,15 +35,13 @@ import org.apache.zookeeper.CreateMode; import org.springframework.beans.factory.annotation.Autowired; -import java.io.File; import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.ConcurrentHashMap; -import static com.alibaba.nacossync.util.DubboConstants.DUBBO_URL_FORMAT; -import static com.alibaba.nacossync.util.DubboConstants.PROTOCOL_KEY; +import static com.alibaba.nacossync.util.StringUtils.convertDubboFullPathForZk; +import static com.alibaba.nacossync.util.StringUtils.convertDubboProvidersPath; /** * Nacos 同步 Zk 数据 @@ -55,21 +52,25 @@ @Slf4j @NacosSyncService(sourceCluster = ClusterTypeEnum.NACOS, destinationCluster = ClusterTypeEnum.ZK) public class NacosSyncToZookeeperServiceImpl implements SyncService { - private static final String SEPARATOR_CHARS = ":"; - private static final int SEGMENT_LENGTH = 2; + /** * @description The Nacos listener map. */ - private Map nacosListenerMap = new ConcurrentHashMap<>(); + private final Map nacosListenerMap = new ConcurrentHashMap<>(); /** * instance backup */ - private Map> instanceBackupMap = new ConcurrentHashMap<>(); + private final Map> instanceBackupMap = new ConcurrentHashMap<>(); + + /** + * listener cache of zookeeper format: taskId -> PathChildrenCache instance + */ + private final Map pathChildrenCacheMap = new ConcurrentHashMap<>(); /** - * 存放zk监听缓存 格式taskId -> PathChildrenCache实例 + * zookeeper path for dubbo providers */ - private Map pathChildrenCacheMap = new ConcurrentHashMap<>(); + private final Map monitorPath = new ConcurrentHashMap<>(); /** * @description The Sky walker cache services. */ @@ -83,7 +84,8 @@ public class NacosSyncToZookeeperServiceImpl implements SyncService { private final ZookeeperServerHolder zookeeperServerHolder; @Autowired - public NacosSyncToZookeeperServiceImpl(SkyWalkerCacheServices skyWalkerCacheServices, NacosServerHolder nacosServerHolder, ZookeeperServerHolder zookeeperServerHolder) { + public NacosSyncToZookeeperServiceImpl(SkyWalkerCacheServices skyWalkerCacheServices, + NacosServerHolder nacosServerHolder, ZookeeperServerHolder zookeeperServerHolder) { this.skyWalkerCacheServices = skyWalkerCacheServices; this.nacosServerHolder = nacosServerHolder; this.zookeeperServerHolder = zookeeperServerHolder; @@ -145,22 +147,25 @@ public boolean sync(TaskDO taskDO) { } // 替换当前备份为最新备份 instanceBackupMap.put(taskDO.getTaskId(), newInstanceUrlSet); - getPathCache(taskDO).getListenable().addListener((zkClient, zkEvent) -> { - - if (zkEvent.getType() == PathChildrenCacheEvent.Type.CHILD_REMOVED) { - List allInstances = - sourceNamingService.getAllInstances(taskDO.getServiceName()); - for (Instance instance : allInstances) { - String instanceUrl = buildSyncInstance(instance, taskDO); - String zkInstancePath = zkEvent.getData().getPath(); - if (zkInstancePath.equals(instanceUrl)) { - zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL) - .forPath(zkInstancePath); - break; + if (!CollectionUtils.isEmpty(sourceInstances)) { + + getPathCache(taskDO).getListenable().addListener((zkClient, zkEvent) -> { + + if (zkEvent.getType() == PathChildrenCacheEvent.Type.CHILD_REMOVED) { + List allInstances = + sourceNamingService.getAllInstances(taskDO.getServiceName()); + for (Instance instance : allInstances) { + String instanceUrl = buildSyncInstance(instance, taskDO); + String zkInstancePath = zkEvent.getData().getPath(); + if (zkInstancePath.equals(instanceUrl)) { + zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL) + .forPath(zkInstancePath); + break; + } } } - } - }); + }); + } } catch (Exception e) { log.error("event process fail, taskId:{}", taskDO.getTaskId(), e); } @@ -185,20 +190,10 @@ protected String buildSyncInstance(Instance instance, TaskDO taskDO) throws Unsu int weight = Integer.parseInt(new DecimalFormat("0").format(Math.ceil(instance.getWeight()))); metaData.put(DubboConstants.WEIGHT_KEY, Integer.toString(weight)); - String servicePath = getServiceInterface(taskDO.getServiceName()); - String urlParam = Joiner.on("&").withKeyValueSeparator("=").join(metaData); - String instanceUrl = - String.format(DUBBO_URL_FORMAT, metaData.get(PROTOCOL_KEY), instance.getIp(), instance.getPort(), urlParam); + String servicePath = monitorPath.computeIfAbsent(taskDO.getTaskId(), + key -> convertDubboProvidersPath(metaData.get(DubboConstants.INTERFACE_KEY))); - return String.join(File.separator, servicePath, URLEncoder.encode(instanceUrl, "UTF-8")); - } - - protected String getServiceInterface(String serviceName) { - String[] segments = StringUtils.split(serviceName, SEPARATOR_CHARS); - if (segments.length < SEGMENT_LENGTH) { - throw new IllegalArgumentException("The length of the split service name must be greater than 2"); - } - return String.format(DubboConstants.DUBBO_PATH_FORMAT, segments[1]); + return convertDubboFullPathForZk(metaData, servicePath, instance.getIp(), instance.getPort()); } /** @@ -210,9 +205,8 @@ protected String getServiceInterface(String serviceName) { private PathChildrenCache getPathCache(TaskDO taskDO) { return pathChildrenCacheMap.computeIfAbsent(taskDO.getTaskId(), (key) -> { try { - PathChildrenCache pathChildrenCache = - new PathChildrenCache(zookeeperServerHolder.get(taskDO.getDestClusterId(), ""), - getServiceInterface(taskDO.getServiceName()), false); + PathChildrenCache pathChildrenCache = new PathChildrenCache( + zookeeperServerHolder.get(taskDO.getDestClusterId(), ""), monitorPath.get(key), false); pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE); return pathChildrenCache; } catch (Exception e) { @@ -223,6 +217,4 @@ private PathChildrenCache getPathCache(TaskDO taskDO) { } - - } diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/extension/impl/ZookeeperSyncToNacosServiceImpl.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/extension/impl/ZookeeperSyncToNacosServiceImpl.java index fb39eb44..96355646 100644 --- a/nacossync-worker/src/main/java/com/alibaba/nacossync/extension/impl/ZookeeperSyncToNacosServiceImpl.java +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/extension/impl/ZookeeperSyncToNacosServiceImpl.java @@ -13,7 +13,6 @@ package com.alibaba.nacossync.extension.impl; import static com.alibaba.nacossync.util.DubboConstants.CATALOG_KEY; -import static com.alibaba.nacossync.util.DubboConstants.DUBBO_PATH_FORMAT; import static com.alibaba.nacossync.util.DubboConstants.GROUP_KEY; import static com.alibaba.nacossync.util.DubboConstants.INSTANCE_IP_KEY; import static com.alibaba.nacossync.util.DubboConstants.INSTANCE_PORT_KEY; @@ -21,6 +20,7 @@ import static com.alibaba.nacossync.util.DubboConstants.PROTOCOL_KEY; import static com.alibaba.nacossync.util.DubboConstants.VERSION_KEY; import static com.alibaba.nacossync.util.DubboConstants.WEIGHT_KEY; +import static com.alibaba.nacossync.util.StringUtils.convertDubboProvidersPath; import static com.alibaba.nacossync.util.StringUtils.parseIpAndPortString; import static com.alibaba.nacossync.util.StringUtils.parseQueryString; @@ -160,16 +160,19 @@ public boolean delete(TaskDO taskDO) { CloseableUtils.closeQuietly(pathChildrenCacheMap.get(taskDO.getTaskId())); NamingService destNamingService = nacosServerHolder .get(taskDO.getDestClusterId(), null); - List allInstances = - destNamingService.getAllInstances(nacosServiceNameMap.get(taskDO.getTaskId())); - for (Instance instance : allInstances) { - if (needDelete(instance.getMetadata(), taskDO)) { - destNamingService - .deregisterInstance(instance.getServiceName(), instance.getIp(), - instance.getPort()); - } - nacosServiceNameMap.remove(taskDO.getTaskId()); + if(nacosServiceNameMap.containsKey(taskDO.getTaskId())){ + List allInstances = + destNamingService + .getAllInstances(nacosServiceNameMap.get(taskDO.getTaskId())); + for (Instance instance : allInstances) { + if (needDelete(instance.getMetadata(), taskDO)) { + destNamingService + .deregisterInstance(instance.getServiceName(), instance.getIp(), + instance.getPort()); + } + nacosServiceNameMap.remove(taskDO.getTaskId()); + } } } catch (Exception e) { @@ -189,7 +192,7 @@ protected PathChildrenCache getPathCache(TaskDO taskDO) { PathChildrenCache pathChildrenCache = new PathChildrenCache( zookeeperServerHolder.get(taskDO.getSourceClusterId(), ""), - monitorPath(taskDO.getServiceName()), false); + convertDubboProvidersPath(taskDO.getServiceName()), false); pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE); return pathChildrenCache; } catch (Exception e) { @@ -201,14 +204,7 @@ protected PathChildrenCache getPathCache(TaskDO taskDO) { } - /** - * create listener path/dubbo/serviceName/providers - * - * @param serviceName service name - */ - private static String monitorPath(String serviceName) { - return String.format(DUBBO_PATH_FORMAT, serviceName); - } + /** * The instance information that needs to be synchronized is matched based on the dubbo version diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/monitor/MetricsManager.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/monitor/MetricsManager.java new file mode 100644 index 00000000..220c6c14 --- /dev/null +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/monitor/MetricsManager.java @@ -0,0 +1,69 @@ +/** + * Alipay.com Inc. Copyright (c) 2004-2019 All Rights Reserved. + */ +package com.alibaba.nacossync.monitor; + +import com.alibaba.nacossync.cache.SkyWalkerCacheServices; +import com.alibaba.nacossync.constant.MetricsStatisticsType; +import com.alibaba.nacossync.dao.ClusterAccessService; +import io.micrometer.core.instrument.Metrics; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Service; + +import java.util.concurrent.TimeUnit; + +/** + * @author NacosSync + * @version $Id: MetricsManager.java, v 0.1 2019年02月25日 上午1:13 NacosSync Exp $ + */ +@Slf4j +@Service +public class MetricsManager implements CommandLineRunner { + + @Autowired + private SkyWalkerCacheServices skyWalkerCacheServices; + + @Autowired + private ClusterAccessService clusterAccessService; + + /** + * Callback used to run the bean. + * + * @param args incoming main method arguments + * @throws Exception on error + */ + @Override + public void run(String... args) throws Exception { + + Metrics.gauge(MetricsStatisticsType.CACHE_SIZE.getMetricsName(), this, + MetricsManager::getCacheSize + ); + Metrics.gauge(MetricsStatisticsType.CLUSTER_SIZE.getMetricsName(), this, + MetricsManager::getClusterSize + ); + Metrics.gauge(MetricsStatisticsType.TASK_SIZE.getMetricsName(), this, + MetricsManager::getTaskSize + ); + + } + + private Integer getClusterSize() { + return clusterAccessService.findPageNoCriteria(1, Integer.MAX_VALUE).getNumber(); + } + + private Integer getTaskSize() { + return clusterAccessService.findPageNoCriteria(1, Integer.MAX_VALUE).getNumber(); + } + + private Integer getCacheSize() { + return skyWalkerCacheServices.getFinishedTaskMap().size(); + } + + public void record(MetricsStatisticsType metricsStatisticsType, long amount) { + + Metrics.timer(metricsStatisticsType.getMetricsName()).record(amount, TimeUnit.MILLISECONDS); + } + +} \ No newline at end of file diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/template/SkyWalkerTemplate.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/template/SkyWalkerTemplate.java index 086ba224..d91223b6 100644 --- a/nacossync-worker/src/main/java/com/alibaba/nacossync/template/SkyWalkerTemplate.java +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/template/SkyWalkerTemplate.java @@ -16,6 +16,7 @@ */ package com.alibaba.nacossync.template; +import com.alibaba.nacossync.monitor.MetricsManager; import lombok.extern.slf4j.Slf4j; import com.alibaba.nacossync.exception.SkyWalkerException; @@ -34,7 +35,6 @@ public static T run(Processor processor, BaseRequest requ try { processor.process(request, result, others); - } catch (Throwable e) { log.error("processor.process error", e); diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/template/processor/ClusterAddProcessor.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/template/processor/ClusterAddProcessor.java index cb8a0335..f4a4a346 100644 --- a/nacossync-worker/src/main/java/com/alibaba/nacossync/template/processor/ClusterAddProcessor.java +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/template/processor/ClusterAddProcessor.java @@ -18,6 +18,7 @@ import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -31,6 +32,8 @@ import com.alibaba.nacossync.template.Processor; import com.alibaba.nacossync.util.SkyWalkerUtil; +import java.util.Collections; + /** * @author NacosSync * @version $Id: ClusterAddProcessor.java, v 0.1 2018-09-30 PM12:22 NacosSync Exp $$ @@ -46,6 +49,15 @@ public void process(ClusterAddRequest clusterAddRequest, ClusterAddResult cluste Object... others) throws Exception { ClusterDO clusterDO = new ClusterDO(); + if (null == clusterAddRequest.getConnectKeyList() || 0 == clusterAddRequest.getConnectKeyList().size()) { + + throw new SkyWalkerException("集群列表不能为空!"); + } + + if (StringUtils.isBlank(clusterAddRequest.getClusterName()) || StringUtils.isBlank(clusterAddRequest.getClusterType())) { + throw new SkyWalkerException("集群名字或者类型不能为空!"); + } + if (!ClusterTypeEnum.contains(clusterAddRequest.getClusterType())) { throw new SkyWalkerException("集群类型不存在:" + clusterAddRequest.getClusterType()); diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/util/DubboConstants.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/util/DubboConstants.java index cb1b006c..d0c291c6 100644 --- a/nacossync-worker/src/main/java/com/alibaba/nacossync/util/DubboConstants.java +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/util/DubboConstants.java @@ -21,9 +21,10 @@ * @date: 2018-12-25 21:34 */ public final class DubboConstants { + public static final char ZOOKEEPER_SEPARATOR='/'; public static final String DUBBO_PATH_FORMAT = - StringUtils.join(new String[] {"/dubbo", "%s", "providers"}, File.separator); - public static final String DUBBO_URL_FORMAT ="%s://%s:%s?%s"; + StringUtils.join(new String[] {"/dubbo", "%s", "providers"}, ZOOKEEPER_SEPARATOR); + public static final String DUBBO_URL_FORMAT ="%s://%s:%s/%s?%s"; public static final String VERSION_KEY = "version"; public static final String GROUP_KEY = "group"; public static final String INTERFACE_KEY = "interface"; diff --git a/nacossync-worker/src/main/java/com/alibaba/nacossync/util/StringUtils.java b/nacossync-worker/src/main/java/com/alibaba/nacossync/util/StringUtils.java index 3156b8ac..9e570045 100644 --- a/nacossync-worker/src/main/java/com/alibaba/nacossync/util/StringUtils.java +++ b/nacossync-worker/src/main/java/com/alibaba/nacossync/util/StringUtils.java @@ -12,11 +12,14 @@ */ package com.alibaba.nacossync.util; +import com.google.common.base.Joiner; import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; +import java.io.File; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; @@ -30,8 +33,11 @@ */ @Slf4j public final class StringUtils { - private static final Pattern KVP_PATTERN = Pattern.compile("([_.a-zA-Z0-9][-_.a-zA-Z0-9]*)[=](.*)"); - private static final Pattern IP_PORT_PATTERN = Pattern.compile(".*/(.*)://(\\d+\\.\\d+\\.\\d+\\.\\d+):(\\d+)"); + + private static final Pattern KVP_PATTERN = Pattern + .compile("([_.a-zA-Z0-9][-_.a-zA-Z0-9]*)[=](.*)"); + private static final Pattern IP_PORT_PATTERN = Pattern + .compile(".*/(.*)://(\\d+\\.\\d+\\.\\d+\\.\\d+):(\\d+)"); /** * parse key-value pair. @@ -89,7 +95,7 @@ public static Map parseIpAndPortString(String path) { String decodePath = URLDecoder.decode(path, "UTF-8"); Matcher matcher = IP_PORT_PATTERN.matcher(decodePath); // extract the ones that match the rules - Map instanceMap = new HashMap<>(); + Map instanceMap = new HashMap<>(3); while (matcher.find()) { // protocol instanceMap.put(PROTOCOL_KEY, matcher.group(1)); @@ -98,7 +104,6 @@ public static Map parseIpAndPortString(String path) { // port instanceMap.put(INSTANCE_PORT_KEY, matcher.group(3)); break; - } return instanceMap; } catch (UnsupportedEncodingException e) { @@ -107,4 +112,26 @@ public static Map parseIpAndPortString(String path) { } } + + public static String convertDubboProvidersPath(String interfaceName) { + return String.format(DUBBO_PATH_FORMAT, interfaceName); + } + + public static String convertDubboFullPathForZk(Map metaData, + String providersPath, String ip, + int port) { + try { + String urlParam = Joiner.on("&").withKeyValueSeparator("=").join(metaData); + String instanceUrl = String + .format(DUBBO_URL_FORMAT, metaData.get(PROTOCOL_KEY), ip, port,metaData.get(INTERFACE_KEY),urlParam); + + return Joiner.on(ZOOKEEPER_SEPARATOR) + .join(providersPath, URLEncoder.encode(instanceUrl, "UTF-8")); + } catch (UnsupportedEncodingException e) { + log.warn("convert Dubbo full path", e); + return ""; + } + + + } } diff --git a/nacossync-worker/src/main/resources/application.properties b/nacossync-worker/src/main/resources/application.properties index 0313521f..a1072cbf 100644 --- a/nacossync-worker/src/main/resources/application.properties +++ b/nacossync-worker/src/main/resources/application.properties @@ -10,4 +10,5 @@ spring.datasource.username=root spring.datasource.password=root - +management.endpoints.web.exposure.include=* +management.endpoint.health.show-details=always diff --git a/nacossync-worker/src/test/java/com/alibaba/nacossync/utils/StringUtilsTest.java b/nacossync-worker/src/test/java/com/alibaba/nacossync/utils/StringUtilsTest.java index a5e3c4a9..58a9c726 100644 --- a/nacossync-worker/src/test/java/com/alibaba/nacossync/utils/StringUtilsTest.java +++ b/nacossync-worker/src/test/java/com/alibaba/nacossync/utils/StringUtilsTest.java @@ -15,8 +15,9 @@ */ public class StringUtilsTest { + private static final String TEST_URL = - "/dubbo/org.apache.dubbo.demo.DemoService/providers/hessian%3A%2F%2F172.16.0.10%3A20880%2Forg.apache.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.2%26generic%3Dfalse%26group%3DtestGroup%26interface%3Dorg.apache.dubbo.demo.DemoService%26methods%3DsayHello%26pid%3D5956%26revision%3D1.0.0%26side%3Dprovider%26timestamp%3D1547285978821%26version%3D1.0.0%26weight%3D1"; + "/dubbo/org.apache.dubbo.demo.DemoService/providers/hessian%3A%2F%2F172.16.0.10%3A20880%2Forg.apache.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26dubbo%3D2.0.2%26generic%3Dfalse%26group%3DtestGroup%26interface%3Dorg.apache.dubbo.demo.DemoService%26methods%3DsayHello%26pid%3D5956%26revision%3D1.0.0%26side%3Dprovider%26timestamp%3D1547285978821%26version%3D1.0.0%26weight%3D1"; @Test public void testParseQueryString() { @@ -40,12 +41,44 @@ public void testParseQueryString() { @Test public void testParseIpAndPortString() { Map exceptedMap = Maps.newHashMap(); - exceptedMap.put("protocol","hessian"); - exceptedMap.put("port","20880"); - exceptedMap.put("ip","172.16.0.10"); + exceptedMap.put("protocol", "hessian"); + exceptedMap.put("port", "20880"); + exceptedMap.put("ip", "172.16.0.10"); Map actualMap = StringUtils.parseIpAndPortString(TEST_URL); Assert.assertThat(actualMap, is(exceptedMap)); } + @Test + public void testConvertDubboProvidersPath() { + String exceptedProviderPath = "/dubbo/org.apache.dubbo.demo.DemoService/providers"; + String actualProviderPath = StringUtils + .convertDubboProvidersPath("org.apache.dubbo.demo.DemoService"); + Assert.assertEquals(exceptedProviderPath, actualProviderPath); + } + + @Test + public void testConvertDubboFullPathForZk() { + String exceptedDubboFullPath = "/dubbo/org.apache.dubbo.demo.DemoService/providers/hessian%3A%2F%2F172.16.0.10%3A20800%2Forg.apache.dubbo.demo.DemoService%3Fside%3Dprovider%26methods%3DsayHello%26dubbo%3D2.0.2%26weight%3D1%26pid%3D5956%26interface%3Dorg.apache.dubbo.demo.DemoService%26version%3D1.0.0%26generic%3Dfalse%26revision%3D1.0.0%26protocol%3Dhessian%26application%3Ddemo-provider%26group%3DtestGroup%26timestamp%3D1547285978821"; + Map metaDataMap = Maps.newHashMap(); + metaDataMap.put("side", "provider"); + metaDataMap.put("application", "demo-provider"); + metaDataMap.put("methods", "sayHello"); + metaDataMap.put("dubbo", "2.0.2"); + metaDataMap.put("weight", "1"); + metaDataMap.put("pid", "5956"); + metaDataMap.put("interface", "org.apache.dubbo.demo.DemoService"); + metaDataMap.put("version", "1.0.0"); + metaDataMap.put("group", "testGroup"); + metaDataMap.put("generic", "false"); + metaDataMap.put("revision", "1.0.0"); + metaDataMap.put("timestamp", "1547285978821"); + metaDataMap.put("protocol", "hessian"); + String actualDubboFullPath = StringUtils + .convertDubboFullPathForZk(metaDataMap, + StringUtils.convertDubboProvidersPath("org.apache.dubbo.demo.DemoService"), + "172.16.0.10", 20800); + Assert.assertEquals(exceptedDubboFullPath, actualDubboFullPath); + } + } diff --git a/pom.xml b/pom.xml index d963a2ba..6455e042 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ com.alibaba.nacossync nacossync-parent - 0.2.0 + 0.3.0 nacossync-console nacossync-worker @@ -75,19 +75,6 @@ nacossync-worker 0.1.0 - - org.springframework - spring-test - - - junit - junit - 4.12 - - - org.springframework.boot - spring-boot-test - @@ -252,24 +239,4 @@ - - - - junit - junit - test - - - - junit - junit - - - - org.springframework - spring-test - 5.0.8.RELEASE - - - \ No newline at end of file