From e189cca8f087feab56239c9fd6ac8ad32be2c966 Mon Sep 17 00:00:00 2001 From: ly <641921791@qq.com> Date: Wed, 25 Sep 2019 19:06:47 +0800 Subject: [PATCH 001/156] Fix #1865 --- .../MysqlHealthCheckProcessor.java | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/MysqlHealthCheckProcessor.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/MysqlHealthCheckProcessor.java index 1ce59f46c43..de99041a16b 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/MysqlHealthCheckProcessor.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/MysqlHealthCheckProcessor.java @@ -21,17 +21,13 @@ import com.alibaba.nacos.naming.misc.Loggers; import com.alibaba.nacos.naming.misc.SwitchDomain; import com.alibaba.nacos.naming.monitor.MetricsMonitor; -import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; import io.netty.channel.ConnectTimeoutException; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.net.SocketTimeoutException; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; +import java.sql.*; import java.util.List; import java.util.concurrent.*; @@ -149,17 +145,11 @@ public void run() { AbstractHealthChecker.Mysql config = (AbstractHealthChecker.Mysql) cluster.getHealthChecker(); if (connection == null || connection.isClosed()) { - MysqlDataSource dataSource = new MysqlDataSource(); - dataSource.setConnectTimeout(CONNECT_TIMEOUT_MS); - dataSource.setSocketTimeout(CONNECT_TIMEOUT_MS); - dataSource.setUser(config.getUser()); - dataSource.setPassword(config.getPwd()); - dataSource.setLoginTimeout(1); - - dataSource.setServerName(ip.getIp()); - dataSource.setPort(ip.getPort()); - - connection = dataSource.getConnection(); + String url = "jdbc:mysql://" + ip.getIp() + ":" + ip.getPort() + + "?connectTimeout=" + CONNECT_TIMEOUT_MS + + "&socketTimeout=" + CONNECT_TIMEOUT_MS + + "&loginTimeout=" + 1; + connection = DriverManager.getConnection(url, config.getUser(), config.getPwd()); CONNECTION_POOL.put(key, connection); } From 0d304c0b7db7a55e1eeb69a73116e860029eca24 Mon Sep 17 00:00:00 2001 From: rushsky518 Date: Fri, 10 Jan 2020 17:50:17 +0800 Subject: [PATCH 002/156] #2237 1. use ByteArrayEntity 2. getDeclaredMethods --- .../com/alibaba/nacos/naming/misc/HttpClient.java | 13 ++++++------- .../com/alibaba/nacos/naming/web/FilterBase.java | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java index 82dc093c2ff..cb075fbb876 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClient.java @@ -31,6 +31,7 @@ import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; +import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; @@ -316,18 +317,16 @@ public static void asyncHttpGetLarge(String url, Map headers, by public static HttpResult httpPutLarge(String url, Map headers, byte[] content) { try { - HttpClientBuilder builder = HttpClients.custom(); - builder.setUserAgent(UtilsAndCommons.SERVER_VERSION); - builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); - + HttpClientBuilder builder = HttpClients.custom() + .setUserAgent(UtilsAndCommons.SERVER_VERSION) + .setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); CloseableHttpClient httpClient = builder.build(); - HttpPut httpPut = new HttpPut(url); + HttpPut httpPut = new HttpPut(url); for (Map.Entry entry : headers.entrySet()) { httpPut.setHeader(entry.getKey(), entry.getValue()); } - - httpPut.setEntity(new StringEntity(new String(content, StandardCharsets.UTF_8), ContentType.create("application/json", StandardCharsets.UTF_8))); + httpPut.setEntity(new ByteArrayEntity(content, ContentType.APPLICATION_JSON)); HttpResponse response = httpClient.execute(httpPut); HttpEntity entity = response.getEntity(); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java b/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java index ab6cf565378..bf94f4e7c31 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java @@ -58,7 +58,7 @@ public Method getMethod(String httpMethod, String path) { private void initClassMethod(Class clazz) { RequestMapping requestMapping = clazz.getAnnotation(RequestMapping.class); String classPath = requestMapping.value()[0]; - for (Method method : clazz.getMethods()) { + for (Method method : clazz.getDeclaredMethods()) { if (!method.isAnnotationPresent(RequestMapping.class)) { parseSubAnnotations(method, classPath); continue; From c99747609ff487c13c6adc58dcd8677e8062f1ab Mon Sep 17 00:00:00 2001 From: rushsky518 Date: Mon, 13 Jan 2020 11:27:46 +0800 Subject: [PATCH 003/156] revert --- .../src/main/java/com/alibaba/nacos/naming/web/FilterBase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java b/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java index bf94f4e7c31..ab6cf565378 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/FilterBase.java @@ -58,7 +58,7 @@ public Method getMethod(String httpMethod, String path) { private void initClassMethod(Class clazz) { RequestMapping requestMapping = clazz.getAnnotation(RequestMapping.class); String classPath = requestMapping.value()[0]; - for (Method method : clazz.getDeclaredMethods()) { + for (Method method : clazz.getMethods()) { if (!method.isAnnotationPresent(RequestMapping.class)) { parseSubAnnotations(method, classPath); continue; From b1ffbad4830730ad754ee7999bea290a96d77e51 Mon Sep 17 00:00:00 2001 From: rushsky518 Date: Wed, 15 Jan 2020 11:52:13 +0800 Subject: [PATCH 004/156] #2237 directly use byte[] --- .../alibaba/nacos/naming/controllers/DistroController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java index 9801c19c617..ab7b9402af1 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java @@ -103,13 +103,13 @@ public ResponseEntity get(@RequestBody JSONObject body) throws Exception { datumMap.put(key, datum); } - String content = new String(serializer.serialize(datumMap), StandardCharsets.UTF_8); + byte[] content = serializer.serialize(datumMap); return ResponseEntity.ok(content); } @GetMapping("/datums") public ResponseEntity getAllDatums() { - String content = new String(serializer.serialize(dataStore.getDataMap()), StandardCharsets.UTF_8); + byte[] content = serializer.serialize(dataStore.getDataMap()); return ResponseEntity.ok(content); } } From 8fe3eba1d7914cc8e7222513152b0cd7e6a7119c Mon Sep 17 00:00:00 2001 From: rushsky518 Date: Fri, 24 Jan 2020 09:56:52 +0800 Subject: [PATCH 005/156] #2237 add a IT_test --- .../nacos/test/naming/NamingProxy_ITCase.java | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java new file mode 100644 index 00000000000..e34eabbf7d4 --- /dev/null +++ b/test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java @@ -0,0 +1,118 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.test.naming; + +import com.alibaba.nacos.api.PropertyKeyConst; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.NamingFactory; +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.utils.NamingUtils; +import com.alibaba.nacos.naming.NamingApp; +import com.alibaba.nacos.naming.cluster.transport.Serializer; +import com.alibaba.nacos.naming.consistency.Datum; +import com.alibaba.nacos.naming.consistency.KeyBuilder; +import com.alibaba.nacos.naming.consistency.ephemeral.distro.DataStore; +import com.alibaba.nacos.naming.core.Instance; +import com.alibaba.nacos.naming.core.Instances; +import com.alibaba.nacos.naming.misc.NamingProxy; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"}, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class NamingProxy_ITCase { + @LocalServerPort + private int port; + @Autowired + private Serializer serializer; + private final DataStore dataStore = new DataStore(); + private NamingService namingService; + private final String namespaceId = "dev"; + private final String serviceName = "biz"; + private final String groupName = "DEFAULT_GROUP"; + + @Before + public void init() throws NacosException { + Properties properties = new Properties(); + properties.put(PropertyKeyConst.NAMESPACE, namespaceId); + properties.put(PropertyKeyConst.SERVER_ADDR, "localhost:" + port); + namingService = NamingFactory.createNamingService(properties); + } + + @Test + public void testSyncData() throws NacosException { + // write data to DataStore + String groupedName = NamingUtils.getGroupedName(serviceName, groupName); + Instances instances = new Instances(); + Instance instance = new Instance(); + instance.setIp("1.2.3.4"); + instance.setPort(8888); + instance.setEphemeral(true); + instance.setServiceName(groupedName); + List instanceList = new ArrayList(1); + instanceList.add(instance); + instances.setInstanceList(instanceList); + String key = KeyBuilder.buildInstanceListKey(namespaceId, instance.getServiceName(), true); + + Datum datum = new Datum<>(); + datum.value = instances; + datum.key = key; + datum.timestamp.incrementAndGet(); + this.dataStore.put(key, datum); + + // sync data to server + Map dataMap = dataStore.getDataMap(); + byte[] content = serializer.serialize(dataMap); + boolean result = NamingProxy.syncData(content, "localhost:" + port); + if (!result) { + Assert.fail("NamingProxy.syncData error"); + } + // query instance by api + List allInstances = namingService.getAllInstances(serviceName); + com.alibaba.nacos.api.naming.pojo.Instance dst = allInstances.get(0); + Assert.assertEquals(instance.getIp(), dst.getIp()); + Assert.assertEquals(instance.getPort(), dst.getPort()); + Assert.assertEquals(instance.getServiceName(), dst.getServiceName()); + } +} + + + + + + + + + + + + + + + + From 93e730d95a40556d9832b5f5e9e0384e693cc780 Mon Sep 17 00:00:00 2001 From: rushsky518 Date: Sat, 25 Jan 2020 13:36:04 +0800 Subject: [PATCH 006/156] wait for async op --- .../nacos/test/naming/NamingProxy_ITCase.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java index e34eabbf7d4..86498ef7932 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java @@ -41,6 +41,7 @@ import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.concurrent.TimeUnit; @RunWith(SpringRunner.class) @SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"}, @@ -65,7 +66,7 @@ public void init() throws NacosException { } @Test - public void testSyncData() throws NacosException { + public void testSyncData() throws NacosException, InterruptedException { // write data to DataStore String groupedName = NamingUtils.getGroupedName(serviceName, groupName); Instances instances = new Instances(); @@ -92,13 +93,24 @@ public void testSyncData() throws NacosException { if (!result) { Assert.fail("NamingProxy.syncData error"); } + // query instance by api - List allInstances = namingService.getAllInstances(serviceName); + List allInstances = namingService.getAllInstances(serviceName, false); + for (int i = 0; i < 3 && allInstances.isEmpty(); i++) { + // wait for async op + TimeUnit.SECONDS.sleep(100); + allInstances = namingService.getAllInstances(serviceName, false); + } + if (allInstances.isEmpty()) { + Assert.fail("instance is empty"); + } + com.alibaba.nacos.api.naming.pojo.Instance dst = allInstances.get(0); Assert.assertEquals(instance.getIp(), dst.getIp()); Assert.assertEquals(instance.getPort(), dst.getPort()); Assert.assertEquals(instance.getServiceName(), dst.getServiceName()); } + } From 9c5bead6fbed0eeeab85d46bea7d165e433a04a8 Mon Sep 17 00:00:00 2001 From: rushsky518 Date: Mon, 27 Jan 2020 20:09:47 +0800 Subject: [PATCH 007/156] remove blank lines --- .../nacos/test/naming/NamingProxy_ITCase.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java index 86498ef7932..8e91cfd02ff 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/NamingProxy_ITCase.java @@ -112,19 +112,3 @@ public void testSyncData() throws NacosException, InterruptedException { } } - - - - - - - - - - - - - - - - From 7179cc34a7b53d4b34678447514846b772179931 Mon Sep 17 00:00:00 2001 From: wangwei Date: Thu, 21 May 2020 22:03:16 +0800 Subject: [PATCH 008/156] [ISSUE #2749]dumpAllTaskMgr did not add the correct processor rebase develop --- .../nacos/config/server/service/dump/DumpService.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java index d30f16797cf..03b57fd9987 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java @@ -120,6 +120,10 @@ protected void init() throws Throwable { dumpAllTaskMgr = new TaskManager("com.alibaba.nacos.server.DumpAllTaskManager"); dumpAllTaskMgr.setDefaultTaskProcessor(dumpAllProcessor); + dumpAllTaskMgr.addProcessor(DumpAllTask.TASK_ID, dumpAllProcessor); + dumpAllTaskMgr.addProcessor(DumpAllBetaTask.TASK_ID, dumpAllBetaProcessor); + dumpAllTaskMgr.addProcessor(DumpAllTagTask.TASK_ID, dumpAllTagProcessor); + // If using embedded distributed storage, you need to wait for the // underlying master to complete the selection if (PropertyUtil.isEmbeddedStorage() && !ApplicationUtils.getStandaloneMode()) { @@ -196,6 +200,9 @@ private void dumpOperate(DumpProcessor processor, DumpAllProcessor dumpAllProces Runnable dumpAllBeta = () -> dumpAllTaskMgr .addTask(DumpAllBetaTask.TASK_ID, new DumpAllBetaTask()); + Runnable dumpAllTag = () -> dumpAllTaskMgr + .addTask(DumpAllTagTask.TASK_ID, new DumpAllTagTask()); + Runnable clearConfigHistory = () -> { log.warn("clearConfigHistory start"); if (canExecute()) { @@ -285,6 +292,9 @@ private void dumpOperate(DumpProcessor processor, DumpAllProcessor dumpAllProces TimerTaskService.scheduleWithFixedDelay(dumpAllBeta, initialDelay, DUMP_ALL_INTERVAL_IN_MINUTE, TimeUnit.MINUTES); + + TimerTaskService.scheduleWithFixedDelay(dumpAllTag, initialDelay, + DUMP_ALL_INTERVAL_IN_MINUTE, TimeUnit.MINUTES); } TimerTaskService From ba5098eaa3c1930055a40f11d6c7e83cf4963700 Mon Sep 17 00:00:00 2001 From: lin-mt Date: Fri, 22 May 2020 08:02:13 +0800 Subject: [PATCH 009/156] fix:#2839 --- .../persistent/raft/RaftStore.java | 63 ++++++++++--------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java index 3dd877b9938..d6c8e3521c2 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java @@ -49,11 +49,13 @@ @Component public class RaftStore { - private Properties meta = new Properties(); + private final Properties meta = new Properties(); - private String metaFileName = UtilsAndCommons.DATA_BASE_DIR + File.separator + "meta.properties"; + private static final String META_FILE_NAME = UtilsAndCommons.DATA_BASE_DIR + File.separator + "meta.properties"; - private String cacheDir = UtilsAndCommons.DATA_BASE_DIR + File.separator + "data"; + private static final String CACHE_DIR = UtilsAndCommons.DATA_BASE_DIR + File.separator + "data"; + + private static final String CACHE_FILE_SUFFIX = ".datum"; public synchronized void loadDatums(RaftCore.Notifier notifier, ConcurrentMap datums) throws Exception { @@ -80,7 +82,7 @@ public synchronized void loadDatums(RaftCore.Notifier notifier, ConcurrentMap Date: Fri, 22 May 2020 08:27:04 +0800 Subject: [PATCH 010/156] fix the style --- .../nacos/naming/consistency/persistent/raft/RaftStore.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java index d6c8e3521c2..bafd2838ca3 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java @@ -301,7 +301,7 @@ private static String encodeDatumKey(String datumKey) { return datumKey.replace(':', '#'); } - private static String decodeDatumKey(String DatumKey) { - return DatumKey.replace("#", ":"); + private static String decodeDatumKey(String datumKey) { + return datumKey.replace("#", ":"); } } From 60860893144cc27758067b4a1ee2f096b7f0477f Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Thu, 28 May 2020 00:14:42 +0800 Subject: [PATCH 011/156] fix #2858 Provide nacosRestTemplate implementation --- .../common/constant/HttpHeaderConsts.java | 2 + .../common/http/client/ClientHttpRequest.java | 42 +++++ .../http/client/ClientHttpResponse.java | 56 ++++++ .../client/DefaultClientHttpResponse.java | 83 +++++++++ .../DefaultSimpleClientHttpRequest.java | 133 ++++++++++++++ .../common/http/client/HttpAccessor.java | 47 +++++ .../nacos/common/http/client/HttpMessage.java | 46 +++++ .../client/HttpMessageConverterExtractor.java | 40 +++++ .../common/http/client/NacosRestTemplate.java | 159 ++++++++++++++++ .../http/client/ResponseEntityExtractor.java | 33 ++++ .../common/http/client/ResponseExtractor.java | 22 +++ .../common/http/client/RestOperations.java | 169 ++++++++++++++++++ .../common/http/handler/ResponseHandler.java | 5 + .../nacos/common/http/param/Header.java | 37 +++- .../nacos/common/http/param/MediaType.java | 2 + .../nacos/common/http/param/Query.java | 3 +- .../nacos/common/model/RequestHttpEntity.java | 68 +++++++ .../nacos/common/model/RestResult.java | 27 ++- .../alibaba/nacos/common/utils/IoUtils.java | 14 ++ .../nacos/common/utils/JacksonUtils.java | 5 + .../alibaba/nacos/common/utils/UriUtils.java | 38 ++++ .../nacos/common/http/HttpUtilsTest.java | 46 ++++- 22 files changed, 1068 insertions(+), 9 deletions(-) create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpRequest.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpResponse.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/DefaultSimpleClientHttpRequest.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/HttpAccessor.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessage.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/utils/UriUtils.java diff --git a/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java b/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java index 810513b3db8..18d5236ce11 100644 --- a/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java +++ b/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java @@ -25,5 +25,7 @@ public interface HttpHeaderConsts { String CLIENT_VERSION_HEADER = "Client-Version"; String USER_AGENT_HEADER = "User-Agent"; String REQUEST_SOURCE_HEADER = "Request-Source"; + String CONTENT_TYPE = "Content-Type"; + String CONTENT_LENGTH = "Content-Length"; } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpRequest.java new file mode 100644 index 00000000000..ce432308eb4 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpRequest.java @@ -0,0 +1,42 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.http.client; + +import com.alibaba.nacos.common.model.RequestHttpEntity; + +import java.io.IOException; +import java.net.URI; + +/** + * Represents a client-side HTTP request. + * Created via an implementation execute. + * + * @author mai.jh + * @date 2020/5/23 + */ +public interface ClientHttpRequest { + + /** + * execute http request + * @param uri http url + * @param httpMethod http request method + * @param requestHttpEntity http request entity + * @return ClientHttpResponse + * @throws IOException IOException + */ + ClientHttpResponse execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity) throws IOException; +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpResponse.java new file mode 100644 index 00000000000..b86ea8d276e --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpResponse.java @@ -0,0 +1,56 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.http.client; + +import com.alibaba.nacos.common.model.RequestHttpEntity; + +import java.io.Closeable; +import java.io.IOException; +import java.net.URI; + +/** + * Represents a client-side HTTP response. + * Obtained via an calling of the + * {@link ClientHttpRequest#execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity)}. + * + * + * @author mai.jh + * @date 2020/5/23 + */ +public interface ClientHttpResponse extends HttpMessage, Closeable{ + + /** + * Return the HTTP status code + * @return the HTTP status as an integer + * @throws IOException in case of I/O errors + */ + int getStatusCode() throws IOException; + + /** + * Return the HTTP status text of the response. + * @return the HTTP status text + * @throws IOException in case of I/O errors + */ + String getStatusText() throws IOException; + + /** + * close response InputStream + * @throws IOException ex + */ + @Override + void close() throws IOException; +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java new file mode 100644 index 00000000000..03eff69865b --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java @@ -0,0 +1,83 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.http.client; + +import com.alibaba.nacos.common.http.param.Header; +import com.sun.xml.internal.ws.util.StreamUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +import java.io.IOException; +import java.io.InputStream; + + +/** + * DefaultClientHttpResponse implementation {@link ClientHttpResponse} + * + * @author mai.jh + * @date 2020/5/25 + */ +public class DefaultClientHttpResponse implements ClientHttpResponse { + + private Header header; + + private int responseCode; + + private String responseCodeMessage; + + private InputStream responseBody; + + public DefaultClientHttpResponse(Header header, int responseCode, String responseCodeMessage, InputStream responseBody) { + this.header = header; + this.responseCode = responseCode; + this.responseCodeMessage = responseCodeMessage; + this.responseBody = responseBody; + } + + @Override + public int getStatusCode() throws IOException { + return responseCode; + } + + @Override + public String getStatusText() throws IOException { + return responseCodeMessage; + } + + @Override + public Header getHeaders() { + return header; + } + + @Override + public InputStream getBody() throws IOException{ + return this.responseBody; + } + + @Override + public void close() { + try { + if (this.responseBody != null) { + this.responseBody.close(); + } + } + catch (Exception ex) { + // ignore + } + } +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultSimpleClientHttpRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultSimpleClientHttpRequest.java new file mode 100644 index 00000000000..00cf7f96cf4 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultSimpleClientHttpRequest.java @@ -0,0 +1,133 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.http.client; + +import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.model.RequestHttpEntity; +import com.alibaba.nacos.common.utils.HttpMethod; +import com.alibaba.nacos.common.utils.IoUtils; +import com.google.common.net.HttpHeaders; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URL; +import java.net.URLConnection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.zip.GZIPInputStream; + +/** + * {@link ClientHttpRequest} implementation that uses standard JDK facilities to + * execute streaming requests + * + * @author mai.jh + * @date 2020/5/24 + */ +public class DefaultSimpleClientHttpRequest implements ClientHttpRequest { + + private static final Logger logger = LoggerFactory.getLogger(NacosRestTemplate.class); + + private int connectTimeout = 3000; + + private int readTimeout = 50000; + + + public void setConnectTimeout(int connectTimeout) { + this.connectTimeout = connectTimeout; + } + + public void setReadTimeout(int readTimeout) { + this.readTimeout = readTimeout; + } + + @Override + public ClientHttpResponse execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity) throws IOException { + Header headers = requestHttpEntity.getHeaders(); + // open URL connection + HttpURLConnection conn = openConnection(uri.toURL()); + prepareConnection(conn, httpMethod); + addHeaders(conn, headers); + if (conn.getDoOutput() && !requestHttpEntity.isEmptyBody()) { + byte[] body = requestHttpEntity.getBody(); + conn.setFixedLengthStreamingMode(body.length); + IoUtils.copy(body, conn.getOutputStream()); + } + conn.connect(); + if (logger.isDebugEnabled()) { + logger.debug("Request from server: " + conn.getURL()); + } + return handleResponse(conn); + } + + private ClientHttpResponse handleResponse(HttpURLConnection conn) throws IOException{ + int responseCode = conn.getResponseCode(); + String responseMessage = conn.getResponseMessage(); + Header responseHeader = Header.newInstance(); + InputStream inputStream = conn.getErrorStream(); + inputStream = inputStream != null ? inputStream : conn.getInputStream(); + + for (Map.Entry> entry : conn.getHeaderFields().entrySet()) { + responseHeader.addParam(entry.getKey(), entry.getValue().get(0)); + } + + return new DefaultClientHttpResponse(responseHeader,responseCode, responseMessage, inputStream); + } + + private HttpURLConnection openConnection(URL url) throws IOException{ + URLConnection urlConnection = url.openConnection(); + if (!HttpURLConnection.class.isInstance(urlConnection)) { + throw new IllegalStateException("HttpURLConnection required for [" + url + "] but got: " + urlConnection); + } + return (HttpURLConnection) urlConnection; + } + + private void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException{ + connection.setRequestMethod(httpMethod); + connection.setDoInput(true); + if (this.connectTimeout >= 0) { + connection.setConnectTimeout(this.connectTimeout); + } + if (this.readTimeout >= 0) { + connection.setReadTimeout(this.readTimeout); + } + if (HttpMethod.POST.equals(httpMethod) || HttpMethod.PUT.equals(httpMethod) || + HttpMethod.PATCH.equals(httpMethod) || HttpMethod.DELETE.equals(httpMethod)) { + connection.setDoOutput(true); + } else { + connection.setDoOutput(false); + } + + + } + + + private void addHeaders(HttpURLConnection connection, Header headers) { + Set> entrySet = headers.getHeader().entrySet(); + for (Map.Entry entry : entrySet) { + connection.addRequestProperty(entry.getKey(), entry.getValue()); + } + connection.addRequestProperty("Accept-Charset", Constants.ENCODE); + + } +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpAccessor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpAccessor.java new file mode 100644 index 00000000000..f89345d23d4 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpAccessor.java @@ -0,0 +1,47 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.http.client; + +/** + * HTTP accessing helpers, defining common properties + * such as the {@link ClientHttpRequest} to operate on. + * + * @author mai.jh + * @date 2020/5/24 + */ +@SuppressWarnings("WeakerAccess") +public class HttpAccessor { + + private ClientHttpRequest requestClient = new DefaultSimpleClientHttpRequest(); + + /** + * set requestClient {@link ClientHttpRequest} + *

The default is a {@link DefaultSimpleClientHttpRequest} based on the JDK's own + * HTTP libraries ({@link java.net.HttpURLConnection}). + * @param requestClient ClientHttpRequest + */ + public void setRequestClient(ClientHttpRequest requestClient) { + if (requestClient == null) { + throw new IllegalArgumentException("ClientHttpRequestFactory must not be null"); + } + this.requestClient = requestClient; + } + + public ClientHttpRequest getRequestClient() { + return requestClient; + } +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessage.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessage.java new file mode 100644 index 00000000000..9b17ef5e14e --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessage.java @@ -0,0 +1,46 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.http.client; + +import com.alibaba.nacos.common.http.param.Header; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Represents the base interface for HTTP request and response messages. + * Consists of {@link Header}, retrievable via {@link #getHeaders()}. + * + * @author mai.jh + * @date 2020/5/23 + */ +public interface HttpMessage { + + /** + * Return the headers of this message. + * @return a corresponding HttpHeaders object (never {@code null}) + */ + Header getHeaders(); + + /** + * Return the body of the message as an input stream. + * @return String response body + * @throws IOException IOException + */ + InputStream getBody() throws IOException; + +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java new file mode 100644 index 00000000000..1ee1c0d30cb --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java @@ -0,0 +1,40 @@ +package com.alibaba.nacos.common.http.client; + +import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.http.handler.ResponseHandler; +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.MediaType; +import com.alibaba.nacos.common.utils.IoUtils; + +import java.lang.reflect.Type; + +/** + * HTTP Message Converter + * to convert the response into a type {@code T}. + * + * @author mai.jh + * @date 2020/5/27 + */ +@SuppressWarnings({"unchecked", "rawtypes", "resource"}) +public class HttpMessageConverterExtractor implements ResponseExtractor{ + + + private Type responseType; + + public HttpMessageConverterExtractor(Type responseType) { + this.responseType = responseType; + } + + @Override + public T extractData(ClientHttpResponse clientHttpResponse) throws Exception { + Header headers = clientHttpResponse.getHeaders(); + String value = headers.getValue(HttpHeaderConsts.CONTENT_TYPE); + String body = IoUtils.toString(clientHttpResponse.getBody(), headers.getCharset()); + if (MediaType.APPLICATION_JSON.equals(value)) { + return ResponseHandler.convert(body, responseType); + } + return (T) body; + } + + +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java new file mode 100644 index 00000000000..34128be7eed --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java @@ -0,0 +1,159 @@ +package com.alibaba.nacos.common.http.client; + +import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.common.http.handler.RequestHandler; +import com.alibaba.nacos.common.http.handler.ResponseHandler; +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.Query; +import com.alibaba.nacos.common.model.RequestHttpEntity; +import com.alibaba.nacos.common.model.RestResult; +import com.alibaba.nacos.common.utils.HttpMethod; +import com.alibaba.nacos.common.utils.IoUtils; +import com.alibaba.nacos.common.utils.UriUtils; +import org.apache.http.HttpStatus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Type; +import java.net.URI; +import java.util.List; +import java.util.Map; + +/** + * NacosRestTemplate, refer to the design of Spring's RestTemplate + * + * @author mai.jh + * @date 2020/5/24 + */ +public class NacosRestTemplate extends HttpAccessor implements RestOperations { + + private static final Logger logger = LoggerFactory.getLogger(NacosRestTemplate.class); + + public NacosRestTemplate(ClientHttpRequest clientHttpRequest) { + super.setRequestClient(clientHttpRequest); + } + + public NacosRestTemplate() { + } + + @Override + public RestResult get(String url, Type responseType, Header header, Query query) throws Exception { + ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); + return execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseExtractor); + } + + @Override + public RestResult get(String url, Type responseType, List headers, Map paramValues) throws Exception { + RequestHttpEntity requestHttpEntity = new RequestHttpEntity( + Header.newInstance().addAll(headers), + Query.newInstance().initParams(paramValues)); + + ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); + return execute(url, HttpMethod.GET, requestHttpEntity, responseExtractor); + } + + @Override + public RestResult get(String url, Header header, Query query, Type responseType) throws Exception { + ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); + return execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseExtractor); + } + + @Override + public RestResult getLarge(String url, Header header, Query query, Object body, Type responseType) throws Exception { + ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); + return execute(url, HttpMethod.GET_LARGE, new RequestHttpEntity(header, query, toByte(body)), responseExtractor); + } + + @Override + public RestResult delete(String url, Header header, Query query, Type responseType) throws Exception { + ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); + return execute(url, HttpMethod.DELETE, new RequestHttpEntity(header, query), responseExtractor); + } + + @Override + public RestResult put(String url, Header header, Query query, Object body, Type responseType) throws Exception { + ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); + return execute(url, HttpMethod.PUT, new RequestHttpEntity(header, query, toByte(body)), responseExtractor); + } + + @Override + public RestResult putJson(String url, List headers, Map paramValues, String body, Type responseType) throws Exception { + RequestHttpEntity requestHttpEntity = new RequestHttpEntity( + Header.newInstance().addAll(headers), + Query.newInstance().initParams(paramValues), + toByte(body)); + + ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); + return execute(url, HttpMethod.PUT, requestHttpEntity, responseExtractor); + } + + @Override + public RestResult putFrom(String url, List headers, Map paramValues, Map bodyValues, Type responseType) throws Exception { + RequestHttpEntity requestHttpEntity = new RequestHttpEntity( + Header.newInstance().addAll(headers), + Query.newInstance().initParams(paramValues), + toByte(bodyValues)); + + ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); + return execute(url, HttpMethod.PUT, requestHttpEntity, responseExtractor); + } + + @Override + public RestResult post(String url, Header header, Query query, Object body, Type responseType) throws Exception { + ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); + return execute(url, HttpMethod.POST, new RequestHttpEntity(header, query, toByte(body)), responseExtractor); + } + + @Override + public RestResult postJson(String url, List headers, Map paramValues, String body, Type responseType) throws Exception { + RequestHttpEntity requestHttpEntity = new RequestHttpEntity( + Header.newInstance().addAll(headers), + Query.newInstance().initParams(paramValues), + toByte(body)); + + ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); + return execute(url, HttpMethod.POST, requestHttpEntity, responseExtractor); + } + + @Override + public RestResult postFrom(String url, List headers, Map paramValues, Map bodyValues, Type responseType) throws Exception { + RequestHttpEntity requestHttpEntity = new RequestHttpEntity( + Header.newInstance().addAll(headers), + Query.newInstance().initParams(paramValues), + toByte(bodyValues)); + + ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); + return execute(url, HttpMethod.POST, requestHttpEntity, responseExtractor); + } + + + private T execute(String url, String httpMethod, RequestHttpEntity requestEntity, + ResponseExtractor responseExtractor) throws Exception { + URI uri = UriUtils.buildUri(url, requestEntity.getQuery()); + if (logger.isDebugEnabled()) { + logger.debug("HTTP " + httpMethod + " " + url); + } + ClientHttpResponse response = null; + try { + ClientHttpRequest requestClient = getRequestClient(); + response = requestClient.execute(uri, httpMethod, requestEntity); + return responseExtractor.extractData(response); + } finally { + if (response != null) { + response.close(); + } + } + } + + private ResponseExtractor> responseEntityExtractor(Type responseType) { + return new ResponseEntityExtractor<>(responseType); + } + + + private byte[] toByte(Object param) throws Exception { + return RequestHandler.parse(param).getBytes(Constants.ENCODE); + } + +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java new file mode 100644 index 00000000000..71e07210950 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java @@ -0,0 +1,33 @@ +package com.alibaba.nacos.common.http.client; + +import com.alibaba.nacos.common.model.RestResult; + +import java.lang.reflect.Type; + +/** + * Response extractor for + * + * @author mai.jh + * @date 2020/5/27 + */ +@SuppressWarnings({"unchecked", "rawtypes", "resource"}) +public class ResponseEntityExtractor implements ResponseExtractor { + + private Type responseType; + + private final HttpMessageConverterExtractor delegate; + + public ResponseEntityExtractor(Type responseType) { + this.responseType = responseType; + this.delegate = new HttpMessageConverterExtractor(responseType); + } + + @Override + public T extractData(ClientHttpResponse response) throws Exception { + T body = this.delegate.extractData(response); + if (body instanceof RestResult) { + return body; + } + return (T) new RestResult<>(response.getHeaders(), response.getStatusCode(), body); + } +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java new file mode 100644 index 00000000000..0829f2d05b8 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java @@ -0,0 +1,22 @@ +package com.alibaba.nacos.common.http.client; + +import java.io.IOException; + +/** + * + * Generic callback interface used by {@link NacosRestTemplate}'s retrieval methods + * Implementations of this interface perform the actual work of extracting data + * + * @author mai.jh + * @date 2020/5/27 + */ +public interface ResponseExtractor { + + /** + * Extract data from the given {@code ClientHttpResponse} and return it. + * @param clientHttpResponse http response + * @return the extracted data + * @throws Exception ex + */ + T extractData(ClientHttpResponse clientHttpResponse) throws Exception; +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java new file mode 100644 index 00000000000..ce2a1e2a314 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java @@ -0,0 +1,169 @@ +package com.alibaba.nacos.common.http.client; + +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.Query; +import com.alibaba.nacos.common.model.RestResult; + +import java.lang.reflect.Type; +import java.util.List; +import java.util.Map; + +/** + * Interface specifying a basic set of RESTful operations. + * + * @author mai.jh + * @date 2020/5/23 + */ +public interface RestOperations { + + /** + * http get + * + * @param url url + * @param responseType return type + * @param header http header param + * @param query http query param + * @return the RestResult + * @throws Exception ex + */ + RestResult get(String url, Type responseType, Header header, Query query) throws Exception; + + /** + * http get + * + * @param url url + * @param responseType return type + * @param headers headers + * @param paramValues paramValues + * @return the RestResult + * @throws Exception ex + */ + RestResult get(String url, Type responseType, List headers, Map paramValues) throws Exception; + + /** + * http get + * + * @param url url + * @param responseType return type + * @param header http header param + * @param query http query param + * @return the RestResult + * @throws Exception ex + */ + RestResult get(String url, Header header, Query query, Type responseType) throws Exception; + + /** + * get request, may be pulling a lot of data + * + * @param url url + * @param header http header param + * @param query http query param + * @param body get with body + * @param responseType return type + * @return {@link RestResult } + * @throws Exception ex + */ + RestResult getLarge(String url, Header header, Query query, Object body, + Type responseType) throws Exception; + + /** + * http delete + * + * @param url url + * @param header http header param + * @param query http query param + * @param responseType return type + * @return {@link RestResult } + * @throws Exception ex + */ + RestResult delete(String url, Header header, Query query, + Type responseType) throws Exception; + + /** + * http put + * + * @param url url + * @param header http header param + * @param query http query param + * @param body http body param + * @param responseType return type + * @return {@link RestResult} + * @throws Exception ex + */ + RestResult put(String url, Header header, Query query, Object body, + Type responseType) throws Exception; + + + /** + * http put Json + * + * @param url url + * @param headers http header param + * @param paramValues http query param + * @param body http body param + * @param responseType return type + * @return {@link RestResult} + * @throws Exception ex + */ + RestResult putJson(String url, List headers, Map paramValues, String body, + Type responseType) throws Exception; + + + /** + * http put from + * + * @param url url + * @param headers http header param + * @param paramValues http query param + * @param bodyValues http body param + * @param responseType return type + * @return {@link RestResult} + * @throws Exception ex + */ + RestResult putFrom(String url, List headers, Map paramValues, + Map bodyValues, Type responseType) throws Exception; + + + /** + * http post + * + * @param url url + * @param header http header param + * @param query http query param + * @param body http body param + * @param responseType return type + * @return {@link RestResult} + * @throws Exception ex + */ + RestResult post(String url, Header header, Query query, Object body, + Type responseType) throws Exception; + + /** + * http post Json + * + * @param url url + * @param headers http header param + * @param paramValues http query param + * @param body http body param + * @param responseType return type + * @return {@link RestResult} + * @throws Exception ex + */ + RestResult postJson(String url, List headers, Map paramValues, String body, + Type responseType) throws Exception; + + + /** + * http post from + * + * @param url url + * @param headers http header param + * @param paramValues http query param + * @param bodyValues http body param + * @param responseType return type + * @return {@link RestResult} + * @throws Exception ex + */ + RestResult postFrom(String url, List headers, Map paramValues, + Map bodyValues, Type responseType) throws Exception; +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java b/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java index 6a0b78e05e3..ce0e531c803 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.common.utils.JacksonUtils; import org.slf4j.LoggerFactory; +import java.io.InputStream; import java.lang.reflect.Type; import org.slf4j.Logger; @@ -37,4 +38,8 @@ public static T convert(String s, Type type) throws Exception { return JacksonUtils.toObj(s, type); } + public static T convert(InputStream inputStream, Class tClass) throws Exception{ + return JacksonUtils.toObj(inputStream, tClass); + } + } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java index 9a10a69805c..b981270d21f 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java @@ -17,6 +17,10 @@ package com.alibaba.nacos.common.http.param; +import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.utils.StringUtils; + import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; @@ -49,7 +53,7 @@ public Header addParam(String key, String value) { return this; } - public Header builded() { + public Header build() { return this; } @@ -76,13 +80,14 @@ public List toList() { return list; } - public void addAll(List list) { + public Header addAll(List list) { if ((list.size() & 1) != 0) { throw new IllegalArgumentException("list size must be a multiple of 2"); } for (int i = 0; i < list.size();) { header.put(list.get(i++), list.get(i++)); } + return this; } public void addAll(Map params) { @@ -91,9 +96,37 @@ public void addAll(Map params) { } } + public String getCharset() { + String value = getValue(HttpHeaderConsts.CONTENT_TYPE); + return (StringUtils.isNotBlank(value) ? analysisCharset(value) : Constants.ENCODE); + } + + private String analysisCharset(String contentType) { + String[] values = contentType.split(";"); + String charset = Constants.ENCODE; + if (values.length == 0) { + return charset; + } + for (String value : values) { + if (value.startsWith("charset=")) { + charset = value.substring("charset=".length()); + } + } + return charset; + } + + + public void clear() { header.clear(); } + + @Override + public String toString() { + return "Header{" + + "headerToMap=" + header + + '}'; + } } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java b/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java index dc279afa026..735bb883a55 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java @@ -35,6 +35,8 @@ private MediaType() {} public static final String APPLICATION_XML = "application/xml"; + public static final String APPLICATION_JSON = "application/json;charset=UTF-8"; + public static final String MULTIPART_FORM_DATA = "multipart/form-data"; public static final String TEXT_HTML = "text/html"; diff --git a/common/src/main/java/com/alibaba/nacos/common/http/param/Query.java b/common/src/main/java/com/alibaba/nacos/common/http/param/Query.java index 3806955d9fd..90862e120a7 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/param/Query.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/param/Query.java @@ -52,10 +52,11 @@ public Object getValue(String key) { return params.get(key); } - public void initParams(Map params) { + public Query initParams(Map params) { for (Map.Entry entry : params.entrySet()) { addParam(entry.getKey(), entry.getValue()); } + return this; } public void initParams(List list) { diff --git a/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java b/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java new file mode 100644 index 00000000000..e9acbe2a31b --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java @@ -0,0 +1,68 @@ +package com.alibaba.nacos.common.model; + +import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.http.handler.RequestHandler; +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.Query; +import com.alibaba.nacos.common.utils.ByteUtils; +import com.alibaba.nacos.common.utils.IoUtils; +import org.apache.commons.lang3.StringUtils; + +import java.net.URI; +import java.util.Arrays; +import java.util.Map; + +/** + * + * Represents an HTTP request , consisting of headers and body. + * + * @author mai.jh + * @date 2020/5/23 + */ +public class RequestHttpEntity { + + private final Header headers = Header.newInstance(); + + private Query query; + + private byte[] body; + + public RequestHttpEntity(Header header, Query query) { + handleHeader(header); + this.query = query; + } + + public RequestHttpEntity(Header header, Query query, byte[] body) throws Exception { + handleHeader(header); + this.query = query; + this.body = body; + } + + private void handleHeader(Header header) { + if (header != null && !header.getHeader().isEmpty()) { + Map headerMap = header.getHeader(); + headers.addAll(headerMap); + } + } + + public Header getHeaders() { + if (!isEmptyBody()) { + headers.addParam(HttpHeaderConsts.CONTENT_LENGTH, String.valueOf(body.length)); + } + return headers; + } + + public Query getQuery() { + return query; + } + + public byte[] getBody() { + return body; + } + + public boolean isEmptyBody() { + return body == null || body.length == 0; + } + + +} diff --git a/common/src/main/java/com/alibaba/nacos/common/model/RestResult.java b/common/src/main/java/com/alibaba/nacos/common/model/RestResult.java index d7893320abe..b4e8a6eb320 100644 --- a/common/src/main/java/com/alibaba/nacos/common/model/RestResult.java +++ b/common/src/main/java/com/alibaba/nacos/common/model/RestResult.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.common.model; +import com.alibaba.nacos.common.http.param.Header; + import java.io.Serializable; /** @@ -25,6 +27,7 @@ public class RestResult implements Serializable { private static final long serialVersionUID = 6095433538316185017L; + private Header header; private int code; private String message; private T data; @@ -32,6 +35,12 @@ public class RestResult implements Serializable { public RestResult() { } + public RestResult(Header header, int code, T data) { + this.header = header; + this.code = code; + this.data = data; + } + public RestResult(int code, String message, T data) { this.code = code; this.setMessage(message); @@ -48,6 +57,14 @@ public RestResult(int code, String message) { this.setMessage(message); } + public Header getHeader() { + return header; + } + + public void setHeader(Header header) { + this.header = header; + } + public int getCode() { return code; } @@ -79,7 +96,8 @@ public boolean ok() { @Override public String toString() { return "RestResult{" + - "code=" + code + + "header=" + header + + ", code=" + code + ", message='" + message + '\'' + ", data=" + data + '}'; @@ -90,6 +108,7 @@ public static ResResultBuilder builder() { } public static final class ResResultBuilder { + private Header header; private int code; private String errMsg; private T data; @@ -97,6 +116,11 @@ public static final class ResResultBuilder { private ResResultBuilder() { } + public ResResultBuilder withHeader(Header header) { + this.header = header; + return this; + } + public ResResultBuilder withCode(int code) { this.code = code; return this; @@ -114,6 +138,7 @@ public ResResultBuilder withData(T data) { public RestResult build() { RestResult restResult = new RestResult(); + restResult.setHeader(header); restResult.setCode(code); restResult.setMessage(errMsg); restResult.setData(data); diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java index 105635fe4e7..51182308583 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java @@ -167,6 +167,20 @@ public static void cleanDirectory(File directory) throws IOException { } } + public static void copy(byte[] in, OutputStream out) throws IOException { + + try { + out.write(in); + } finally { + try { + out.close(); + } catch (IOException io) { + } + + } + + } + static public long copy(InputStream input, OutputStream output) throws IOException { byte[] buffer = new byte[1024]; int bytesRead; diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java index 9f4c94fdd05..fe68044527c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.InputStream; import java.lang.reflect.Type; /** @@ -48,6 +49,10 @@ public static T toObj(byte[] json, Type cls) throws Exception { return toObj(StringUtils.newString4UTF8(json), cls); } + public static T toObj(InputStream inputStream, Class tClass) throws Exception { + return mapper.readValue(inputStream, tClass); + } + public static T toObj(String json, Class cls) throws Exception { return mapper.readValue(json, cls); } diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/UriUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/UriUtils.java new file mode 100644 index 00000000000..5d08724152c --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/utils/UriUtils.java @@ -0,0 +1,38 @@ +package com.alibaba.nacos.common.utils; + +import com.alibaba.nacos.common.http.param.Query; + +import java.net.URI; +import java.net.URISyntaxException; + +/** + * URI build utils + * + * @author mai.jh + * @date 2020/5/24 + */ +public class UriUtils { + + private final static UriUtils URI_UTILS = new UriUtils(); + + private UriUtils() { + } + + + public static UriUtils newInstance() { + return URI_UTILS; + } + + /** + * build URI By url and query + * @param url url + * @param query query param {@link Query} + * @return + */ + public static URI buildUri(String url, Query query) throws URISyntaxException { + if (!query.isEmpty()) { + url = url + "?" + query.toQueryUrl(); + } + return new URI(url); + } +} diff --git a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java index 66a8a030a9e..84f6ba8a88e 100644 --- a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java @@ -1,12 +1,48 @@ package com.alibaba.nacos.common.http; +import com.alibaba.fastjson.JSON; +import com.alibaba.nacos.common.http.client.NacosRestTemplate; +import com.alibaba.nacos.common.http.handler.ResponseHandler; +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.Query; +import com.alibaba.nacos.common.model.RestResult; +import com.alibaba.nacos.common.utils.JacksonUtils; +import com.google.common.reflect.TypeToken; import org.junit.Test; +import sun.tools.jstat.Token; -public class HttpUtilsTest { +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; - @Test - public void test_url_encode() throws Exception { +public class HttpUtilsTest { - } + private NacosRestTemplate nacosRestTemplate = new NacosRestTemplate(); -} \ No newline at end of file + + private final String CONFIG_CONTROLLER_PATH = "/nacos/v1/cs"; + private final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns"; + private final String IP = "http://localhost:8848"; + + + @Test + public void test_url_post() throws Exception{ + String url = IP + CONFIG_INSTANCE_PATH + "/instance"; + Query query = Query.newInstance() + .addParam("ip", "11.11.11.11") + .addParam("port", "8080") + .addParam("serviceName", "app-test"); + + RestResult post = nacosRestTemplate.post(url, Header.newInstance(), query, null, RestResult.class); + System.out.println(JSON.toJSONString(post)); + } + + @Test + public void test_url_get() throws Exception { + String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; + Query query = Query.newInstance().addParam("serviceName", "app-test"); + RestResult get = nacosRestTemplate.get(url, Header.newInstance(), query, Map.class); + System.out.println(JSON.toJSONString(get)); + } + +} From d08eac68823fe7ff31b5d825173df9efc0048439 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Thu, 28 May 2020 10:36:19 +0800 Subject: [PATCH 012/156] change http client implementation, Unified apacheHttpClient --- .../nacos/common/http/HttpClientManager.java | 9 ++ .../http/client/ApacheHttpClientRequest.java | 98 +++++++++++++ .../client/DefaultClientHttpResponse.java | 8 +- .../DefaultSimpleClientHttpRequest.java | 133 ------------------ .../common/http/client/HttpAccessor.java | 47 ------- ...ttpRequest.java => HttpClientRequest.java} | 9 +- ...pResponse.java => HttpClientResponse.java} | 25 +++- .../nacos/common/http/client/HttpMessage.java | 46 ------ .../client/HttpMessageConverterExtractor.java | 2 +- .../common/http/client/NacosRestTemplate.java | 17 +-- .../http/client/ResponseEntityExtractor.java | 2 +- .../common/http/client/ResponseExtractor.java | 2 +- .../nacos/common/model/RequestHttpEntity.java | 3 - .../nacos/common/http/HttpUtilsTest.java | 2 +- 14 files changed, 143 insertions(+), 260 deletions(-) create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java delete mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/DefaultSimpleClientHttpRequest.java delete mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/HttpAccessor.java rename common/src/main/java/com/alibaba/nacos/common/http/client/{ClientHttpRequest.java => HttpClientRequest.java} (80%) rename common/src/main/java/com/alibaba/nacos/common/http/client/{ClientHttpResponse.java => HttpClientResponse.java} (68%) delete mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessage.java diff --git a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java index 317249fa603..265069ae601 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.common.http; +import com.alibaba.nacos.common.http.client.ApacheHttpClientRequest; +import com.alibaba.nacos.common.http.client.NacosRestTemplate; import com.alibaba.nacos.common.utils.ShutdownUtils; import org.apache.http.client.config.RequestConfig; import org.apache.http.impl.client.HttpClients; @@ -44,6 +46,9 @@ public class HttpClientManager { private static final NAsyncHttpClient ASYNC_HTTP_CLIENT = new NacosAsyncHttpClient( HttpAsyncClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build()); + private static final NacosRestTemplate NACOS_REST_TEMPLATE = new NacosRestTemplate + (new ApacheHttpClientRequest(HttpClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build())); + static { ShutdownUtils.addShutdownHook(new Runnable() { @Override @@ -69,4 +74,8 @@ public static NAsyncHttpClient getAsyncHttpClient() { return ASYNC_HTTP_CLIENT; } + public static NacosRestTemplate getNacosRestTemplate() { + return NACOS_REST_TEMPLATE; + } + } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java new file mode 100644 index 00000000000..11716eac070 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java @@ -0,0 +1,98 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.http.client; + +import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.common.http.BaseHttpMethod; +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.model.RequestHttpEntity; +import com.alibaba.nacos.common.utils.HttpMethod; +import com.alibaba.nacos.common.utils.IoUtils; +import com.google.common.net.HttpHeaders; +import org.apache.http.HttpEntity; +import org.apache.http.StatusLine; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.impl.client.CloseableHttpClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URL; +import java.net.URLConnection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.zip.GZIPInputStream; + +/** + * {@link HttpClientRequest} implementation that uses apache http client to + * execute streaming requests + * + * @author mai.jh + * @date 2020/5/24 + */ +public class ApacheHttpClientRequest implements HttpClientRequest { + + private static final Logger logger = LoggerFactory.getLogger(NacosRestTemplate.class); + + + private CloseableHttpClient client; + + public ApacheHttpClientRequest(CloseableHttpClient client) { + this.client = client; + } + + @Override + public HttpClientResponse execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity) throws Exception { + HttpRequestBase request = build(uri, httpMethod, requestHttpEntity); + CloseableHttpResponse response = client.execute(request); + if (logger.isDebugEnabled()) { + logger.debug("Request from server: " + request.getURI().toString()); + } + return handleResponse(response); + } + + private HttpClientResponse handleResponse(CloseableHttpResponse response) throws IOException{ + StatusLine statusLine = response.getStatusLine(); + HttpEntity entity = response.getEntity(); + int responseCode = statusLine.getStatusCode(); + String responseMessage = statusLine.getReasonPhrase(); + org.apache.http.Header[] allHeaders = response.getAllHeaders(); + Header responseHeader = Header.newInstance(); + + for (org.apache.http.Header header : allHeaders) { + responseHeader.addParam(header.getName(), header.getValue()); + } + + return new DefaultClientHttpResponse(responseHeader,responseCode, responseMessage, entity.getContent()); + } + + + protected HttpRequestBase build(URI uri, String method, RequestHttpEntity requestHttpEntity) throws Exception { + Header headers = requestHttpEntity.getHeaders(); + BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method); + httpMethod.init(uri.toString()); + httpMethod.initHeader(headers); + httpMethod.initEntity(requestHttpEntity.getBody(), headers.getValue("Content-Type")); + return httpMethod.getRequestBase(); + } +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java index 03eff69865b..f8e55aa0587 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java @@ -27,12 +27,12 @@ /** - * DefaultClientHttpResponse implementation {@link ClientHttpResponse} + * DefaultClientHttpResponse implementation {@link HttpClientResponse} * * @author mai.jh * @date 2020/5/25 */ -public class DefaultClientHttpResponse implements ClientHttpResponse { +public class DefaultClientHttpResponse implements HttpClientResponse { private Header header; @@ -50,12 +50,12 @@ public DefaultClientHttpResponse(Header header, int responseCode, String respons } @Override - public int getStatusCode() throws IOException { + public int getStatusCode() { return responseCode; } @Override - public String getStatusText() throws IOException { + public String getStatusText() { return responseCodeMessage; } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultSimpleClientHttpRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultSimpleClientHttpRequest.java deleted file mode 100644 index 00cf7f96cf4..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultSimpleClientHttpRequest.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.alibaba.nacos.common.http.client; - -import com.alibaba.nacos.api.common.Constants; -import com.alibaba.nacos.common.http.param.Header; -import com.alibaba.nacos.common.model.RequestHttpEntity; -import com.alibaba.nacos.common.utils.HttpMethod; -import com.alibaba.nacos.common.utils.IoUtils; -import com.google.common.net.HttpHeaders; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.HttpURLConnection; -import java.net.URI; -import java.net.URL; -import java.net.URLConnection; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.zip.GZIPInputStream; - -/** - * {@link ClientHttpRequest} implementation that uses standard JDK facilities to - * execute streaming requests - * - * @author mai.jh - * @date 2020/5/24 - */ -public class DefaultSimpleClientHttpRequest implements ClientHttpRequest { - - private static final Logger logger = LoggerFactory.getLogger(NacosRestTemplate.class); - - private int connectTimeout = 3000; - - private int readTimeout = 50000; - - - public void setConnectTimeout(int connectTimeout) { - this.connectTimeout = connectTimeout; - } - - public void setReadTimeout(int readTimeout) { - this.readTimeout = readTimeout; - } - - @Override - public ClientHttpResponse execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity) throws IOException { - Header headers = requestHttpEntity.getHeaders(); - // open URL connection - HttpURLConnection conn = openConnection(uri.toURL()); - prepareConnection(conn, httpMethod); - addHeaders(conn, headers); - if (conn.getDoOutput() && !requestHttpEntity.isEmptyBody()) { - byte[] body = requestHttpEntity.getBody(); - conn.setFixedLengthStreamingMode(body.length); - IoUtils.copy(body, conn.getOutputStream()); - } - conn.connect(); - if (logger.isDebugEnabled()) { - logger.debug("Request from server: " + conn.getURL()); - } - return handleResponse(conn); - } - - private ClientHttpResponse handleResponse(HttpURLConnection conn) throws IOException{ - int responseCode = conn.getResponseCode(); - String responseMessage = conn.getResponseMessage(); - Header responseHeader = Header.newInstance(); - InputStream inputStream = conn.getErrorStream(); - inputStream = inputStream != null ? inputStream : conn.getInputStream(); - - for (Map.Entry> entry : conn.getHeaderFields().entrySet()) { - responseHeader.addParam(entry.getKey(), entry.getValue().get(0)); - } - - return new DefaultClientHttpResponse(responseHeader,responseCode, responseMessage, inputStream); - } - - private HttpURLConnection openConnection(URL url) throws IOException{ - URLConnection urlConnection = url.openConnection(); - if (!HttpURLConnection.class.isInstance(urlConnection)) { - throw new IllegalStateException("HttpURLConnection required for [" + url + "] but got: " + urlConnection); - } - return (HttpURLConnection) urlConnection; - } - - private void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException{ - connection.setRequestMethod(httpMethod); - connection.setDoInput(true); - if (this.connectTimeout >= 0) { - connection.setConnectTimeout(this.connectTimeout); - } - if (this.readTimeout >= 0) { - connection.setReadTimeout(this.readTimeout); - } - if (HttpMethod.POST.equals(httpMethod) || HttpMethod.PUT.equals(httpMethod) || - HttpMethod.PATCH.equals(httpMethod) || HttpMethod.DELETE.equals(httpMethod)) { - connection.setDoOutput(true); - } else { - connection.setDoOutput(false); - } - - - } - - - private void addHeaders(HttpURLConnection connection, Header headers) { - Set> entrySet = headers.getHeader().entrySet(); - for (Map.Entry entry : entrySet) { - connection.addRequestProperty(entry.getKey(), entry.getValue()); - } - connection.addRequestProperty("Accept-Charset", Constants.ENCODE); - - } -} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpAccessor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpAccessor.java deleted file mode 100644 index f89345d23d4..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpAccessor.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.alibaba.nacos.common.http.client; - -/** - * HTTP accessing helpers, defining common properties - * such as the {@link ClientHttpRequest} to operate on. - * - * @author mai.jh - * @date 2020/5/24 - */ -@SuppressWarnings("WeakerAccess") -public class HttpAccessor { - - private ClientHttpRequest requestClient = new DefaultSimpleClientHttpRequest(); - - /** - * set requestClient {@link ClientHttpRequest} - *

The default is a {@link DefaultSimpleClientHttpRequest} based on the JDK's own - * HTTP libraries ({@link java.net.HttpURLConnection}). - * @param requestClient ClientHttpRequest - */ - public void setRequestClient(ClientHttpRequest requestClient) { - if (requestClient == null) { - throw new IllegalArgumentException("ClientHttpRequestFactory must not be null"); - } - this.requestClient = requestClient; - } - - public ClientHttpRequest getRequestClient() { - return requestClient; - } -} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java similarity index 80% rename from common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpRequest.java rename to common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java index ce432308eb4..33780cf5424 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java @@ -18,7 +18,6 @@ import com.alibaba.nacos.common.model.RequestHttpEntity; -import java.io.IOException; import java.net.URI; /** @@ -28,15 +27,15 @@ * @author mai.jh * @date 2020/5/23 */ -public interface ClientHttpRequest { +public interface HttpClientRequest { /** * execute http request * @param uri http url * @param httpMethod http request method * @param requestHttpEntity http request entity - * @return ClientHttpResponse - * @throws IOException IOException + * @return HttpClientResponse + * @throws Exception ex */ - ClientHttpResponse execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity) throws IOException; + HttpClientResponse execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity) throws Exception; } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientResponse.java similarity index 68% rename from common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpResponse.java rename to common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientResponse.java index b86ea8d276e..e1a0d49c089 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ClientHttpResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientResponse.java @@ -16,36 +16,49 @@ package com.alibaba.nacos.common.http.client; +import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.model.RequestHttpEntity; import java.io.Closeable; import java.io.IOException; +import java.io.InputStream; import java.net.URI; /** * Represents a client-side HTTP response. * Obtained via an calling of the - * {@link ClientHttpRequest#execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity)}. + * {@link HttpClientRequest#execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity)}. * * * @author mai.jh * @date 2020/5/23 */ -public interface ClientHttpResponse extends HttpMessage, Closeable{ +public interface HttpClientResponse extends Closeable{ + + /** + * Return the headers of this message. + * @return a corresponding HttpHeaders object (never {@code null}) + */ + Header getHeaders(); + + /** + * Return the body of the message as an input stream. + * @return String response body + * @throws IOException IOException + */ + InputStream getBody() throws IOException; /** * Return the HTTP status code * @return the HTTP status as an integer - * @throws IOException in case of I/O errors */ - int getStatusCode() throws IOException; + int getStatusCode(); /** * Return the HTTP status text of the response. * @return the HTTP status text - * @throws IOException in case of I/O errors */ - String getStatusText() throws IOException; + String getStatusText(); /** * close response InputStream diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessage.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessage.java deleted file mode 100644 index 9b17ef5e14e..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessage.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.alibaba.nacos.common.http.client; - -import com.alibaba.nacos.common.http.param.Header; - -import java.io.IOException; -import java.io.InputStream; - -/** - * Represents the base interface for HTTP request and response messages. - * Consists of {@link Header}, retrievable via {@link #getHeaders()}. - * - * @author mai.jh - * @date 2020/5/23 - */ -public interface HttpMessage { - - /** - * Return the headers of this message. - * @return a corresponding HttpHeaders object (never {@code null}) - */ - Header getHeaders(); - - /** - * Return the body of the message as an input stream. - * @return String response body - * @throws IOException IOException - */ - InputStream getBody() throws IOException; - -} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java index 1ee1c0d30cb..b0a326e175d 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java @@ -26,7 +26,7 @@ public HttpMessageConverterExtractor(Type responseType) { } @Override - public T extractData(ClientHttpResponse clientHttpResponse) throws Exception { + public T extractData(HttpClientResponse clientHttpResponse) throws Exception { Header headers = clientHttpResponse.getHeaders(); String value = headers.getValue(HttpHeaderConsts.CONTENT_TYPE); String body = IoUtils.toString(clientHttpResponse.getBody(), headers.getCharset()); diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java index 34128be7eed..d515e8dfb07 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java @@ -2,20 +2,15 @@ import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.common.http.handler.RequestHandler; -import com.alibaba.nacos.common.http.handler.ResponseHandler; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.model.RequestHttpEntity; import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.HttpMethod; -import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.UriUtils; -import org.apache.http.HttpStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.io.InputStream; import java.lang.reflect.Type; import java.net.URI; import java.util.List; @@ -27,15 +22,14 @@ * @author mai.jh * @date 2020/5/24 */ -public class NacosRestTemplate extends HttpAccessor implements RestOperations { +public class NacosRestTemplate implements RestOperations { private static final Logger logger = LoggerFactory.getLogger(NacosRestTemplate.class); - public NacosRestTemplate(ClientHttpRequest clientHttpRequest) { - super.setRequestClient(clientHttpRequest); - } + private HttpClientRequest requestClient; - public NacosRestTemplate() { + public NacosRestTemplate(HttpClientRequest requestClient) { + this.requestClient = requestClient; } @Override @@ -135,9 +129,8 @@ private T execute(String url, String httpMethod, RequestHttpEntity requestEn if (logger.isDebugEnabled()) { logger.debug("HTTP " + httpMethod + " " + url); } - ClientHttpResponse response = null; + HttpClientResponse response = null; try { - ClientHttpRequest requestClient = getRequestClient(); response = requestClient.execute(uri, httpMethod, requestEntity); return responseExtractor.extractData(response); } finally { diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java index 71e07210950..16bd5fb959e 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java @@ -23,7 +23,7 @@ public ResponseEntityExtractor(Type responseType) { } @Override - public T extractData(ClientHttpResponse response) throws Exception { + public T extractData(HttpClientResponse response) throws Exception { T body = this.delegate.extractData(response); if (body instanceof RestResult) { return body; diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java index 0829f2d05b8..159cb01f24a 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java @@ -18,5 +18,5 @@ public interface ResponseExtractor { * @return the extracted data * @throws Exception ex */ - T extractData(ClientHttpResponse clientHttpResponse) throws Exception; + T extractData(HttpClientResponse clientHttpResponse) throws Exception; } diff --git a/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java b/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java index e9acbe2a31b..3b341ed8dda 100644 --- a/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java +++ b/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java @@ -46,9 +46,6 @@ private void handleHeader(Header header) { } public Header getHeaders() { - if (!isEmptyBody()) { - headers.addParam(HttpHeaderConsts.CONTENT_LENGTH, String.valueOf(body.length)); - } return headers; } diff --git a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java index 84f6ba8a88e..b1b11f16c5a 100644 --- a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java @@ -17,7 +17,7 @@ public class HttpUtilsTest { - private NacosRestTemplate nacosRestTemplate = new NacosRestTemplate(); + private NacosRestTemplate nacosRestTemplate = HttpClientManager.getNacosRestTemplate(); private final String CONFIG_CONTROLLER_PATH = "/nacos/v1/cs"; From 377bbb88cf9577f0f4d2fe1ad9b5628d65af5a8a Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Thu, 28 May 2020 10:45:33 +0800 Subject: [PATCH 013/156] Add some callouts --- .../http/client/ApacheHttpClientRequest.java | 13 ------------- .../http/client/DefaultClientHttpResponse.java | 3 --- .../client/HttpMessageConverterExtractor.java | 17 ++++++++++++++++- .../common/http/client/NacosRestTemplate.java | 16 ++++++++++++++++ .../http/client/ResponseEntityExtractor.java | 16 ++++++++++++++++ .../common/http/client/ResponseExtractor.java | 18 ++++++++++++++++-- .../common/http/client/RestOperations.java | 16 ++++++++++++++++ 7 files changed, 80 insertions(+), 19 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java index 11716eac070..afdd737d83c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java @@ -16,13 +16,9 @@ package com.alibaba.nacos.common.http.client; -import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.common.http.BaseHttpMethod; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.model.RequestHttpEntity; -import com.alibaba.nacos.common.utils.HttpMethod; -import com.alibaba.nacos.common.utils.IoUtils; -import com.google.common.net.HttpHeaders; import org.apache.http.HttpEntity; import org.apache.http.StatusLine; import org.apache.http.client.methods.CloseableHttpResponse; @@ -32,16 +28,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.HttpURLConnection; import java.net.URI; -import java.net.URL; -import java.net.URLConnection; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.zip.GZIPInputStream; /** * {@link HttpClientRequest} implementation that uses apache http client to diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java index f8e55aa0587..ccbd3256a79 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java @@ -17,9 +17,6 @@ package com.alibaba.nacos.common.http.client; import com.alibaba.nacos.common.http.param.Header; -import com.sun.xml.internal.ws.util.StreamUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.io.IOException; diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java index b0a326e175d..04827241f48 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.common.http.client; import com.alibaba.nacos.common.constant.HttpHeaderConsts; @@ -18,7 +34,6 @@ @SuppressWarnings({"unchecked", "rawtypes", "resource"}) public class HttpMessageConverterExtractor implements ResponseExtractor{ - private Type responseType; public HttpMessageConverterExtractor(Type responseType) { diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java index d515e8dfb07..6fbfa1ae868 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.common.http.client; import com.alibaba.nacos.api.common.Constants; diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java index 16bd5fb959e..56bf374b5c1 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.common.http.client; import com.alibaba.nacos.common.model.RestResult; diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java index 159cb01f24a..6a6b85de9dc 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java @@ -1,6 +1,20 @@ -package com.alibaba.nacos.common.http.client; +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import java.io.IOException; +package com.alibaba.nacos.common.http.client; /** * diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java index ce2a1e2a314..cc26dd68ccb 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.common.http.client; import com.alibaba.nacos.common.http.param.Header; From 52426d43b572beda323eb07cd046628309f31290 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Thu, 28 May 2020 14:38:01 +0800 Subject: [PATCH 014/156] change http client implementation, Unified apacheHttpClient --- ...nse.java => ApacheClientHttpResponse.java} | 41 ++++++++++--------- .../http/client/ApacheHttpClientRequest.java | 19 +-------- .../common/http/client/NacosRestTemplate.java | 37 +++++++---------- .../common/http/client/RestOperations.java | 16 ++++---- .../nacos/common/model/RequestHttpEntity.java | 15 ++----- 5 files changed, 51 insertions(+), 77 deletions(-) rename common/src/main/java/com/alibaba/nacos/common/http/client/{DefaultClientHttpResponse.java => ApacheClientHttpResponse.java} (52%) diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java similarity index 52% rename from common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java rename to common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java index ccbd3256a79..8d1f3118b75 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java @@ -17,6 +17,9 @@ package com.alibaba.nacos.common.http.client; import com.alibaba.nacos.common.http.param.Header; +import org.apache.http.StatusLine; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.utils.HttpClientUtils; import java.io.IOException; @@ -24,53 +27,53 @@ /** - * DefaultClientHttpResponse implementation {@link HttpClientResponse} + * ApacheClientHttpResponse implementation {@link HttpClientResponse} * * @author mai.jh * @date 2020/5/25 */ -public class DefaultClientHttpResponse implements HttpClientResponse { +public class ApacheClientHttpResponse implements HttpClientResponse { - private Header header; + private CloseableHttpResponse response; - private int responseCode; + private Header responseHeader; - private String responseCodeMessage; - - private InputStream responseBody; - - public DefaultClientHttpResponse(Header header, int responseCode, String responseCodeMessage, InputStream responseBody) { - this.header = header; - this.responseCode = responseCode; - this.responseCodeMessage = responseCodeMessage; - this.responseBody = responseBody; + public ApacheClientHttpResponse(CloseableHttpResponse response) { + this.response = response; } @Override public int getStatusCode() { - return responseCode; + return this.response.getStatusLine().getStatusCode(); } @Override public String getStatusText() { - return responseCodeMessage; + return this.response.getStatusLine().getReasonPhrase(); } @Override public Header getHeaders() { - return header; + if (this.responseHeader == null) { + this.responseHeader = Header.newInstance(); + org.apache.http.Header[] allHeaders = response.getAllHeaders(); + for (org.apache.http.Header header : allHeaders) { + this.responseHeader.addParam(header.getName(), header.getValue()); + } + } + return this.responseHeader; } @Override public InputStream getBody() throws IOException{ - return this.responseBody; + return response.getEntity().getContent(); } @Override public void close() { try { - if (this.responseBody != null) { - this.responseBody.close(); + if (this.response != null) { + HttpClientUtils.closeQuietly(response); } } catch (Exception ex) { diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java index afdd737d83c..103c8fa41e3 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java @@ -55,26 +55,11 @@ public HttpClientResponse execute(URI uri, String httpMethod, RequestHttpEntity if (logger.isDebugEnabled()) { logger.debug("Request from server: " + request.getURI().toString()); } - return handleResponse(response); + return new ApacheClientHttpResponse(response); } - private HttpClientResponse handleResponse(CloseableHttpResponse response) throws IOException{ - StatusLine statusLine = response.getStatusLine(); - HttpEntity entity = response.getEntity(); - int responseCode = statusLine.getStatusCode(); - String responseMessage = statusLine.getReasonPhrase(); - org.apache.http.Header[] allHeaders = response.getAllHeaders(); - Header responseHeader = Header.newInstance(); - for (org.apache.http.Header header : allHeaders) { - responseHeader.addParam(header.getName(), header.getValue()); - } - - return new DefaultClientHttpResponse(responseHeader,responseCode, responseMessage, entity.getContent()); - } - - - protected HttpRequestBase build(URI uri, String method, RequestHttpEntity requestHttpEntity) throws Exception { + private HttpRequestBase build(URI uri, String method, RequestHttpEntity requestHttpEntity) throws Exception { Header headers = requestHttpEntity.getHeaders(); BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method); httpMethod.init(uri.toString()); diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java index 6fbfa1ae868..8368354a58a 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java @@ -16,8 +16,6 @@ package com.alibaba.nacos.common.http.client; -import com.alibaba.nacos.api.common.Constants; -import com.alibaba.nacos.common.http.handler.RequestHandler; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.model.RequestHttpEntity; @@ -73,7 +71,7 @@ public RestResult get(String url, Header header, Query query, Type respon @Override public RestResult getLarge(String url, Header header, Query query, Object body, Type responseType) throws Exception { ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.GET_LARGE, new RequestHttpEntity(header, query, toByte(body)), responseExtractor); + return execute(url, HttpMethod.GET_LARGE, new RequestHttpEntity(header, query, body), responseExtractor); } @Override @@ -85,26 +83,26 @@ public RestResult delete(String url, Header header, Query query, Type res @Override public RestResult put(String url, Header header, Query query, Object body, Type responseType) throws Exception { ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.PUT, new RequestHttpEntity(header, query, toByte(body)), responseExtractor); + return execute(url, HttpMethod.PUT, new RequestHttpEntity(header, query, body), responseExtractor); } @Override - public RestResult putJson(String url, List headers, Map paramValues, String body, Type responseType) throws Exception { + public RestResult putJson(String url, Header header, Map paramValues, String body, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( - Header.newInstance().addAll(headers), + header, Query.newInstance().initParams(paramValues), - toByte(body)); + body); ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); return execute(url, HttpMethod.PUT, requestHttpEntity, responseExtractor); } @Override - public RestResult putFrom(String url, List headers, Map paramValues, Map bodyValues, Type responseType) throws Exception { + public RestResult putFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( - Header.newInstance().addAll(headers), + header, Query.newInstance().initParams(paramValues), - toByte(bodyValues)); + bodyValues); ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); return execute(url, HttpMethod.PUT, requestHttpEntity, responseExtractor); @@ -113,26 +111,26 @@ public RestResult putFrom(String url, List headers, Map RestResult post(String url, Header header, Query query, Object body, Type responseType) throws Exception { ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.POST, new RequestHttpEntity(header, query, toByte(body)), responseExtractor); + return execute(url, HttpMethod.POST, new RequestHttpEntity(header, query, body), responseExtractor); } @Override - public RestResult postJson(String url, List headers, Map paramValues, String body, Type responseType) throws Exception { + public RestResult postJson(String url, Header header, Map paramValues, String body, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( - Header.newInstance().addAll(headers), + header, Query.newInstance().initParams(paramValues), - toByte(body)); + body); ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); return execute(url, HttpMethod.POST, requestHttpEntity, responseExtractor); } @Override - public RestResult postFrom(String url, List headers, Map paramValues, Map bodyValues, Type responseType) throws Exception { + public RestResult postFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( - Header.newInstance().addAll(headers), + header, Query.newInstance().initParams(paramValues), - toByte(bodyValues)); + bodyValues); ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); return execute(url, HttpMethod.POST, requestHttpEntity, responseExtractor); @@ -160,9 +158,4 @@ private ResponseExtractor> responseEntityExtractor(Type respon return new ResponseEntityExtractor<>(responseType); } - - private byte[] toByte(Object param) throws Exception { - return RequestHandler.parse(param).getBytes(Constants.ENCODE); - } - } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java index cc26dd68ccb..f2453e33831 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java @@ -114,14 +114,14 @@ RestResult put(String url, Header header, Query query, Object body, * http put Json * * @param url url - * @param headers http header param + * @param header http header param * @param paramValues http query param * @param body http body param * @param responseType return type * @return {@link RestResult} * @throws Exception ex */ - RestResult putJson(String url, List headers, Map paramValues, String body, + RestResult putJson(String url, Header header, Map paramValues, String body, Type responseType) throws Exception; @@ -129,14 +129,14 @@ RestResult putJson(String url, List headers, Map * http put from * * @param url url - * @param headers http header param + * @param header http header param * @param paramValues http query param * @param bodyValues http body param * @param responseType return type * @return {@link RestResult} * @throws Exception ex */ - RestResult putFrom(String url, List headers, Map paramValues, + RestResult putFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception; @@ -158,14 +158,14 @@ RestResult post(String url, Header header, Query query, Object body, * http post Json * * @param url url - * @param headers http header param + * @param header http header param * @param paramValues http query param * @param body http body param * @param responseType return type * @return {@link RestResult} * @throws Exception ex */ - RestResult postJson(String url, List headers, Map paramValues, String body, + RestResult postJson(String url, Header header, Map paramValues, String body, Type responseType) throws Exception; @@ -173,13 +173,13 @@ RestResult postJson(String url, List headers, Map * http post from * * @param url url - * @param headers http header param + * @param header http header param * @param paramValues http query param * @param bodyValues http body param * @param responseType return type * @return {@link RestResult} * @throws Exception ex */ - RestResult postFrom(String url, List headers, Map paramValues, + RestResult postFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception; } diff --git a/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java b/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java index 3b341ed8dda..93636e3d9ed 100644 --- a/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java +++ b/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java @@ -1,15 +1,8 @@ package com.alibaba.nacos.common.model; -import com.alibaba.nacos.common.constant.HttpHeaderConsts; -import com.alibaba.nacos.common.http.handler.RequestHandler; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; -import com.alibaba.nacos.common.utils.ByteUtils; -import com.alibaba.nacos.common.utils.IoUtils; -import org.apache.commons.lang3.StringUtils; -import java.net.URI; -import java.util.Arrays; import java.util.Map; /** @@ -25,14 +18,14 @@ public class RequestHttpEntity { private Query query; - private byte[] body; + private Object body; public RequestHttpEntity(Header header, Query query) { handleHeader(header); this.query = query; } - public RequestHttpEntity(Header header, Query query, byte[] body) throws Exception { + public RequestHttpEntity(Header header, Query query, Object body) throws Exception { handleHeader(header); this.query = query; this.body = body; @@ -53,12 +46,12 @@ public Query getQuery() { return query; } - public byte[] getBody() { + public Object getBody() { return body; } public boolean isEmptyBody() { - return body == null || body.length == 0; + return body == null; } From 238b2386a702db384f8b5d7696a33d331defdc94 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Thu, 28 May 2020 18:23:29 +0800 Subject: [PATCH 015/156] change http client implementation, Unified apacheHttpClient --- .../nacos/common/http/BaseHttpMethod.java | 25 +++++++- .../http/client/ApacheHttpClientRequest.java | 14 +++-- .../common/http/client/HttpClientRequest.java | 1 + .../client/HttpMessageConverterExtractor.java | 3 +- .../common/http/client/NacosRestTemplate.java | 39 ++++++++----- .../common/http/client/RestOperations.java | 54 +++++++++++------ .../nacos/common/http/param/Header.java | 3 +- .../nacos/common/http/param/MediaType.java | 2 +- .../nacos/common/model/RequestHttpEntity.java | 7 ++- .../nacos/common/http/HttpUtilsTest.java | 58 ++++++++++++++++--- 10 files changed, 154 insertions(+), 52 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpMethod.java b/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpMethod.java index 9906a272de3..fcc129dfb00 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpMethod.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpMethod.java @@ -16,14 +16,22 @@ package com.alibaba.nacos.common.http; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.common.http.handler.RequestHandler; import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.MediaType; import com.alibaba.nacos.common.utils.HttpMethod; + +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpEntity; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpRequest; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpHead; @@ -34,6 +42,7 @@ import org.apache.http.client.methods.HttpTrace; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; +import org.apache.http.message.BasicNameValuePair; /** * @author liaochuntao @@ -157,7 +166,6 @@ public void initEntity(Object body, String mediaType) throws Exception { if (body == null) { return; } - if (requestBase instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestBase; ContentType contentType = ContentType.create(mediaType); @@ -166,6 +174,21 @@ public void initEntity(Object body, String mediaType) throws Exception { } } + public void initFromEntity(Map body, String charset) throws Exception{ + if (body.isEmpty()) { + return; + } + List params = new ArrayList<>(body.size()); + for (Map.Entry entry : body.entrySet()) { + params.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + } + if (requestBase instanceof HttpEntityEnclosingRequest) { + HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestBase; + HttpEntity entity = new UrlEncodedFormEntity(params, charset); + request.setEntity(entity); + } + } + public HttpRequestBase getRequestBase() { return (HttpRequestBase) requestBase; } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java index 103c8fa41e3..2ae11401fd7 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java @@ -16,19 +16,19 @@ package com.alibaba.nacos.common.http.client; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.http.BaseHttpMethod; import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.MediaType; import com.alibaba.nacos.common.model.RequestHttpEntity; -import org.apache.http.HttpEntity; -import org.apache.http.StatusLine; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.impl.client.CloseableHttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; import java.net.URI; +import java.util.Map; /** * {@link HttpClientRequest} implementation that uses apache http client to @@ -37,6 +37,7 @@ * @author mai.jh * @date 2020/5/24 */ +@SuppressWarnings({"unchecked", "rawtypes", "resource"}) public class ApacheHttpClientRequest implements HttpClientRequest { private static final Logger logger = LoggerFactory.getLogger(NacosRestTemplate.class); @@ -64,7 +65,12 @@ private HttpRequestBase build(URI uri, String method, RequestHttpEntity requestH BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method); httpMethod.init(uri.toString()); httpMethod.initHeader(headers); - httpMethod.initEntity(requestHttpEntity.getBody(), headers.getValue("Content-Type")); + if (MediaType.APPLICATION_FORM_URLENCODED.equals(headers.getValue(HttpHeaderConsts.CONTENT_TYPE)) + && requestHttpEntity.getBody() instanceof Map) { + httpMethod.initFromEntity((Map) requestHttpEntity.getBody(), headers.getCharset()); + } else { + httpMethod.initEntity(requestHttpEntity.getBody(), headers.getValue(HttpHeaderConsts.CONTENT_TYPE)); + } return httpMethod.getRequestBase(); } } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java index 33780cf5424..6d0db11c4eb 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java @@ -29,6 +29,7 @@ */ public interface HttpClientRequest { + /** * execute http request * @param uri http url diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java index 04827241f48..e5636307db6 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java @@ -21,6 +21,7 @@ import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.MediaType; import com.alibaba.nacos.common.utils.IoUtils; +import org.apache.http.HttpStatus; import java.lang.reflect.Type; @@ -45,7 +46,7 @@ public T extractData(HttpClientResponse clientHttpResponse) throws Exception { Header headers = clientHttpResponse.getHeaders(); String value = headers.getValue(HttpHeaderConsts.CONTENT_TYPE); String body = IoUtils.toString(clientHttpResponse.getBody(), headers.getCharset()); - if (MediaType.APPLICATION_JSON.equals(value)) { + if (MediaType.APPLICATION_JSON.equals(value) && HttpStatus.SC_OK == clientHttpResponse.getStatusCode()) { return ResponseHandler.convert(body, responseType); } return (T) body; diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java index 8368354a58a..18fecf8b915 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java @@ -16,7 +16,9 @@ package com.alibaba.nacos.common.http.client; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.MediaType; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.model.RequestHttpEntity; import com.alibaba.nacos.common.model.RestResult; @@ -27,7 +29,6 @@ import java.lang.reflect.Type; import java.net.URI; -import java.util.List; import java.util.Map; /** @@ -43,30 +44,23 @@ public class NacosRestTemplate implements RestOperations { private HttpClientRequest requestClient; public NacosRestTemplate(HttpClientRequest requestClient) { - this.requestClient = requestClient; + this.requestClient = requestClient; } @Override - public RestResult get(String url, Type responseType, Header header, Query query) throws Exception { + public RestResult get(String url, Header header, Query query, Type responseType) throws Exception { ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); return execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseExtractor); } @Override - public RestResult get(String url, Type responseType, List headers, Map paramValues) throws Exception { - RequestHttpEntity requestHttpEntity = new RequestHttpEntity( - Header.newInstance().addAll(headers), - Query.newInstance().initParams(paramValues)); + public RestResult get(String url, Header header, Map paramValues, Type responseType) throws Exception { + RequestHttpEntity requestHttpEntity = new RequestHttpEntity(header, Query.newInstance().initParams(paramValues)); ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); return execute(url, HttpMethod.GET, requestHttpEntity, responseExtractor); } - @Override - public RestResult get(String url, Header header, Query query, Type responseType) throws Exception { - ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseExtractor); - } @Override public RestResult getLarge(String url, Header header, Query query, Object body, Type responseType) throws Exception { @@ -97,10 +91,18 @@ public RestResult putJson(String url, Header header, Map return execute(url, HttpMethod.PUT, requestHttpEntity, responseExtractor); } + @Override + public RestResult putFrom(String url, Header header, Query query, Map bodyValues, Type responseType) throws Exception { + RequestHttpEntity requestHttpEntity = new RequestHttpEntity( + header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues); + ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); + return execute(url, HttpMethod.PUT, requestHttpEntity, responseExtractor); + } + @Override public RestResult putFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( - header, + header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), Query.newInstance().initParams(paramValues), bodyValues); @@ -125,10 +127,18 @@ public RestResult postJson(String url, Header header, Map return execute(url, HttpMethod.POST, requestHttpEntity, responseExtractor); } + @Override + public RestResult postFrom(String url, Header header, Query query, Map bodyValues, Type responseType) throws Exception { + RequestHttpEntity requestHttpEntity = new RequestHttpEntity( + header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues); + ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); + return execute(url, HttpMethod.POST, requestHttpEntity, responseExtractor); + } + @Override public RestResult postFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( - header, + header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), Query.newInstance().initParams(paramValues), bodyValues); @@ -158,4 +168,5 @@ private ResponseExtractor> responseEntityExtractor(Type respon return new ResponseEntityExtractor<>(responseType); } + } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java index f2453e33831..5de1c98813c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java @@ -42,31 +42,19 @@ public interface RestOperations { * @return the RestResult * @throws Exception ex */ - RestResult get(String url, Type responseType, Header header, Query query) throws Exception; + RestResult get(String url, Header header, Query query, Type responseType) throws Exception; /** * http get * * @param url url * @param responseType return type - * @param headers headers + * @param header headers * @param paramValues paramValues * @return the RestResult * @throws Exception ex */ - RestResult get(String url, Type responseType, List headers, Map paramValues) throws Exception; - - /** - * http get - * - * @param url url - * @param responseType return type - * @param header http header param - * @param query http query param - * @return the RestResult - * @throws Exception ex - */ - RestResult get(String url, Header header, Query query, Type responseType) throws Exception; + RestResult get(String url, Header header, Map paramValues, Type responseType) throws Exception; /** * get request, may be pulling a lot of data @@ -114,7 +102,7 @@ RestResult put(String url, Header header, Query query, Object body, * http put Json * * @param url url - * @param header http header param + * @param header http header param * @param paramValues http query param * @param body http body param * @param responseType return type @@ -124,12 +112,26 @@ RestResult put(String url, Header header, Query query, Object body, RestResult putJson(String url, Header header, Map paramValues, String body, Type responseType) throws Exception; + /** + * http put from + * + * @param url url + * @param header http header param + * @param query http query param + * @param bodyValues http body param + * @param responseType return type + * @return {@link RestResult} + * @throws Exception ex + */ + RestResult putFrom(String url, Header header, Query query, + Map bodyValues, Type responseType) throws Exception; + /** * http put from * * @param url url - * @param header http header param + * @param header http header param * @param paramValues http query param * @param bodyValues http body param * @param responseType return type @@ -158,7 +160,7 @@ RestResult post(String url, Header header, Query query, Object body, * http post Json * * @param url url - * @param header http header param + * @param header http header param * @param paramValues http query param * @param body http body param * @param responseType return type @@ -173,7 +175,21 @@ RestResult postJson(String url, Header header, Map paramV * http post from * * @param url url - * @param header http header param + * @param header http header param + * @param query http query param + * @param bodyValues http body param + * @param responseType return type + * @return {@link RestResult} + * @throws Exception ex + */ + RestResult postFrom(String url, Header header, Query query, + Map bodyValues, Type responseType) throws Exception; + + /** + * http post from + * + * @param url url + * @param header http header param * @param paramValues http query param * @param bodyValues http body param * @param responseType return type diff --git a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java index b981270d21f..75c72948381 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java @@ -38,7 +38,7 @@ public class Header { private Header() { header = new LinkedHashMap(); - addParam("Content-Type", "application/json"); + addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_JSON); addParam("Accept-Charset", "UTF-8"); addParam("Accept-Encoding", "gzip"); addParam("Content-Encoding", "gzip"); @@ -117,6 +117,7 @@ private String analysisCharset(String contentType) { + public void clear() { header.clear(); } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java b/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java index 735bb883a55..a5246f811b7 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java @@ -25,7 +25,7 @@ private MediaType() {} public static final String APPLICATION_ATOM_XML = "application/atom+xml"; - public static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded"; + public static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded;charset=utf-8"; public static final String APPLICATION_OCTET_STREAM = "application/octet-stream"; diff --git a/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java b/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java index 93636e3d9ed..1d3b3585d50 100644 --- a/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java +++ b/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java @@ -1,6 +1,8 @@ package com.alibaba.nacos.common.model; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.MediaType; import com.alibaba.nacos.common.http.param.Query; import java.util.Map; @@ -16,7 +18,7 @@ public class RequestHttpEntity { private final Header headers = Header.newInstance(); - private Query query; + private final Query query; private Object body; @@ -25,7 +27,7 @@ public RequestHttpEntity(Header header, Query query) { this.query = query; } - public RequestHttpEntity(Header header, Query query, Object body) throws Exception { + public RequestHttpEntity(Header header, Query query, Object body) { handleHeader(header); this.query = query; this.body = body; @@ -54,5 +56,4 @@ public boolean isEmptyBody() { return body == null; } - } diff --git a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java index b1b11f16c5a..15a3f351fb9 100644 --- a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java @@ -8,6 +8,7 @@ import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.JacksonUtils; import com.google.common.reflect.TypeToken; +import org.junit.Assert; import org.junit.Test; import sun.tools.jstat.Token; @@ -26,23 +27,64 @@ public class HttpUtilsTest { @Test - public void test_url_post() throws Exception{ + public void test_url_get() throws Exception { + String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; + Query query = Query.newInstance().addParam("serviceName", "app-test"); + RestResult restResult = nacosRestTemplate.get(url, Header.newInstance(), query, Map.class); + Assert.assertTrue(restResult.ok()); + Assert.assertEquals(restResult.getData().get("dom"), "app-test"); + System.out.println(JSON.toJSONString(restResult)); + } + + @Test + public void test_url_get_by_map() throws Exception { + String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; + Map param = new HashMap<>(); + param.put("serviceName", "app-test"); + RestResult restResult = nacosRestTemplate.get(url, Header.newInstance(), param, Map.class); + Assert.assertTrue(restResult.ok()); + Assert.assertEquals(restResult.getData().get("dom"), "app-test"); + System.out.println(JSON.toJSONString(restResult)); + } + + @Test + public void test_url_delete() throws Exception{ String url = IP + CONFIG_INSTANCE_PATH + "/instance"; Query query = Query.newInstance() .addParam("ip", "11.11.11.11") .addParam("port", "8080") .addParam("serviceName", "app-test"); + RestResult restResult = nacosRestTemplate.delete(url, Header.newInstance(), query, RestResult.class); + Assert.assertTrue(restResult.ok()); + System.out.println(JSON.toJSONString(restResult)); + } - RestResult post = nacosRestTemplate.post(url, Header.newInstance(), query, null, RestResult.class); - System.out.println(JSON.toJSONString(post)); + @Test + public void test_url_put_from() throws Exception{ + String url = IP + CONFIG_INSTANCE_PATH + "/instance"; + Map param = new HashMap<>(); + param.put("serviceName", "app-test-change"); + param.put("port", "8080"); + param.put("ip", "11.11.11.11"); + RestResult restResult = nacosRestTemplate.putFrom(url, Header.newInstance(), Query.newInstance(), param, String.class); + Assert.assertTrue(restResult.ok()); + System.out.println(JSON.toJSONString(restResult)); } + @Test - public void test_url_get() throws Exception { - String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; - Query query = Query.newInstance().addParam("serviceName", "app-test"); - RestResult get = nacosRestTemplate.get(url, Header.newInstance(), query, Map.class); - System.out.println(JSON.toJSONString(get)); + public void test_url_post_from() throws Exception{ + String url = IP + CONFIG_INSTANCE_PATH + "/instance"; + Map param = new HashMap<>(); + param.put("serviceName", "app-test"); + param.put("port", "8080"); + param.put("ip", "11.11.11.11"); + RestResult restResult = nacosRestTemplate.postFrom(url, Header.newInstance(), Query.newInstance(), param, String.class); + Assert.assertTrue(restResult.ok()); + System.out.println(JSON.toJSONString(restResult)); } + + + } From 0d775ea665b02e4b5e19919e68262f4d29225913 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Thu, 28 May 2020 18:24:58 +0800 Subject: [PATCH 016/156] change http client implementation, Unified apacheHttpClient --- .../nacos/common/http/client/ApacheClientHttpResponse.java | 1 - .../com/alibaba/nacos/common/http/client/RestOperations.java | 1 - 2 files changed, 2 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java index 8d1f3118b75..226a40fc23e 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java @@ -17,7 +17,6 @@ package com.alibaba.nacos.common.http.client; import com.alibaba.nacos.common.http.param.Header; -import org.apache.http.StatusLine; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.utils.HttpClientUtils; diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java index 5de1c98813c..4d1dea3bafd 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java @@ -21,7 +21,6 @@ import com.alibaba.nacos.common.model.RestResult; import java.lang.reflect.Type; -import java.util.List; import java.util.Map; /** From 92ca0e1d66cdd1858521462a0b752d0264ce222e Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Thu, 28 May 2020 22:25:28 +0800 Subject: [PATCH 017/156] Remove extra lines --- .../com/alibaba/nacos/common/http/client/NacosRestTemplate.java | 1 - 1 file changed, 1 deletion(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java index 18fecf8b915..ed944c2a506 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java @@ -61,7 +61,6 @@ public RestResult get(String url, Header header, Map para return execute(url, HttpMethod.GET, requestHttpEntity, responseExtractor); } - @Override public RestResult getLarge(String url, Header header, Query query, Object body, Type responseType) throws Exception { ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); From 886fe6de00cdcefda6c9434c9211c3da40fc2166 Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Fri, 29 May 2020 14:12:32 +0800 Subject: [PATCH 018/156] [#1815]define LifeCycle interface and related abstract class. --- .../common/executor/ExecutorFactory.java | 1 - .../common/lifecycle/AbstractLifeCycle.java | 233 ++++++++++++++++++ .../nacos/common/lifecycle/LifeCycle.java | 75 ++++++ .../common/lifecycle/LifeCycleListener.java | 48 ++++ .../common/lifecycle/LifeCycleState.java | 36 +++ 5 files changed, 392 insertions(+), 1 deletion(-) create mode 100644 common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleListener.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java diff --git a/common/src/main/java/com/alibaba/nacos/common/executor/ExecutorFactory.java b/common/src/main/java/com/alibaba/nacos/common/executor/ExecutorFactory.java index c3e0daa9c92..55f8925fe9b 100644 --- a/common/src/main/java/com/alibaba/nacos/common/executor/ExecutorFactory.java +++ b/common/src/main/java/com/alibaba/nacos/common/executor/ExecutorFactory.java @@ -16,7 +16,6 @@ package com.alibaba.nacos.common.executor; -import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ForkJoinPool; diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java new file mode 100644 index 00000000000..471ab4f295a --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java @@ -0,0 +1,233 @@ +package com.alibaba.nacos.common.lifecycle; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.EventListener; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +/** + * Basic implementation of the life cycle interface for services. + * + * @author zongtanghu + */ +public abstract class AbstractLifeCycle implements LifeCycle{ + + private static final Logger LOG = LoggerFactory.getLogger(AbstractLifeCycle.class); + + private final Object lock = new Object(); + private volatile LifeCycleState state = LifeCycleState.STOPPED; + private final List eventListeners = new CopyOnWriteArrayList<>(); + + /** + * Method to override to start the lifecycle. + * + * @throws Exception If there was a problem starting. Will cause a transition to FAILED state + */ + protected void doStart() throws Exception { + } + + /** + * Method to override to stop the lifecycle. + * + * @throws Exception If there was a problem stopping. Will cause a transition to FAILED state + */ + protected void doStop() throws Exception { + } + + @Override + public void start() throws Exception { + synchronized (this.lock) + { + try + { + switch (state) { + case STARTED: + return; + case STARTING: + case STOPPING: + throw new IllegalStateException(getState()); + default: + try { + setStarting(); + doStart(); + setStarted(); + } catch (Exception e) { + if (LOG.isDebugEnabled()) + LOG.debug("Unable to stop", e); + setStopping(); + doStop(); + setStopped(); + } + } + } catch (Throwable e) { + setFailed(e); + throw e; + } + } + } + + @Override + public void stop() throws Exception { + synchronized (this.lock) { + try + { + switch (this.state) { + case STOPPED: + return; + case STARTING: + case STOPPING: + throw new IllegalStateException(getState()); + default: + setStopping(); + doStop(); + setStopped(); + } + } catch (Throwable e) { + setFailed(e); + throw e; + } + } + } + + @Override + public boolean isRunning() { + switch (this.state) + { + case STARTED: + case STARTING: + return true; + default: + return false; + } + } + + @Override + public boolean isStarted() { + return this.state == LifeCycleState.STARTED; + } + + @Override + public boolean isStarting() { + return this.state == LifeCycleState.STARTING; + } + + @Override + public boolean isStopping() { + return this.state == LifeCycleState.STOPPING; + } + + @Override + public boolean isStopped() { + return this.state == LifeCycleState.STOPPED; + } + + @Override + public boolean isFailed() { + return this.state == LifeCycleState.FAILED; + } + + @Override + public boolean addLifeCycleListener(LifeCycleListener listener) { + if (this.eventListeners.contains(listener)) { + return false; + } + return this.eventListeners.add(listener); + } + + @Override + public boolean removeLifeCycleListener(LifeCycleListener listener) { + return this.eventListeners.remove(listener); + } + + /** + * Get the service's current state and return. + * + * @return service's current state. + */ + public String getState() { + return this.state.toString(); + } + + /** + * If the service's current status is STARTING state, it will set STOPPED state. + * And the method will execute lifeCycleStopped event if it has already been registered before. + * + */ + private void setStarted() { + if (this.state == LifeCycleState.STARTING) { + this.state = LifeCycleState.STARTED; + if (LOG.isDebugEnabled()) + LOG.debug("STARTED {}", this); + + for (EventListener listener : eventListeners) + if (listener instanceof LifeCycleListener) + ((LifeCycleListener)listener).lifeCycleStarted(this); + } + } + + /** + * The service which implement AbstractLifeCycle will set STARTING state. + * And it will execute lifeCycleStarting event if it has already been registered before. + * + */ + private void setStarting() { + if (LOG.isDebugEnabled()) + LOG.debug("STARTING {}", this); + this.state = LifeCycleState.STARTING; + + for (EventListener listener : this.eventListeners) { + if (listener instanceof LifeCycleListener) + ((LifeCycleListener) listener).lifeCycleStarting(this); + } + } + + /** + * The service which implement AbstractLifeCycle will set STOPPING state. + * And it will execute lifeCycleStopping event if it has been registered before. + * + */ + private void setStopping() { + if (LOG.isDebugEnabled()) + LOG.debug("STOPPING {}", this); + this.state = LifeCycleState.STOPPING; + for (EventListener listener : this.eventListeners) { + if (listener instanceof LifeCycleListener) + ((LifeCycleListener) listener).lifeCycleStopping(this); + } + } + + /** + * If the service's current status is STARTING state, it will set STOPPED state. + * And the method will execute lifeCycleStopped event if it has already been registered before. + * + */ + private void setStopped() { + if (this.state == LifeCycleState.STOPPING) { + this.state = LifeCycleState.STOPPED; + if (LOG.isDebugEnabled()) + LOG.debug("STOPPED {}", this); + for (EventListener listener : this.eventListeners) { + if (listener instanceof LifeCycleListener) + ((LifeCycleListener) listener).lifeCycleStopped(this); + } + } + } + + /** + * If some exceptions happen, the service will set FAILED state. And it will + * execute lifeCycleFailure event if it has already been registered before. + * + * @param tb Exception which happens. + */ + private void setFailed(Throwable tb) { + this.state = LifeCycleState.FAILED; + if (LOG.isDebugEnabled()) + LOG.warn("FAILED " + this + ": " + tb, tb); + for (EventListener listener : this.eventListeners) { + if (listener instanceof LifeCycleListener) + ((LifeCycleListener)listener).lifeCycleFailure(this, tb); + } + } +} diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java new file mode 100644 index 00000000000..4ab7e2c7285 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java @@ -0,0 +1,75 @@ +package com.alibaba.nacos.common.lifecycle; + +/** + * + * The lifecycle interface for generic service. Classes are need to implement + * this interface have a defined life cycle defined by the methods of this interface. + * + * @author zongtanghu + */ +public interface LifeCycle { + + /** + * Starts the service. + * + * @throws Exception If the service fails to start. + */ + public void start() throws Exception; + + /** + * Stops the service. + * + * @throws Exception If the service fails to stop. + */ + public void stop() throws Exception; + + /** + * @return true if the component is starting or has been started. + */ + public boolean isRunning(); + + /** + * @return true if the service has been started. + * + */ + public boolean isStarted(); + + /** + * @return true if the component is starting. + * + */ + public boolean isStarting(); + + /** + * @return true if the service is stopping. + * + */ + public boolean isStopping(); + + /** + * @return true if the service has been stopped. + * + */ + public boolean isStopped(); + + /** + * @return true if the component has failed to start or has failed to stop. + */ + public boolean isFailed(); + + /** + * add a LifeCycleListener. + * + * @param listener event listener + * @return the result whether to add LifeCycleListener or not. + */ + public boolean addLifeCycleListener(LifeCycleListener listener); + + /** + * remove a LifeCycleListener. + * + * @param listener event listener + * @return the result whether to remove LifeCycleListener or not. + */ + public boolean removeLifeCycleListener(LifeCycleListener listener); +} diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleListener.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleListener.java new file mode 100644 index 00000000000..de423973651 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleListener.java @@ -0,0 +1,48 @@ +package com.alibaba.nacos.common.lifecycle; + + +import java.util.EventListener; + +/** + * A listener for Lifecycle events. + * + * @author zongtanghu + */ +public interface LifeCycleListener extends EventListener { + + /** + * Listening for sevice starting event. + * + * @param event + */ + default void lifeCycleStarting(LifeCycle event) { } + + /** + * Listening for sevice started event. + * + * @param event + */ + default void lifeCycleStarted(LifeCycle event) { } + + /** + * Listening for sevice failure event. + * + * @param event + * @param cause + */ + default void lifeCycleFailure(LifeCycle event, Throwable cause) { } + + /** + * Listening for sevice stopping event. + * + * @param event + */ + default void lifeCycleStopping(LifeCycle event) { } + + /** + * Listening for sevice stopped event. + * + * @param event + */ + default void lifeCycleStopped(LifeCycle event) { } +} diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java new file mode 100644 index 00000000000..3b51300624d --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java @@ -0,0 +1,36 @@ +package com.alibaba.nacos.common.lifecycle; + +/** + * Define serveral lifecycle state. + * + * @author zongtanghu + */ +public enum LifeCycleState { + + STOPPED("STOPPED"), + STARTING("STARTING"), + STARTED("STARTED"), + STOPPING("STOPPING"), + FAILED("FAILED"); + + private String name; + + LifeCycleState(String name) { + this.name = name; + } + + @Override + public String toString() { + return "LifeCycleState{" + + "name='" + name + '\'' + + '}'; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} From 57ca56c07bbc18ce44ed845607557b239a832948 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Fri, 29 May 2020 18:09:04 +0800 Subject: [PATCH 019/156] add async http request client --- .../nacos/common/http/HttpClientManager.java | 16 +- .../client/ApacheAsyncHttpClientRequest.java | 84 ++++++++ .../http/client/ApacheClientHttpResponse.java | 6 +- .../http/client/ApacheHttpClientRequest.java | 11 +- .../http/client/AsyncHttpClientRequest.java | 48 +++++ .../http/client/AsyncRestOperations.java | 201 ++++++++++++++++++ .../common/http/client/HttpClientRequest.java | 3 +- .../http/client/HttpClientResponse.java | 2 +- .../client/HttpMessageConverterExtractor.java | 56 ----- .../http/client/NacosAsyncRestTemplate.java | 171 +++++++++++++++ .../common/http/client/NacosRestTemplate.java | 55 +++-- .../http/client/ResponseEntityExtractor.java | 49 ----- .../common/http/client/ResponseExtractor.java | 36 ---- .../common/http/client/RestOperations.java | 4 +- .../common/http/handler/ResponseHandler.java | 22 ++ .../nacos/common/http/AsyncHttpUtils.java | 72 +++++++ .../nacos/common/http/HttpUtilsTest.java | 5 - 17 files changed, 653 insertions(+), 188 deletions(-) create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/ApacheAsyncHttpClientRequest.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/AsyncHttpClientRequest.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/AsyncRestOperations.java delete mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java delete mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java delete mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java create mode 100644 common/src/test/java/com/alibaba/nacos/common/http/AsyncHttpUtils.java diff --git a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java index 265069ae601..0272c1b6128 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java @@ -16,7 +16,9 @@ package com.alibaba.nacos.common.http; +import com.alibaba.nacos.common.http.client.ApacheAsyncHttpClientRequest; import com.alibaba.nacos.common.http.client.ApacheHttpClientRequest; +import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate; import com.alibaba.nacos.common.http.client.NacosRestTemplate; import com.alibaba.nacos.common.utils.ShutdownUtils; import org.apache.http.client.config.RequestConfig; @@ -46,8 +48,12 @@ public class HttpClientManager { private static final NAsyncHttpClient ASYNC_HTTP_CLIENT = new NacosAsyncHttpClient( HttpAsyncClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build()); - private static final NacosRestTemplate NACOS_REST_TEMPLATE = new NacosRestTemplate - (new ApacheHttpClientRequest(HttpClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build())); + private static final NacosRestTemplate NACOS_REST_TEMPLATE = new NacosRestTemplate( + new ApacheHttpClientRequest(HttpClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build())); + + private static final NacosAsyncRestTemplate NACOS_ASYNC_REST_TEMPLATE = new NacosAsyncRestTemplate( + new ApacheAsyncHttpClientRequest(HttpAsyncClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build())); + static { ShutdownUtils.addShutdownHook(new Runnable() { @@ -57,6 +63,8 @@ public void run() { try { SYNC_HTTP_CLIENT.close(); ASYNC_HTTP_CLIENT.close(); + NACOS_REST_TEMPLATE.close(); + NACOS_ASYNC_REST_TEMPLATE.close(); } catch (Exception ignore) { } @@ -78,4 +86,8 @@ public static NacosRestTemplate getNacosRestTemplate() { return NACOS_REST_TEMPLATE; } + public static NacosAsyncRestTemplate getNacosAsyncRestTemplate() { + return NACOS_ASYNC_REST_TEMPLATE; + } + } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheAsyncHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheAsyncHttpClientRequest.java new file mode 100644 index 00000000000..436c38979b9 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheAsyncHttpClientRequest.java @@ -0,0 +1,84 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.http.client; + + +import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.http.handler.ResponseHandler; +import com.alibaba.nacos.common.model.RequestHttpEntity; +import com.alibaba.nacos.common.model.RestResult; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.concurrent.FutureCallback; +import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.net.URI; + +/** + * {@link AsyncHttpClientRequest} implementation that uses apache async http client to + * execute streaming requests + * + * @author mai.jh + * @date 2020/5/29 + */ +public class ApacheAsyncHttpClientRequest implements AsyncHttpClientRequest { + + private final CloseableHttpAsyncClient asyncClient; + + public ApacheAsyncHttpClientRequest(CloseableHttpAsyncClient asyncClient) { + this.asyncClient = asyncClient; + if (!this.asyncClient.isRunning()) { + this.asyncClient.start(); + } + } + + @Override + public void execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity, final Type responseType, final Callback callback) throws Exception { + HttpRequestBase httpRequestBase = ApacheHttpClientRequest.build(uri, httpMethod, requestHttpEntity); + asyncClient.execute(httpRequestBase, new FutureCallback() { + @Override + public void completed(HttpResponse result) { + ApacheClientHttpResponse response = new ApacheClientHttpResponse(result); + try { + RestResult restResult = ResponseHandler.responseEntityExtractor(response, responseType); + callback.onReceive(restResult); + } catch (Exception e) { + callback.onError(e); + } + } + + @Override + public void failed(Exception ex) { + callback.onError(ex); + } + + @Override + public void cancelled() { + + } + }); + + } + + + @Override + public void close() throws IOException { + this.asyncClient.close(); + } +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java index 226a40fc23e..ab410c76cdd 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java @@ -17,7 +17,7 @@ package com.alibaba.nacos.common.http.client; import com.alibaba.nacos.common.http.param.Header; -import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.HttpResponse; import org.apache.http.client.utils.HttpClientUtils; @@ -33,11 +33,11 @@ */ public class ApacheClientHttpResponse implements HttpClientResponse { - private CloseableHttpResponse response; + private HttpResponse response; private Header responseHeader; - public ApacheClientHttpResponse(CloseableHttpResponse response) { + public ApacheClientHttpResponse(HttpResponse response) { this.response = response; } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java index 2ae11401fd7..aee8d794ff8 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java @@ -27,6 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; import java.net.URI; import java.util.Map; @@ -42,8 +43,7 @@ public class ApacheHttpClientRequest implements HttpClientRequest { private static final Logger logger = LoggerFactory.getLogger(NacosRestTemplate.class); - - private CloseableHttpClient client; + private final CloseableHttpClient client; public ApacheHttpClientRequest(CloseableHttpClient client) { this.client = client; @@ -60,7 +60,7 @@ public HttpClientResponse execute(URI uri, String httpMethod, RequestHttpEntity } - private HttpRequestBase build(URI uri, String method, RequestHttpEntity requestHttpEntity) throws Exception { + static HttpRequestBase build(URI uri, String method, RequestHttpEntity requestHttpEntity) throws Exception { Header headers = requestHttpEntity.getHeaders(); BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method); httpMethod.init(uri.toString()); @@ -73,4 +73,9 @@ private HttpRequestBase build(URI uri, String method, RequestHttpEntity requestH } return httpMethod.getRequestBase(); } + + @Override + public void close() throws IOException { + client.close(); + } } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/AsyncHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/AsyncHttpClientRequest.java new file mode 100644 index 00000000000..365460a8a63 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/AsyncHttpClientRequest.java @@ -0,0 +1,48 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.http.client; + +import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.model.RequestHttpEntity; + +import java.io.Closeable; +import java.lang.reflect.Type; +import java.net.URI; + +/** + * Represents a client-side Async HTTP request. + * Created via an implementation execute. + * + * @author mai.jh + * @date 2020/5/29 + */ +public interface AsyncHttpClientRequest extends Closeable { + + + /** + * execute async http request + * + * @param uri http url + * @param httpMethod http request method + * @param requestHttpEntity http request entity + * @param responseType http response type + * @param callback http response callback + * @throws Exception ex + */ + void execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity, + final Type responseType, final Callback callback) throws Exception; +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/AsyncRestOperations.java b/common/src/main/java/com/alibaba/nacos/common/http/client/AsyncRestOperations.java new file mode 100644 index 00000000000..a1a2c099ea5 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/AsyncRestOperations.java @@ -0,0 +1,201 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.http.client; + +import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.Query; + +import java.lang.reflect.Type; +import java.util.Map; + +/** + * Interface specifying a basic set of async http operations. + * + * @author mai.jh + * @date 2020/5/23 + */ +public interface AsyncRestOperations { + + /** + * http get + * + * @param url url + * @param responseType return type + * @param header http header param + * @param query http query param + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ + void get(String url, Header header, Query query, Type responseType, Callback callback) throws Exception; + + /** + * http get + * + * @param url url + * @param header headers + * @param paramValues paramValues + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ + void get(String url, Header header, Map paramValues, + Type responseType, Callback callback) throws Exception; + + /** + * get request, may be pulling a lot of data + * + * @param url url + * @param header http header param + * @param query http query param + * @param body get with body + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ + void getLarge(String url, Header header, Query query, Object body, + Type responseType, Callback callback) throws Exception; + + /** + * http delete + * + * @param url url + * @param header http header param + * @param query http query param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ + void delete(String url, Header header, Query query, + Type responseType, Callback callback) throws Exception; + + /** + * http put + * + * @param url url + * @param header http header param + * @param query http query param + * @param body http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ + void put(String url, Header header, Query query, Object body, + Type responseType, Callback callback) throws Exception; + + + /** + * http put Json + * + * @param url url + * @param header http header param + * @param paramValues http query param + * @param body http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ + void putJson(String url, Header header, Map paramValues, String body, + Type responseType, Callback callback) throws Exception; + + /** + * http put from + * + * @param url url + * @param header http header param + * @param query http query param + * @param bodyValues http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ + void putFrom(String url, Header header, Query query, + Map bodyValues, Type responseType, Callback callback) throws Exception; + + + /** + * http put from + * + * @param url url + * @param header http header param + * @param paramValues http query param + * @param bodyValues http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ + void putFrom(String url, Header header, Map paramValues, + Map bodyValues, Type responseType, Callback callback) throws Exception; + + + /** + * http post + * + * @param url url + * @param header http header param + * @param query http query param + * @param body http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ + void post(String url, Header header, Query query, Object body, + Type responseType, Callback callback) throws Exception; + + /** + * http post Json + * + * @param url url + * @param header http header param + * @param paramValues http query param + * @param body http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ + void postJson(String url, Header header, Map paramValues, String body, + Type responseType, Callback callback) throws Exception; + + + /** + * http post from + * + * @param url url + * @param header http header param + * @param query http query param + * @param bodyValues http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ + void postFrom(String url, Header header, Query query, + Map bodyValues, Type responseType, Callback callback) throws Exception; + + /** + * http post from + * + * @param url url + * @param header http header param + * @param paramValues http query param + * @param bodyValues http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ + void postFrom(String url, Header header, Map paramValues, + Map bodyValues, Type responseType, Callback callback) throws Exception; +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java index 6d0db11c4eb..ab6999b601d 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.common.model.RequestHttpEntity; +import java.io.Closeable; import java.net.URI; /** @@ -27,7 +28,7 @@ * @author mai.jh * @date 2020/5/23 */ -public interface HttpClientRequest { +public interface HttpClientRequest extends Closeable { /** diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientResponse.java index e1a0d49c089..6e1ad5db7ab 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientResponse.java @@ -33,7 +33,7 @@ * @author mai.jh * @date 2020/5/23 */ -public interface HttpClientResponse extends Closeable{ +public interface HttpClientResponse extends Closeable { /** * Return the headers of this message. diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java deleted file mode 100644 index e5636307db6..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpMessageConverterExtractor.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.alibaba.nacos.common.http.client; - -import com.alibaba.nacos.common.constant.HttpHeaderConsts; -import com.alibaba.nacos.common.http.handler.ResponseHandler; -import com.alibaba.nacos.common.http.param.Header; -import com.alibaba.nacos.common.http.param.MediaType; -import com.alibaba.nacos.common.utils.IoUtils; -import org.apache.http.HttpStatus; - -import java.lang.reflect.Type; - -/** - * HTTP Message Converter - * to convert the response into a type {@code T}. - * - * @author mai.jh - * @date 2020/5/27 - */ -@SuppressWarnings({"unchecked", "rawtypes", "resource"}) -public class HttpMessageConverterExtractor implements ResponseExtractor{ - - private Type responseType; - - public HttpMessageConverterExtractor(Type responseType) { - this.responseType = responseType; - } - - @Override - public T extractData(HttpClientResponse clientHttpResponse) throws Exception { - Header headers = clientHttpResponse.getHeaders(); - String value = headers.getValue(HttpHeaderConsts.CONTENT_TYPE); - String body = IoUtils.toString(clientHttpResponse.getBody(), headers.getCharset()); - if (MediaType.APPLICATION_JSON.equals(value) && HttpStatus.SC_OK == clientHttpResponse.getStatusCode()) { - return ResponseHandler.convert(body, responseType); - } - return (T) body; - } - - -} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java new file mode 100644 index 00000000000..c497e14bd04 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java @@ -0,0 +1,171 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.common.http.client; + +import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.MediaType; +import com.alibaba.nacos.common.http.param.Query; +import com.alibaba.nacos.common.model.RequestHttpEntity; +import com.alibaba.nacos.common.utils.HttpMethod; +import com.alibaba.nacos.common.utils.UriUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Type; +import java.net.URI; +import java.util.Map; + +/** + * NacosAsyncRestTemplate async + * + * @author mai.jh + * @date 2020/5/29 + * @see AsyncHttpClientRequest + * @see HttpClientResponse + */ +public class NacosAsyncRestTemplate implements AsyncRestOperations { + + private static final Logger logger = LoggerFactory.getLogger(NacosAsyncRestTemplate.class); + + + private AsyncHttpClientRequest clientRequest; + + public NacosAsyncRestTemplate(AsyncHttpClientRequest clientRequest) { + this.clientRequest = clientRequest; + } + + + @Override + public void get(String url, Header header, Query query, Type responseType, Callback callback) throws Exception { + + execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseType, callback); + } + + @Override + public void get(String url, Header header, Map paramValues, + Type responseType, Callback callback) throws Exception { + + execute(url, HttpMethod.GET, new RequestHttpEntity(header, + Query.newInstance().initParams(paramValues)), responseType, callback); + } + + @Override + public void getLarge(String url, Header header, Query query, Object body, + Type responseType, Callback callback) throws Exception { + + execute(url, HttpMethod.GET_LARGE, + new RequestHttpEntity(header, query, body), responseType, callback); + } + + @Override + public void delete(String url, Header header, Query query, + Type responseType, Callback callback) throws Exception { + + execute(url, HttpMethod.DELETE, + new RequestHttpEntity(header, query), responseType, callback); + } + + @Override + public void put(String url, Header header, Query query, Object body, + Type responseType, Callback callback) throws Exception { + + execute(url, HttpMethod.PUT, + new RequestHttpEntity(header, query, body), responseType, callback); + } + + @Override + public void putJson(String url, Header header, Map paramValues, + String body, Type responseType, Callback callback) throws Exception { + + execute(url, HttpMethod.PUT, new RequestHttpEntity( + header, Query.newInstance().initParams(paramValues), body), responseType, callback); + + } + + @Override + public void putFrom(String url, Header header, Query query, Map bodyValues, + Type responseType, Callback callback) throws Exception { + + execute(url, HttpMethod.PUT, new RequestHttpEntity( + header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), + query, bodyValues), responseType, callback); + } + + @Override + public void putFrom(String url, Header header, Map paramValues, + Map bodyValues, Type responseType, Callback callback) throws Exception { + + execute(url, HttpMethod.PUT, new RequestHttpEntity( + header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), + Query.newInstance().initParams(paramValues), bodyValues), responseType, callback); + } + + @Override + public void post(String url, Header header, Query query, Object body, + Type responseType, Callback callback) throws Exception { + + execute(url, HttpMethod.POST, new RequestHttpEntity( + header, query, body), responseType, callback); + } + + @Override + public void postJson(String url, Header header, Map paramValues, + String body, Type responseType, Callback callback) throws Exception { + + execute(url, HttpMethod.POST, new RequestHttpEntity( + header, Query.newInstance().initParams(paramValues), body), responseType, callback); + } + + @Override + public void postFrom(String url, Header header, Query query, Map bodyValues, + Type responseType, Callback callback) throws Exception { + + execute(url, HttpMethod.POST, new RequestHttpEntity( + header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues), + responseType, callback); + } + + @Override + public void postFrom(String url, Header header, Map paramValues, + Map bodyValues, Type responseType, Callback callback) throws Exception { + + execute(url, HttpMethod.POST, new RequestHttpEntity( + header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), + Query.newInstance().initParams(paramValues), + bodyValues), responseType, callback); + + } + + private void execute(String url, String httpMethod, RequestHttpEntity requestEntity, + Type responseType, Callback callback) throws Exception { + + URI uri = UriUtils.buildUri(url, requestEntity.getQuery()); + if (logger.isDebugEnabled()) { + logger.debug("HTTP " + httpMethod + " " + url); + } + clientRequest.execute(uri, httpMethod, requestEntity, responseType, callback); + } + + /** + * close request client + */ + public void close() throws Exception { + clientRequest.close(); + } +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java index ed944c2a506..9825775dfa3 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.common.http.client; import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.http.handler.ResponseHandler; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.MediaType; import com.alibaba.nacos.common.http.param.Query; @@ -32,10 +33,12 @@ import java.util.Map; /** - * NacosRestTemplate, refer to the design of Spring's RestTemplate + * NacosRestTemplate * * @author mai.jh * @date 2020/5/24 + * @see HttpClientRequest + * @see HttpClientResponse */ public class NacosRestTemplate implements RestOperations { @@ -49,34 +52,29 @@ public NacosRestTemplate(HttpClientRequest requestClient) { @Override public RestResult get(String url, Header header, Query query, Type responseType) throws Exception { - ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseExtractor); + return execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseType); } @Override public RestResult get(String url, Header header, Map paramValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity(header, Query.newInstance().initParams(paramValues)); - ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.GET, requestHttpEntity, responseExtractor); + return execute(url, HttpMethod.GET, requestHttpEntity, responseType); } @Override public RestResult getLarge(String url, Header header, Query query, Object body, Type responseType) throws Exception { - ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.GET_LARGE, new RequestHttpEntity(header, query, body), responseExtractor); + return execute(url, HttpMethod.GET_LARGE, new RequestHttpEntity(header, query, body), responseType); } @Override public RestResult delete(String url, Header header, Query query, Type responseType) throws Exception { - ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.DELETE, new RequestHttpEntity(header, query), responseExtractor); + return execute(url, HttpMethod.DELETE, new RequestHttpEntity(header, query), responseType); } @Override public RestResult put(String url, Header header, Query query, Object body, Type responseType) throws Exception { - ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.PUT, new RequestHttpEntity(header, query, body), responseExtractor); + return execute(url, HttpMethod.PUT, new RequestHttpEntity(header, query, body), responseType); } @Override @@ -86,16 +84,14 @@ public RestResult putJson(String url, Header header, Map Query.newInstance().initParams(paramValues), body); - ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.PUT, requestHttpEntity, responseExtractor); + return execute(url, HttpMethod.PUT, requestHttpEntity, responseType); } @Override public RestResult putFrom(String url, Header header, Query query, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues); - ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.PUT, requestHttpEntity, responseExtractor); + return execute(url, HttpMethod.PUT, requestHttpEntity, responseType); } @Override @@ -105,14 +101,12 @@ public RestResult putFrom(String url, Header header, Map Query.newInstance().initParams(paramValues), bodyValues); - ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.PUT, requestHttpEntity, responseExtractor); + return execute(url, HttpMethod.PUT, requestHttpEntity, responseType); } @Override public RestResult post(String url, Header header, Query query, Object body, Type responseType) throws Exception { - ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.POST, new RequestHttpEntity(header, query, body), responseExtractor); + return execute(url, HttpMethod.POST, new RequestHttpEntity(header, query, body), responseType); } @Override @@ -122,16 +116,14 @@ public RestResult postJson(String url, Header header, Map Query.newInstance().initParams(paramValues), body); - ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.POST, requestHttpEntity, responseExtractor); + return execute(url, HttpMethod.POST, requestHttpEntity, responseType); } @Override public RestResult postFrom(String url, Header header, Query query, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues); - ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.POST, requestHttpEntity, responseExtractor); + return execute(url, HttpMethod.POST, requestHttpEntity, responseType); } @Override @@ -141,13 +133,12 @@ public RestResult postFrom(String url, Header header, Map Query.newInstance().initParams(paramValues), bodyValues); - ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); - return execute(url, HttpMethod.POST, requestHttpEntity, responseExtractor); + return execute(url, HttpMethod.POST, requestHttpEntity, responseType); } - private T execute(String url, String httpMethod, RequestHttpEntity requestEntity, - ResponseExtractor responseExtractor) throws Exception { + private RestResult execute(String url, String httpMethod, RequestHttpEntity requestEntity, + Type responseType) throws Exception { URI uri = UriUtils.buildUri(url, requestEntity.getQuery()); if (logger.isDebugEnabled()) { logger.debug("HTTP " + httpMethod + " " + url); @@ -155,7 +146,7 @@ private T execute(String url, String httpMethod, RequestHttpEntity requestEn HttpClientResponse response = null; try { response = requestClient.execute(uri, httpMethod, requestEntity); - return responseExtractor.extractData(response); + return ResponseHandler.responseEntityExtractor(response, responseType); } finally { if (response != null) { response.close(); @@ -163,9 +154,13 @@ private T execute(String url, String httpMethod, RequestHttpEntity requestEn } } - private ResponseExtractor> responseEntityExtractor(Type responseType) { - return new ResponseEntityExtractor<>(responseType); + /** + * close request client + */ + public void close() throws Exception{ + requestClient.close(); } + } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java deleted file mode 100644 index 56bf374b5c1..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseEntityExtractor.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.alibaba.nacos.common.http.client; - -import com.alibaba.nacos.common.model.RestResult; - -import java.lang.reflect.Type; - -/** - * Response extractor for - * - * @author mai.jh - * @date 2020/5/27 - */ -@SuppressWarnings({"unchecked", "rawtypes", "resource"}) -public class ResponseEntityExtractor implements ResponseExtractor { - - private Type responseType; - - private final HttpMessageConverterExtractor delegate; - - public ResponseEntityExtractor(Type responseType) { - this.responseType = responseType; - this.delegate = new HttpMessageConverterExtractor(responseType); - } - - @Override - public T extractData(HttpClientResponse response) throws Exception { - T body = this.delegate.extractData(response); - if (body instanceof RestResult) { - return body; - } - return (T) new RestResult<>(response.getHeaders(), response.getStatusCode(), body); - } -} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java deleted file mode 100644 index 6a6b85de9dc..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ResponseExtractor.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.alibaba.nacos.common.http.client; - -/** - * - * Generic callback interface used by {@link NacosRestTemplate}'s retrieval methods - * Implementations of this interface perform the actual work of extracting data - * - * @author mai.jh - * @date 2020/5/27 - */ -public interface ResponseExtractor { - - /** - * Extract data from the given {@code ClientHttpResponse} and return it. - * @param clientHttpResponse http response - * @return the extracted data - * @throws Exception ex - */ - T extractData(HttpClientResponse clientHttpResponse) throws Exception; -} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java index 4d1dea3bafd..d8c28b5dcb3 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java @@ -35,9 +35,9 @@ public interface RestOperations { * http get * * @param url url - * @param responseType return type * @param header http header param * @param query http query param + * @param responseType return type * @return the RestResult * @throws Exception ex */ @@ -47,9 +47,9 @@ public interface RestOperations { * http get * * @param url url - * @param responseType return type * @param header headers * @param paramValues paramValues + * @param responseType return type * @return the RestResult * @throws Exception ex */ diff --git a/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java b/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java index ce0e531c803..aea2dfc0787 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java @@ -16,7 +16,14 @@ package com.alibaba.nacos.common.http.handler; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.http.client.HttpClientResponse; +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.MediaType; +import com.alibaba.nacos.common.model.RestResult; +import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.JacksonUtils; +import org.apache.http.HttpStatus; import org.slf4j.LoggerFactory; import java.io.InputStream; @@ -42,4 +49,19 @@ public static T convert(InputStream inputStream, Class tClass) throws Exc return JacksonUtils.toObj(inputStream, tClass); } + @SuppressWarnings({"unchecked", "rawtypes", "resource"}) + public static RestResult responseEntityExtractor(HttpClientResponse response, Type type) throws Exception{ + Header headers = response.getHeaders(); + String contentType = headers.getValue(HttpHeaderConsts.CONTENT_TYPE); + String body = IoUtils.toString(response.getBody(), headers.getCharset()); + T extractBody = (T) body; + if (MediaType.APPLICATION_JSON.equals(contentType) && HttpStatus.SC_OK == response.getStatusCode()) { + extractBody = convert(body, type); + } + if (extractBody instanceof RestResult) { + return (RestResult) extractBody; + } + return new RestResult<>(response.getHeaders(), response.getStatusCode(), extractBody); + } + } diff --git a/common/src/test/java/com/alibaba/nacos/common/http/AsyncHttpUtils.java b/common/src/test/java/com/alibaba/nacos/common/http/AsyncHttpUtils.java new file mode 100644 index 00000000000..e72ac7fc8e7 --- /dev/null +++ b/common/src/test/java/com/alibaba/nacos/common/http/AsyncHttpUtils.java @@ -0,0 +1,72 @@ +package com.alibaba.nacos.common.http; + +import com.alibaba.fastjson.JSON; +import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate; +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.Query; +import com.alibaba.nacos.common.model.RestResult; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * AsyncHttpUtils + * + * @author mai.jh + * @date 2020/5/29 + */ +public class AsyncHttpUtils { + + private NacosAsyncRestTemplate nacosRestTemplate = HttpClientManager.getNacosAsyncRestTemplate(); + + private final String CONFIG_CONTROLLER_PATH = "/nacos/v1/cs"; + private final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns"; + private final String IP = "http://localhost:8848"; + + @Test + public void test_url_get() throws Exception{ + String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; + Query query = Query.newInstance().addParam("serviceName", "app-test"); + nacosRestTemplate.get(url, Header.newInstance(), query, Map.class, new Callback() { + @Override + public void onReceive(RestResult result) { + System.out.println(JSON.toJSONString(result)); +// Assert.assertTrue(result.ok()); +// Assert.assertEquals(result.getData().get("dom"), "app-test"); + } + + @Override + public void onError(Throwable throwable) { + + } + }); + Thread.sleep(20000); + + } + + @Test + public void test_url_post_from() throws Exception{ + String url = IP + CONFIG_INSTANCE_PATH + "/instance"; + Map param = new HashMap<>(); + param.put("serviceName", "app-test"); + param.put("port", "8080"); + param.put("ip", "11.11.11.11"); + nacosRestTemplate.postFrom(url, Header.newInstance(), Query.newInstance(), param, String.class, new Callback() { + @Override + public void onReceive(RestResult result) { + System.out.println(JSON.toJSONString(result)); +// Assert.assertTrue(result.ok()); + } + + @Override + public void onError(Throwable throwable) { + System.out.println(throwable.getMessage()); + } + }); + Thread.sleep(20000); + } + + +} diff --git a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java index 15a3f351fb9..1b12f0b42a3 100644 --- a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java @@ -2,17 +2,12 @@ import com.alibaba.fastjson.JSON; import com.alibaba.nacos.common.http.client.NacosRestTemplate; -import com.alibaba.nacos.common.http.handler.ResponseHandler; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.model.RestResult; -import com.alibaba.nacos.common.utils.JacksonUtils; -import com.google.common.reflect.TypeToken; import org.junit.Assert; import org.junit.Test; -import sun.tools.jstat.Token; -import java.util.ArrayList; import java.util.HashMap; import java.util.Map; From 6981fbc57641b953afd1e42cad2827e27c8ae390 Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Fri, 29 May 2020 19:10:06 +0800 Subject: [PATCH 020/156] [#1815]add ResourceLifeCycleManager,adjust some code and fix some unit test cases. --- .../client/config/NacosConfigService.java | 14 +- .../nacos/client/config/http/HttpAgent.java | 3 +- .../client/config/http/MetricsHttpAgent.java | 39 ++++-- .../client/config/http/ServerHttpAgent.java | 74 ++++++++--- .../common/lifecycle/AbstractLifeCycle.java | 58 ++------- .../nacos/common/lifecycle/LifeCycle.java | 32 ++--- .../common/lifecycle/LifeCycleListener.java | 48 ------- .../common/lifecycle/LifeCycleState.java | 19 +++ .../lifecycle/ResourceLifeCycleManager.java | 123 ++++++++++++++++++ .../nacos/test/config/ConfigAPI_ITCase.java | 9 +- .../ConfigExportAndImportAPI_ITCase.java | 6 +- 11 files changed, 270 insertions(+), 155 deletions(-) delete mode 100644 common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleListener.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java diff --git a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java index 4c5a82e384a..28a383452f6 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java @@ -34,6 +34,7 @@ import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.ValidatorUtils; +import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -56,10 +57,12 @@ public class NacosConfigService implements ConfigService { private static final long POST_TIMEOUT = 3000L; + private static final ResourceLifeCycleManager RESOURCE_MANAGER = ResourceLifeCycleManager.getInstance(); + /** * http agent */ - private HttpAgent agent; + private MetricsHttpAgent agent; /** * longpolling */ @@ -78,7 +81,14 @@ public NacosConfigService(Properties properties) throws NacosException { } initNamespace(properties); agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); - agent.start(); + agent.fetchServerIpList(); + try { + agent.start(); + }catch (Exception e) { + LOGGER.error("An exception occurred during resource start : {}", e); + } + RESOURCE_MANAGER.register(agent); + worker = new ClientWorker(agent, configFilterChainManager, properties); } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java index 1b30bcc1ed4..bc6fbc96b5a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java @@ -28,12 +28,13 @@ * @author Nacos */ public interface HttpAgent { + /** * start to get nacos ip list * @return Nothing. * @throws NacosException on get ip list error. */ - void start() throws NacosException; + void fetchServerIpList() throws NacosException; /** * invoke http get method diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java index 2449c41316f..e50ca659aea 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.monitor.MetricsMonitor; +import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; import io.prometheus.client.Histogram; import java.io.IOException; @@ -28,16 +29,16 @@ * * @author Nacos */ -public class MetricsHttpAgent implements HttpAgent { - private HttpAgent httpAgent; +public class MetricsHttpAgent extends AbstractLifeCycle implements HttpAgent { - public MetricsHttpAgent(HttpAgent httpAgent) { - this.httpAgent = httpAgent; + private ServerHttpAgent serverHttpAgent; + + public MetricsHttpAgent(ServerHttpAgent serverHttpAgent) { + this.serverHttpAgent = serverHttpAgent; } - @Override - public void start() throws NacosException { - httpAgent.start(); + public void fetchServerIpList() throws NacosException { + this.serverHttpAgent.fetchServerIpList(); } @Override @@ -45,7 +46,7 @@ public HttpResult httpGet(String path, List headers, List paramV Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("GET", path, "NA"); HttpResult result; try { - result = httpAgent.httpGet(path, headers, paramValues, encoding, readTimeoutMs); + result = this.serverHttpAgent.httpGet(path, headers, paramValues, encoding, readTimeoutMs); } catch (IOException e) { throw e; } finally { @@ -61,7 +62,7 @@ public HttpResult httpPost(String path, List headers, List param Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("POST", path, "NA"); HttpResult result; try { - result = httpAgent.httpPost(path, headers, paramValues, encoding, readTimeoutMs); + result = this.serverHttpAgent.httpPost(path, headers, paramValues, encoding, readTimeoutMs); } catch (IOException e) { throw e; } finally { @@ -77,7 +78,7 @@ public HttpResult httpDelete(String path, List headers, List par Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("DELETE", path, "NA"); HttpResult result; try { - result = httpAgent.httpDelete(path, headers, paramValues, encoding, readTimeoutMs); + result = this.serverHttpAgent.httpDelete(path, headers, paramValues, encoding, readTimeoutMs); } catch (IOException e) { throw e; @@ -91,22 +92,32 @@ public HttpResult httpDelete(String path, List headers, List par @Override public String getName() { - return httpAgent.getName(); + return this.serverHttpAgent.getName(); } @Override public String getNamespace() { - return httpAgent.getNamespace(); + return this.serverHttpAgent.getNamespace(); } @Override public String getTenant() { - return httpAgent.getTenant(); + return this.serverHttpAgent.getTenant(); } @Override public String getEncode() { - return httpAgent.getEncode(); + return this.serverHttpAgent.getEncode(); + } + + @Override + public void doStart() throws Exception { + this.serverHttpAgent.doStart(); + } + + @Override + public void doStop() throws Exception { + this.serverHttpAgent.doStop(); } } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java index 4a042460ae2..4aca49c4e7a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java @@ -28,6 +28,7 @@ import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.TemplateUtils; +import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; import com.alibaba.nacos.common.utils.IoUtils; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.type.TypeReference; @@ -51,7 +52,7 @@ * * @author water.lyl */ -public class ServerHttpAgent implements HttpAgent { +public class ServerHttpAgent extends AbstractLifeCycle implements HttpAgent { private static final Logger LOGGER = LogUtils.logger(ServerHttpAgent.class); @@ -61,6 +62,8 @@ public class ServerHttpAgent implements HttpAgent { private long securityInfoRefreshIntervalMills = TimeUnit.SECONDS.toMillis(5); + private ScheduledExecutorService executorService; + /** * @param path 相对于web应用根,以/开头 * @param headers @@ -258,23 +261,6 @@ public ServerHttpAgent(Properties properties) throws NacosException { namespaceId = properties.getProperty(PropertyKeyConst.NAMESPACE); init(properties); securityProxy.login(serverListMgr.getServerUrls()); - - ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - Thread t = new Thread(r); - t.setName("com.alibaba.nacos.client.config.security.updater"); - t.setDaemon(true); - return t; - } - }); - - executorService.scheduleWithFixedDelay(new Runnable() { - @Override - public void run() { - securityProxy.login(serverListMgr.getServerUrls()); - } - }, 0, securityInfoRefreshIntervalMills, TimeUnit.MILLISECONDS); } private void injectSecurityInfo(List params) { @@ -329,7 +315,57 @@ private void initMaxRetry(Properties properties) { } @Override - public synchronized void start() throws NacosException { + protected void doStart() throws Exception { + LOGGER.info("do start begin"); + + // init executorService + this.executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread t = new Thread(r); + t.setName("com.alibaba.nacos.client.config.security.updater"); + t.setDaemon(true); + return t; + } + }); + + this.executorService.scheduleWithFixedDelay(new Runnable() { + @Override + public void run() { + securityProxy.login(serverListMgr.getServerUrls()); + } + }, 0, securityInfoRefreshIntervalMills, TimeUnit.MILLISECONDS); + + LOGGER.info("do start end"); + } + + @Override + protected void doStop() throws Exception { + LOGGER.info("do stop begin"); + + this.executorService.shutdown(); + int retry = 3; + while (retry > 0) { + retry --; + try { + if (this.executorService.awaitTermination(10, TimeUnit.SECONDS)) { + return; + } + } catch (InterruptedException e) { + this.executorService.shutdownNow(); + Thread.interrupted(); + } catch (Throwable ex) { + LOGGER.error("shutdown the executor has error : {}", ex); + } + this.executorService.shutdownNow(); + } + + LOGGER.info("do stop end"); + } + + @Override + public void fetchServerIpList() throws NacosException { + // fetch server address urls list serverListMgr.start(); } diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java index 471ab4f295a..343d02b50bb 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java @@ -12,13 +12,12 @@ * * @author zongtanghu */ -public abstract class AbstractLifeCycle implements LifeCycle{ +public abstract class AbstractLifeCycle implements LifeCycle { private static final Logger LOG = LoggerFactory.getLogger(AbstractLifeCycle.class); private final Object lock = new Object(); private volatile LifeCycleState state = LifeCycleState.STOPPED; - private final List eventListeners = new CopyOnWriteArrayList<>(); /** * Method to override to start the lifecycle. @@ -54,8 +53,9 @@ public void start() throws Exception { doStart(); setStarted(); } catch (Exception e) { - if (LOG.isDebugEnabled()) + if (LOG.isDebugEnabled()) { LOG.debug("Unable to stop", e); + } setStopping(); doStop(); setStopped(); @@ -128,19 +128,6 @@ public boolean isFailed() { return this.state == LifeCycleState.FAILED; } - @Override - public boolean addLifeCycleListener(LifeCycleListener listener) { - if (this.eventListeners.contains(listener)) { - return false; - } - return this.eventListeners.add(listener); - } - - @Override - public boolean removeLifeCycleListener(LifeCycleListener listener) { - return this.eventListeners.remove(listener); - } - /** * Get the service's current state and return. * @@ -152,82 +139,61 @@ public String getState() { /** * If the service's current status is STARTING state, it will set STOPPED state. - * And the method will execute lifeCycleStopped event if it has already been registered before. * */ private void setStarted() { if (this.state == LifeCycleState.STARTING) { this.state = LifeCycleState.STARTED; - if (LOG.isDebugEnabled()) + if (LOG.isDebugEnabled()) { LOG.debug("STARTED {}", this); - - for (EventListener listener : eventListeners) - if (listener instanceof LifeCycleListener) - ((LifeCycleListener)listener).lifeCycleStarted(this); + } } } /** * The service which implement AbstractLifeCycle will set STARTING state. - * And it will execute lifeCycleStarting event if it has already been registered before. * */ private void setStarting() { - if (LOG.isDebugEnabled()) + if (LOG.isDebugEnabled()) { LOG.debug("STARTING {}", this); - this.state = LifeCycleState.STARTING; - - for (EventListener listener : this.eventListeners) { - if (listener instanceof LifeCycleListener) - ((LifeCycleListener) listener).lifeCycleStarting(this); } + this.state = LifeCycleState.STARTING; } /** * The service which implement AbstractLifeCycle will set STOPPING state. - * And it will execute lifeCycleStopping event if it has been registered before. * */ private void setStopping() { - if (LOG.isDebugEnabled()) + if (LOG.isDebugEnabled()) { LOG.debug("STOPPING {}", this); - this.state = LifeCycleState.STOPPING; - for (EventListener listener : this.eventListeners) { - if (listener instanceof LifeCycleListener) - ((LifeCycleListener) listener).lifeCycleStopping(this); } + this.state = LifeCycleState.STOPPING; } /** * If the service's current status is STARTING state, it will set STOPPED state. - * And the method will execute lifeCycleStopped event if it has already been registered before. * */ private void setStopped() { if (this.state == LifeCycleState.STOPPING) { this.state = LifeCycleState.STOPPED; - if (LOG.isDebugEnabled()) + if (LOG.isDebugEnabled()) { LOG.debug("STOPPED {}", this); - for (EventListener listener : this.eventListeners) { - if (listener instanceof LifeCycleListener) - ((LifeCycleListener) listener).lifeCycleStopped(this); } } } /** - * If some exceptions happen, the service will set FAILED state. And it will - * execute lifeCycleFailure event if it has already been registered before. + * If some exceptions happen, the service will set FAILED state. * * @param tb Exception which happens. */ private void setFailed(Throwable tb) { this.state = LifeCycleState.FAILED; - if (LOG.isDebugEnabled()) + if (LOG.isDebugEnabled()) { LOG.warn("FAILED " + this + ": " + tb, tb); - for (EventListener listener : this.eventListeners) { - if (listener instanceof LifeCycleListener) - ((LifeCycleListener)listener).lifeCycleFailure(this, tb); } } } diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java index 4ab7e2c7285..19ed4166a5a 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java @@ -24,52 +24,46 @@ public interface LifeCycle { public void stop() throws Exception; /** + * This method will return the service whether is running or not. + * * @return true if the component is starting or has been started. */ public boolean isRunning(); /** + * This method will return the service whether is started or not. + * * @return true if the service has been started. * */ public boolean isStarted(); /** + * This method will return the service whether is starting or not. + * * @return true if the component is starting. * */ public boolean isStarting(); /** - * @return true if the service is stopping. + * This method will return the service whether is stopping or not. * + * @return true if the service is stopping. */ public boolean isStopping(); /** - * @return true if the service has been stopped. + * This method will return the service whether is stopped or not. * + * @return true if the service has been stopped. */ public boolean isStopped(); /** - * @return true if the component has failed to start or has failed to stop. - */ - public boolean isFailed(); - - /** - * add a LifeCycleListener. - * - * @param listener event listener - * @return the result whether to add LifeCycleListener or not. - */ - public boolean addLifeCycleListener(LifeCycleListener listener); - - /** - * remove a LifeCycleListener. + * This method will return the service whether is failed or not. * - * @param listener event listener - * @return the result whether to remove LifeCycleListener or not. + * @return true if the service has failed to start or has failed to stop. */ - public boolean removeLifeCycleListener(LifeCycleListener listener); + public boolean isFailed(); } diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleListener.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleListener.java deleted file mode 100644 index de423973651..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleListener.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.alibaba.nacos.common.lifecycle; - - -import java.util.EventListener; - -/** - * A listener for Lifecycle events. - * - * @author zongtanghu - */ -public interface LifeCycleListener extends EventListener { - - /** - * Listening for sevice starting event. - * - * @param event - */ - default void lifeCycleStarting(LifeCycle event) { } - - /** - * Listening for sevice started event. - * - * @param event - */ - default void lifeCycleStarted(LifeCycle event) { } - - /** - * Listening for sevice failure event. - * - * @param event - * @param cause - */ - default void lifeCycleFailure(LifeCycle event, Throwable cause) { } - - /** - * Listening for sevice stopping event. - * - * @param event - */ - default void lifeCycleStopping(LifeCycle event) { } - - /** - * Listening for sevice stopped event. - * - * @param event - */ - default void lifeCycleStopped(LifeCycle event) { } -} diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java index 3b51300624d..e725931395c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java @@ -7,10 +7,29 @@ */ public enum LifeCycleState { + /** + * The service's current state is STOPPED. + */ STOPPED("STOPPED"), + + /** + * The service's current state is STARTING. + */ STARTING("STARTING"), + + /** + * The service's current state is STARTED. + */ STARTED("STARTED"), + + /** + * The service's current state is STOPPING. + */ STOPPING("STOPPING"), + + /** + * The service's current state is FAILED. + */ FAILED("FAILED"); private String name; diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java new file mode 100644 index 00000000000..34037c79864 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java @@ -0,0 +1,123 @@ +package com.alibaba.nacos.common.lifecycle; + +import com.alibaba.nacos.common.utils.ShutdownUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * A class to manage every instances' life cycle which register into it. + * @author zongtanghu + * + */ +public final class ResourceLifeCycleManager { + + private static final Logger LOGGER = LoggerFactory.getLogger(ResourceLifeCycleManager.class); + + /** + * + */ + private List lifeCycleResources; + + /** + * Map + */ + private Map lockers = new ConcurrentHashMap(8); + + private static final ResourceLifeCycleManager INSTANCE = new ResourceLifeCycleManager(); + + private static final AtomicBoolean CLOSED = new AtomicBoolean(false); + + static { + INSTANCE.init(); + ShutdownUtils.addShutdownHook(new Thread(new Runnable() { + @Override + public void run() { + LOGGER.warn("[LifeCycleManager] Start destroying Every Instance"); + shutdown(); + LOGGER.warn("[LifeCycleManager] Destruction of the end"); + } + })); + } + + private void init() { + this.lifeCycleResources = new CopyOnWriteArrayList(); + } + + public static ResourceLifeCycleManager getInstance() { + return INSTANCE; + } + + /** + * Destroy all of the life cycle resources which are managed by ResourceLifeCycleManager. + * + */ + public static void shutdown() { + if (!CLOSED.compareAndSet(false, true)) { + return; + } + + List instances = INSTANCE.lifeCycleResources; + for (AbstractLifeCycle instance : instances) { + INSTANCE.destroy(instance); + } + } + + /** + * Destroy the life cycle resource instance. + * + * @param instance the life cycle resource instance which is need to be destroyed. + */ + public void destroy(AbstractLifeCycle instance) { + final Object monitor = lockers.get(instance); + if (monitor == null) { + return; + } + synchronized (monitor) { + try { + // the life cycle resources which managed are do stop method. + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Life cycle resources do stop"); + } + instance.stop(); + } catch (Exception e) { + LOGGER.error("An exception occurred during resource do stop : {}", e); + } + } + } + + + /** + * Cancel the specified lifecycle resource instance management. + * + * @param instance the management life cycle resource instances; + * + */ + public void deregister(AbstractLifeCycle instance) { + if (this.lifeCycleResources.contains(instance)) { + final Object monitor = lockers.get(instance); + synchronized (monitor) { + this.lifeCycleResources.remove(instance); + } + } + } + + /** + * Register the life cycle resource instances into the lifeCycleResources lists. + * + * @param instance the management life cycle resource instances. + */ + public void register(AbstractLifeCycle instance) { + if (!lifeCycleResources.contains(instance)) { + synchronized(this) { + lockers.put(instance, new Object()); + } + this.lifeCycleResources.add(instance); + } + } +} diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java index 7b47b24156c..6bcbf9a29f4 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java @@ -51,12 +51,12 @@ */ @RunWith(SpringRunner.class) @SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) + webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) public class ConfigAPI_ITCase { public static final long TIME_OUT = 5000; static ConfigService iconfig = null; - static HttpAgent agent = null; + static MetricsHttpAgent agent = null; static final String CONFIG_CONTROLLER_PATH = "/v1/cs/configs"; String SPECIAL_CHARACTERS = "!@#$%^&*()_+-=_|/'?."; @@ -72,8 +72,8 @@ public void setUp() throws Exception { properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1"+":"+port); properties.put(PropertyKeyConst.CONTEXT_PATH, "/nacos"); iconfig = NacosFactory.createConfigService(properties); - agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); + agent.fetchServerIpList(); agent.start(); } @@ -89,6 +89,7 @@ public void cleanup() throws Exception { e.printStackTrace(); Assert.fail(); } + agent.stop(); } /** @@ -96,7 +97,7 @@ public void cleanup() throws Exception { * @TestStep : * @ExpectResult : */ - @Test(timeout = 3*TIME_OUT) + @Test(timeout = 3 * TIME_OUT) public void nacos_getconfig_1() throws Exception { final String content = "test"; boolean result = iconfig.publishConfig(dataId, group, content); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java index 65e9dfd67db..725f825abaf 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java @@ -57,7 +57,7 @@ public class ConfigExportAndImportAPI_ITCase { private String SERVER_ADDR = null; - private HttpAgent agent = null; + private MetricsHttpAgent agent = null; @Before @@ -67,6 +67,7 @@ public void setUp() throws Exception { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1"+":"+port); agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); + agent.fetchServerIpList(); agent.start(); Map prarm = new HashMap<>(7); @@ -92,7 +93,7 @@ public void setUp() throws Exception { } @After - public void cleanup(){ + public void cleanup() throws Exception{ HttpSimpleClient.HttpResult result; try { List params2 = Arrays.asList("dataId", "testNoAppname1.yml", "group", "EXPORT_IMPORT_TEST_GROUP", "beta", "false"); @@ -121,6 +122,7 @@ public void cleanup(){ } catch (Exception e) { Assert.fail(); } + agent.stop(); } @Test(timeout = 3*TIME_OUT) From ea03121e2603cd448402253161cf758881556216 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Sat, 30 May 2020 22:39:04 +0800 Subject: [PATCH 021/156] add NacosRestTemplate and NacosAsyncRestTemplate test example --- .../http/client/NacosAsyncRestTemplate.java | 1 - .../nacos/common/http/AsyncHttpUtils.java | 72 -------- .../nacos/common/http/HttpUtilsTest.java | 92 ++-------- .../common/NacosAsyncRestTemplate_ITCase.java | 157 ++++++++++++++++++ .../test/common/NacosRestTemplate_ITCase.java | 117 +++++++++++++ 5 files changed, 292 insertions(+), 147 deletions(-) delete mode 100644 common/src/test/java/com/alibaba/nacos/common/http/AsyncHttpUtils.java create mode 100644 test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java create mode 100644 test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java index c497e14bd04..aa4c4c95f0f 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java @@ -43,7 +43,6 @@ public class NacosAsyncRestTemplate implements AsyncRestOperations { private static final Logger logger = LoggerFactory.getLogger(NacosAsyncRestTemplate.class); - private AsyncHttpClientRequest clientRequest; public NacosAsyncRestTemplate(AsyncHttpClientRequest clientRequest) { diff --git a/common/src/test/java/com/alibaba/nacos/common/http/AsyncHttpUtils.java b/common/src/test/java/com/alibaba/nacos/common/http/AsyncHttpUtils.java deleted file mode 100644 index e72ac7fc8e7..00000000000 --- a/common/src/test/java/com/alibaba/nacos/common/http/AsyncHttpUtils.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.alibaba.nacos.common.http; - -import com.alibaba.fastjson.JSON; -import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate; -import com.alibaba.nacos.common.http.param.Header; -import com.alibaba.nacos.common.http.param.Query; -import com.alibaba.nacos.common.model.RestResult; -import org.junit.Assert; -import org.junit.Test; - -import java.util.HashMap; -import java.util.Map; - -/** - * AsyncHttpUtils - * - * @author mai.jh - * @date 2020/5/29 - */ -public class AsyncHttpUtils { - - private NacosAsyncRestTemplate nacosRestTemplate = HttpClientManager.getNacosAsyncRestTemplate(); - - private final String CONFIG_CONTROLLER_PATH = "/nacos/v1/cs"; - private final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns"; - private final String IP = "http://localhost:8848"; - - @Test - public void test_url_get() throws Exception{ - String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; - Query query = Query.newInstance().addParam("serviceName", "app-test"); - nacosRestTemplate.get(url, Header.newInstance(), query, Map.class, new Callback() { - @Override - public void onReceive(RestResult result) { - System.out.println(JSON.toJSONString(result)); -// Assert.assertTrue(result.ok()); -// Assert.assertEquals(result.getData().get("dom"), "app-test"); - } - - @Override - public void onError(Throwable throwable) { - - } - }); - Thread.sleep(20000); - - } - - @Test - public void test_url_post_from() throws Exception{ - String url = IP + CONFIG_INSTANCE_PATH + "/instance"; - Map param = new HashMap<>(); - param.put("serviceName", "app-test"); - param.put("port", "8080"); - param.put("ip", "11.11.11.11"); - nacosRestTemplate.postFrom(url, Header.newInstance(), Query.newInstance(), param, String.class, new Callback() { - @Override - public void onReceive(RestResult result) { - System.out.println(JSON.toJSONString(result)); -// Assert.assertTrue(result.ok()); - } - - @Override - public void onError(Throwable throwable) { - System.out.println(throwable.getMessage()); - } - }); - Thread.sleep(20000); - } - - -} diff --git a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java index 1b12f0b42a3..0bac86939c8 100644 --- a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java @@ -1,85 +1,29 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.nacos.common.http; -import com.alibaba.fastjson.JSON; -import com.alibaba.nacos.common.http.client.NacosRestTemplate; -import com.alibaba.nacos.common.http.param.Header; -import com.alibaba.nacos.common.http.param.Query; -import com.alibaba.nacos.common.model.RestResult; -import org.junit.Assert; -import org.junit.Test; - -import java.util.HashMap; -import java.util.Map; - -public class HttpUtilsTest { - - private NacosRestTemplate nacosRestTemplate = HttpClientManager.getNacosRestTemplate(); - - - private final String CONFIG_CONTROLLER_PATH = "/nacos/v1/cs"; - private final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns"; - private final String IP = "http://localhost:8848"; - - - @Test - public void test_url_get() throws Exception { - String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; - Query query = Query.newInstance().addParam("serviceName", "app-test"); - RestResult restResult = nacosRestTemplate.get(url, Header.newInstance(), query, Map.class); - Assert.assertTrue(restResult.ok()); - Assert.assertEquals(restResult.getData().get("dom"), "app-test"); - System.out.println(JSON.toJSONString(restResult)); - } - @Test - public void test_url_get_by_map() throws Exception { - String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; - Map param = new HashMap<>(); - param.put("serviceName", "app-test"); - RestResult restResult = nacosRestTemplate.get(url, Header.newInstance(), param, Map.class); - Assert.assertTrue(restResult.ok()); - Assert.assertEquals(restResult.getData().get("dom"), "app-test"); - System.out.println(JSON.toJSONString(restResult)); - } +import org.junit.Test; - @Test - public void test_url_delete() throws Exception{ - String url = IP + CONFIG_INSTANCE_PATH + "/instance"; - Query query = Query.newInstance() - .addParam("ip", "11.11.11.11") - .addParam("port", "8080") - .addParam("serviceName", "app-test"); - RestResult restResult = nacosRestTemplate.delete(url, Header.newInstance(), query, RestResult.class); - Assert.assertTrue(restResult.ok()); - System.out.println(JSON.toJSONString(restResult)); - } +public class HttpUtilsTest { @Test - public void test_url_put_from() throws Exception{ - String url = IP + CONFIG_INSTANCE_PATH + "/instance"; - Map param = new HashMap<>(); - param.put("serviceName", "app-test-change"); - param.put("port", "8080"); - param.put("ip", "11.11.11.11"); - RestResult restResult = nacosRestTemplate.putFrom(url, Header.newInstance(), Query.newInstance(), param, String.class); - Assert.assertTrue(restResult.ok()); - System.out.println(JSON.toJSONString(restResult)); - } - + public void test_url_encode() throws Exception { - @Test - public void test_url_post_from() throws Exception{ - String url = IP + CONFIG_INSTANCE_PATH + "/instance"; - Map param = new HashMap<>(); - param.put("serviceName", "app-test"); - param.put("port", "8080"); - param.put("ip", "11.11.11.11"); - RestResult restResult = nacosRestTemplate.postFrom(url, Header.newInstance(), Query.newInstance(), param, String.class); - Assert.assertTrue(restResult.ok()); - System.out.println(JSON.toJSONString(restResult)); } - - } diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java new file mode 100644 index 00000000000..736fda258cf --- /dev/null +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java @@ -0,0 +1,157 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.test.common; + +import com.alibaba.nacos.Nacos; +import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.http.HttpClientManager; +import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate; +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.Query; +import com.alibaba.nacos.common.model.RestResult; +import org.junit.Assert; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.HashMap; +import java.util.Map; + +/** + * NacosAsyncRestTemplate_ITCase + * + * @author mai.jh + * @date 2020/5/29 + */ +@SuppressWarnings("all") +@FixMethodOrder(MethodSorters.JVM) +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, + webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +public class NacosAsyncRestTemplate_ITCase { + + private NacosAsyncRestTemplate nacosRestTemplate = HttpClientManager.getNacosAsyncRestTemplate(); + + private final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns"; + private final String IP = "http://127.0.0.1:8848"; + + private class CallbackMap implements Callback { + + private RestResult restResult; + private Throwable throwable; + + @Override + public void onReceive(RestResult result) { + restResult = result; + } + + @Override + public void onError(Throwable throwable) { + throwable = throwable; + } + + public RestResult getRestResult() { + return restResult; + } + + public Throwable getThrowable() { + return throwable; + } + } + + @Test + public void test_url_post_from() throws Exception{ + String url = IP + CONFIG_INSTANCE_PATH + "/instance"; + Map param = new HashMap<>(); + param.put("serviceName", "app-test"); + param.put("port", "8080"); + param.put("ip", "11.11.11.11"); + CallbackMap callbackMap = new CallbackMap<>(); + nacosRestTemplate.postFrom(url, Header.newInstance(), Query.newInstance(), param, String.class, callbackMap); + Thread.sleep(2000); + RestResult restResult = callbackMap.getRestResult(); + System.out.println(restResult.getData()); + Assert.assertTrue(restResult.ok()); + } + + @Test + public void test_url_put_from() throws Exception{ + String url = IP + CONFIG_INSTANCE_PATH + "/instance"; + Map param = new HashMap<>(); + param.put("serviceName", "app-test-change"); + param.put("port", "8080"); + param.put("ip", "11.11.11.11"); + CallbackMap callbackMap = new CallbackMap<>(); + nacosRestTemplate.putFrom(url, Header.newInstance(), Query.newInstance(), param, String.class, callbackMap); + Thread.sleep(2000); + RestResult restResult = callbackMap.getRestResult(); + System.out.println(restResult.getData()); + Assert.assertTrue(restResult.ok()); + } + + + @Test + public void test_url_get() throws Exception { + String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; + Query query = Query.newInstance().addParam("serviceName", "app-test"); + CallbackMap callbackMap = new CallbackMap<>(); + nacosRestTemplate.get(url, Header.newInstance(), query, Map.class, callbackMap); + Thread.sleep(2000); + RestResult restResult = callbackMap.getRestResult(); + System.out.println(restResult.getData()); + Assert.assertTrue(restResult.ok()); + Assert.assertEquals(restResult.getData().get("dom"), "app-test"); + + } + + @Test + public void test_url_by_map() throws Exception{ + String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; + Map param = new HashMap<>(); + param.put("serviceName", "app-test"); + CallbackMap callbackMap = new CallbackMap<>(); + nacosRestTemplate.get(url, Header.newInstance(), param, Map.class, callbackMap); + Thread.sleep(2000); + RestResult restResult = callbackMap.getRestResult(); + System.out.println(restResult.getData()); + Assert.assertTrue(restResult.ok()); + Assert.assertEquals(restResult.getData().get("dom"), "app-test"); + + } + + @Test + public void test_url_delete() throws Exception{ + String url = IP + CONFIG_INSTANCE_PATH + "/instance"; + Query query = Query.newInstance() + .addParam("ip", "11.11.11.11") + .addParam("port", "8080") + .addParam("serviceName", "app-test"); + CallbackMap callbackMap = new CallbackMap<>(); + nacosRestTemplate.delete(url, Header.newInstance(), query, RestResult.class, callbackMap); + Thread.sleep(2000); + RestResult restResult = callbackMap.getRestResult(); + System.out.println(restResult.getData()); + Assert.assertTrue(restResult.ok()); + + } + + + + +} diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java new file mode 100644 index 00000000000..dd565ac06cd --- /dev/null +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java @@ -0,0 +1,117 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.test.common; + +import com.alibaba.nacos.Nacos; +import com.alibaba.nacos.common.http.HttpClientManager; +import com.alibaba.nacos.common.http.client.NacosRestTemplate; +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.http.param.Query; +import com.alibaba.nacos.common.model.RestResult; +import org.junit.Assert; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.HashMap; +import java.util.Map; + +/** + * NacosRestTemplate_ITCase + * + * @author mai.jh + * @date 2020/5/30 + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, + webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@FixMethodOrder(MethodSorters.JVM) +public class NacosRestTemplate_ITCase { + + private NacosRestTemplate nacosRestTemplate = HttpClientManager.getNacosRestTemplate(); + + + private final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns"; + private final String IP = "http://127.0.0.1:8848"; + + @Test + public void test_url_post_from() throws Exception{ + String url = IP + CONFIG_INSTANCE_PATH + "/instance"; + Map param = new HashMap<>(); + param.put("serviceName", "app-test"); + param.put("port", "8080"); + param.put("ip", "11.11.11.11"); + RestResult restResult = nacosRestTemplate.postFrom(url, Header.newInstance(), Query.newInstance(), param, String.class); + Assert.assertTrue(restResult.ok()); + System.out.println(restResult.getData()); + } + + @Test + public void test_url_put_from() throws Exception{ + String url = IP + CONFIG_INSTANCE_PATH + "/instance"; + Map param = new HashMap<>(); + param.put("serviceName", "app-test-change"); + param.put("port", "8080"); + param.put("ip", "11.11.11.11"); + RestResult restResult = nacosRestTemplate.putFrom(url, Header.newInstance(), Query.newInstance(), param, String.class); + Assert.assertTrue(restResult.ok()); + System.out.println(restResult.getData()); + } + + @Test + public void test_url_get() throws Exception { + String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; + Query query = Query.newInstance().addParam("serviceName", "app-test"); + RestResult restResult = nacosRestTemplate.get(url, Header.newInstance(), query, Map.class); + Assert.assertTrue(restResult.ok()); + Assert.assertEquals(restResult.getData().get("dom"), "app-test"); + System.out.println(restResult.getData()); + } + + @Test + public void test_url_get_by_map() throws Exception { + String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; + Map param = new HashMap<>(); + param.put("serviceName", "app-test"); + RestResult restResult = nacosRestTemplate.get(url, Header.newInstance(), param, Map.class); + Assert.assertTrue(restResult.ok()); + Assert.assertEquals(restResult.getData().get("dom"), "app-test"); + System.out.println(restResult.getData()); + } + + @Test + public void test_url_delete() throws Exception{ + String url = IP + CONFIG_INSTANCE_PATH + "/instance"; + Query query = Query.newInstance() + .addParam("ip", "11.11.11.11") + .addParam("port", "8080") + .addParam("serviceName", "app-test"); + RestResult restResult = nacosRestTemplate.delete(url, Header.newInstance(), query, RestResult.class); + Assert.assertTrue(restResult.ok()); + System.out.println(restResult.getData()); + } + + + + + + + + +} From a3a64c5fa38187e5543597d2e424cca64649a944 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Sat, 30 May 2020 23:20:22 +0800 Subject: [PATCH 022/156] change annotate --- .../alibaba/nacos/common/http/client/HttpClientResponse.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientResponse.java index 6e1ad5db7ab..15043d7df36 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientResponse.java @@ -26,9 +26,6 @@ /** * Represents a client-side HTTP response. - * Obtained via an calling of the - * {@link HttpClientRequest#execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity)}. - * * * @author mai.jh * @date 2020/5/23 From 11cc8dad021ed0716929fa332db688e963967c73 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Sat, 30 May 2020 23:56:17 +0800 Subject: [PATCH 023/156] Resolve conflicts --- .../nacos/common/http/HttpClientManager.java | 14 +++++++------- .../alibaba/nacos/common/utils/JacksonUtils.java | 7 ++----- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java index a796965d86a..99111944a44 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java @@ -50,13 +50,13 @@ public class HttpClientManager { private static final NAsyncHttpClient ASYNC_HTTP_CLIENT = new NacosAsyncHttpClient( HttpAsyncClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build()); - - private static final NacosRestTemplate NACOS_REST_TEMPLATE = new NacosRestTemplate( + + private static final NacosRestTemplate NACOS_REST_TEMPLATE = new NacosRestTemplate( new ApacheHttpClientRequest(HttpClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build())); - private static final NacosAsyncRestTemplate NACOS_ASYNC_REST_TEMPLATE = new NacosAsyncRestTemplate( + private static final NacosAsyncRestTemplate NACOS_ASYNC_REST_TEMPLATE = new NacosAsyncRestTemplate( new ApacheAsyncHttpClientRequest(HttpAsyncClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build())); - + private static final AtomicBoolean alreadyShutdown = new AtomicBoolean(false); @@ -80,11 +80,11 @@ public static NAsyncHttpClient getAsyncHttpClient() { public static NacosRestTemplate getNacosRestTemplate() { return NACOS_REST_TEMPLATE; - } + } - public static NacosAsyncRestTemplate getNacosAsyncRestTemplate() { + public static NacosAsyncRestTemplate getNacosAsyncRestTemplate() { return NACOS_ASYNC_REST_TEMPLATE; - } + } public static void shutdown() { if (!alreadyShutdown.compareAndSet(false, true)) { diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java index cd600cdce55..306132f8ad4 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java @@ -29,7 +29,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import java.io.InputStream; -import java.io.IOException +import java.io.IOException; import java.lang.reflect.Type; /** @@ -81,11 +81,8 @@ public static T toObj(InputStream inputStream, Class tClass) throws Excep return mapper.readValue(inputStream, tClass); } - public static T toObj(String json, Class cls) throws Exception { - return mapper.readValue(json, cls); - } - public static T toObj(byte[] json, TypeReference typeReference) { + public static T toObj(byte[] json, TypeReference typeReference) { try { return toObj(StringUtils.newString4UTF8(json), typeReference); } catch (Exception e) { From 6f3b3e3c9bdb25b1848715428826c009c2fb7c74 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Sun, 31 May 2020 00:10:14 +0800 Subject: [PATCH 024/156] test change --- .../nacos/test/common/NacosAsyncRestTemplate_ITCase.java | 8 ++++++-- .../nacos/test/common/NacosRestTemplate_ITCase.java | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java index 736fda258cf..358c2ce3ba8 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java @@ -28,6 +28,7 @@ import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; import java.util.HashMap; @@ -43,13 +44,16 @@ @FixMethodOrder(MethodSorters.JVM) @RunWith(SpringRunner.class) @SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class NacosAsyncRestTemplate_ITCase { + @LocalServerPort + private int port; + private NacosAsyncRestTemplate nacosRestTemplate = HttpClientManager.getNacosAsyncRestTemplate(); private final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns"; - private final String IP = "http://127.0.0.1:8848"; + private final String IP = "http://127.0.0.1:" + port; private class CallbackMap implements Callback { diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java index dd565ac06cd..33884c896ec 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java @@ -27,6 +27,7 @@ import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; import java.util.HashMap; @@ -40,15 +41,18 @@ */ @RunWith(SpringRunner.class) @SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @FixMethodOrder(MethodSorters.JVM) public class NacosRestTemplate_ITCase { + @LocalServerPort + private int port; + private NacosRestTemplate nacosRestTemplate = HttpClientManager.getNacosRestTemplate(); private final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns"; - private final String IP = "http://127.0.0.1:8848"; + private final String IP = "http://127.0.0.1:" + port; @Test public void test_url_post_from() throws Exception{ From 2b7ebbf5bceb565cf6eaa3f12164f6fb1d5cff77 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Sun, 31 May 2020 00:23:58 +0800 Subject: [PATCH 025/156] test change --- .../test/common/NacosAsyncRestTemplate_ITCase.java | 9 ++++++++- .../nacos/test/common/NacosRestTemplate_ITCase.java | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java index 358c2ce3ba8..151f1f46040 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.test.common; import com.alibaba.nacos.Nacos; +import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.common.http.Callback; import com.alibaba.nacos.common.http.HttpClientManager; import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate; @@ -23,6 +24,7 @@ import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.model.RestResult; import org.junit.Assert; +import org.junit.Before; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; @@ -53,7 +55,12 @@ public class NacosAsyncRestTemplate_ITCase { private NacosAsyncRestTemplate nacosRestTemplate = HttpClientManager.getNacosAsyncRestTemplate(); private final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns"; - private final String IP = "http://127.0.0.1:" + port; + private String IP = null; + + @Before + public void init() throws NacosException { + IP = String.format("http://localhost:%d", port); + } private class CallbackMap implements Callback { diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java index 33884c896ec..9a28d22fe99 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java @@ -16,12 +16,14 @@ package com.alibaba.nacos.test.common; import com.alibaba.nacos.Nacos; +import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.common.http.HttpClientManager; import com.alibaba.nacos.common.http.client.NacosRestTemplate; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.model.RestResult; import org.junit.Assert; +import org.junit.Before; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; @@ -52,7 +54,13 @@ public class NacosRestTemplate_ITCase { private final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns"; - private final String IP = "http://127.0.0.1:" + port; + private String IP = null; + + @Before + public void init() throws NacosException { + IP = String.format("http://localhost:%d", port); + } + @Test public void test_url_post_from() throws Exception{ From 6d1baed235386290ed5bac012865d5949344fe98 Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Sun, 31 May 2020 12:20:20 +0800 Subject: [PATCH 026/156] [#1815]add the related lifecycle test cases codes. --- .../lifecycle/ResourceLifeCycleManager.java | 3 + .../common/LifeCycle/LifeCycleClassTest.java | 81 +++++++++++++++++ .../LifeCycle/LifeCycleManagerTest.java | 87 +++++++++++++++++++ .../nacos/common/http/HttpUtilsTest.java | 12 --- 4 files changed, 171 insertions(+), 12 deletions(-) create mode 100644 common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java create mode 100644 common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java delete mode 100644 common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java index 34037c79864..0c6d7d7e83f 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java @@ -85,6 +85,8 @@ public void destroy(AbstractLifeCycle instance) { LOGGER.debug("Life cycle resources do stop"); } instance.stop(); + INSTANCE.lifeCycleResources.remove(instance); + INSTANCE.lockers.remove(instance); } catch (Exception e) { LOGGER.error("An exception occurred during resource do stop : {}", e); } @@ -103,6 +105,7 @@ public void deregister(AbstractLifeCycle instance) { final Object monitor = lockers.get(instance); synchronized (monitor) { this.lifeCycleResources.remove(instance); + this.lockers.remove(instance); } } } diff --git a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java new file mode 100644 index 00000000000..e34edb2f92c --- /dev/null +++ b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java @@ -0,0 +1,81 @@ +package com.alibaba.nacos.common.LifeCycle; + +import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; +import com.alibaba.nacos.common.lifecycle.LifeCycleState; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Test serveral cases for lifecycle class instance. + * + * @author zongtanghu + */ +public class LifeCycleClassTest { + + private Resource res; + + @Before + public void setup() throws Exception{ + this.res = new Resource(0); + this.res.start(); + Assert.assertEquals(this.res.getCounter(), 1); + } + + @After + public void cleanup() throws Exception{ + this.res.stop(); + Assert.assertEquals(this.res.getCounter(), 0); + Assert.assertTrue(this.res.isStopped()); + } + + @Test + public void testResource_LifeCycleMethod() throws Exception{ + this.res.doStart(); + Assert.assertEquals(this.res.getCounter(), 2); + this.res.doStart(); + Assert.assertEquals(this.res.getCounter(), 3); + this.res.doStop(); + Assert.assertEquals(this.res.getCounter(), 2); + this.res.doStop(); + Assert.assertEquals(this.res.getCounter(), 1); + } + + @Test + public void testResource_GetState() throws Exception{ + Assert.assertEquals(this.res.getState(), LifeCycleState.STARTED.toString()); + Assert.assertTrue(this.res.isStarted()); + Assert.assertFalse(this.res.isFailed()); + Assert.assertTrue(this.res.isRunning()); + Assert.assertFalse(this.res.isStopped()); + } + + class Resource extends AbstractLifeCycle { + + private int counter; + + public Resource(int counter) { + this.counter = counter; + } + + public int getCounter() { + return counter; + } + + public void setCounter(int counter) { + this.counter = counter; + } + + @Override + protected void doStart() throws Exception { + counter++; + } + + @Override + protected void doStop() throws Exception { + counter--; + } + } +} diff --git a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java new file mode 100644 index 00000000000..6375a1fff73 --- /dev/null +++ b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java @@ -0,0 +1,87 @@ +package com.alibaba.nacos.common.LifeCycle; + +import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; +import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +/** + * Test serveral cases for lifecycle Manager class instance. + * + * @author zongtanghu + */ +public class LifeCycleManagerTest { + + private Resource res; + private static final ResourceLifeCycleManager RESOURCE_MANAGER = ResourceLifeCycleManager.getInstance(); + + @Before + public void setup() throws Exception{ + this.res = new Resource(0); + this.res.start(); + RESOURCE_MANAGER.register(this.res); + Assert.assertEquals(this.res.getCounter(), 1); + } + + @After + public void cleanup() throws Exception{ + RESOURCE_MANAGER.shutdown(); + Assert.assertEquals(this.res.getCounter(), 0); + // here, double check shutdown called by two times whether the result is ok. + RESOURCE_MANAGER.shutdown(); + Assert.assertEquals(this.res.getCounter(), 0); + // here, check whether the buffer data in resource manager is correct. + RESOURCE_MANAGER.destroy(this.res); + Assert.assertEquals(this.res.getCounter(), 0); + } + + @Test + public void testLifeCycleManager() throws Exception{ + this.res.doStart(); + Assert.assertEquals(this.res.getCounter(), 2); + this.res.doStop(); + Assert.assertEquals(this.res.getCounter(), 1); + } + + @Test + public void testLifeCycleManager_deregister() throws Exception{ + + Resource temp = new Resource(0); + temp.start(); + RESOURCE_MANAGER.register(temp); + RESOURCE_MANAGER.deregister(temp); + RESOURCE_MANAGER.destroy(temp); + + Assert.assertEquals(temp.getCounter(), 1); + } + + class Resource extends AbstractLifeCycle { + + private int counter; + + public Resource(int counter) { + this.counter = counter; + } + + public int getCounter() { + return counter; + } + + public void setCounter(int counter) { + this.counter = counter; + } + + @Override + protected void doStart() throws Exception { + counter++; + } + + @Override + protected void doStop() throws Exception { + counter--; + } + } +} diff --git a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java deleted file mode 100644 index 66a8a030a9e..00000000000 --- a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.alibaba.nacos.common.http; - -import org.junit.Test; - -public class HttpUtilsTest { - - @Test - public void test_url_encode() throws Exception { - - } - -} \ No newline at end of file From 2cb690a6a0ee5eead744bb56e123a09b1ca012fc Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Sun, 31 May 2020 13:26:06 +0800 Subject: [PATCH 027/156] [#1815]add class file license. --- .../common/lifecycle/AbstractLifeCycle.java | 19 +++++++++++++++---- .../nacos/common/lifecycle/LifeCycle.java | 15 +++++++++++++++ .../common/lifecycle/LifeCycleState.java | 15 +++++++++++++++ .../lifecycle/ResourceLifeCycleManager.java | 15 +++++++++++++++ .../LifeCycle/LifeCycleManagerTest.java | 15 +++++++++++++++ 5 files changed, 75 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java index 343d02b50bb..141d1ef562c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java @@ -1,12 +1,23 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.nacos.common.lifecycle; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.EventListener; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; - /** * Basic implementation of the life cycle interface for services. * diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java index 19ed4166a5a..bb9593cfa78 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java @@ -1,3 +1,18 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.nacos.common.lifecycle; /** diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java index e725931395c..57472c30425 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java @@ -1,3 +1,18 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.nacos.common.lifecycle; /** diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java index 0c6d7d7e83f..dc4f1209ca3 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java @@ -1,3 +1,18 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.nacos.common.lifecycle; import com.alibaba.nacos.common.utils.ShutdownUtils; diff --git a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java index 6375a1fff73..ad66ba9167a 100644 --- a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.nacos.common.LifeCycle; import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; From ab3b234987458a492eb52738764542c085953c85 Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Sun, 31 May 2020 13:26:27 +0800 Subject: [PATCH 028/156] [#1815]add class file license. --- .../common/LifeCycle/LifeCycleClassTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java index e34edb2f92c..9128aec2311 100644 --- a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.nacos.common.LifeCycle; import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; From 031c9ef52124fa8d82cd9dff65486f7c7f9be96d Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Sun, 31 May 2020 17:24:01 +0800 Subject: [PATCH 029/156] perfect NacosRestTemplate and restResult --- .../nacos/common/http/HttpRestResult.java | 32 +++++++ .../alibaba/nacos/common/http/HttpUtils.java | 16 ++++ .../client/ApacheAsyncHttpClientRequest.java | 6 +- .../http/client/AsyncRestOperations.java | 87 ++++++++++++++--- .../http/client/NacosAsyncRestTemplate.java | 21 ++-- .../common/http/client/NacosRestTemplate.java | 49 +++++----- .../common/http/client/RestOperations.java | 95 +++++++++++++------ .../common/http/handler/ResponseHandler.java | 20 +++- .../nacos/common/http/param/Header.java | 7 ++ .../nacos/common/model/RestResult.java | 27 +----- .../alibaba/nacos/common/utils/UriUtils.java | 38 -------- .../common/NacosAsyncRestTemplate_ITCase.java | 22 +++-- .../test/common/NacosRestTemplate_ITCase.java | 36 ++++--- 13 files changed, 292 insertions(+), 164 deletions(-) create mode 100644 common/src/main/java/com/alibaba/nacos/common/http/HttpRestResult.java delete mode 100644 common/src/main/java/com/alibaba/nacos/common/utils/UriUtils.java diff --git a/common/src/main/java/com/alibaba/nacos/common/http/HttpRestResult.java b/common/src/main/java/com/alibaba/nacos/common/http/HttpRestResult.java new file mode 100644 index 00000000000..beff90832ea --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/http/HttpRestResult.java @@ -0,0 +1,32 @@ +package com.alibaba.nacos.common.http; + +import com.alibaba.nacos.common.http.param.Header; +import com.alibaba.nacos.common.model.RestResult; + +/** + * http RestResult + * + * @author mai.jh + * @date 2020/5/31 + */ +public class HttpRestResult extends RestResult { + + private static final long serialVersionUID = 3766947816720175947L; + private Header header; + + public HttpRestResult() { + } + + public HttpRestResult(Header header, int code, T data) { + super(code, data); + this.header = header; + } + + public Header getHeader() { + return header; + } + + public void setHeader(Header header) { + this.header = header; + } +} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/HttpUtils.java b/common/src/main/java/com/alibaba/nacos/common/http/HttpUtils.java index b29a69bbc20..eb5df471b74 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/HttpUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/HttpUtils.java @@ -16,9 +16,12 @@ package com.alibaba.nacos.common.http; +import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.utils.StringUtils; import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -121,6 +124,19 @@ public static String decode(String str, String encode) throws UnsupportedEncodin return innerDecode(null, str, encode); } + /** + * build URI By url and query + * @param url url + * @param query query param {@link Query} + * @return + */ + public static URI buildUri(String url, Query query) throws URISyntaxException { + if (!query.isEmpty()) { + url = url + "?" + query.toQueryUrl(); + } + return new URI(url); + } + private static String innerDecode(String pre, String now, String encode) throws UnsupportedEncodingException { // Because the data may be encoded by the URL more than once, // it needs to be decoded recursively until it is fully successful diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheAsyncHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheAsyncHttpClientRequest.java index 436c38979b9..95f858a8710 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheAsyncHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheAsyncHttpClientRequest.java @@ -18,9 +18,9 @@ import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.http.handler.ResponseHandler; import com.alibaba.nacos.common.model.RequestHttpEntity; -import com.alibaba.nacos.common.model.RestResult; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.concurrent.FutureCallback; @@ -56,8 +56,8 @@ public void execute(URI uri, String httpMethod, RequestHttpEntity requestHtt public void completed(HttpResponse result) { ApacheClientHttpResponse response = new ApacheClientHttpResponse(result); try { - RestResult restResult = ResponseHandler.responseEntityExtractor(response, responseType); - callback.onReceive(restResult); + HttpRestResult httpRestResult = ResponseHandler.responseEntityExtractor(response, responseType); + callback.onReceive(httpRestResult); } catch (Exception e) { callback.onError(e); } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/AsyncRestOperations.java b/common/src/main/java/com/alibaba/nacos/common/http/client/AsyncRestOperations.java index a1a2c099ea5..bc5a58deefd 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/AsyncRestOperations.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/AsyncRestOperations.java @@ -32,7 +32,11 @@ public interface AsyncRestOperations { /** - * http get + * async http get + * URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. * * @param url url * @param responseType return type @@ -44,7 +48,11 @@ public interface AsyncRestOperations { void get(String url, Header header, Query query, Type responseType, Callback callback) throws Exception; /** - * http get + * async http get + * URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. * * @param url url * @param header headers @@ -57,7 +65,12 @@ void get(String url, Header header, Map paramValues, Type responseType, Callback callback) throws Exception; /** - * get request, may be pulling a lot of data + * async get request, may be pulling a lot of data + * URL request params are expanded using the given query {@link Query}, + * More request parameters can be set via body. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. * * @param url url * @param header http header param @@ -71,7 +84,11 @@ void getLarge(String url, Header header, Query query, Object body, Type responseType, Callback callback) throws Exception; /** - * http delete + * async http delete + * URL request params are expanded using the given query {@link Query}, + *

{@code responseType} can be an RestResult or RestResult data {@code T} type + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. * * @param url url * @param header http header param @@ -84,7 +101,12 @@ void delete(String url, Header header, Query query, Type responseType, Callback callback) throws Exception; /** - * http put + * async http put + * Create a new resource by PUTting the given body to http request. + *

URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. * * @param url url * @param header http header param @@ -99,7 +121,13 @@ void put(String url, Header header, Query query, Object body, /** - * http put Json + * async http put Json + * Create a new resource by PUTting the given body to http request, + * http header contentType default 'application/json;charset=UTF-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. * * @param url url * @param header http header param @@ -113,7 +141,13 @@ void putJson(String url, Header header, Map paramValues, Str Type responseType, Callback callback) throws Exception; /** - * http put from + * async http put from + * Create a new resource by PUTting the given map {@code bodyValues} to http request, + * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. + *

URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. * * @param url url * @param header http header param @@ -128,7 +162,13 @@ void putFrom(String url, Header header, Query query, /** - * http put from + * async http put from + * Create a new resource by PUTting the given map {@code bodyValues} to http request, + * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. * * @param url url * @param header http header param @@ -143,7 +183,12 @@ void putFrom(String url, Header header, Map paramValues, /** - * http post + * async http post + * Create a new resource by POSTing the given object to the http request. + *

URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. * * @param url url * @param header http header param @@ -157,7 +202,13 @@ void post(String url, Header header, Query query, Object body, Type responseType, Callback callback) throws Exception; /** - * http post Json + * async http post Json + * Create a new resource by POSTing the given object to the http request, + * http header contentType default 'application/json;charset=UTF-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. * * @param url url * @param header http header param @@ -172,7 +223,13 @@ void postJson(String url, Header header, Map paramValues, St /** - * http post from + * async http post from + * Create a new resource by PUTting the given map {@code bodyValues} to http request, + * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. + *

URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. * * @param url url * @param header http header param @@ -186,7 +243,13 @@ void postFrom(String url, Header header, Query query, Map bodyValues, Type responseType, Callback callback) throws Exception; /** - * http post from + * async http post from + * Create a new resource by PUTting the given map {@code bodyValues} to http request, + * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. * * @param url url * @param header http header param diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java index aa4c4c95f0f..aad7e9b0fe6 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java @@ -16,14 +16,13 @@ package com.alibaba.nacos.common.http.client; -import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.http.HttpUtils; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.MediaType; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.model.RequestHttpEntity; import com.alibaba.nacos.common.utils.HttpMethod; -import com.alibaba.nacos.common.utils.UriUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -93,7 +92,9 @@ public void putJson(String url, Header header, Map paramValu String body, Type responseType, Callback callback) throws Exception { execute(url, HttpMethod.PUT, new RequestHttpEntity( - header, Query.newInstance().initParams(paramValues), body), responseType, callback); + header.setContentType(MediaType.APPLICATION_JSON), + Query.newInstance().initParams(paramValues), body), + responseType, callback); } @@ -102,7 +103,7 @@ public void putFrom(String url, Header header, Query query, Map callback) throws Exception { execute(url, HttpMethod.PUT, new RequestHttpEntity( - header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), + header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues), responseType, callback); } @@ -111,7 +112,7 @@ public void putFrom(String url, Header header, Map paramValu Map bodyValues, Type responseType, Callback callback) throws Exception { execute(url, HttpMethod.PUT, new RequestHttpEntity( - header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), + header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), Query.newInstance().initParams(paramValues), bodyValues), responseType, callback); } @@ -128,7 +129,9 @@ public void postJson(String url, Header header, Map paramVal String body, Type responseType, Callback callback) throws Exception { execute(url, HttpMethod.POST, new RequestHttpEntity( - header, Query.newInstance().initParams(paramValues), body), responseType, callback); + header.setContentType(MediaType.APPLICATION_JSON), + Query.newInstance().initParams(paramValues), body), + responseType, callback); } @Override @@ -136,7 +139,7 @@ public void postFrom(String url, Header header, Query query, Map callback) throws Exception { execute(url, HttpMethod.POST, new RequestHttpEntity( - header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues), + header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues), responseType, callback); } @@ -145,7 +148,7 @@ public void postFrom(String url, Header header, Map paramVal Map bodyValues, Type responseType, Callback callback) throws Exception { execute(url, HttpMethod.POST, new RequestHttpEntity( - header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), + header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), Query.newInstance().initParams(paramValues), bodyValues), responseType, callback); @@ -154,7 +157,7 @@ public void postFrom(String url, Header header, Map paramVal private void execute(String url, String httpMethod, RequestHttpEntity requestEntity, Type responseType, Callback callback) throws Exception { - URI uri = UriUtils.buildUri(url, requestEntity.getQuery()); + URI uri = HttpUtils.buildUri(url, requestEntity.getQuery()); if (logger.isDebugEnabled()) { logger.debug("HTTP " + httpMethod + " " + url); } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java index 9825775dfa3..5b7216a77d8 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java @@ -16,15 +16,14 @@ package com.alibaba.nacos.common.http.client; -import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.http.HttpRestResult; +import com.alibaba.nacos.common.http.HttpUtils; import com.alibaba.nacos.common.http.handler.ResponseHandler; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.MediaType; import com.alibaba.nacos.common.http.param.Query; import com.alibaba.nacos.common.model.RequestHttpEntity; -import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.HttpMethod; -import com.alibaba.nacos.common.utils.UriUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -51,36 +50,36 @@ public NacosRestTemplate(HttpClientRequest requestClient) { } @Override - public RestResult get(String url, Header header, Query query, Type responseType) throws Exception { + public HttpRestResult get(String url, Header header, Query query, Type responseType) throws Exception { return execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseType); } @Override - public RestResult get(String url, Header header, Map paramValues, Type responseType) throws Exception { + public HttpRestResult get(String url, Header header, Map paramValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity(header, Query.newInstance().initParams(paramValues)); return execute(url, HttpMethod.GET, requestHttpEntity, responseType); } @Override - public RestResult getLarge(String url, Header header, Query query, Object body, Type responseType) throws Exception { + public HttpRestResult getLarge(String url, Header header, Query query, Object body, Type responseType) throws Exception { return execute(url, HttpMethod.GET_LARGE, new RequestHttpEntity(header, query, body), responseType); } @Override - public RestResult delete(String url, Header header, Query query, Type responseType) throws Exception { + public HttpRestResult delete(String url, Header header, Query query, Type responseType) throws Exception { return execute(url, HttpMethod.DELETE, new RequestHttpEntity(header, query), responseType); } @Override - public RestResult put(String url, Header header, Query query, Object body, Type responseType) throws Exception { + public HttpRestResult put(String url, Header header, Query query, Object body, Type responseType) throws Exception { return execute(url, HttpMethod.PUT, new RequestHttpEntity(header, query, body), responseType); } @Override - public RestResult putJson(String url, Header header, Map paramValues, String body, Type responseType) throws Exception { + public HttpRestResult putJson(String url, Header header, Map paramValues, String body, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( - header, + header.setContentType(MediaType.APPLICATION_JSON), Query.newInstance().initParams(paramValues), body); @@ -88,16 +87,16 @@ public RestResult putJson(String url, Header header, Map } @Override - public RestResult putFrom(String url, Header header, Query query, Map bodyValues, Type responseType) throws Exception { + public HttpRestResult putFrom(String url, Header header, Query query, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( - header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues); + header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues); return execute(url, HttpMethod.PUT, requestHttpEntity, responseType); } @Override - public RestResult putFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception { + public HttpRestResult putFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( - header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), + header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), Query.newInstance().initParams(paramValues), bodyValues); @@ -105,14 +104,15 @@ public RestResult putFrom(String url, Header header, Map } @Override - public RestResult post(String url, Header header, Query query, Object body, Type responseType) throws Exception { - return execute(url, HttpMethod.POST, new RequestHttpEntity(header, query, body), responseType); + public HttpRestResult post(String url, Header header, Query query, Object body, Type responseType) throws Exception { + return execute(url, HttpMethod.POST, new RequestHttpEntity(header, query, body), + responseType); } @Override - public RestResult postJson(String url, Header header, Map paramValues, String body, Type responseType) throws Exception { + public HttpRestResult postJson(String url, Header header, Map paramValues, String body, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( - header, + header.setContentType(MediaType.APPLICATION_JSON), Query.newInstance().initParams(paramValues), body); @@ -120,16 +120,17 @@ public RestResult postJson(String url, Header header, Map } @Override - public RestResult postFrom(String url, Header header, Query query, Map bodyValues, Type responseType) throws Exception { + public HttpRestResult postFrom(String url, Header header, Query query, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( - header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues); + header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues); + return execute(url, HttpMethod.POST, requestHttpEntity, responseType); } @Override - public RestResult postFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception { + public HttpRestResult postFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( - header.addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED), + header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), Query.newInstance().initParams(paramValues), bodyValues); @@ -137,9 +138,9 @@ public RestResult postFrom(String url, Header header, Map } - private RestResult execute(String url, String httpMethod, RequestHttpEntity requestEntity, + private HttpRestResult execute(String url, String httpMethod, RequestHttpEntity requestEntity, Type responseType) throws Exception { - URI uri = UriUtils.buildUri(url, requestEntity.getQuery()); + URI uri = HttpUtils.buildUri(url, requestEntity.getQuery()); if (logger.isDebugEnabled()) { logger.debug("HTTP " + httpMethod + " " + url); } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java index d8c28b5dcb3..49a4697237f 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java @@ -16,9 +16,9 @@ package com.alibaba.nacos.common.http.client; +import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; -import com.alibaba.nacos.common.model.RestResult; import java.lang.reflect.Type; import java.util.Map; @@ -33,168 +33,207 @@ public interface RestOperations { /** * http get + * URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. * * @param url url * @param header http header param * @param query http query param * @param responseType return type - * @return the RestResult + * @return the HttpRestResult * @throws Exception ex */ - RestResult get(String url, Header header, Query query, Type responseType) throws Exception; + HttpRestResult get(String url, Header header, Query query, Type responseType) throws Exception; /** * http get + * URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. * * @param url url * @param header headers * @param paramValues paramValues * @param responseType return type - * @return the RestResult + * @return the HttpRestResult * @throws Exception ex */ - RestResult get(String url, Header header, Map paramValues, Type responseType) throws Exception; + HttpRestResult get(String url, Header header, Map paramValues, Type responseType) throws Exception; /** * get request, may be pulling a lot of data + * URL request params are expanded using the given query {@link Query}, + * More request parameters can be set via body. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. * * @param url url * @param header http header param * @param query http query param * @param body get with body * @param responseType return type - * @return {@link RestResult } + * @return {@link HttpRestResult } * @throws Exception ex */ - RestResult getLarge(String url, Header header, Query query, Object body, + HttpRestResult getLarge(String url, Header header, Query query, Object body, Type responseType) throws Exception; /** * http delete + * URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. * * @param url url * @param header http header param * @param query http query param * @param responseType return type - * @return {@link RestResult } + * @return {@link HttpRestResult } * @throws Exception ex */ - RestResult delete(String url, Header header, Query query, + HttpRestResult delete(String url, Header header, Query query, Type responseType) throws Exception; /** * http put + * Create a new resource by PUTting the given body to http request. + *

URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. * * @param url url * @param header http header param * @param query http query param * @param body http body param * @param responseType return type - * @return {@link RestResult} + * @return {@link HttpRestResult} * @throws Exception ex */ - RestResult put(String url, Header header, Query query, Object body, + HttpRestResult put(String url, Header header, Query query, Object body, Type responseType) throws Exception; /** - * http put Json + * http put json + * Create a new resource by PUTting the given body to http request, + * http header contentType default 'application/json;charset=UTF-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. * * @param url url * @param header http header param * @param paramValues http query param * @param body http body param * @param responseType return type - * @return {@link RestResult} + * @return {@link HttpRestResult} * @throws Exception ex */ - RestResult putJson(String url, Header header, Map paramValues, String body, - Type responseType) throws Exception; + HttpRestResult putJson(String url, Header header, Map paramValues, String body, + Type responseType) throws Exception; /** * http put from + * Create a new resource by PUTting the given map {@code bodyValues} to http request, + * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. + *

URL request params are expanded using the given query {@code Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. * * @param url url * @param header http header param * @param query http query param * @param bodyValues http body param * @param responseType return type - * @return {@link RestResult} + * @return {@link HttpRestResult} * @throws Exception ex */ - RestResult putFrom(String url, Header header, Query query, + HttpRestResult putFrom(String url, Header header, Query query, Map bodyValues, Type responseType) throws Exception; /** * http put from + * Create a new resource by PUTting the given map {@code bodyValues} to http request, + * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. * * @param url url * @param header http header param * @param paramValues http query param * @param bodyValues http body param * @param responseType return type - * @return {@link RestResult} + * @return {@link HttpRestResult} * @throws Exception ex */ - RestResult putFrom(String url, Header header, Map paramValues, + HttpRestResult putFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception; /** * http post + * Create a new resource by POSTing the given object to the http request. + *

URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. * * @param url url * @param header http header param * @param query http query param * @param body http body param * @param responseType return type - * @return {@link RestResult} + * @return {@link HttpRestResult} * @throws Exception ex */ - RestResult post(String url, Header header, Query query, Object body, + HttpRestResult post(String url, Header header, Query query, Object body, Type responseType) throws Exception; /** - * http post Json + * http post json + * Create a new resource by POSTing the given object to the http request, + * http header contentType default 'application/json;charset=UTF-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. * * @param url url * @param header http header param * @param paramValues http query param * @param body http body param * @param responseType return type - * @return {@link RestResult} + * @return {@link HttpRestResult} * @throws Exception ex */ - RestResult postJson(String url, Header header, Map paramValues, String body, + HttpRestResult postJson(String url, Header header, Map paramValues, String body, Type responseType) throws Exception; /** * http post from + * Create a new resource by PUTting the given map {@code bodyValues} to http request, + * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. + *

URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. * * @param url url * @param header http header param * @param query http query param * @param bodyValues http body param * @param responseType return type - * @return {@link RestResult} + * @return {@link HttpRestResult} * @throws Exception ex */ - RestResult postFrom(String url, Header header, Query query, + HttpRestResult postFrom(String url, Header header, Query query, Map bodyValues, Type responseType) throws Exception; /** * http post from + * Create a new resource by PUTting the given map {@code bodyValues} to http request, + * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. * * @param url url * @param header http header param * @param paramValues http query param * @param bodyValues http body param * @param responseType return type - * @return {@link RestResult} + * @return {@link HttpRestResult} * @throws Exception ex */ - RestResult postFrom(String url, Header header, Map paramValues, + HttpRestResult postFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception; } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java b/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java index aea2dfc0787..d9620578d39 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.common.http.handler; import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.http.client.HttpClientResponse; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.MediaType; @@ -28,6 +29,7 @@ import java.io.InputStream; import java.lang.reflect.Type; + import org.slf4j.Logger; /** @@ -45,12 +47,12 @@ public static T convert(String s, Type type) throws Exception { return JacksonUtils.toObj(s, type); } - public static T convert(InputStream inputStream, Class tClass) throws Exception{ + public static T convert(InputStream inputStream, Class tClass) throws Exception { return JacksonUtils.toObj(inputStream, tClass); } @SuppressWarnings({"unchecked", "rawtypes", "resource"}) - public static RestResult responseEntityExtractor(HttpClientResponse response, Type type) throws Exception{ + public static HttpRestResult responseEntityExtractor(HttpClientResponse response, Type type) throws Exception { Header headers = response.getHeaders(); String contentType = headers.getValue(HttpHeaderConsts.CONTENT_TYPE); String body = IoUtils.toString(response.getBody(), headers.getCharset()); @@ -59,9 +61,19 @@ public static RestResult responseEntityExtractor(HttpClientResponse respo extractBody = convert(body, type); } if (extractBody instanceof RestResult) { - return (RestResult) extractBody; + HttpRestResult httpRestResult = convert((RestResult) extractBody); + httpRestResult.setHeader(headers); + return httpRestResult; } - return new RestResult<>(response.getHeaders(), response.getStatusCode(), extractBody); + return new HttpRestResult<>(response.getHeaders(), response.getStatusCode(), extractBody); + } + + private static HttpRestResult convert(RestResult restResult) { + HttpRestResult httpRestResult = new HttpRestResult<>(); + httpRestResult.setCode(restResult.getCode()); + httpRestResult.setData(restResult.getData()); + httpRestResult.setMessage(restResult.getMessage()); + return httpRestResult; } } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java index 75c72948381..ecc9cd87447 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java @@ -53,6 +53,13 @@ public Header addParam(String key, String value) { return this; } + public Header setContentType(String contentType) { + if (contentType == null) { + contentType = MediaType.APPLICATION_JSON; + } + return addParam(HttpHeaderConsts.CONTENT_TYPE, contentType); + } + public Header build() { return this; } diff --git a/common/src/main/java/com/alibaba/nacos/common/model/RestResult.java b/common/src/main/java/com/alibaba/nacos/common/model/RestResult.java index b4e8a6eb320..d7893320abe 100644 --- a/common/src/main/java/com/alibaba/nacos/common/model/RestResult.java +++ b/common/src/main/java/com/alibaba/nacos/common/model/RestResult.java @@ -16,8 +16,6 @@ package com.alibaba.nacos.common.model; -import com.alibaba.nacos.common.http.param.Header; - import java.io.Serializable; /** @@ -27,7 +25,6 @@ public class RestResult implements Serializable { private static final long serialVersionUID = 6095433538316185017L; - private Header header; private int code; private String message; private T data; @@ -35,12 +32,6 @@ public class RestResult implements Serializable { public RestResult() { } - public RestResult(Header header, int code, T data) { - this.header = header; - this.code = code; - this.data = data; - } - public RestResult(int code, String message, T data) { this.code = code; this.setMessage(message); @@ -57,14 +48,6 @@ public RestResult(int code, String message) { this.setMessage(message); } - public Header getHeader() { - return header; - } - - public void setHeader(Header header) { - this.header = header; - } - public int getCode() { return code; } @@ -96,8 +79,7 @@ public boolean ok() { @Override public String toString() { return "RestResult{" + - "header=" + header + - ", code=" + code + + "code=" + code + ", message='" + message + '\'' + ", data=" + data + '}'; @@ -108,7 +90,6 @@ public static ResResultBuilder builder() { } public static final class ResResultBuilder { - private Header header; private int code; private String errMsg; private T data; @@ -116,11 +97,6 @@ public static final class ResResultBuilder { private ResResultBuilder() { } - public ResResultBuilder withHeader(Header header) { - this.header = header; - return this; - } - public ResResultBuilder withCode(int code) { this.code = code; return this; @@ -138,7 +114,6 @@ public ResResultBuilder withData(T data) { public RestResult build() { RestResult restResult = new RestResult(); - restResult.setHeader(header); restResult.setCode(code); restResult.setMessage(errMsg); restResult.setData(data); diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/UriUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/UriUtils.java deleted file mode 100644 index 5d08724152c..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/utils/UriUtils.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.alibaba.nacos.common.utils; - -import com.alibaba.nacos.common.http.param.Query; - -import java.net.URI; -import java.net.URISyntaxException; - -/** - * URI build utils - * - * @author mai.jh - * @date 2020/5/24 - */ -public class UriUtils { - - private final static UriUtils URI_UTILS = new UriUtils(); - - private UriUtils() { - } - - - public static UriUtils newInstance() { - return URI_UTILS; - } - - /** - * build URI By url and query - * @param url url - * @param query query param {@link Query} - * @return - */ - public static URI buildUri(String url, Query query) throws URISyntaxException { - if (!query.isEmpty()) { - url = url + "?" + query.toQueryUrl(); - } - return new URI(url); - } -} diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java index 151f1f46040..66722c6e6c9 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.common.http.Callback; import com.alibaba.nacos.common.http.HttpClientManager; +import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; @@ -64,12 +65,12 @@ public void init() throws NacosException { private class CallbackMap implements Callback { - private RestResult restResult; + private HttpRestResult restResult; private Throwable throwable; @Override public void onReceive(RestResult result) { - restResult = result; + restResult = (HttpRestResult) result; } @Override @@ -77,7 +78,7 @@ public void onError(Throwable throwable) { throwable = throwable; } - public RestResult getRestResult() { + public HttpRestResult getRestResult() { return restResult; } @@ -96,8 +97,9 @@ public void test_url_post_from() throws Exception{ CallbackMap callbackMap = new CallbackMap<>(); nacosRestTemplate.postFrom(url, Header.newInstance(), Query.newInstance(), param, String.class, callbackMap); Thread.sleep(2000); - RestResult restResult = callbackMap.getRestResult(); + HttpRestResult restResult = callbackMap.getRestResult(); System.out.println(restResult.getData()); + System.out.println(restResult.getHeader()); Assert.assertTrue(restResult.ok()); } @@ -111,8 +113,9 @@ public void test_url_put_from() throws Exception{ CallbackMap callbackMap = new CallbackMap<>(); nacosRestTemplate.putFrom(url, Header.newInstance(), Query.newInstance(), param, String.class, callbackMap); Thread.sleep(2000); - RestResult restResult = callbackMap.getRestResult(); + HttpRestResult restResult = callbackMap.getRestResult(); System.out.println(restResult.getData()); + System.out.println(restResult.getHeader()); Assert.assertTrue(restResult.ok()); } @@ -124,8 +127,9 @@ public void test_url_get() throws Exception { CallbackMap callbackMap = new CallbackMap<>(); nacosRestTemplate.get(url, Header.newInstance(), query, Map.class, callbackMap); Thread.sleep(2000); - RestResult restResult = callbackMap.getRestResult(); + HttpRestResult restResult = callbackMap.getRestResult(); System.out.println(restResult.getData()); + System.out.println(restResult.getHeader()); Assert.assertTrue(restResult.ok()); Assert.assertEquals(restResult.getData().get("dom"), "app-test"); @@ -139,8 +143,9 @@ public void test_url_by_map() throws Exception{ CallbackMap callbackMap = new CallbackMap<>(); nacosRestTemplate.get(url, Header.newInstance(), param, Map.class, callbackMap); Thread.sleep(2000); - RestResult restResult = callbackMap.getRestResult(); + HttpRestResult restResult = callbackMap.getRestResult(); System.out.println(restResult.getData()); + System.out.println(restResult.getHeader()); Assert.assertTrue(restResult.ok()); Assert.assertEquals(restResult.getData().get("dom"), "app-test"); @@ -156,8 +161,9 @@ public void test_url_delete() throws Exception{ CallbackMap callbackMap = new CallbackMap<>(); nacosRestTemplate.delete(url, Header.newInstance(), query, RestResult.class, callbackMap); Thread.sleep(2000); - RestResult restResult = callbackMap.getRestResult(); + HttpRestResult restResult = callbackMap.getRestResult(); System.out.println(restResult.getData()); + System.out.println(restResult.getHeader()); Assert.assertTrue(restResult.ok()); } diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java index 9a28d22fe99..d67d175e4c5 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.Nacos; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.common.http.HttpClientManager; +import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.http.client.NacosRestTemplate; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; @@ -53,7 +54,8 @@ public class NacosRestTemplate_ITCase { private NacosRestTemplate nacosRestTemplate = HttpClientManager.getNacosRestTemplate(); - private final String CONFIG_INSTANCE_PATH = "/nacos/v1/ns"; + private final String INSTANCE_PATH = "/nacos/v1/ns"; + private final String CONFIG_PATH = "/nacos/v1/cs"; private String IP = null; @Before @@ -61,36 +63,46 @@ public void init() throws NacosException { IP = String.format("http://localhost:%d", port); } + @Test + public void test_url_get_return_restResult() throws Exception{ + String url = IP + CONFIG_PATH + "/configs"; + Query query = Query.newInstance().addParam("beta", true).addParam("dataId","test-1").addParam("group", "DEFAULT_GROUP"); + HttpRestResult restResult = nacosRestTemplate.get(url, Header.newInstance(), query, RestResult.class); + Assert.assertTrue(restResult.ok()); + System.out.println(restResult.getData()); + System.out.println(restResult.getHeader()); + } + @Test public void test_url_post_from() throws Exception{ - String url = IP + CONFIG_INSTANCE_PATH + "/instance"; + String url = IP + INSTANCE_PATH + "/instance"; Map param = new HashMap<>(); param.put("serviceName", "app-test"); param.put("port", "8080"); param.put("ip", "11.11.11.11"); - RestResult restResult = nacosRestTemplate.postFrom(url, Header.newInstance(), Query.newInstance(), param, String.class); + HttpRestResult restResult = nacosRestTemplate.postFrom(url, Header.newInstance(), Query.newInstance(), param, String.class); Assert.assertTrue(restResult.ok()); System.out.println(restResult.getData()); } @Test public void test_url_put_from() throws Exception{ - String url = IP + CONFIG_INSTANCE_PATH + "/instance"; + String url = IP + INSTANCE_PATH + "/instance"; Map param = new HashMap<>(); param.put("serviceName", "app-test-change"); param.put("port", "8080"); param.put("ip", "11.11.11.11"); - RestResult restResult = nacosRestTemplate.putFrom(url, Header.newInstance(), Query.newInstance(), param, String.class); + HttpRestResult restResult = nacosRestTemplate.putFrom(url, Header.newInstance(), Query.newInstance(), param, String.class); Assert.assertTrue(restResult.ok()); System.out.println(restResult.getData()); } @Test public void test_url_get() throws Exception { - String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; + String url = IP + INSTANCE_PATH + "/instance/list"; Query query = Query.newInstance().addParam("serviceName", "app-test"); - RestResult restResult = nacosRestTemplate.get(url, Header.newInstance(), query, Map.class); + HttpRestResult restResult = nacosRestTemplate.get(url, Header.newInstance(), query, Map.class); Assert.assertTrue(restResult.ok()); Assert.assertEquals(restResult.getData().get("dom"), "app-test"); System.out.println(restResult.getData()); @@ -98,10 +110,10 @@ public void test_url_get() throws Exception { @Test public void test_url_get_by_map() throws Exception { - String url = IP + CONFIG_INSTANCE_PATH + "/instance/list"; + String url = IP + INSTANCE_PATH + "/instance/list"; Map param = new HashMap<>(); param.put("serviceName", "app-test"); - RestResult restResult = nacosRestTemplate.get(url, Header.newInstance(), param, Map.class); + HttpRestResult restResult = nacosRestTemplate.get(url, Header.newInstance(), param, Map.class); Assert.assertTrue(restResult.ok()); Assert.assertEquals(restResult.getData().get("dom"), "app-test"); System.out.println(restResult.getData()); @@ -109,14 +121,14 @@ public void test_url_get_by_map() throws Exception { @Test public void test_url_delete() throws Exception{ - String url = IP + CONFIG_INSTANCE_PATH + "/instance"; + String url = IP + INSTANCE_PATH + "/instance"; Query query = Query.newInstance() .addParam("ip", "11.11.11.11") .addParam("port", "8080") .addParam("serviceName", "app-test"); - RestResult restResult = nacosRestTemplate.delete(url, Header.newInstance(), query, RestResult.class); + HttpRestResult restResult = nacosRestTemplate.delete(url, Header.newInstance(), query, RestResult.class); Assert.assertTrue(restResult.ok()); - System.out.println(restResult.getData()); + System.out.println(restResult); } From 7e05f3e18ed8b941a425a10f16c3cd102d381147 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Sun, 31 May 2020 18:18:41 +0800 Subject: [PATCH 030/156] add class file license. --- .../nacos/common/model/RequestHttpEntity.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java b/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java index 1d3b3585d50..dc56afa72de 100644 --- a/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java +++ b/common/src/main/java/com/alibaba/nacos/common/model/RequestHttpEntity.java @@ -1,8 +1,22 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.common.model; -import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.http.param.Header; -import com.alibaba.nacos.common.http.param.MediaType; import com.alibaba.nacos.common.http.param.Query; import java.util.Map; From 0c02111eb16835e2e8f143f2dff2ce41d85a7f24 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Sun, 31 May 2020 18:19:06 +0800 Subject: [PATCH 031/156] add class file license. --- .../nacos/common/http/HttpRestResult.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/common/src/main/java/com/alibaba/nacos/common/http/HttpRestResult.java b/common/src/main/java/com/alibaba/nacos/common/http/HttpRestResult.java index beff90832ea..a5c96a16c65 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/HttpRestResult.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/HttpRestResult.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.common.http; import com.alibaba.nacos.common.http.param.Header; From 4fc79b012f6567bde4ce8adfe473dafe74c39427 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Mon, 1 Jun 2020 15:42:20 +0800 Subject: [PATCH 032/156] code format. --- .../java/com/alibaba/nacos/common/http/HttpClientManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java index 99111944a44..e86eef7b452 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java @@ -94,8 +94,8 @@ public static void shutdown() { try { SYNC_HTTP_CLIENT.close(); ASYNC_HTTP_CLIENT.close(); - NACOS_REST_TEMPLATE.close(); - NACOS_ASYNC_REST_TEMPLATE.close(); + NACOS_REST_TEMPLATE.close(); + NACOS_ASYNC_REST_TEMPLATE.close(); } catch (Exception ex) { logger.error("An exception occurred when the HTTP client was closed : {}", From 8768de406c1273fffa1e18201b24d1f9c2a3171e Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Mon, 1 Jun 2020 15:49:28 +0800 Subject: [PATCH 033/156] Merge code. --- .../alibaba/nacos/common/utils/JacksonUtils.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java index 445e3f8b2c0..969dc2bf91a 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java @@ -92,22 +92,6 @@ public static T toObj(byte[] json, TypeReference typeReference) { throw new NacosDeserializationException(e); } } - try { - return toObj(StringUtils.newString4UTF8(json), cls); - } - catch (Exception e) { - throw new NacosDeserializationException(e); - } - } - - public static T toObj(byte[] json, TypeReference typeReference) { - try { - return toObj(StringUtils.newString4UTF8(json), typeReference); - } - catch (Exception e) { - throw new NacosDeserializationException(e); - } - } public static T toObj(String json, Class cls) { try { From 379aa15c85f0a78b52cd782296ec5fcb9831f9df Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Mon, 1 Jun 2020 16:02:41 +0800 Subject: [PATCH 034/156] Added syntax compatible with Java 6 --- .../java/com/alibaba/nacos/common/http/BaseHttpMethod.java | 2 +- .../alibaba/nacos/common/http/handler/ResponseHandler.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpMethod.java b/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpMethod.java index fcc129dfb00..4bbdcd4d132 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpMethod.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpMethod.java @@ -178,7 +178,7 @@ public void initFromEntity(Map body, String charset) throws Exce if (body.isEmpty()) { return; } - List params = new ArrayList<>(body.size()); + List params = new ArrayList(body.size()); for (Map.Entry entry : body.entrySet()) { params.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java b/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java index d9620578d39..bea02ebb2b8 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java @@ -65,11 +65,11 @@ public static HttpRestResult responseEntityExtractor(HttpClientResponse r httpRestResult.setHeader(headers); return httpRestResult; } - return new HttpRestResult<>(response.getHeaders(), response.getStatusCode(), extractBody); + return new HttpRestResult(response.getHeaders(), response.getStatusCode(), extractBody); } private static HttpRestResult convert(RestResult restResult) { - HttpRestResult httpRestResult = new HttpRestResult<>(); + HttpRestResult httpRestResult = new HttpRestResult(); httpRestResult.setCode(restResult.getCode()); httpRestResult.setData(restResult.getData()); httpRestResult.setMessage(restResult.getMessage()); From 8d6dd8f41b362967ec9d19a041a9bc9b2ab76095 Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Mon, 1 Jun 2020 17:57:35 +0800 Subject: [PATCH 035/156] [#1815]adjust codes and reput the lifecycle to ConfigService class level. --- .../nacos/api/config/ConfigService.java | 11 +++ .../client/config/NacosConfigService.java | 32 ++++--- .../nacos/client/config/http/HttpAgent.java | 3 +- .../client/config/http/MetricsHttpAgent.java | 40 +++++---- .../client/config/http/ServerHttpAgent.java | 83 ++++++++----------- .../client/config/impl/ClientWorker.java | 17 +++- .../common/lifecycle/AbstractLifeCycle.java | 9 +- .../nacos/common/lifecycle/Closeable.java | 28 +++++++ .../nacos/common/utils/ThreadUtils.java | 25 ++++++ .../LifeCycle/LifeCycleManagerTest.java | 3 + .../nacos/test/config/ConfigAPI_ITCase.java | 5 +- .../ConfigExportAndImportAPI_ITCase.java | 5 +- 12 files changed, 173 insertions(+), 88 deletions(-) create mode 100644 common/src/main/java/com/alibaba/nacos/common/lifecycle/Closeable.java diff --git a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java index 87234f160e0..2ffc647a948 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java @@ -105,4 +105,15 @@ public interface ConfigService { */ String getServerStatus(); + /** + * Shutdown the resouces which are managed by config service object instance. + * + */ + /** + * Close the resouces which are managed by config service object instance. + * + * @throws Exception Exception occours when executing close operation. + */ + void shutdown() throws Exception; + } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java index 28a383452f6..b4ba5953c51 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java @@ -34,12 +34,15 @@ import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.ValidatorUtils; +import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; +import com.alibaba.nacos.common.lifecycle.LifeCycleState; import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import java.io.IOException; import java.net.HttpURLConnection; +import java.rmi.server.ExportException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -51,18 +54,16 @@ * @author Nacos */ @SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") -public class NacosConfigService implements ConfigService { +public class NacosConfigService extends AbstractLifeCycle implements ConfigService { private static final Logger LOGGER = LogUtils.logger(NacosConfigService.class); private static final long POST_TIMEOUT = 3000L; - private static final ResourceLifeCycleManager RESOURCE_MANAGER = ResourceLifeCycleManager.getInstance(); - /** * http agent */ - private MetricsHttpAgent agent; + private HttpAgent agent; /** * longpolling */ @@ -72,6 +73,8 @@ public class NacosConfigService implements ConfigService { private ConfigFilterChainManager configFilterChainManager = new ConfigFilterChainManager(); public NacosConfigService(Properties properties) throws NacosException { + super(); + ValidatorUtils.checkInitParam(properties); String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE); if (StringUtils.isBlank(encodeTmp)) { @@ -82,13 +85,6 @@ public NacosConfigService(Properties properties) throws NacosException { initNamespace(properties); agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); agent.fetchServerIpList(); - try { - agent.start(); - }catch (Exception e) { - LOGGER.error("An exception occurred during resource start : {}", e); - } - RESOURCE_MANAGER.register(agent); - worker = new ClientWorker(agent, configFilterChainManager, properties); } @@ -291,4 +287,18 @@ public String getServerStatus() { } } + @Override + public void shutdown() throws Exception { + this.stop(); + } + + @Override + protected void doStart() throws Exception { + } + + @Override + protected void doStop() throws Exception { + this.agent.shutdown(); + this.worker.shutdown(); + } } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java index bc6fbc96b5a..5f80cf5f901 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java @@ -17,6 +17,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; +import com.alibaba.nacos.common.lifecycle.Closeable; import java.io.IOException; import java.util.List; @@ -27,7 +28,7 @@ * * @author Nacos */ -public interface HttpAgent { +public interface HttpAgent extends Closeable { /** * start to get nacos ip list diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java index e50ca659aea..4b0b75e4b18 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java @@ -13,12 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/** + * MetricsHttpAgent + * + * @author Nacos + */ package com.alibaba.nacos.client.config.http; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.monitor.MetricsMonitor; -import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; import io.prometheus.client.Histogram; import java.io.IOException; @@ -29,16 +33,16 @@ * * @author Nacos */ -public class MetricsHttpAgent extends AbstractLifeCycle implements HttpAgent { - - private ServerHttpAgent serverHttpAgent; +public class MetricsHttpAgent implements HttpAgent { + private HttpAgent httpAgent; - public MetricsHttpAgent(ServerHttpAgent serverHttpAgent) { - this.serverHttpAgent = serverHttpAgent; + public MetricsHttpAgent(HttpAgent httpAgent) { + this.httpAgent = httpAgent; } + @Override public void fetchServerIpList() throws NacosException { - this.serverHttpAgent.fetchServerIpList(); + httpAgent.fetchServerIpList(); } @Override @@ -46,7 +50,7 @@ public HttpResult httpGet(String path, List headers, List paramV Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("GET", path, "NA"); HttpResult result; try { - result = this.serverHttpAgent.httpGet(path, headers, paramValues, encoding, readTimeoutMs); + result = httpAgent.httpGet(path, headers, paramValues, encoding, readTimeoutMs); } catch (IOException e) { throw e; } finally { @@ -62,7 +66,7 @@ public HttpResult httpPost(String path, List headers, List param Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("POST", path, "NA"); HttpResult result; try { - result = this.serverHttpAgent.httpPost(path, headers, paramValues, encoding, readTimeoutMs); + result = httpAgent.httpPost(path, headers, paramValues, encoding, readTimeoutMs); } catch (IOException e) { throw e; } finally { @@ -78,7 +82,7 @@ public HttpResult httpDelete(String path, List headers, List par Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("DELETE", path, "NA"); HttpResult result; try { - result = this.serverHttpAgent.httpDelete(path, headers, paramValues, encoding, readTimeoutMs); + result = httpAgent.httpDelete(path, headers, paramValues, encoding, readTimeoutMs); } catch (IOException e) { throw e; @@ -92,32 +96,32 @@ public HttpResult httpDelete(String path, List headers, List par @Override public String getName() { - return this.serverHttpAgent.getName(); + return httpAgent.getName(); } @Override public String getNamespace() { - return this.serverHttpAgent.getNamespace(); + return httpAgent.getNamespace(); } @Override public String getTenant() { - return this.serverHttpAgent.getTenant(); + return httpAgent.getTenant(); } @Override public String getEncode() { - return this.serverHttpAgent.getEncode(); + return httpAgent.getEncode(); } @Override - public void doStart() throws Exception { - this.serverHttpAgent.doStart(); + public void close() throws IOException { + } @Override - public void doStop() throws Exception { - this.serverHttpAgent.doStop(); + public void shutdown() throws InterruptedException { + httpAgent.shutdown(); } } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java index 4aca49c4e7a..b1c1e102b51 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java @@ -30,6 +30,7 @@ import com.alibaba.nacos.client.utils.TemplateUtils; import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; import com.alibaba.nacos.common.utils.IoUtils; +import com.alibaba.nacos.common.utils.ThreadUtils; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.type.TypeReference; import org.apache.commons.lang3.StringUtils; @@ -52,7 +53,7 @@ * * @author water.lyl */ -public class ServerHttpAgent extends AbstractLifeCycle implements HttpAgent { +public class ServerHttpAgent implements HttpAgent { private static final Logger LOGGER = LogUtils.logger(ServerHttpAgent.class); @@ -261,6 +262,26 @@ public ServerHttpAgent(Properties properties) throws NacosException { namespaceId = properties.getProperty(PropertyKeyConst.NAMESPACE); init(properties); securityProxy.login(serverListMgr.getServerUrls()); + + + // init executorService + this.executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread t = new Thread(r); + t.setName("com.alibaba.nacos.client.config.security.updater"); + t.setDaemon(true); + return t; + } + }); + + this.executorService.scheduleWithFixedDelay(new Runnable() { + @Override + public void run() { + securityProxy.login(serverListMgr.getServerUrls()); + } + }, 0, securityInfoRefreshIntervalMills, TimeUnit.MILLISECONDS); + } private void injectSecurityInfo(List params) { @@ -314,55 +335,6 @@ private void initMaxRetry(Properties properties) { maxRetry = NumberUtils.toInt(String.valueOf(properties.get(PropertyKeyConst.MAX_RETRY)), Constants.MAX_RETRY); } - @Override - protected void doStart() throws Exception { - LOGGER.info("do start begin"); - - // init executorService - this.executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - Thread t = new Thread(r); - t.setName("com.alibaba.nacos.client.config.security.updater"); - t.setDaemon(true); - return t; - } - }); - - this.executorService.scheduleWithFixedDelay(new Runnable() { - @Override - public void run() { - securityProxy.login(serverListMgr.getServerUrls()); - } - }, 0, securityInfoRefreshIntervalMills, TimeUnit.MILLISECONDS); - - LOGGER.info("do start end"); - } - - @Override - protected void doStop() throws Exception { - LOGGER.info("do stop begin"); - - this.executorService.shutdown(); - int retry = 3; - while (retry > 0) { - retry --; - try { - if (this.executorService.awaitTermination(10, TimeUnit.SECONDS)) { - return; - } - } catch (InterruptedException e) { - this.executorService.shutdownNow(); - Thread.interrupted(); - } catch (Throwable ex) { - LOGGER.error("shutdown the executor has error : {}", ex); - } - this.executorService.shutdownNow(); - } - - LOGGER.info("do stop end"); - } - @Override public void fetchServerIpList() throws NacosException { // fetch server address urls list @@ -467,6 +439,17 @@ public String getEncode() { return encode; } + @Override + public void close() throws IOException { + } + + @Override + public void shutdown() throws InterruptedException { + LOGGER.info("do shutdown begin"); + ThreadUtils.shutdown(this.executorService); + LOGGER.info("do shutdown stop"); + } + @SuppressWarnings("PMD.ClassNamingShouldBeCamelRule") private static class STSCredential { @JsonProperty(value = "AccessKeyId") diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index 9218eb2fc16..8a8d6c5550f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -25,12 +25,14 @@ import com.alibaba.nacos.client.config.http.HttpAgent; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.utils.ContentUtils; +import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.naming.utils.CollectionUtils; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.TenantUtil; +import com.alibaba.nacos.common.utils.ThreadUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; @@ -55,7 +57,7 @@ * * @author Nacos */ -public class ClientWorker { +public class ClientWorker implements Closeable { private static final Logger LOGGER = LogUtils.logger(ClientWorker.class); @@ -490,6 +492,19 @@ private void init(Properties properties) { enableRemoteSyncConfig = Boolean.parseBoolean(properties.getProperty(PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG)); } + @Override + public void close() throws IOException { + + } + + @Override + public void shutdown() throws InterruptedException { + LOGGER.info("do shutdown begin"); + ThreadUtils.shutdown(this.executorService); + ThreadUtils.shutdown(this.executor); + LOGGER.info("do shutdown stop"); + } + class LongPollingRunnable implements Runnable { private int taskId; diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java index 141d1ef562c..bf0a716525b 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java @@ -28,7 +28,14 @@ public abstract class AbstractLifeCycle implements LifeCycle { private static final Logger LOG = LoggerFactory.getLogger(AbstractLifeCycle.class); private final Object lock = new Object(); - private volatile LifeCycleState state = LifeCycleState.STOPPED; + private volatile LifeCycleState state; + private static final ResourceLifeCycleManager RESOURCE_MANAGER = ResourceLifeCycleManager.getInstance(); + + + public AbstractLifeCycle() { + this.state = LifeCycleState.STOPPED; + RESOURCE_MANAGER.register(this); + } /** * Method to override to start the lifecycle. diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/Closeable.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/Closeable.java new file mode 100644 index 00000000000..5dd442ae9e3 --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/Closeable.java @@ -0,0 +1,28 @@ +package com.alibaba.nacos.common.lifecycle; + +import java.io.IOException; + +/** + * An interface is used to define the resource's close and shutdown, + * such as IO Connection and ThreadPool. + * + * @author zongtanghu + * + */ +public interface Closeable { + + /** + * Close the Resources, such as IO Connection resourcese. + * + * @throws IOException + */ + public void close() throws IOException; + + /** + * Shutdown the Resources, such as Thread Pool. + * + * @throws InterruptedException + */ + public void shutdown() throws InterruptedException; + +} diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/ThreadUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/ThreadUtils.java index 52ddfba5246..d82e8c55349 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/ThreadUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/ThreadUtils.java @@ -16,7 +16,11 @@ package com.alibaba.nacos.common.utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; /** @@ -24,6 +28,8 @@ */ public final class ThreadUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(ThreadUtils.class); + public static void objectWait(Object object) { try { object.wait(); @@ -71,6 +77,25 @@ public static int getSuitableThreadCount() { return workerCount; } + public static void shutdown(ExecutorService executor) { + executor.shutdown(); + int retry = 3; + while (retry > 0) { + retry --; + try { + if (executor.awaitTermination(10, TimeUnit.SECONDS)) { + return; + } + } catch (InterruptedException e) { + executor.shutdownNow(); + Thread.interrupted(); + } catch (Throwable ex) { + LOGGER.error("shutdown the executor has error : {}", ex); + } + executor.shutdownNow(); + } + } + private final static int THREAD_MULTIPLER = 2; } diff --git a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java index ad66ba9167a..5e7be909b6d 100644 --- a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.common.LifeCycle; import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; +import com.alibaba.nacos.common.lifecycle.LifeCycleState; import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import org.junit.After; import org.junit.Assert; @@ -78,9 +79,11 @@ class Resource extends AbstractLifeCycle { private int counter; public Resource(int counter) { + super(); this.counter = counter; } + public int getCounter() { return counter; } diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java index 6bcbf9a29f4..5c7999f781e 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java @@ -56,7 +56,7 @@ public class ConfigAPI_ITCase { public static final long TIME_OUT = 5000; static ConfigService iconfig = null; - static MetricsHttpAgent agent = null; + static HttpAgent agent = null; static final String CONFIG_CONTROLLER_PATH = "/v1/cs/configs"; String SPECIAL_CHARACTERS = "!@#$%^&*()_+-=_|/'?."; @@ -74,7 +74,6 @@ public void setUp() throws Exception { iconfig = NacosFactory.createConfigService(properties); agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); agent.fetchServerIpList(); - agent.start(); } @After @@ -89,7 +88,7 @@ public void cleanup() throws Exception { e.printStackTrace(); Assert.fail(); } - agent.stop(); + agent.shutdown(); } /** diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java index 725f825abaf..2d9a604c915 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java @@ -57,7 +57,7 @@ public class ConfigExportAndImportAPI_ITCase { private String SERVER_ADDR = null; - private MetricsHttpAgent agent = null; + private HttpAgent agent = null; @Before @@ -68,7 +68,6 @@ public void setUp() throws Exception { properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1"+":"+port); agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); agent.fetchServerIpList(); - agent.start(); Map prarm = new HashMap<>(7); prarm.put("dataId", "testNoAppname1.yml"); @@ -122,7 +121,7 @@ public void cleanup() throws Exception{ } catch (Exception e) { Assert.fail(); } - agent.stop(); + agent.shutdown(); } @Test(timeout = 3*TIME_OUT) From a83241205ccf9ff651065a1874a18be6918ceddc Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Mon, 1 Jun 2020 23:51:53 +0800 Subject: [PATCH 036/156] =?UTF-8?q?optimize=20response=20handler,=20remove?= =?UTF-8?q?=20AsyncRestOperations=EF=BC=8CRestOperations.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NacosDeserializationException.java | 14 + .../nacos/common/http/HttpClientManager.java | 8 +- .../http/client/AsyncRestOperations.java | 264 ------------------ ...ava => DefaultAsyncHttpClientRequest.java} | 8 +- ...se.java => DefaultClientHttpResponse.java} | 4 +- ...est.java => DefaultHttpClientRequest.java} | 6 +- .../http/client/NacosAsyncRestTemplate.java | 206 +++++++++++++- .../common/http/client/NacosRestTemplate.java | 183 +++++++++++- .../common/http/client/RestOperations.java | 239 ---------------- .../common/http/handler/ResponseHandler.java | 18 +- .../nacos/common/utils/JacksonUtils.java | 9 + .../common/NacosAsyncRestTemplate_ITCase.java | 2 +- .../test/common/NacosRestTemplate_ITCase.java | 3 +- 13 files changed, 416 insertions(+), 548 deletions(-) delete mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/AsyncRestOperations.java rename common/src/main/java/com/alibaba/nacos/common/http/client/{ApacheAsyncHttpClientRequest.java => DefaultAsyncHttpClientRequest.java} (87%) rename common/src/main/java/com/alibaba/nacos/common/http/client/{ApacheClientHttpResponse.java => DefaultClientHttpResponse.java} (94%) rename common/src/main/java/com/alibaba/nacos/common/http/client/{ApacheHttpClientRequest.java => DefaultHttpClientRequest.java} (93%) delete mode 100644 common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosDeserializationException.java b/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosDeserializationException.java index 77c423e87cd..78180917bab 100644 --- a/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosDeserializationException.java +++ b/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosDeserializationException.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.api.exception.runtime; +import java.lang.reflect.Type; + /** * Nacos deserialization exception. * @@ -42,6 +44,10 @@ public NacosDeserializationException(Class targetClass) { this.targetClass = targetClass; } + public NacosDeserializationException(Type targetType) { + super(ERROR_CODE, String.format(MSG_FOR_SPECIFIED_CLASS, targetType.toString())); + } + public NacosDeserializationException(Throwable throwable) { super(ERROR_CODE, DEFAULT_MSG, throwable); } @@ -51,6 +57,14 @@ public NacosDeserializationException(Class targetClass, Throwable throwable) this.targetClass = targetClass; } + public NacosDeserializationException(Type targetType, Throwable throwable) { + super(ERROR_CODE, String.format(MSG_FOR_SPECIFIED_CLASS, targetType.toString()), throwable); + } + + + + + public Class getTargetClass() { return targetClass; } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java index e86eef7b452..55efdbc519e 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java @@ -16,8 +16,8 @@ package com.alibaba.nacos.common.http; -import com.alibaba.nacos.common.http.client.ApacheAsyncHttpClientRequest; -import com.alibaba.nacos.common.http.client.ApacheHttpClientRequest; +import com.alibaba.nacos.common.http.client.DefaultAsyncHttpClientRequest; +import com.alibaba.nacos.common.http.client.DefaultHttpClientRequest; import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate; import com.alibaba.nacos.common.http.client.NacosRestTemplate; import com.alibaba.nacos.common.utils.ShutdownUtils; @@ -52,10 +52,10 @@ public class HttpClientManager { HttpAsyncClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build()); private static final NacosRestTemplate NACOS_REST_TEMPLATE = new NacosRestTemplate( - new ApacheHttpClientRequest(HttpClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build())); + new DefaultHttpClientRequest(HttpClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build())); private static final NacosAsyncRestTemplate NACOS_ASYNC_REST_TEMPLATE = new NacosAsyncRestTemplate( - new ApacheAsyncHttpClientRequest(HttpAsyncClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build())); + new DefaultAsyncHttpClientRequest(HttpAsyncClients.custom().setDefaultRequestConfig(DEFAULT_CONFIG).build())); private static final AtomicBoolean alreadyShutdown = new AtomicBoolean(false); diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/AsyncRestOperations.java b/common/src/main/java/com/alibaba/nacos/common/http/client/AsyncRestOperations.java deleted file mode 100644 index bc5a58deefd..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/AsyncRestOperations.java +++ /dev/null @@ -1,264 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.alibaba.nacos.common.http.client; - -import com.alibaba.nacos.common.http.Callback; -import com.alibaba.nacos.common.http.param.Header; -import com.alibaba.nacos.common.http.param.Query; - -import java.lang.reflect.Type; -import java.util.Map; - -/** - * Interface specifying a basic set of async http operations. - * - * @author mai.jh - * @date 2020/5/23 - */ -public interface AsyncRestOperations { - - /** - * async http get - * URL request params are expanded using the given query {@link Query}. - *

{@code responseType} can be an RestResult or RestResult data {@code T} type. - *

{@code callback} Result callback execution, - * if you need response headers, you can convert the received RestResult to HttpRestResult. - * - * @param url url - * @param responseType return type - * @param header http header param - * @param query http query param - * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} - * @throws Exception ex - */ - void get(String url, Header header, Query query, Type responseType, Callback callback) throws Exception; - - /** - * async http get - * URL request params are expanded using the given map {@code paramValues}. - *

{@code responseType} can be an RestResult or RestResult data {@code T} type. - *

{@code callback} Result callback execution, - * if you need response headers, you can convert the received RestResult to HttpRestResult. - * - * @param url url - * @param header headers - * @param paramValues paramValues - * @param responseType return type - * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} - * @throws Exception ex - */ - void get(String url, Header header, Map paramValues, - Type responseType, Callback callback) throws Exception; - - /** - * async get request, may be pulling a lot of data - * URL request params are expanded using the given query {@link Query}, - * More request parameters can be set via body. - *

{@code responseType} can be an RestResult or RestResult data {@code T} type. - *

{@code callback} Result callback execution, - * if you need response headers, you can convert the received RestResult to HttpRestResult. - * - * @param url url - * @param header http header param - * @param query http query param - * @param body get with body - * @param responseType return type - * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} - * @throws Exception ex - */ - void getLarge(String url, Header header, Query query, Object body, - Type responseType, Callback callback) throws Exception; - - /** - * async http delete - * URL request params are expanded using the given query {@link Query}, - *

{@code responseType} can be an RestResult or RestResult data {@code T} type - *

{@code callback} Result callback execution, - * if you need response headers, you can convert the received RestResult to HttpRestResult. - * - * @param url url - * @param header http header param - * @param query http query param - * @param responseType return type - * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} - * @throws Exception ex - */ - void delete(String url, Header header, Query query, - Type responseType, Callback callback) throws Exception; - - /** - * async http put - * Create a new resource by PUTting the given body to http request. - *

URL request params are expanded using the given query {@link Query}. - *

{@code responseType} can be an RestResult or RestResult data {@code T} type - *

{@code callback} Result callback execution, - * if you need response headers, you can convert the received RestResult to HttpRestResult. - * - * @param url url - * @param header http header param - * @param query http query param - * @param body http body param - * @param responseType return type - * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} - * @throws Exception ex - */ - void put(String url, Header header, Query query, Object body, - Type responseType, Callback callback) throws Exception; - - - /** - * async http put Json - * Create a new resource by PUTting the given body to http request, - * http header contentType default 'application/json;charset=UTF-8'. - *

URL request params are expanded using the given map {@code paramValues}. - *

{@code responseType} can be an RestResult or RestResult data {@code T} type - *

{@code callback} Result callback execution, - * if you need response headers, you can convert the received RestResult to HttpRestResult. - * - * @param url url - * @param header http header param - * @param paramValues http query param - * @param body http body param - * @param responseType return type - * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} - * @throws Exception ex - */ - void putJson(String url, Header header, Map paramValues, String body, - Type responseType, Callback callback) throws Exception; - - /** - * async http put from - * Create a new resource by PUTting the given map {@code bodyValues} to http request, - * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. - *

URL request params are expanded using the given query {@link Query}. - *

{@code responseType} can be an RestResult or RestResult data {@code T} type. - *

{@code callback} Result callback execution, - * if you need response headers, you can convert the received RestResult to HttpRestResult. - * - * @param url url - * @param header http header param - * @param query http query param - * @param bodyValues http body param - * @param responseType return type - * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} - * @throws Exception ex - */ - void putFrom(String url, Header header, Query query, - Map bodyValues, Type responseType, Callback callback) throws Exception; - - - /** - * async http put from - * Create a new resource by PUTting the given map {@code bodyValues} to http request, - * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. - *

URL request params are expanded using the given map {@code paramValues}. - *

{@code responseType} can be an RestResult or RestResult data {@code T} type. - *

{@code callback} Result callback execution, - * if you need response headers, you can convert the received RestResult to HttpRestResult. - * - * @param url url - * @param header http header param - * @param paramValues http query param - * @param bodyValues http body param - * @param responseType return type - * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} - * @throws Exception ex - */ - void putFrom(String url, Header header, Map paramValues, - Map bodyValues, Type responseType, Callback callback) throws Exception; - - - /** - * async http post - * Create a new resource by POSTing the given object to the http request. - *

URL request params are expanded using the given query {@link Query}. - *

{@code responseType} can be an RestResult or RestResult data {@code T} type. - *

{@code callback} Result callback execution, - * if you need response headers, you can convert the received RestResult to HttpRestResult. - * - * @param url url - * @param header http header param - * @param query http query param - * @param body http body param - * @param responseType return type - * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} - * @throws Exception ex - */ - void post(String url, Header header, Query query, Object body, - Type responseType, Callback callback) throws Exception; - - /** - * async http post Json - * Create a new resource by POSTing the given object to the http request, - * http header contentType default 'application/json;charset=UTF-8'. - *

URL request params are expanded using the given map {@code paramValues}. - *

{@code responseType} can be an RestResult or RestResult data {@code T} type. - *

{@code callback} Result callback execution, - * if you need response headers, you can convert the received RestResult to HttpRestResult. - * - * @param url url - * @param header http header param - * @param paramValues http query param - * @param body http body param - * @param responseType return type - * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} - * @throws Exception ex - */ - void postJson(String url, Header header, Map paramValues, String body, - Type responseType, Callback callback) throws Exception; - - - /** - * async http post from - * Create a new resource by PUTting the given map {@code bodyValues} to http request, - * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. - *

URL request params are expanded using the given query {@link Query}. - *

{@code responseType} can be an RestResult or RestResult data {@code T} type. - *

{@code callback} Result callback execution, - * if you need response headers, you can convert the received RestResult to HttpRestResult. - * - * @param url url - * @param header http header param - * @param query http query param - * @param bodyValues http body param - * @param responseType return type - * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} - * @throws Exception ex - */ - void postFrom(String url, Header header, Query query, - Map bodyValues, Type responseType, Callback callback) throws Exception; - - /** - * async http post from - * Create a new resource by PUTting the given map {@code bodyValues} to http request, - * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. - *

URL request params are expanded using the given map {@code paramValues}. - *

{@code responseType} can be an RestResult or RestResult data {@code T} type. - *

{@code callback} Result callback execution, - * if you need response headers, you can convert the received RestResult to HttpRestResult. - * - * @param url url - * @param header http header param - * @param paramValues http query param - * @param bodyValues http body param - * @param responseType return type - * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} - * @throws Exception ex - */ - void postFrom(String url, Header header, Map paramValues, - Map bodyValues, Type responseType, Callback callback) throws Exception; -} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheAsyncHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultAsyncHttpClientRequest.java similarity index 87% rename from common/src/main/java/com/alibaba/nacos/common/http/client/ApacheAsyncHttpClientRequest.java rename to common/src/main/java/com/alibaba/nacos/common/http/client/DefaultAsyncHttpClientRequest.java index 95f858a8710..e3947877f5e 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheAsyncHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultAsyncHttpClientRequest.java @@ -37,11 +37,11 @@ * @author mai.jh * @date 2020/5/29 */ -public class ApacheAsyncHttpClientRequest implements AsyncHttpClientRequest { +public class DefaultAsyncHttpClientRequest implements AsyncHttpClientRequest { private final CloseableHttpAsyncClient asyncClient; - public ApacheAsyncHttpClientRequest(CloseableHttpAsyncClient asyncClient) { + public DefaultAsyncHttpClientRequest(CloseableHttpAsyncClient asyncClient) { this.asyncClient = asyncClient; if (!this.asyncClient.isRunning()) { this.asyncClient.start(); @@ -50,11 +50,11 @@ public ApacheAsyncHttpClientRequest(CloseableHttpAsyncClient asyncClient) { @Override public void execute(URI uri, String httpMethod, RequestHttpEntity requestHttpEntity, final Type responseType, final Callback callback) throws Exception { - HttpRequestBase httpRequestBase = ApacheHttpClientRequest.build(uri, httpMethod, requestHttpEntity); + HttpRequestBase httpRequestBase = DefaultHttpClientRequest.build(uri, httpMethod, requestHttpEntity); asyncClient.execute(httpRequestBase, new FutureCallback() { @Override public void completed(HttpResponse result) { - ApacheClientHttpResponse response = new ApacheClientHttpResponse(result); + DefaultClientHttpResponse response = new DefaultClientHttpResponse(result); try { HttpRestResult httpRestResult = ResponseHandler.responseEntityExtractor(response, responseType); callback.onReceive(httpRestResult); diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java similarity index 94% rename from common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java rename to common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java index ab410c76cdd..37911865142 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheClientHttpResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java @@ -31,13 +31,13 @@ * @author mai.jh * @date 2020/5/25 */ -public class ApacheClientHttpResponse implements HttpClientResponse { +public class DefaultClientHttpResponse implements HttpClientResponse { private HttpResponse response; private Header responseHeader; - public ApacheClientHttpResponse(HttpResponse response) { + public DefaultClientHttpResponse(HttpResponse response) { this.response = response; } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java similarity index 93% rename from common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java rename to common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java index aee8d794ff8..1facf19daff 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/ApacheHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java @@ -39,13 +39,13 @@ * @date 2020/5/24 */ @SuppressWarnings({"unchecked", "rawtypes", "resource"}) -public class ApacheHttpClientRequest implements HttpClientRequest { +public class DefaultHttpClientRequest implements HttpClientRequest { private static final Logger logger = LoggerFactory.getLogger(NacosRestTemplate.class); private final CloseableHttpClient client; - public ApacheHttpClientRequest(CloseableHttpClient client) { + public DefaultHttpClientRequest(CloseableHttpClient client) { this.client = client; } @@ -56,7 +56,7 @@ public HttpClientResponse execute(URI uri, String httpMethod, RequestHttpEntity if (logger.isDebugEnabled()) { logger.debug("Request from server: " + request.getURI().toString()); } - return new ApacheClientHttpResponse(response); + return new DefaultClientHttpResponse(response); } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java index aad7e9b0fe6..32c28da7757 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java @@ -38,7 +38,7 @@ * @see AsyncHttpClientRequest * @see HttpClientResponse */ -public class NacosAsyncRestTemplate implements AsyncRestOperations { +public class NacosAsyncRestTemplate { private static final Logger logger = LoggerFactory.getLogger(NacosAsyncRestTemplate.class); @@ -49,13 +49,39 @@ public NacosAsyncRestTemplate(AsyncHttpClientRequest clientRequest) { } - @Override + /** + * async http get + * URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. + * + * @param url url + * @param responseType return type + * @param header http header param + * @param query http query param + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ public void get(String url, Header header, Query query, Type responseType, Callback callback) throws Exception { execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseType, callback); } - @Override + /** + * async http get + * URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. + * + * @param url url + * @param header headers + * @param paramValues paramValues + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ public void get(String url, Header header, Map paramValues, Type responseType, Callback callback) throws Exception { @@ -63,7 +89,22 @@ public void get(String url, Header header, Map paramValues, Query.newInstance().initParams(paramValues)), responseType, callback); } - @Override + /** + * async get request, may be pulling a lot of data + * URL request params are expanded using the given query {@link Query}, + * More request parameters can be set via body. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. + * + * @param url url + * @param header http header param + * @param query http query param + * @param body get with body + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ public void getLarge(String url, Header header, Query query, Object body, Type responseType, Callback callback) throws Exception { @@ -71,7 +112,20 @@ public void getLarge(String url, Header header, Query query, Object body, new RequestHttpEntity(header, query, body), responseType, callback); } - @Override + /** + * async http delete + * URL request params are expanded using the given query {@link Query}, + *

{@code responseType} can be an RestResult or RestResult data {@code T} type + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. + * + * @param url url + * @param header http header param + * @param query http query param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ public void delete(String url, Header header, Query query, Type responseType, Callback callback) throws Exception { @@ -79,7 +133,22 @@ public void delete(String url, Header header, Query query, new RequestHttpEntity(header, query), responseType, callback); } - @Override + /** + * async http put + * Create a new resource by PUTting the given body to http request. + *

URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. + * + * @param url url + * @param header http header param + * @param query http query param + * @param body http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ public void put(String url, Header header, Query query, Object body, Type responseType, Callback callback) throws Exception { @@ -87,7 +156,23 @@ public void put(String url, Header header, Query query, Object body, new RequestHttpEntity(header, query, body), responseType, callback); } - @Override + /** + * async http put Json + * Create a new resource by PUTting the given body to http request, + * http header contentType default 'application/json;charset=UTF-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. + * + * @param url url + * @param header http header param + * @param paramValues http query param + * @param body http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ public void putJson(String url, Header header, Map paramValues, String body, Type responseType, Callback callback) throws Exception { @@ -98,7 +183,23 @@ public void putJson(String url, Header header, Map paramValu } - @Override + /** + * async http put from + * Create a new resource by PUTting the given map {@code bodyValues} to http request, + * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. + *

URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. + * + * @param url url + * @param header http header param + * @param query http query param + * @param bodyValues http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ public void putFrom(String url, Header header, Query query, Map bodyValues, Type responseType, Callback callback) throws Exception { @@ -107,7 +208,23 @@ public void putFrom(String url, Header header, Query query, MapURL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. + * + * @param url url + * @param header http header param + * @param paramValues http query param + * @param bodyValues http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ public void putFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType, Callback callback) throws Exception { @@ -116,7 +233,22 @@ public void putFrom(String url, Header header, Map paramValu Query.newInstance().initParams(paramValues), bodyValues), responseType, callback); } - @Override + /** + * async http post + * Create a new resource by POSTing the given object to the http request. + *

URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. + * + * @param url url + * @param header http header param + * @param query http query param + * @param body http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ public void post(String url, Header header, Query query, Object body, Type responseType, Callback callback) throws Exception { @@ -124,7 +256,23 @@ public void post(String url, Header header, Query query, Object body, header, query, body), responseType, callback); } - @Override + /** + * async http post Json + * Create a new resource by POSTing the given object to the http request, + * http header contentType default 'application/json;charset=UTF-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. + * + * @param url url + * @param header http header param + * @param paramValues http query param + * @param body http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ public void postJson(String url, Header header, Map paramValues, String body, Type responseType, Callback callback) throws Exception { @@ -134,7 +282,23 @@ public void postJson(String url, Header header, Map paramVal responseType, callback); } - @Override + /** + * async http post from + * Create a new resource by PUTting the given map {@code bodyValues} to http request, + * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. + *

URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. + * + * @param url url + * @param header http header param + * @param query http query param + * @param bodyValues http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ public void postFrom(String url, Header header, Query query, Map bodyValues, Type responseType, Callback callback) throws Exception { @@ -143,7 +307,23 @@ public void postFrom(String url, Header header, Query query, MapURL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an RestResult or RestResult data {@code T} type. + *

{@code callback} Result callback execution, + * if you need response headers, you can convert the received RestResult to HttpRestResult. + * + * @param url url + * @param header http header param + * @param paramValues http query param + * @param bodyValues http body param + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + * @throws Exception ex + */ public void postFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType, Callback callback) throws Exception { diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java index 5b7216a77d8..c5141d10487 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java @@ -33,13 +33,14 @@ /** * NacosRestTemplate + * Interface specifying a basic set of RESTful operations. * * @author mai.jh * @date 2020/5/24 * @see HttpClientRequest * @see HttpClientResponse */ -public class NacosRestTemplate implements RestOperations { +public class NacosRestTemplate { private static final Logger logger = LoggerFactory.getLogger(NacosRestTemplate.class); @@ -49,34 +50,107 @@ public NacosRestTemplate(HttpClientRequest requestClient) { this.requestClient = requestClient; } - @Override + /** + * http get + * URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. + * + * @param url url + * @param header http header param + * @param query http query param + * @param responseType return type + * @return {@link HttpRestResult} + * @throws Exception ex + */ public HttpRestResult get(String url, Header header, Query query, Type responseType) throws Exception { return execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseType); } - @Override + /** + * http get + * URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. + * + * @param url url + * @param header headers + * @param paramValues paramValues + * @param responseType return type + * @return {@link HttpRestResult} + * @throws Exception ex + */ public HttpRestResult get(String url, Header header, Map paramValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity(header, Query.newInstance().initParams(paramValues)); return execute(url, HttpMethod.GET, requestHttpEntity, responseType); } - @Override + /** + * get request, may be pulling a lot of data + * URL request params are expanded using the given query {@link Query}, + * More request parameters can be set via body. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. + * + * @param url url + * @param header http header param + * @param query http query param + * @param body get with body + * @param responseType return type + * @return {@link HttpRestResult} + * @throws Exception ex + */ public HttpRestResult getLarge(String url, Header header, Query query, Object body, Type responseType) throws Exception { return execute(url, HttpMethod.GET_LARGE, new RequestHttpEntity(header, query, body), responseType); } - @Override + /** + * http delete + * URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. + * + * @param url url + * @param header http header param + * @param query http query param + * @param responseType return type + * @return {@link HttpRestResult} + * @throws Exception ex + */ public HttpRestResult delete(String url, Header header, Query query, Type responseType) throws Exception { return execute(url, HttpMethod.DELETE, new RequestHttpEntity(header, query), responseType); } - @Override + /** + * http put + * Create a new resource by PUTting the given body to http request. + *

URL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. + * + * @param url url + * @param header http header param + * @param query http query param + * @param body http body param + * @param responseType return type + * @return {@link HttpRestResult} + * @throws Exception ex + */ public HttpRestResult put(String url, Header header, Query query, Object body, Type responseType) throws Exception { return execute(url, HttpMethod.PUT, new RequestHttpEntity(header, query, body), responseType); } - @Override + /** + * http put json + * Create a new resource by PUTting the given body to http request, + * http header contentType default 'application/json;charset=UTF-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. + * + * @param url url + * @param header http header param + * @param paramValues http query param + * @param body http body param + * @param responseType return type + * @return {@link HttpRestResult} + * @throws Exception ex + */ public HttpRestResult putJson(String url, Header header, Map paramValues, String body, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( header.setContentType(MediaType.APPLICATION_JSON), @@ -86,14 +160,42 @@ public HttpRestResult putJson(String url, Header header, MapURL request params are expanded using the given query {@code Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. + * + * @param url url + * @param header http header param + * @param query http query param + * @param bodyValues http body param + * @param responseType return type + * @return {@link HttpRestResult} + * @throws Exception ex + */ public HttpRestResult putFrom(String url, Header header, Query query, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues); return execute(url, HttpMethod.PUT, requestHttpEntity, responseType); } - @Override + /** + * http put from + * Create a new resource by PUTting the given map {@code bodyValues} to http request, + * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. + * + * @param url url + * @param header http header param + * @param paramValues http query param + * @param bodyValues http body param + * @param responseType return type + * @return {@link HttpRestResult} + * @throws Exception ex + */ public HttpRestResult putFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), @@ -103,13 +205,40 @@ public HttpRestResult putFrom(String url, Header header, MapURL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. + * + * @param url url + * @param header http header param + * @param query http query param + * @param body http body param + * @param responseType return type + * @return {@link HttpRestResult} + * @throws Exception ex + */ public HttpRestResult post(String url, Header header, Query query, Object body, Type responseType) throws Exception { return execute(url, HttpMethod.POST, new RequestHttpEntity(header, query, body), responseType); } - @Override + /** + * http post json + * Create a new resource by POSTing the given object to the http request, + * http header contentType default 'application/json;charset=UTF-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. + * + * @param url url + * @param header http header param + * @param paramValues http query param + * @param body http body param + * @param responseType return type + * @return {@link HttpRestResult} + * @throws Exception ex + */ public HttpRestResult postJson(String url, Header header, Map paramValues, String body, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( header.setContentType(MediaType.APPLICATION_JSON), @@ -119,7 +248,21 @@ public HttpRestResult postJson(String url, Header header, MapURL request params are expanded using the given query {@link Query}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. + * + * @param url url + * @param header http header param + * @param query http query param + * @param bodyValues http body param + * @param responseType return type + * @return {@link HttpRestResult} + * @throws Exception ex + */ public HttpRestResult postFrom(String url, Header header, Query query, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues); @@ -127,7 +270,21 @@ public HttpRestResult postFrom(String url, Header header, Query query, Ma return execute(url, HttpMethod.POST, requestHttpEntity, responseType); } - @Override + /** + * http post from + * Create a new resource by PUTting the given map {@code bodyValues} to http request, + * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. + *

URL request params are expanded using the given map {@code paramValues}. + *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. + * + * @param url url + * @param header http header param + * @param paramValues http query param + * @param bodyValues http body param + * @param responseType return type + * @return {@link HttpRestResult} + * @throws Exception ex + */ public HttpRestResult postFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java b/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java deleted file mode 100644 index 49a4697237f..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/RestOperations.java +++ /dev/null @@ -1,239 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.alibaba.nacos.common.http.client; - -import com.alibaba.nacos.common.http.HttpRestResult; -import com.alibaba.nacos.common.http.param.Header; -import com.alibaba.nacos.common.http.param.Query; - -import java.lang.reflect.Type; -import java.util.Map; - -/** - * Interface specifying a basic set of RESTful operations. - * - * @author mai.jh - * @date 2020/5/23 - */ -public interface RestOperations { - - /** - * http get - * URL request params are expanded using the given query {@link Query}. - *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. - * - * @param url url - * @param header http header param - * @param query http query param - * @param responseType return type - * @return the HttpRestResult - * @throws Exception ex - */ - HttpRestResult get(String url, Header header, Query query, Type responseType) throws Exception; - - /** - * http get - * URL request params are expanded using the given query {@link Query}. - *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. - * - * @param url url - * @param header headers - * @param paramValues paramValues - * @param responseType return type - * @return the HttpRestResult - * @throws Exception ex - */ - HttpRestResult get(String url, Header header, Map paramValues, Type responseType) throws Exception; - - /** - * get request, may be pulling a lot of data - * URL request params are expanded using the given query {@link Query}, - * More request parameters can be set via body. - *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. - * - * @param url url - * @param header http header param - * @param query http query param - * @param body get with body - * @param responseType return type - * @return {@link HttpRestResult } - * @throws Exception ex - */ - HttpRestResult getLarge(String url, Header header, Query query, Object body, - Type responseType) throws Exception; - - /** - * http delete - * URL request params are expanded using the given query {@link Query}. - *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. - * - * @param url url - * @param header http header param - * @param query http query param - * @param responseType return type - * @return {@link HttpRestResult } - * @throws Exception ex - */ - HttpRestResult delete(String url, Header header, Query query, - Type responseType) throws Exception; - - /** - * http put - * Create a new resource by PUTting the given body to http request. - *

URL request params are expanded using the given query {@link Query}. - *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. - * - * @param url url - * @param header http header param - * @param query http query param - * @param body http body param - * @param responseType return type - * @return {@link HttpRestResult} - * @throws Exception ex - */ - HttpRestResult put(String url, Header header, Query query, Object body, - Type responseType) throws Exception; - - - /** - * http put json - * Create a new resource by PUTting the given body to http request, - * http header contentType default 'application/json;charset=UTF-8'. - *

URL request params are expanded using the given map {@code paramValues}. - *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. - * - * @param url url - * @param header http header param - * @param paramValues http query param - * @param body http body param - * @param responseType return type - * @return {@link HttpRestResult} - * @throws Exception ex - */ - HttpRestResult putJson(String url, Header header, Map paramValues, String body, - Type responseType) throws Exception; - - /** - * http put from - * Create a new resource by PUTting the given map {@code bodyValues} to http request, - * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. - *

URL request params are expanded using the given query {@code Query}. - *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. - * - * @param url url - * @param header http header param - * @param query http query param - * @param bodyValues http body param - * @param responseType return type - * @return {@link HttpRestResult} - * @throws Exception ex - */ - HttpRestResult putFrom(String url, Header header, Query query, - Map bodyValues, Type responseType) throws Exception; - - - /** - * http put from - * Create a new resource by PUTting the given map {@code bodyValues} to http request, - * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. - *

URL request params are expanded using the given map {@code paramValues}. - *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. - * - * @param url url - * @param header http header param - * @param paramValues http query param - * @param bodyValues http body param - * @param responseType return type - * @return {@link HttpRestResult} - * @throws Exception ex - */ - HttpRestResult putFrom(String url, Header header, Map paramValues, - Map bodyValues, Type responseType) throws Exception; - - - /** - * http post - * Create a new resource by POSTing the given object to the http request. - *

URL request params are expanded using the given query {@link Query}. - *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. - * - * @param url url - * @param header http header param - * @param query http query param - * @param body http body param - * @param responseType return type - * @return {@link HttpRestResult} - * @throws Exception ex - */ - HttpRestResult post(String url, Header header, Query query, Object body, - Type responseType) throws Exception; - - /** - * http post json - * Create a new resource by POSTing the given object to the http request, - * http header contentType default 'application/json;charset=UTF-8'. - *

URL request params are expanded using the given map {@code paramValues}. - *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. - * - * @param url url - * @param header http header param - * @param paramValues http query param - * @param body http body param - * @param responseType return type - * @return {@link HttpRestResult} - * @throws Exception ex - */ - HttpRestResult postJson(String url, Header header, Map paramValues, String body, - Type responseType) throws Exception; - - - /** - * http post from - * Create a new resource by PUTting the given map {@code bodyValues} to http request, - * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. - *

URL request params are expanded using the given query {@link Query}. - *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. - * - * @param url url - * @param header http header param - * @param query http query param - * @param bodyValues http body param - * @param responseType return type - * @return {@link HttpRestResult} - * @throws Exception ex - */ - HttpRestResult postFrom(String url, Header header, Query query, - Map bodyValues, Type responseType) throws Exception; - - /** - * http post from - * Create a new resource by PUTting the given map {@code bodyValues} to http request, - * http header contentType default 'application/x-www-form-urlencoded;charset=utf-8'. - *

URL request params are expanded using the given map {@code paramValues}. - *

{@code responseType} can be an HttpRestResult or HttpRestResult data {@code T} type. - * - * @param url url - * @param header http header param - * @param paramValues http query param - * @param bodyValues http body param - * @param responseType return type - * @return {@link HttpRestResult} - * @throws Exception ex - */ - HttpRestResult postFrom(String url, Header header, Map paramValues, - Map bodyValues, Type responseType) throws Exception; -} diff --git a/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java b/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java index bea02ebb2b8..9047a79bf7d 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.common.http.handler; +import com.alibaba.nacos.api.exception.runtime.NacosDeserializationException; import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.http.client.HttpClientResponse; @@ -47,19 +48,27 @@ public static T convert(String s, Type type) throws Exception { return JacksonUtils.toObj(s, type); } - public static T convert(InputStream inputStream, Class tClass) throws Exception { - return JacksonUtils.toObj(inputStream, tClass); + public static T convert(InputStream inputStream, Type type) throws Exception { + return JacksonUtils.toObj(inputStream, type); } @SuppressWarnings({"unchecked", "rawtypes", "resource"}) public static HttpRestResult responseEntityExtractor(HttpClientResponse response, Type type) throws Exception { Header headers = response.getHeaders(); String contentType = headers.getValue(HttpHeaderConsts.CONTENT_TYPE); - String body = IoUtils.toString(response.getBody(), headers.getCharset()); - T extractBody = (T) body; + InputStream body = response.getBody(); + T extractBody = null; if (MediaType.APPLICATION_JSON.equals(contentType) && HttpStatus.SC_OK == response.getStatusCode()) { extractBody = convert(body, type); } + if (extractBody == null) { + if (!String.class.toString().equals(type.toString())) { + logger.error("if the response contentType is not [application/json]," + + " only support to java.lang.String"); + throw new NacosDeserializationException(type); + } + extractBody = (T)IoUtils.toString(body, headers.getCharset()); + } if (extractBody instanceof RestResult) { HttpRestResult httpRestResult = convert((RestResult) extractBody); httpRestResult.setHeader(headers); @@ -76,4 +85,5 @@ private static HttpRestResult convert(RestResult restResult) { return httpRestResult; } + } diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java index 969dc2bf91a..e74ff7d68a1 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java @@ -120,6 +120,15 @@ public static T toObj(String json, TypeReference typeReference) { } } + public static T toObj(InputStream inputStream, Type type) { + try { + return mapper.readValue(inputStream, mapper.constructType(type)); + } + catch (IOException e) { + throw new NacosDeserializationException(type, e); + } + } + public static JsonNode toObj(String json) { try { return mapper.readTree(json); diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java index 66722c6e6c9..b8e467023c9 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java @@ -159,7 +159,7 @@ public void test_url_delete() throws Exception{ .addParam("port", "8080") .addParam("serviceName", "app-test"); CallbackMap callbackMap = new CallbackMap<>(); - nacosRestTemplate.delete(url, Header.newInstance(), query, RestResult.class, callbackMap); + nacosRestTemplate.delete(url, Header.newInstance(), query, String.class, callbackMap); Thread.sleep(2000); HttpRestResult restResult = callbackMap.getRestResult(); System.out.println(restResult.getData()); diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java index d67d175e4c5..c45b673d569 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java @@ -58,6 +58,7 @@ public class NacosRestTemplate_ITCase { private final String CONFIG_PATH = "/nacos/v1/cs"; private String IP = null; + @Before public void init() throws NacosException { IP = String.format("http://localhost:%d", port); @@ -126,7 +127,7 @@ public void test_url_delete() throws Exception{ .addParam("ip", "11.11.11.11") .addParam("port", "8080") .addParam("serviceName", "app-test"); - HttpRestResult restResult = nacosRestTemplate.delete(url, Header.newInstance(), query, RestResult.class); + HttpRestResult restResult = nacosRestTemplate.delete(url, Header.newInstance(), query, String.class); Assert.assertTrue(restResult.ok()); System.out.println(restResult); } From 2ab274216a59cf3e1765de0a940c8ae86d316869 Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Tue, 2 Jun 2020 19:55:30 +0800 Subject: [PATCH 037/156] [#1815]adjust and optimize codes, reput the lifecycle to ConfigService class level at the same time. --- .../com/alibaba/nacos/api/NacosFactory.java | 20 ++ .../nacos/api/config/ConfigFactory.java | 15 ++ .../nacos/api/config/ConfigService.java | 11 +- .../nacos/api/exception/NacosException.java | 5 + .../nacos/api/naming/NamingFactory.java | 16 ++ .../nacos/api/naming/NamingService.java | 7 + .../client/config/NacosConfigService.java | 22 +- .../client/config/http/MetricsHttpAgent.java | 26 +-- .../client/config/http/ServerHttpAgent.java | 111 +++++---- .../client/config/impl/ClientWorker.java | 125 +++++----- .../client/config/impl/ServerListManager.java | 40 +++- .../client/config/impl/TimerService.java | 44 ---- .../client/naming/NacosNamingService.java | 21 +- .../naming/backups/FailoverReactor.java | 34 ++- .../nacos/client/naming/beat/BeatReactor.java | 49 ++-- .../nacos/client/naming/core/Balancer.java | 6 - .../client/naming/core/EventDispatcher.java | 12 +- .../nacos/client/naming/core/HostReactor.java | 15 +- .../client/naming/core/PushReceiver.java | 28 ++- .../nacos/client/naming/net/NamingProxy.java | 27 ++- .../com/alibaba/nacos/client/ConfigTest.java | 10 +- .../common/lifecycle/AbstractLifeCycle.java | 217 ------------------ .../nacos/common/lifecycle/Closeable.java | 14 +- .../nacos/common/lifecycle/LifeCycle.java | 59 +---- .../common/lifecycle/LifeCycleState.java | 70 ------ .../lifecycle/ResourceLifeCycleManager.java | 47 ++-- .../nacos/common/utils/ThreadUtils.java | 4 +- .../common/LifeCycle/LifeCycleClassTest.java | 42 ++-- .../LifeCycle/LifeCycleManagerTest.java | 27 ++- .../nacos/test/config/ConfigAPI_ITCase.java | 3 +- .../ConfigLongPollReturnChanges_ITCase.java | 9 + .../test/config/ConfigLongPoll_ITCase.java | 9 + .../nacos/test/core/BaseClusterTest.java | 10 +- .../test/core/auth/ConfigAuth_ITCase.java | 7 +- 34 files changed, 472 insertions(+), 690 deletions(-) delete mode 100644 client/src/main/java/com/alibaba/nacos/client/config/impl/TimerService.java delete mode 100644 common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java delete mode 100644 common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java diff --git a/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java b/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java index 82228c0270d..1872697f3a3 100644 --- a/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java @@ -98,4 +98,24 @@ public static NamingMaintainService createMaintainService(Properties properties) return NamingMaintainFactory.createMaintainService(properties); } + /** + * Destroy config service instance. + * + * @param configService + * @throws NacosException Exception. + */ + public static void destroyConfigService(ConfigService configService) throws NacosException{ + ConfigFactory.destroyConfigService(configService); + } + + /** + * Destroy naming service instance. + * + * @param namingService + * @throws NacosException Exception. + */ + public static void destroyNamingService(NamingService namingService) throws NacosException{ + NamingFactory.destroyNamingService(namingService); + } + } diff --git a/api/src/main/java/com/alibaba/nacos/api/config/ConfigFactory.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigFactory.java index 450c138533e..65e867d57dc 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/ConfigFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigFactory.java @@ -59,4 +59,19 @@ public static ConfigService createConfigService(String serverAddr) throws NacosE return createConfigService(properties); } + /** + * Destroy Config Instance's Resources. + * + * @param configService + * @throws NacosException + */ + public static void destroyConfigService(ConfigService configService) throws NacosException{ + try { + configService.shutDown(); + configService = null; + }catch (Throwable e) { + throw new NacosException(NacosException.RESOURCE_DESTROY_FAILED, e); + } + } + } diff --git a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java index 2ffc647a948..657317423a1 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java @@ -106,14 +106,9 @@ public interface ConfigService { String getServerStatus(); /** - * Shutdown the resouces which are managed by config service object instance. + * Shutdown the resource service * + * @throws NacosException exception. */ - /** - * Close the resouces which are managed by config service object instance. - * - * @throws Exception Exception occours when executing close operation. - */ - void shutdown() throws Exception; - + void shutDown() throws NacosException; } diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java index ac3f7ec3aeb..45594761f57 100644 --- a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java +++ b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java @@ -146,4 +146,9 @@ public String toString() { public static final int RESOURCE_NOT_FOUND = -404; + /** + * resource instance destroy failed. + */ + public static final int RESOURCE_DESTROY_FAILED = 410; + } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java index 745ccd275f4..570fa6108fb 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java @@ -18,6 +18,7 @@ import java.lang.reflect.Constructor; import java.util.Properties; +import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.exception.NacosException; /** @@ -48,4 +49,19 @@ public static NamingService createNamingService(Properties properties) throws Na throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e); } } + + /** + * Destroy Config Instance's Resources. + * + * @param namingService + * @throws NacosException + */ + public static void destroyNamingService(NamingService namingService) throws NacosException{ + try { + namingService.shutDown(); + namingService = null; + }catch (Throwable e) { + throw new NacosException(NacosException.RESOURCE_DESTROY_FAILED, e); + } + } } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java index 46b2f3df80b..ebb1f209f13 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java @@ -555,4 +555,11 @@ public interface NamingService { * @return is server healthy */ String getServerStatus(); + + /** + * Shutdown the resource service. + * + * @throws NacosException exception. + */ + void shutDown() throws NacosException; } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java index b4ba5953c51..57c106ec59b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java @@ -34,15 +34,13 @@ import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.ValidatorUtils; -import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; -import com.alibaba.nacos.common.lifecycle.LifeCycleState; +import com.alibaba.nacos.common.lifecycle.LifeCycle; import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import java.io.IOException; import java.net.HttpURLConnection; -import java.rmi.server.ExportException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -54,7 +52,7 @@ * @author Nacos */ @SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") -public class NacosConfigService extends AbstractLifeCycle implements ConfigService { +public class NacosConfigService implements ConfigService, LifeCycle { private static final Logger LOGGER = LogUtils.logger(NacosConfigService.class); @@ -72,9 +70,9 @@ public class NacosConfigService extends AbstractLifeCycle implements ConfigServi private String encode; private ConfigFilterChainManager configFilterChainManager = new ConfigFilterChainManager(); - public NacosConfigService(Properties properties) throws NacosException { - super(); + private static final ResourceLifeCycleManager RESOURCE_MANAGER = ResourceLifeCycleManager.getInstance(); + public NacosConfigService(Properties properties) throws NacosException { ValidatorUtils.checkInitParam(properties); String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE); if (StringUtils.isBlank(encodeTmp)) { @@ -83,9 +81,11 @@ public NacosConfigService(Properties properties) throws NacosException { encode = encodeTmp.trim(); } initNamespace(properties); + agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); agent.fetchServerIpList(); worker = new ClientWorker(agent, configFilterChainManager, properties); + RESOURCE_MANAGER.register(this); } private void initNamespace(Properties properties) { @@ -288,16 +288,12 @@ public String getServerStatus() { } @Override - public void shutdown() throws Exception { - this.stop(); - } - - @Override - protected void doStart() throws Exception { + public void shutDown() throws NacosException{ + destroy(); } @Override - protected void doStop() throws Exception { + public void destroy() throws NacosException{ this.agent.shutdown(); this.worker.shutdown(); } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java index 4b0b75e4b18..8551665a5d0 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java @@ -34,6 +34,7 @@ * @author Nacos */ public class MetricsHttpAgent implements HttpAgent { + private HttpAgent httpAgent; public MetricsHttpAgent(HttpAgent httpAgent) { @@ -42,7 +43,7 @@ public MetricsHttpAgent(HttpAgent httpAgent) { @Override public void fetchServerIpList() throws NacosException { - httpAgent.fetchServerIpList(); + this.httpAgent.fetchServerIpList(); } @Override @@ -50,7 +51,7 @@ public HttpResult httpGet(String path, List headers, List paramV Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("GET", path, "NA"); HttpResult result; try { - result = httpAgent.httpGet(path, headers, paramValues, encoding, readTimeoutMs); + result = this.httpAgent.httpGet(path, headers, paramValues, encoding, readTimeoutMs); } catch (IOException e) { throw e; } finally { @@ -66,7 +67,7 @@ public HttpResult httpPost(String path, List headers, List param Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("POST", path, "NA"); HttpResult result; try { - result = httpAgent.httpPost(path, headers, paramValues, encoding, readTimeoutMs); + result = this.httpAgent.httpPost(path, headers, paramValues, encoding, readTimeoutMs); } catch (IOException e) { throw e; } finally { @@ -82,7 +83,7 @@ public HttpResult httpDelete(String path, List headers, List par Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("DELETE", path, "NA"); HttpResult result; try { - result = httpAgent.httpDelete(path, headers, paramValues, encoding, readTimeoutMs); + result = this.httpAgent.httpDelete(path, headers, paramValues, encoding, readTimeoutMs); } catch (IOException e) { throw e; @@ -96,32 +97,27 @@ public HttpResult httpDelete(String path, List headers, List par @Override public String getName() { - return httpAgent.getName(); + return this.httpAgent.getName(); } @Override public String getNamespace() { - return httpAgent.getNamespace(); + return this.httpAgent.getNamespace(); } @Override public String getTenant() { - return httpAgent.getTenant(); + return this.httpAgent.getTenant(); } @Override public String getEncode() { - return httpAgent.getEncode(); - } - - @Override - public void close() throws IOException { - + return this.httpAgent.getEncode(); } @Override - public void shutdown() throws InterruptedException { - httpAgent.shutdown(); + public void shutdown() throws NacosException{ + this.httpAgent.shutdown(); } } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java index b1c1e102b51..76e9cfc9086 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java @@ -28,7 +28,6 @@ import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.TemplateUtils; -import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.ThreadUtils; import com.fasterxml.jackson.annotation.JsonProperty; @@ -80,7 +79,7 @@ public HttpResult httpGet(String path, List headers, List paramV final long endTime = System.currentTimeMillis() + readTimeoutMs; final boolean isSSL = false; injectSecurityInfo(paramValues); - String currentServerAddr = serverListMgr.getCurrentServerAddr(); + String currentServerAddr = this.serverListMgr.getCurrentServerAddr(); int maxRetry = this.maxRetry; do { @@ -96,10 +95,10 @@ public HttpResult httpGet(String path, List headers, List paramV || result.code == HttpURLConnection.HTTP_BAD_GATEWAY || result.code == HttpURLConnection.HTTP_UNAVAILABLE) { LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", - serverListMgr.getCurrentServerAddr(), result.code); + this.serverListMgr.getCurrentServerAddr(), result.code); } else { // Update the currently available server addr - serverListMgr.updateCurrentServerAddr(currentServerAddr); + this.serverListMgr.updateCurrentServerAddr(currentServerAddr); return result; } } catch (ConnectException ce) { @@ -111,14 +110,14 @@ public HttpResult httpGet(String path, List headers, List paramV throw ioe; } - if (serverListMgr.getIterator().hasNext()) { - currentServerAddr = serverListMgr.getIterator().next(); + if (this.serverListMgr.getIterator().hasNext()) { + currentServerAddr = this.serverListMgr.getIterator().next(); } else { maxRetry--; if (maxRetry < 0) { throw new ConnectException("[NACOS HTTP-GET] The maximum number of tolerable server reconnection errors has been reached"); } - serverListMgr.refreshCurrentServerAddr(); + this.serverListMgr.refreshCurrentServerAddr(); } } while (System.currentTimeMillis() <= endTime); @@ -133,7 +132,7 @@ public HttpResult httpPost(String path, List headers, List param final long endTime = System.currentTimeMillis() + readTimeoutMs; boolean isSSL = false; injectSecurityInfo(paramValues); - String currentServerAddr = serverListMgr.getCurrentServerAddr(); + String currentServerAddr = this.serverListMgr.getCurrentServerAddr(); int maxRetry = this.maxRetry; do { @@ -154,7 +153,7 @@ public HttpResult httpPost(String path, List headers, List param currentServerAddr, result.code); } else { // Update the currently available server addr - serverListMgr.updateCurrentServerAddr(currentServerAddr); + this.serverListMgr.updateCurrentServerAddr(currentServerAddr); return result; } } catch (ConnectException ce) { @@ -166,14 +165,14 @@ public HttpResult httpPost(String path, List headers, List param throw ioe; } - if (serverListMgr.getIterator().hasNext()) { - currentServerAddr = serverListMgr.getIterator().next(); + if (this.serverListMgr.getIterator().hasNext()) { + currentServerAddr = this.serverListMgr.getIterator().next(); } else { maxRetry--; if (maxRetry < 0) { throw new ConnectException("[NACOS HTTP-POST] The maximum number of tolerable server reconnection errors has been reached"); } - serverListMgr.refreshCurrentServerAddr(); + this.serverListMgr.refreshCurrentServerAddr(); } } while (System.currentTimeMillis() <= endTime); @@ -188,7 +187,7 @@ public HttpResult httpDelete(String path, List headers, List par final long endTime = System.currentTimeMillis() + readTimeoutMs; boolean isSSL = false; injectSecurityInfo(paramValues); - String currentServerAddr = serverListMgr.getCurrentServerAddr(); + String currentServerAddr = this.serverListMgr.getCurrentServerAddr(); int maxRetry = this.maxRetry; do { @@ -204,10 +203,10 @@ public HttpResult httpDelete(String path, List headers, List par || result.code == HttpURLConnection.HTTP_BAD_GATEWAY || result.code == HttpURLConnection.HTTP_UNAVAILABLE) { LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", - serverListMgr.getCurrentServerAddr(), result.code); + this.serverListMgr.getCurrentServerAddr(), result.code); } else { // Update the currently available server addr - serverListMgr.updateCurrentServerAddr(currentServerAddr); + this.serverListMgr.updateCurrentServerAddr(currentServerAddr); return result; } } catch (ConnectException ce) { @@ -221,14 +220,14 @@ public HttpResult httpDelete(String path, List headers, List par throw ioe; } - if (serverListMgr.getIterator().hasNext()) { - currentServerAddr = serverListMgr.getIterator().next(); + if (this.serverListMgr.getIterator().hasNext()) { + currentServerAddr = this.serverListMgr.getIterator().next(); } else { maxRetry--; if (maxRetry < 0) { throw new ConnectException("[NACOS HTTP-DELETE] The maximum number of tolerable server reconnection errors has been reached"); } - serverListMgr.refreshCurrentServerAddr(); + this.serverListMgr.refreshCurrentServerAddr(); } } while (System.currentTimeMillis() <= endTime); @@ -238,8 +237,8 @@ public HttpResult httpDelete(String path, List headers, List par } private String getUrl(String serverAddr, String relativePath) { - String contextPath = serverListMgr.getContentPath().startsWith("/") ? - serverListMgr.getContentPath() : "/" + serverListMgr.getContentPath(); + String contextPath = this.serverListMgr.getContentPath().startsWith("/") ? + this.serverListMgr.getContentPath() : "/" + this.serverListMgr.getContentPath(); return serverAddr + contextPath + relativePath; } @@ -248,20 +247,20 @@ public static String getAppname() { } public ServerHttpAgent(ServerListManager mgr) { - serverListMgr = mgr; + this.serverListMgr = mgr; } public ServerHttpAgent(ServerListManager mgr, Properties properties) { - serverListMgr = mgr; + this.serverListMgr = mgr; init(properties); } public ServerHttpAgent(Properties properties) throws NacosException { - serverListMgr = new ServerListManager(properties); - securityProxy = new SecurityProxy(properties); - namespaceId = properties.getProperty(PropertyKeyConst.NAMESPACE); + this.serverListMgr = new ServerListManager(properties); + this.securityProxy = new SecurityProxy(properties); + this.namespaceId = properties.getProperty(PropertyKeyConst.NAMESPACE); init(properties); - securityProxy.login(serverListMgr.getServerUrls()); + this.securityProxy.login(this.serverListMgr.getServerUrls()); // init executorService @@ -318,27 +317,27 @@ private void initAkSk(Properties properties) { String ak = properties.getProperty(PropertyKeyConst.ACCESS_KEY); if (StringUtils.isBlank(ak)) { - accessKey = SpasAdapter.getAk(); + this.accessKey = SpasAdapter.getAk(); } else { - accessKey = ak; + this.accessKey = ak; } String sk = properties.getProperty(PropertyKeyConst.SECRET_KEY); if (StringUtils.isBlank(sk)) { - secretKey = SpasAdapter.getSk(); + this.secretKey = SpasAdapter.getSk(); } else { - secretKey = sk; + this.secretKey = sk; } } private void initMaxRetry(Properties properties) { - maxRetry = NumberUtils.toInt(String.valueOf(properties.get(PropertyKeyConst.MAX_RETRY)), Constants.MAX_RETRY); + this.maxRetry = NumberUtils.toInt(String.valueOf(properties.get(PropertyKeyConst.MAX_RETRY)), Constants.MAX_RETRY); } @Override public void fetchServerIpList() throws NacosException { // fetch server address urls list - serverListMgr.start(); + this.serverListMgr.start(); } private List getSpasHeaders(List paramValues) throws IOException { @@ -346,15 +345,15 @@ private List getSpasHeaders(List paramValues) throws IOException // STS 临时凭证鉴权的优先级高于 AK/SK 鉴权 if (STSConfig.getInstance().isSTSOn()) { STSCredential sTSCredential = getSTSCredential(); - accessKey = sTSCredential.accessKeyId; - secretKey = sTSCredential.accessKeySecret; + this.accessKey = sTSCredential.accessKeyId; + this.secretKey = sTSCredential.accessKeySecret; newHeaders.add("Spas-SecurityToken"); newHeaders.add(sTSCredential.securityToken); } - if (StringUtils.isNotEmpty(accessKey) && StringUtils.isNotEmpty(secretKey)) { + if (StringUtils.isNotEmpty(this.accessKey) && StringUtils.isNotEmpty(secretKey)) { newHeaders.add("Spas-AccessKey"); - newHeaders.add(accessKey); + newHeaders.add(this.accessKey); List signHeaders = SpasAdapter.getSignHeaders(paramValues, secretKey); if (signHeaders != null) { newHeaders.addAll(signHeaders); @@ -365,22 +364,22 @@ private List getSpasHeaders(List paramValues) throws IOException private STSCredential getSTSCredential() throws IOException { boolean cacheSecurityCredentials = STSConfig.getInstance().isCacheSecurityCredentials(); - if (cacheSecurityCredentials && sTSCredential != null) { + if (cacheSecurityCredentials && this.sTSCredential != null) { long currentTime = System.currentTimeMillis(); - long expirationTime = sTSCredential.expiration.getTime(); + long expirationTime = this.sTSCredential.expiration.getTime(); int timeToRefreshInMillisecond = STSConfig.getInstance().getTimeToRefreshInMillisecond(); if (expirationTime - currentTime > timeToRefreshInMillisecond) { - return sTSCredential; + return this.sTSCredential; } } String stsResponse = getSTSResponse(); STSCredential stsCredentialTmp = JSONUtils.deserializeObject(stsResponse, new TypeReference() { }); - sTSCredential = stsCredentialTmp; - LOGGER.info("[getSTSCredential] code:{}, accessKeyId:{}, lastUpdated:{}, expiration:{}", sTSCredential.getCode(), - sTSCredential.getAccessKeyId(), sTSCredential.getLastUpdated(), sTSCredential.getExpiration()); - return sTSCredential; + this.sTSCredential = stsCredentialTmp; + LOGGER.info("[getSTSCredential] code:{}, accessKeyId:{}, lastUpdated:{}, expiration:{}", this.sTSCredential.getCode(), + this.sTSCredential.getAccessKeyId(), this.sTSCredential.getLastUpdated(), this.sTSCredential.getExpiration()); + return this.sTSCredential; } private static String getSTSResponse() throws IOException { @@ -421,30 +420,26 @@ private static String getSTSResponse() throws IOException { @Override public String getName() { - return serverListMgr.getName(); + return this.serverListMgr.getName(); } @Override public String getNamespace() { - return serverListMgr.getNamespace(); + return this.serverListMgr.getNamespace(); } @Override public String getTenant() { - return serverListMgr.getTenant(); + return this.serverListMgr.getTenant(); } @Override public String getEncode() { - return encode; + return this.encode; } @Override - public void close() throws IOException { - } - - @Override - public void shutdown() throws InterruptedException { + public void shutdown() throws NacosException{ LOGGER.info("do shutdown begin"); ThreadUtils.shutdown(this.executorService); LOGGER.info("do shutdown stop"); @@ -484,12 +479,12 @@ public String getCode() { @Override public String toString() { return "STSCredential{" + - "accessKeyId='" + accessKeyId + '\'' + - ", accessKeySecret='" + accessKeySecret + '\'' + - ", expiration=" + expiration + - ", securityToken='" + securityToken + '\'' + - ", lastUpdated=" + lastUpdated + - ", code='" + code + '\'' + + "accessKeyId='" + this.accessKeyId + '\'' + + ", accessKeySecret='" + this.accessKeySecret + '\'' + + ", expiration=" + this.expiration + + ", securityToken='" + this.securityToken + '\'' + + ", lastUpdated=" + this.lastUpdated + + ", code='" + this.code + '\'' + '}'; } } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index 8a8d6c5550f..a5c98e9406b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -113,26 +113,26 @@ public void removeTenantListener(String dataId, String group, Listener listener) void removeCache(String dataId, String group) { String groupKey = GroupKey.getKey(dataId, group); - synchronized (cacheMap) { - Map copy = new HashMap(cacheMap.get()); + synchronized (this.cacheMap) { + Map copy = new HashMap(this.cacheMap.get()); copy.remove(groupKey); - cacheMap.set(copy); + this.cacheMap.set(copy); } - LOGGER.info("[{}] [unsubscribe] {}", agent.getName(), groupKey); + LOGGER.info("[{}] [unsubscribe] {}", this.agent.getName(), groupKey); - MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size()); + MetricsMonitor.getListenConfigCountMonitor().set(this.cacheMap.get().size()); } void removeCache(String dataId, String group, String tenant) { String groupKey = GroupKey.getKeyTenant(dataId, group, tenant); - synchronized (cacheMap) { - Map copy = new HashMap(cacheMap.get()); + synchronized (this.cacheMap) { + Map copy = new HashMap(this.cacheMap.get()); copy.remove(groupKey); - cacheMap.set(copy); + this.cacheMap.set(copy); } - LOGGER.info("[{}] [unsubscribe] {}", agent.getName(), groupKey); + LOGGER.info("[{}] [unsubscribe] {}", this.agent.getName(), groupKey); - MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size()); + MetricsMonitor.getListenConfigCountMonitor().set(this.cacheMap.get().size()); } public CacheData addCacheDataIfAbsent(String dataId, String group) { @@ -142,9 +142,9 @@ public CacheData addCacheDataIfAbsent(String dataId, String group) { } String key = GroupKey.getKey(dataId, group); - cache = new CacheData(configFilterChainManager, agent.getName(), dataId, group); + cache = new CacheData(this.configFilterChainManager, this.agent.getName(), dataId, group); - synchronized (cacheMap) { + synchronized (this.cacheMap) { CacheData cacheFromMap = getCache(dataId, group); // multiple listeners on the same dataid+group and race condition,so double check again //other listener thread beat me to set to cacheMap @@ -153,18 +153,18 @@ public CacheData addCacheDataIfAbsent(String dataId, String group) { //reset so that server not hang this check cache.setInitializing(true); } else { - int taskId = cacheMap.get().size() / (int) ParamUtil.getPerTaskConfigSize(); + int taskId = this.cacheMap.get().size() / (int) ParamUtil.getPerTaskConfigSize(); cache.setTaskId(taskId); } - Map copy = new HashMap(cacheMap.get()); + Map copy = new HashMap(this.cacheMap.get()); copy.put(key, cache); - cacheMap.set(copy); + this.cacheMap.set(copy); } - LOGGER.info("[{}] [subscribe] {}", agent.getName(), key); + LOGGER.info("[{}] [subscribe] {}", this.agent.getName(), key); - MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size()); + MetricsMonitor.getListenConfigCountMonitor().set(this.cacheMap.get().size()); return cache; } @@ -175,7 +175,7 @@ public CacheData addCacheDataIfAbsent(String dataId, String group, String tenant return cache; } String key = GroupKey.getKeyTenant(dataId, group, tenant); - synchronized (cacheMap) { + synchronized (this.cacheMap) { CacheData cacheFromMap = getCache(dataId, group, tenant); // multiple listeners on the same dataid+group and race condition,so // double check again @@ -185,21 +185,21 @@ public CacheData addCacheDataIfAbsent(String dataId, String group, String tenant // reset so that server not hang this check cache.setInitializing(true); } else { - cache = new CacheData(configFilterChainManager, agent.getName(), dataId, group, tenant); + cache = new CacheData(this.configFilterChainManager, this.agent.getName(), dataId, group, tenant); // fix issue # 1317 - if (enableRemoteSyncConfig) { + if (this.enableRemoteSyncConfig) { String[] ct = getServerConfig(dataId, group, tenant, 3000L); cache.setContent(ct[0]); } } - Map copy = new HashMap(cacheMap.get()); + Map copy = new HashMap(this.cacheMap.get()); copy.put(key, cache); - cacheMap.set(copy); + this.cacheMap.set(copy); } - LOGGER.info("[{}] [subscribe] {}", agent.getName(), key); + LOGGER.info("[{}] [subscribe] {}", this.agent.getName(), key); - MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size()); + MetricsMonitor.getListenConfigCountMonitor().set(this.cacheMap.get().size()); return cache; } @@ -230,10 +230,10 @@ public String[] getServerConfig(String dataId, String group, String tenant, long } else { params = new ArrayList(Arrays.asList("dataId", dataId, "group", group, "tenant", tenant)); } - result = agent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, params, agent.getEncode(), readTimeout); + result = this.agent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, params, this.agent.getEncode(), readTimeout); } catch (IOException e) { String message = String.format( - "[%s] [sub-server] get server config exception, dataId=%s, group=%s, tenant=%s", agent.getName(), + "[%s] [sub-server] get server config exception, dataId=%s, group=%s, tenant=%s", this.agent.getName(), dataId, group, tenant); LOGGER.error(message, e); throw new NacosException(NacosException.SERVER_ERROR, e); @@ -241,7 +241,7 @@ public String[] getServerConfig(String dataId, String group, String tenant, long switch (result.code) { case HttpURLConnection.HTTP_OK: - LocalConfigInfoProcessor.saveSnapshot(agent.getName(), dataId, group, tenant, result.content); + LocalConfigInfoProcessor.saveSnapshot(this.agent.getName(), dataId, group, tenant, result.content); ct[0] = result.content; if (result.headers.containsKey(CONFIG_TYPE)) { ct[1] = result.headers.get(CONFIG_TYPE).get(0); @@ -250,22 +250,22 @@ public String[] getServerConfig(String dataId, String group, String tenant, long } return ct; case HttpURLConnection.HTTP_NOT_FOUND: - LocalConfigInfoProcessor.saveSnapshot(agent.getName(), dataId, group, tenant, null); + LocalConfigInfoProcessor.saveSnapshot(this.agent.getName(), dataId, group, tenant, null); return ct; case HttpURLConnection.HTTP_CONFLICT: { LOGGER.error( "[{}] [sub-server-error] get server config being modified concurrently, dataId={}, group={}, " - + "tenant={}", agent.getName(), dataId, group, tenant); + + "tenant={}", this.agent.getName(), dataId, group, tenant); throw new NacosException(NacosException.CONFLICT, "data being modified, dataId=" + dataId + ",group=" + group + ",tenant=" + tenant); } case HttpURLConnection.HTTP_FORBIDDEN: { - LOGGER.error("[{}] [sub-server-error] no right, dataId={}, group={}, tenant={}", agent.getName(), dataId, + LOGGER.error("[{}] [sub-server-error] no right, dataId={}, group={}, tenant={}", this.agent.getName(), dataId, group, tenant); throw new NacosException(result.code, result.content); } default: { - LOGGER.error("[{}] [sub-server-error] dataId={}, group={}, tenant={}, code={}", agent.getName(), dataId, + LOGGER.error("[{}] [sub-server-error] dataId={}, group={}, tenant={}, code={}", this.agent.getName(), dataId, group, tenant, result.code); throw new NacosException(result.code, "http error, code=" + result.code + ",dataId=" + dataId + ",group=" + group + ",tenant=" + tenant); @@ -277,25 +277,25 @@ private void checkLocalConfig(CacheData cacheData) { final String dataId = cacheData.dataId; final String group = cacheData.group; final String tenant = cacheData.tenant; - File path = LocalConfigInfoProcessor.getFailoverFile(agent.getName(), dataId, group, tenant); + File path = LocalConfigInfoProcessor.getFailoverFile(this.agent.getName(), dataId, group, tenant); // 没有 -> 有 if (!cacheData.isUseLocalConfigInfo() && path.exists()) { - String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant); + String content = LocalConfigInfoProcessor.getFailover(this.agent.getName(), dataId, group, tenant); String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); cacheData.setUseLocalConfigInfo(true); cacheData.setLocalConfigInfoVersion(path.lastModified()); cacheData.setContent(content); LOGGER.warn("[{}] [failover-change] failover file created. dataId={}, group={}, tenant={}, md5={}, content={}", - agent.getName(), dataId, group, tenant, md5, ContentUtils.truncateContent(content)); + this.agent.getName(), dataId, group, tenant, md5, ContentUtils.truncateContent(content)); return; } // 有 -> 没有。不通知业务监听器,从server拿到配置后通知。 if (cacheData.isUseLocalConfigInfo() && !path.exists()) { cacheData.setUseLocalConfigInfo(false); - LOGGER.warn("[{}] [failover-change] failover file deleted. dataId={}, group={}, tenant={}", agent.getName(), + LOGGER.warn("[{}] [failover-change] failover file deleted. dataId={}, group={}, tenant={}", this.agent.getName(), dataId, group, tenant); return; } @@ -303,13 +303,13 @@ private void checkLocalConfig(CacheData cacheData) { // 有变更 if (cacheData.isUseLocalConfigInfo() && path.exists() && cacheData.getLocalConfigInfoVersion() != path.lastModified()) { - String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant); + String content = LocalConfigInfoProcessor.getFailover(this.agent.getName(), dataId, group, tenant); String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); cacheData.setUseLocalConfigInfo(true); cacheData.setLocalConfigInfoVersion(path.lastModified()); cacheData.setContent(content); LOGGER.warn("[{}] [failover-change] failover file changed. dataId={}, group={}, tenant={}, md5={}, content={}", - agent.getName(), dataId, group, tenant, md5, ContentUtils.truncateContent(content)); + this.agent.getName(), dataId, group, tenant, md5, ContentUtils.truncateContent(content)); } } @@ -319,15 +319,15 @@ private String null2defaultGroup(String group) { public void checkConfigInfo() { // 分任务 - int listenerSize = cacheMap.get().size(); + int listenerSize = this.cacheMap.get().size(); // 向上取整为批数 int longingTaskCount = (int) Math.ceil(listenerSize / ParamUtil.getPerTaskConfigSize()); - if (longingTaskCount > currentLongingTaskCount) { - for (int i = (int) currentLongingTaskCount; i < longingTaskCount; i++) { + if (longingTaskCount > this.currentLongingTaskCount) { + for (int i = (int) this.currentLongingTaskCount; i < longingTaskCount; i++) { // 要判断任务是否在执行 这块需要好好想想。 任务列表现在是无序的。变化过程可能有问题 - executorService.execute(new LongPollingRunnable(i)); + this.executorService.execute(new LongPollingRunnable(i)); } - currentLongingTaskCount = longingTaskCount; + this.currentLongingTaskCount = longingTaskCount; } } @@ -369,7 +369,7 @@ List checkUpdateConfigStr(String probeUpdateString, boolean isInitializi List headers = new ArrayList(2); headers.add("Long-Pulling-Timeout"); - headers.add("" + timeout); + headers.add("" + this.timeout); // told server do not hang me up if new initializing cacheData added in if (isInitializingCacheList) { @@ -385,20 +385,20 @@ List checkUpdateConfigStr(String probeUpdateString, boolean isInitializi // In order to prevent the server from handling the delay of the client's long task, // increase the client's read timeout to avoid this problem. - long readTimeoutMs = timeout + (long) Math.round(timeout >> 1); - HttpResult result = agent.httpPost(Constants.CONFIG_CONTROLLER_PATH + "/listener", headers, params, - agent.getEncode(), readTimeoutMs); + long readTimeoutMs = this.timeout + (long) Math.round(this.timeout >> 1); + HttpResult result = this.agent.httpPost(Constants.CONFIG_CONTROLLER_PATH + "/listener", headers, params, + this.agent.getEncode(), readTimeoutMs); if (HttpURLConnection.HTTP_OK == result.code) { setHealthServer(true); return parseUpdateDataIdResponse(result.content); } else { setHealthServer(false); - LOGGER.error("[{}] [check-update] get changed dataId error, code: {}", agent.getName(), result.code); + LOGGER.error("[{}] [check-update] get changed dataId error, code: {}", this.agent.getName(), result.code); } } catch (IOException e) { setHealthServer(false); - LOGGER.error("[" + agent.getName() + "] [check-update] get changed dataId exception", e); + LOGGER.error("[" + this.agent.getName() + "] [check-update] get changed dataId exception", e); throw e; } return Collections.emptyList(); @@ -415,7 +415,7 @@ private List parseUpdateDataIdResponse(String response) { try { response = URLDecoder.decode(response, "UTF-8"); } catch (Exception e) { - LOGGER.error("[" + agent.getName() + "] [polling-resp] decode modifiedDataIdsString error", e); + LOGGER.error("[" + this.agent.getName() + "] [polling-resp] decode modifiedDataIdsString error", e); } List updateList = new LinkedList(); @@ -427,14 +427,14 @@ private List parseUpdateDataIdResponse(String response) { String group = keyArr[1]; if (keyArr.length == 2) { updateList.add(GroupKey.getKey(dataId, group)); - LOGGER.info("[{}] [polling-resp] config changed. dataId={}, group={}", agent.getName(), dataId, group); + LOGGER.info("[{}] [polling-resp] config changed. dataId={}, group={}", this.agent.getName(), dataId, group); } else if (keyArr.length == 3) { String tenant = keyArr[2]; updateList.add(GroupKey.getKeyTenant(dataId, group, tenant)); - LOGGER.info("[{}] [polling-resp] config changed. dataId={}, group={}, tenant={}", agent.getName(), + LOGGER.info("[{}] [polling-resp] config changed. dataId={}, group={}, tenant={}", this.agent.getName(), dataId, group, tenant); } else { - LOGGER.error("[{}] [polling-resp] invalid dataIdAndGroup error {}", agent.getName(), dataIdAndGroup); + LOGGER.error("[{}] [polling-resp] invalid dataIdAndGroup error {}", this.agent.getName(), dataIdAndGroup); } } } @@ -450,7 +450,7 @@ public ClientWorker(final HttpAgent agent, final ConfigFilterChainManager config init(properties); - executor = Executors.newScheduledThreadPool(1, new ThreadFactory() { + this.executor = Executors.newScheduledThreadPool(1, new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); @@ -460,7 +460,7 @@ public Thread newThread(Runnable r) { } }); - executorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() { + this.executorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); @@ -470,7 +470,7 @@ public Thread newThread(Runnable r) { } }); - executor.scheduleWithFixedDelay(new Runnable() { + this.executor.scheduleWithFixedDelay(new Runnable() { @Override public void run() { try { @@ -484,21 +484,16 @@ public void run() { private void init(Properties properties) { - timeout = Math.max(NumberUtils.toInt(properties.getProperty(PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT), + this.timeout = Math.max(NumberUtils.toInt(properties.getProperty(PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT), Constants.CONFIG_LONG_POLL_TIMEOUT), Constants.MIN_CONFIG_LONG_POLL_TIMEOUT); - taskPenaltyTime = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.CONFIG_RETRY_TIME), Constants.CONFIG_RETRY_TIME); + this.taskPenaltyTime = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.CONFIG_RETRY_TIME), Constants.CONFIG_RETRY_TIME); - enableRemoteSyncConfig = Boolean.parseBoolean(properties.getProperty(PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG)); + this.enableRemoteSyncConfig = Boolean.parseBoolean(properties.getProperty(PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG)); } @Override - public void close() throws IOException { - - } - - @Override - public void shutdown() throws InterruptedException { + public void shutdown() throws NacosException { LOGGER.info("do shutdown begin"); ThreadUtils.shutdown(this.executorService); ThreadUtils.shutdown(this.executor); @@ -520,7 +515,7 @@ public void run() { try { // check failover config for (CacheData cacheData : cacheMap.get().values()) { - if (cacheData.getTaskId() == taskId) { + if (cacheData.getTaskId() == this.taskId) { cacheDatas.add(cacheData); try { checkLocalConfig(cacheData); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java index 3018d82263c..e9a4771912f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java @@ -18,20 +18,23 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.SystemPropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.client.config.impl.EventDispatcher.ServerlistChangeEvent; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.utils.*; +import com.alibaba.nacos.common.executor.ExecutorFactory; +import com.alibaba.nacos.common.executor.NameThreadFactory; +import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.StringUtils; +import com.alibaba.nacos.common.utils.ThreadUtils; import org.slf4j.Logger; import java.io.IOException; import java.io.StringReader; import java.net.HttpURLConnection; import java.util.*; -import java.util.concurrent.Callable; -import java.util.concurrent.TimeUnit; - +import java.util.concurrent.*; /** @@ -39,11 +42,12 @@ * * @author Nacos */ -public class ServerListManager { +public class ServerListManager implements Closeable { private static final Logger LOGGER = LogUtils.logger(ServerListManager.class); private static final String HTTPS = "https://"; private static final String HTTP = "http://"; + private ScheduledExecutorService executorService; public ServerListManager() { isFixed = false; @@ -94,6 +98,7 @@ public ServerListManager(String endpoint, String namespace) throws NacosExceptio Properties properties = new Properties(); properties.setProperty(PropertyKeyConst.ENDPOINT, endpoint); endpoint = initEndpoint(properties); + initExecutor(); if (StringUtils.isBlank(endpoint)) { throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank"); @@ -114,11 +119,15 @@ public ServerListManager(String endpoint, String namespace) throws NacosExceptio } } + + public ServerListManager(Properties properties) throws NacosException { isStarted = false; serverAddrsStr = properties.getProperty(PropertyKeyConst.SERVER_ADDR); String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE); initParam(properties); + initExecutor(); + if (StringUtils.isNotEmpty(serverAddrsStr)) { isFixed = true; List serverAddrs = new ArrayList(); @@ -208,6 +217,19 @@ public String call() { return StringUtils.isNotBlank(endpointTmp) ? endpointTmp : ""; } + private void initExecutor() { + // init executorService + this.executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread t = new Thread(r); + t.setName("com.alibaba.nacos.client.ServerListManager"); + t.setDaemon(true); + return t; + } + }); + } + public synchronized void start() throws NacosException { if (isStarted || isFixed) { @@ -231,7 +253,8 @@ public synchronized void start() throws NacosException { "fail to get NACOS-server serverlist! env:" + name + ", not connnect url:" + addressServerUrl); } - TimerService.scheduleWithFixedDelay(getServersTask, 0L, 30L, TimeUnit.SECONDS); + // executor schedules the timer task + this.executorService.scheduleWithFixedDelay(getServersTask,0L, 30L, TimeUnit.SECONDS); isStarted = true; } @@ -246,6 +269,13 @@ Iterator iterator() { return new ServerAddressIterator(serverUrls); } + @Override + public void shutdown() throws NacosException{ + LOGGER.info("do shutdown begin"); + ThreadUtils.shutdown(this.executorService); + LOGGER.info("do shutdown stop"); + } + class GetServerListTask implements Runnable { final String url; diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/TimerService.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/TimerService.java deleted file mode 100644 index 53f4e048fff..00000000000 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/TimerService.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.nacos.client.config.impl; - -import java.util.concurrent.*; - -/** - * Time Service - * - * @author Nacos - */ -public class TimerService { - - static public ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, - long delay, TimeUnit unit) { - return scheduledExecutor.scheduleWithFixedDelay(command, initialDelay, delay, unit); - } - - @SuppressWarnings("PMD.ThreadPoolCreationRule") - static ScheduledExecutorService scheduledExecutor = Executors - .newSingleThreadScheduledExecutor(new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - Thread t = new Thread(r); - t.setName("com.alibaba.nacos.client.Timer"); - t.setDaemon(true); - return t; - } - }); - -} diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index d6e26120aef..00cb30bde45 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -35,6 +35,8 @@ import com.alibaba.nacos.client.naming.utils.InitUtils; import com.alibaba.nacos.client.naming.utils.UtilAndComs; import com.alibaba.nacos.client.utils.ValidatorUtils; +import com.alibaba.nacos.common.lifecycle.LifeCycle; +import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; @@ -50,7 +52,7 @@ * @author nkorange */ @SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") -public class NacosNamingService implements NamingService { +public class NacosNamingService implements NamingService, LifeCycle { /** * Each Naming service should have different namespace. @@ -73,6 +75,9 @@ public class NacosNamingService implements NamingService { private NamingProxy serverProxy; + private static final ResourceLifeCycleManager RESOURCE_MANAGER = ResourceLifeCycleManager.getInstance(); + + public NacosNamingService(String serverList) { Properties properties = new Properties(); properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList); @@ -96,6 +101,7 @@ private void init(Properties properties) { beatReactor = new BeatReactor(serverProxy, initClientBeatThreadCount(properties)); hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, isLoadCacheAtStart(properties), initPollingThreadCount(properties)); + RESOURCE_MANAGER.register(this); } private int initClientBeatThreadCount(Properties properties) { @@ -486,4 +492,17 @@ private List selectInstances(ServiceInfo serviceInfo, boolean healthy) public BeatReactor getBeatReactor() { return beatReactor; } + + @Override + public void shutDown() throws NacosException{ + destroy(); + } + + @Override + public void destroy() throws NacosException{ + this.beatReactor.shutdown(); + this.eventDispatcher.shutdown(); + this.hostReactor.shutdown(); + this.serverProxy.shutdown(); + } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index d969259bdea..81cdb4b8f92 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -15,14 +15,17 @@ */ package com.alibaba.nacos.client.naming.backups; +import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.client.naming.cache.ConcurrentDiskUtil; import com.alibaba.nacos.client.naming.cache.DiskCache; import com.alibaba.nacos.client.naming.core.HostReactor; import com.alibaba.nacos.client.naming.utils.CollectionUtils; import com.alibaba.nacos.client.naming.utils.UtilAndComs; +import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.utils.JacksonUtils; +import com.alibaba.nacos.common.utils.ThreadUtils; import org.apache.commons.lang3.StringUtils; import java.io.BufferedReader; @@ -37,29 +40,31 @@ /** * @author nkorange */ -public class FailoverReactor { +public class FailoverReactor implements Closeable { private String failoverDir; private HostReactor hostReactor; + private ScheduledExecutorService executorService; + public FailoverReactor(HostReactor hostReactor, String cacheDir) { this.hostReactor = hostReactor; this.failoverDir = cacheDir + "/failover"; + // init executorService + this.executorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r); + thread.setDaemon(true); + thread.setName("com.alibaba.nacos.naming.failover"); + return thread; + } + }); this.init(); } private Map serviceMap = new ConcurrentHashMap(); - private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - Thread thread = new Thread(r); - thread.setDaemon(true); - thread.setName("com.alibaba.nacos.naming.failover"); - return thread; - } - }); - private Map switchParams = new ConcurrentHashMap(); private static final long DAY_PERIOD_MINUTES = 24 * 60; @@ -99,6 +104,13 @@ public Date addDay(Date date, int num) { return startDT.getTime(); } + @Override + public void shutdown() throws NacosException { + NAMING_LOGGER.info("do shutdown begin"); + ThreadUtils.shutdown(this.executorService); + NAMING_LOGGER.info("do shutdown stop"); + } + class SwitchRefresher implements Runnable { long lastModifiedMillis = 0L; diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java index 60fb52dc339..3056d6bb4c1 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java @@ -24,7 +24,9 @@ import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.naming.net.NamingProxy; import com.alibaba.nacos.client.naming.utils.UtilAndComs; +import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.utils.JacksonUtils; +import com.alibaba.nacos.common.utils.ThreadUtils; import com.fasterxml.jackson.databind.JsonNode; import java.util.Map; @@ -35,7 +37,7 @@ /** * @author harold */ -public class BeatReactor { +public class BeatReactor implements Closeable { private ScheduledExecutorService executorService; @@ -51,7 +53,7 @@ public BeatReactor(NamingProxy serverProxy) { public BeatReactor(NamingProxy serverProxy, int threadCount) { this.serverProxy = serverProxy; - executorService = new ScheduledThreadPoolExecutor(threadCount, new ThreadFactory() { + this.executorService = new ScheduledThreadPoolExecutor(threadCount, new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r); @@ -67,17 +69,17 @@ public void addBeatInfo(String serviceName, BeatInfo beatInfo) { String key = buildKey(serviceName, beatInfo.getIp(), beatInfo.getPort()); BeatInfo existBeat = null; //fix #1733 - if ((existBeat = dom2Beat.remove(key)) != null) { + if ((existBeat = this.dom2Beat.remove(key)) != null) { existBeat.setStopped(true); } - dom2Beat.put(key, beatInfo); - executorService.schedule(new BeatTask(beatInfo), beatInfo.getPeriod(), TimeUnit.MILLISECONDS); - MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size()); + this.dom2Beat.put(key, beatInfo); + this.executorService.schedule(new BeatTask(beatInfo), beatInfo.getPeriod(), TimeUnit.MILLISECONDS); + MetricsMonitor.getDom2BeatSizeMonitor().set(this.dom2Beat.size()); } public void removeBeatInfo(String serviceName, String ip, int port) { NAMING_LOGGER.info("[BEAT] removing beat: {}:{}:{} from beat map.", serviceName, ip, port); - BeatInfo beatInfo = dom2Beat.remove(buildKey(serviceName, ip, port)); + BeatInfo beatInfo = this.dom2Beat.remove(buildKey(serviceName, ip, port)); if (beatInfo == null) { return; } @@ -90,6 +92,13 @@ private String buildKey(String serviceName, String ip, int port) { + ip + Constants.NAMING_INSTANCE_ID_SPLITTER + port; } + @Override + public void shutdown() throws NacosException{ + NAMING_LOGGER.info("do shutdown begin"); + ThreadUtils.shutdown(this.executorService); + NAMING_LOGGER.info("do shutdown stop"); + } + class BeatTask implements Runnable { BeatInfo beatInfo; @@ -100,12 +109,12 @@ public BeatTask(BeatInfo beatInfo) { @Override public void run() { - if (beatInfo.isStopped()) { + if (this.beatInfo.isStopped()) { return; } - long nextTime = beatInfo.getPeriod(); + long nextTime = this.beatInfo.getPeriod(); try { - JsonNode result = serverProxy.sendBeat(beatInfo, BeatReactor.this.lightBeatEnabled); + JsonNode result = serverProxy.sendBeat(this.beatInfo, BeatReactor.this.lightBeatEnabled); long interval = result.get("clientBeatInterval").asInt(); boolean lightBeatEnabled = false; if (result.has(CommonParams.LIGHT_BEAT_ENABLED)) { @@ -121,26 +130,26 @@ public void run() { } if (code == NamingResponseCode.RESOURCE_NOT_FOUND) { Instance instance = new Instance(); - instance.setPort(beatInfo.getPort()); - instance.setIp(beatInfo.getIp()); - instance.setWeight(beatInfo.getWeight()); - instance.setMetadata(beatInfo.getMetadata()); - instance.setClusterName(beatInfo.getCluster()); - instance.setServiceName(beatInfo.getServiceName()); + instance.setPort(this.beatInfo.getPort()); + instance.setIp(this.beatInfo.getIp()); + instance.setWeight(this.beatInfo.getWeight()); + instance.setMetadata(this.beatInfo.getMetadata()); + instance.setClusterName(this.beatInfo.getCluster()); + instance.setServiceName(this.beatInfo.getServiceName()); instance.setInstanceId(instance.getInstanceId()); instance.setEphemeral(true); try { - serverProxy.registerService(beatInfo.getServiceName(), - NamingUtils.getGroupName(beatInfo.getServiceName()), instance); + serverProxy.registerService(this.beatInfo.getServiceName(), + NamingUtils.getGroupName(this.beatInfo.getServiceName()), instance); } catch (Exception ignore) { } } } catch (NacosException ne) { NAMING_LOGGER.error("[CLIENT-BEAT] failed to send beat: {}, code: {}, msg: {}", - JacksonUtils.toJson(beatInfo), ne.getErrCode(), ne.getErrMsg()); + JacksonUtils.toJson(this.beatInfo), ne.getErrCode(), ne.getErrMsg()); } - executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS); + executorService.schedule(new BeatTask(this.beatInfo), nextTime, TimeUnit.MILLISECONDS); } } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/Balancer.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/Balancer.java index 48109a91827..c37364d44be 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/Balancer.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/Balancer.java @@ -23,7 +23,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; @@ -32,11 +31,6 @@ */ public class Balancer { - /** - * report status to server - */ - public final static List UNCONSISTENT_SERVICE_WITH_ADDRESS_SERVER = new CopyOnWriteArrayList(); - public static class RandomByWeight { public static List selectAll(ServiceInfo serviceInfo) { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java index 28f4b694f18..c2de028fbc7 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java @@ -15,11 +15,14 @@ */ package com.alibaba.nacos.client.naming.core; +import com.alibaba.nacos.api.exception.NacosException; 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.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.client.naming.utils.CollectionUtils; +import com.alibaba.nacos.common.lifecycle.Closeable; +import com.alibaba.nacos.common.utils.ThreadUtils; import java.util.ArrayList; import java.util.Collections; @@ -32,7 +35,7 @@ /** * @author xuanyin */ -public class EventDispatcher { +public class EventDispatcher implements Closeable { private ExecutorService executor = null; @@ -109,6 +112,13 @@ public void serviceChanged(ServiceInfo serviceInfo) { changedServices.add(serviceInfo); } + @Override + public void shutdown() throws NacosException { + NAMING_LOGGER.info("do shutdown begin"); + ThreadUtils.shutdown(this.executor); + NAMING_LOGGER.info("do shutdown stop"); + } + private class Notifier implements Runnable { @Override public void run() { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java index 04d269d2a79..9996a0c29b4 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java @@ -23,8 +23,10 @@ import com.alibaba.nacos.client.naming.cache.DiskCache; import com.alibaba.nacos.client.naming.net.NamingProxy; import com.alibaba.nacos.client.naming.utils.UtilAndComs; +import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.utils.JacksonUtils; +import com.alibaba.nacos.common.utils.ThreadUtils; import org.apache.commons.lang3.StringUtils; import java.util.*; @@ -35,7 +37,7 @@ /** * @author xuanyin */ -public class HostReactor { +public class HostReactor implements Closeable { private static final long DEFAULT_DELAY = 1000L; @@ -65,8 +67,8 @@ public HostReactor(EventDispatcher eventDispatcher, NamingProxy serverProxy, Str public HostReactor(EventDispatcher eventDispatcher, NamingProxy serverProxy, String cacheDir, boolean loadCacheAtStart, int pollingThreadCount) { - - executor = new ScheduledThreadPoolExecutor(pollingThreadCount, new ThreadFactory() { + // init executorService + this.executor = new ScheduledThreadPoolExecutor(pollingThreadCount, new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r); @@ -303,6 +305,13 @@ public void refreshOnly(String serviceName, String clusters) { } } + @Override + public void shutdown() throws NacosException{ + NAMING_LOGGER.info("do shutdown begin"); + ThreadUtils.shutdown(this.executor); + NAMING_LOGGER.info("do shutdown stop"); + } + public class UpdateTask implements Runnable { long lastRefTime = Long.MAX_VALUE; private String clusters; diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java index 56a933eaceb..aacd27717f5 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java @@ -15,9 +15,12 @@ */ package com.alibaba.nacos.client.naming.core; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.IoUtils; +import com.alibaba.nacos.common.utils.ThreadUtils; import java.net.DatagramPacket; import java.net.DatagramSocket; @@ -31,7 +34,7 @@ /** * @author xuanyin */ -public class PushReceiver implements Runnable { +public class PushReceiver implements Runnable, Closeable { private ScheduledExecutorService executorService; @@ -44,9 +47,9 @@ public class PushReceiver implements Runnable { public PushReceiver(HostReactor hostReactor) { try { this.hostReactor = hostReactor; - udpSocket = new DatagramSocket(); + this.udpSocket = new DatagramSocket(); - executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { + this.executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r); @@ -56,7 +59,7 @@ public Thread newThread(Runnable r) { } }); - executorService.execute(this); + this.executorService.execute(this); } catch (Exception e) { NAMING_LOGGER.error("[NA] init udp socket failed", e); } @@ -70,7 +73,7 @@ public void run() { byte[] buffer = new byte[UDP_MSS]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); - udpSocket.receive(packet); + this.udpSocket.receive(packet); String json = new String(IoUtils.tryDecompress(packet.getData()), "UTF-8").trim(); NAMING_LOGGER.info("received push data: " + json + " from " + packet.getAddress().toString()); @@ -78,7 +81,7 @@ public void run() { PushPacket pushPacket = JacksonUtils.toObj(json, PushPacket.class); String ack; if ("dom".equals(pushPacket.type) || "service".equals(pushPacket.type)) { - hostReactor.processServiceJSON(pushPacket.data); + this.hostReactor.processServiceJSON(pushPacket.data); // send ack to server ack = "{\"type\": \"push-ack\"" @@ -89,7 +92,7 @@ public void run() { ack = "{\"type\": \"dump-ack\"" + ", \"lastRefTime\": \"" + pushPacket.lastRefTime + "\", \"data\":" + "\"" - + StringUtils.escapeJavaScript(JacksonUtils.toJson(hostReactor.getServiceInfoMap())) + + StringUtils.escapeJavaScript(JacksonUtils.toJson(this.hostReactor.getServiceInfoMap())) + "\"}"; } else { // do nothing send ack only @@ -98,7 +101,7 @@ public void run() { + "\", \"data\":" + "\"\"}"; } - udpSocket.send(new DatagramPacket(ack.getBytes(Charset.forName("UTF-8")), + this.udpSocket.send(new DatagramPacket(ack.getBytes(Charset.forName("UTF-8")), ack.getBytes(Charset.forName("UTF-8")).length, packet.getSocketAddress())); } catch (Exception e) { NAMING_LOGGER.error("[NA] error while receiving push data", e); @@ -106,6 +109,13 @@ public void run() { } } + @Override + public void shutdown() throws NacosException { + NAMING_LOGGER.info("do shutdown begin"); + ThreadUtils.shutdown(this.executorService); + NAMING_LOGGER.info("do shutdown stop"); + } + public static class PushPacket { public String type; public long lastRefTime; @@ -113,6 +123,6 @@ public static class PushPacket { } public int getUDPPort() { - return udpSocket.getLocalPort(); + return this.udpSocket.getLocalPort(); } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index 253d7b5978e..2bf27d22597 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -37,11 +37,8 @@ import com.alibaba.nacos.client.utils.AppNameUtils; import com.alibaba.nacos.client.utils.TemplateUtils; import com.alibaba.nacos.common.constant.HttpHeaderConsts; -import com.alibaba.nacos.common.utils.HttpMethod; -import com.alibaba.nacos.common.utils.IoUtils; -import com.alibaba.nacos.common.utils.JacksonUtils; -import com.alibaba.nacos.common.utils.UuidUtils; -import com.alibaba.nacos.common.utils.VersionUtils; +import com.alibaba.nacos.common.lifecycle.Closeable; +import com.alibaba.nacos.common.utils.*; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; @@ -60,7 +57,7 @@ /** * @author nkorange */ -public class NamingProxy { +public class NamingProxy implements Closeable { private static final int DEFAULT_SERVER_PORT = 8848; @@ -86,9 +83,11 @@ public class NamingProxy { private Properties properties; + private ScheduledExecutorService executorService; + public NamingProxy(String namespaceId, String endpoint, String serverList, Properties properties) { - securityProxy = new SecurityProxy(properties); + this.securityProxy = new SecurityProxy(properties); this.properties = properties; this.setServerPort(DEFAULT_SERVER_PORT); this.namespaceId = namespaceId; @@ -105,7 +104,7 @@ public NamingProxy(String namespaceId, String endpoint, String serverList, Prope private void initRefreshTask() { - ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(2, new ThreadFactory() { + this.executorService = new ScheduledThreadPoolExecutor(2, new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); @@ -115,7 +114,7 @@ public Thread newThread(Runnable r) { } }); - executorService.scheduleWithFixedDelay(new Runnable() { + this.executorService.scheduleWithFixedDelay(new Runnable() { @Override public void run() { refreshSrvIfNeed(); @@ -123,7 +122,7 @@ public void run() { }, 0, vipSrvRefInterMillis, TimeUnit.MILLISECONDS); - executorService.scheduleWithFixedDelay(new Runnable() { + this.executorService.scheduleWithFixedDelay(new Runnable() { @Override public void run() { securityProxy.login(getServerList()); @@ -131,7 +130,7 @@ public void run() { }, 0, securityInfoRefreshIntervalMills, TimeUnit.MILLISECONDS); refreshSrvIfNeed(); - securityProxy.login(getServerList()); + this.securityProxy.login(getServerList()); } public List getServerListFromEndpoint() { @@ -583,5 +582,11 @@ public void setServerPort(int serverPort) { } } + @Override + public void shutdown() throws NacosException{ + NAMING_LOGGER.info("do shutdown begin"); + ThreadUtils.shutdown(this.executorService); + NAMING_LOGGER.info("do shutdown stop"); + } } diff --git a/client/src/test/java/com/alibaba/nacos/client/ConfigTest.java b/client/src/test/java/com/alibaba/nacos/client/ConfigTest.java index 0a0ca4b7a18..394c80a1207 100644 --- a/client/src/test/java/com/alibaba/nacos/client/ConfigTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/ConfigTest.java @@ -22,10 +22,7 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.common.utils.ThreadUtils; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.*; import java.util.Properties; @@ -47,6 +44,11 @@ public void before() throws Exception { configService = NacosFactory.createConfigService(properties); } + @After + public void cleanup() throws Exception { + configService.shutDown(); + } + @Test public void test() throws Exception { final String dataId = "lessspring"; diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java deleted file mode 100644 index bf0a716525b..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/AbstractLifeCycle.java +++ /dev/null @@ -1,217 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.nacos.common.lifecycle; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Basic implementation of the life cycle interface for services. - * - * @author zongtanghu - */ -public abstract class AbstractLifeCycle implements LifeCycle { - - private static final Logger LOG = LoggerFactory.getLogger(AbstractLifeCycle.class); - - private final Object lock = new Object(); - private volatile LifeCycleState state; - private static final ResourceLifeCycleManager RESOURCE_MANAGER = ResourceLifeCycleManager.getInstance(); - - - public AbstractLifeCycle() { - this.state = LifeCycleState.STOPPED; - RESOURCE_MANAGER.register(this); - } - - /** - * Method to override to start the lifecycle. - * - * @throws Exception If there was a problem starting. Will cause a transition to FAILED state - */ - protected void doStart() throws Exception { - } - - /** - * Method to override to stop the lifecycle. - * - * @throws Exception If there was a problem stopping. Will cause a transition to FAILED state - */ - protected void doStop() throws Exception { - } - - @Override - public void start() throws Exception { - synchronized (this.lock) - { - try - { - switch (state) { - case STARTED: - return; - case STARTING: - case STOPPING: - throw new IllegalStateException(getState()); - default: - try { - setStarting(); - doStart(); - setStarted(); - } catch (Exception e) { - if (LOG.isDebugEnabled()) { - LOG.debug("Unable to stop", e); - } - setStopping(); - doStop(); - setStopped(); - } - } - } catch (Throwable e) { - setFailed(e); - throw e; - } - } - } - - @Override - public void stop() throws Exception { - synchronized (this.lock) { - try - { - switch (this.state) { - case STOPPED: - return; - case STARTING: - case STOPPING: - throw new IllegalStateException(getState()); - default: - setStopping(); - doStop(); - setStopped(); - } - } catch (Throwable e) { - setFailed(e); - throw e; - } - } - } - - @Override - public boolean isRunning() { - switch (this.state) - { - case STARTED: - case STARTING: - return true; - default: - return false; - } - } - - @Override - public boolean isStarted() { - return this.state == LifeCycleState.STARTED; - } - - @Override - public boolean isStarting() { - return this.state == LifeCycleState.STARTING; - } - - @Override - public boolean isStopping() { - return this.state == LifeCycleState.STOPPING; - } - - @Override - public boolean isStopped() { - return this.state == LifeCycleState.STOPPED; - } - - @Override - public boolean isFailed() { - return this.state == LifeCycleState.FAILED; - } - - /** - * Get the service's current state and return. - * - * @return service's current state. - */ - public String getState() { - return this.state.toString(); - } - - /** - * If the service's current status is STARTING state, it will set STOPPED state. - * - */ - private void setStarted() { - if (this.state == LifeCycleState.STARTING) { - this.state = LifeCycleState.STARTED; - if (LOG.isDebugEnabled()) { - LOG.debug("STARTED {}", this); - } - } - } - - /** - * The service which implement AbstractLifeCycle will set STARTING state. - * - */ - private void setStarting() { - if (LOG.isDebugEnabled()) { - LOG.debug("STARTING {}", this); - } - this.state = LifeCycleState.STARTING; - } - - /** - * The service which implement AbstractLifeCycle will set STOPPING state. - * - */ - private void setStopping() { - if (LOG.isDebugEnabled()) { - LOG.debug("STOPPING {}", this); - } - this.state = LifeCycleState.STOPPING; - } - - /** - * If the service's current status is STARTING state, it will set STOPPED state. - * - */ - private void setStopped() { - if (this.state == LifeCycleState.STOPPING) { - this.state = LifeCycleState.STOPPED; - if (LOG.isDebugEnabled()) { - LOG.debug("STOPPED {}", this); - } - } - } - - /** - * If some exceptions happen, the service will set FAILED state. - * - * @param tb Exception which happens. - */ - private void setFailed(Throwable tb) { - this.state = LifeCycleState.FAILED; - if (LOG.isDebugEnabled()) { - LOG.warn("FAILED " + this + ": " + tb, tb); - } - } -} diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/Closeable.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/Closeable.java index 5dd442ae9e3..6f969b180cc 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/Closeable.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/Closeable.java @@ -1,6 +1,7 @@ package com.alibaba.nacos.common.lifecycle; -import java.io.IOException; + +import com.alibaba.nacos.api.exception.NacosException; /** * An interface is used to define the resource's close and shutdown, @@ -11,18 +12,11 @@ */ public interface Closeable { - /** - * Close the Resources, such as IO Connection resourcese. - * - * @throws IOException - */ - public void close() throws IOException; - /** * Shutdown the Resources, such as Thread Pool. * - * @throws InterruptedException + * @throws NacosException exception. */ - public void shutdown() throws InterruptedException; + public void shutdown() throws NacosException; } diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java index bb9593cfa78..3e06a841bf9 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java @@ -15,6 +15,8 @@ */ package com.alibaba.nacos.common.lifecycle; +import com.alibaba.nacos.api.exception.NacosException; + /** * * The lifecycle interface for generic service. Classes are need to implement @@ -25,60 +27,9 @@ public interface LifeCycle { /** - * Starts the service. - * - * @throws Exception If the service fails to start. - */ - public void start() throws Exception; - - /** - * Stops the service. - * - * @throws Exception If the service fails to stop. - */ - public void stop() throws Exception; - - /** - * This method will return the service whether is running or not. - * - * @return true if the component is starting or has been started. - */ - public boolean isRunning(); - - /** - * This method will return the service whether is started or not. - * - * @return true if the service has been started. - * - */ - public boolean isStarted(); - - /** - * This method will return the service whether is starting or not. - * - * @return true if the component is starting. - * - */ - public boolean isStarting(); - - /** - * This method will return the service whether is stopping or not. - * - * @return true if the service is stopping. - */ - public boolean isStopping(); - - /** - * This method will return the service whether is stopped or not. - * - * @return true if the service has been stopped. - */ - public boolean isStopped(); - - /** - * This method will return the service whether is failed or not. + * Destroy the service. * - * @return true if the service has failed to start or has failed to stop. + * @throws NacosException An NacosException occours when destroy service. */ - public boolean isFailed(); + public void destroy() throws NacosException; } diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java deleted file mode 100644 index 57472c30425..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycleState.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.nacos.common.lifecycle; - -/** - * Define serveral lifecycle state. - * - * @author zongtanghu - */ -public enum LifeCycleState { - - /** - * The service's current state is STOPPED. - */ - STOPPED("STOPPED"), - - /** - * The service's current state is STARTING. - */ - STARTING("STARTING"), - - /** - * The service's current state is STARTED. - */ - STARTED("STARTED"), - - /** - * The service's current state is STOPPING. - */ - STOPPING("STOPPING"), - - /** - * The service's current state is FAILED. - */ - FAILED("FAILED"); - - private String name; - - LifeCycleState(String name) { - this.name = name; - } - - @Override - public String toString() { - return "LifeCycleState{" + - "name='" + name + '\'' + - '}'; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java index dc4f1209ca3..4a84202eaa0 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java @@ -37,12 +37,12 @@ public final class ResourceLifeCycleManager { /** * */ - private List lifeCycleResources; + private List lifeCycleResources; /** * Map */ - private Map lockers = new ConcurrentHashMap(8); + private Map lockers = new ConcurrentHashMap(8); private static final ResourceLifeCycleManager INSTANCE = new ResourceLifeCycleManager(); @@ -61,7 +61,7 @@ public void run() { } private void init() { - this.lifeCycleResources = new CopyOnWriteArrayList(); + this.lifeCycleResources = new CopyOnWriteArrayList(); } public static ResourceLifeCycleManager getInstance() { @@ -76,9 +76,8 @@ public static void shutdown() { if (!CLOSED.compareAndSet(false, true)) { return; } - - List instances = INSTANCE.lifeCycleResources; - for (AbstractLifeCycle instance : instances) { + List instances = INSTANCE.lifeCycleResources; + for (LifeCycle instance : instances) { INSTANCE.destroy(instance); } } @@ -88,8 +87,8 @@ public static void shutdown() { * * @param instance the life cycle resource instance which is need to be destroyed. */ - public void destroy(AbstractLifeCycle instance) { - final Object monitor = lockers.get(instance); + public void destroy(LifeCycle instance) { + final Object monitor = this.lockers.get(instance); if (monitor == null) { return; } @@ -99,9 +98,15 @@ public void destroy(AbstractLifeCycle instance) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Life cycle resources do stop"); } - instance.stop(); - INSTANCE.lifeCycleResources.remove(instance); - INSTANCE.lockers.remove(instance); + + // here, double check if lockers contains or not + if (!this.lockers.containsKey(instance)) { + return; + } + + instance.destroy(); + this.lifeCycleResources.remove(instance); + this.lockers.remove(instance); } catch (Exception e) { LOGGER.error("An exception occurred during resource do stop : {}", e); } @@ -115,12 +120,14 @@ public void destroy(AbstractLifeCycle instance) { * @param instance the management life cycle resource instances; * */ - public void deregister(AbstractLifeCycle instance) { + public void deregister(LifeCycle instance) { if (this.lifeCycleResources.contains(instance)) { - final Object monitor = lockers.get(instance); + final Object monitor = this.lockers.get(instance); synchronized (monitor) { - this.lifeCycleResources.remove(instance); - this.lockers.remove(instance); + if (this.lockers.containsKey(instance)) { + this.lockers.remove(instance); + this.lifeCycleResources.remove(instance); + } } } } @@ -130,12 +137,14 @@ public void deregister(AbstractLifeCycle instance) { * * @param instance the management life cycle resource instances. */ - public void register(AbstractLifeCycle instance) { - if (!lifeCycleResources.contains(instance)) { + public void register(LifeCycle instance) { + if (!this.lifeCycleResources.contains(instance)) { synchronized(this) { - lockers.put(instance, new Object()); + if (!this.lockers.containsKey(instance)) { + this.lockers.put(instance, new Object()); + this.lifeCycleResources.add(instance); + } } - this.lifeCycleResources.add(instance); } } } diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/ThreadUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/ThreadUtils.java index d82e8c55349..f98eb62873b 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/ThreadUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/ThreadUtils.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.common.utils; +import com.alibaba.nacos.api.exception.NacosException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -77,7 +78,7 @@ public static int getSuitableThreadCount() { return workerCount; } - public static void shutdown(ExecutorService executor) { + public static void shutdown(ExecutorService executor) throws NacosException { executor.shutdown(); int retry = 3; while (retry > 0) { @@ -91,6 +92,7 @@ public static void shutdown(ExecutorService executor) { Thread.interrupted(); } catch (Throwable ex) { LOGGER.error("shutdown the executor has error : {}", ex); + throw new NacosException(NacosException.RESOURCE_DESTROY_FAILED, ex); } executor.shutdownNow(); } diff --git a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java index 9128aec2311..463ea1db89b 100644 --- a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java @@ -15,9 +15,7 @@ */ package com.alibaba.nacos.common.LifeCycle; -import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; -import com.alibaba.nacos.common.lifecycle.LifeCycleState; - +import com.alibaba.nacos.common.lifecycle.LifeCycle; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -35,39 +33,28 @@ public class LifeCycleClassTest { @Before public void setup() throws Exception{ this.res = new Resource(0); - this.res.start(); - Assert.assertEquals(this.res.getCounter(), 1); + Assert.assertEquals(this.res.getCounter(), 0); } @After public void cleanup() throws Exception{ - this.res.stop(); + this.res.destroy(); Assert.assertEquals(this.res.getCounter(), 0); - Assert.assertTrue(this.res.isStopped()); } @Test public void testResource_LifeCycleMethod() throws Exception{ - this.res.doStart(); + this.res.increament(); + Assert.assertEquals(this.res.getCounter(), 1); + this.res.increament(); Assert.assertEquals(this.res.getCounter(), 2); - this.res.doStart(); + this.res.increament(); Assert.assertEquals(this.res.getCounter(), 3); - this.res.doStop(); - Assert.assertEquals(this.res.getCounter(), 2); - this.res.doStop(); - Assert.assertEquals(this.res.getCounter(), 1); + this.res.increament(); + Assert.assertEquals(this.res.getCounter(), 4); } - @Test - public void testResource_GetState() throws Exception{ - Assert.assertEquals(this.res.getState(), LifeCycleState.STARTED.toString()); - Assert.assertTrue(this.res.isStarted()); - Assert.assertFalse(this.res.isFailed()); - Assert.assertTrue(this.res.isRunning()); - Assert.assertFalse(this.res.isStopped()); - } - - class Resource extends AbstractLifeCycle { + class Resource implements LifeCycle { private int counter; @@ -83,14 +70,13 @@ public void setCounter(int counter) { this.counter = counter; } - @Override - protected void doStart() throws Exception { - counter++; + public void increament() { + this.counter++; } @Override - protected void doStop() throws Exception { - counter--; + public void destroy() { + this.counter = 0; } } } diff --git a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java index 5e7be909b6d..2494371b8fc 100644 --- a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java @@ -15,8 +15,7 @@ */ package com.alibaba.nacos.common.LifeCycle; -import com.alibaba.nacos.common.lifecycle.AbstractLifeCycle; -import com.alibaba.nacos.common.lifecycle.LifeCycleState; +import com.alibaba.nacos.common.lifecycle.LifeCycle; import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import org.junit.After; import org.junit.Assert; @@ -37,8 +36,8 @@ public class LifeCycleManagerTest { @Before public void setup() throws Exception{ this.res = new Resource(0); - this.res.start(); RESOURCE_MANAGER.register(this.res); + this.res.increament(); Assert.assertEquals(this.res.getCounter(), 1); } @@ -56,30 +55,31 @@ public void cleanup() throws Exception{ @Test public void testLifeCycleManager() throws Exception{ - this.res.doStart(); + this.res.increament(); Assert.assertEquals(this.res.getCounter(), 2); - this.res.doStop(); - Assert.assertEquals(this.res.getCounter(), 1); + this.res.increament(); + Assert.assertEquals(this.res.getCounter(), 3); } @Test public void testLifeCycleManager_deregister() throws Exception{ Resource temp = new Resource(0); - temp.start(); RESOURCE_MANAGER.register(temp); + temp.increament(); RESOURCE_MANAGER.deregister(temp); RESOURCE_MANAGER.destroy(temp); Assert.assertEquals(temp.getCounter(), 1); + temp.destroy(); + Assert.assertEquals(temp.getCounter(), 0); } - class Resource extends AbstractLifeCycle { + class Resource implements LifeCycle { private int counter; public Resource(int counter) { - super(); this.counter = counter; } @@ -92,14 +92,13 @@ public void setCounter(int counter) { this.counter = counter; } - @Override - protected void doStart() throws Exception { - counter++; + public void increament() { + this.counter++; } @Override - protected void doStop() throws Exception { - counter--; + public void destroy() { + this.counter = 0; } } } diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java index 5c7999f781e..fe6d6971de2 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java @@ -84,11 +84,12 @@ public void cleanup() throws Exception { result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); Assert.assertEquals(true, JSON.parseObject(result.content).getBoolean("data")); + NacosFactory.destroyConfigService(iconfig); + agent.shutdown(); } catch (Exception e) { e.printStackTrace(); Assert.fail(); } - agent.shutdown(); } /** diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java index c3e2a5ff42b..d4922dfc90d 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java @@ -24,6 +24,7 @@ import com.alibaba.nacos.api.config.PropertyChangeType; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -56,6 +57,14 @@ public void init() throws NacosException { configService = NacosFactory.createConfigService(properties); } + @After + public void destroy(){ + try { + NacosFactory.destroyConfigService(configService); + }catch (NacosException ex) { + } + } + @Test public void testAdd() throws InterruptedException, NacosException { CountDownLatch latch = new CountDownLatch(1); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java index 3ca5df344de..683e336fac2 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java @@ -23,6 +23,7 @@ import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.core.utils.ApplicationUtils; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -58,6 +59,14 @@ public void init() throws NacosException { configService = NacosFactory.createConfigService(properties); } + @After + public void destroy(){ + try { + NacosFactory.destroyConfigService(configService); + }catch (NacosException ex) { + } + } + @Test public void test() throws InterruptedException, NacosException { diff --git a/test/src/test/java/com/alibaba/nacos/test/core/BaseClusterTest.java b/test/src/test/java/com/alibaba/nacos/test/core/BaseClusterTest.java index 844f07f25a3..d0c509f4ef2 100644 --- a/test/src/test/java/com/alibaba/nacos/test/core/BaseClusterTest.java +++ b/test/src/test/java/com/alibaba/nacos/test/core/BaseClusterTest.java @@ -157,7 +157,15 @@ public static void after() throws Exception { try { System.out.println("start close : " + context); context.close(); - } catch (Exception ignore) { + NacosFactory.destroyConfigService(iconfig7); + NacosFactory.destroyConfigService(iconfig8); + NacosFactory.destroyConfigService(iconfig9); + + NacosFactory.destroyNamingService(inaming7); + NacosFactory.destroyNamingService(inaming8); + NacosFactory.destroyNamingService(inaming9); + + } catch (Exception ignore) { } finally { System.out.println("finished close : " + context); latch.countDown(); diff --git a/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java index 4b6f15ac6da..e9c181c5eee 100644 --- a/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java @@ -65,8 +65,13 @@ public void init() throws Exception { } @After - public void destroy() { + public void destroy(){ super.destroy(); + try { + NacosFactory.destroyConfigService(iconfig); + }catch (NacosException ex) { + + } } From 5e2840a6bd1a84ad9e954ac781b5accbef0a12c0 Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Wed, 3 Jun 2020 10:46:41 +0800 Subject: [PATCH 038/156] [#1815]adjust and optimize codes. --- .../client/config/NacosConfigService.java | 5 +-- .../client/config/http/ServerHttpAgent.java | 5 ++- .../client/config/impl/ClientWorker.java | 42 +++++++++++++------ .../client/config/impl/ServerListManager.java | 28 +++++++++---- .../client/naming/NacosNamingService.java | 6 +-- .../naming/backups/FailoverReactor.java | 5 ++- .../nacos/client/naming/beat/BeatReactor.java | 5 ++- .../client/naming/core/EventDispatcher.java | 5 ++- .../nacos/client/naming/core/HostReactor.java | 5 ++- .../client/naming/core/PushReceiver.java | 5 ++- .../nacos/client/naming/net/NamingProxy.java | 5 ++- .../lifecycle/ResourceLifeCycleManager.java | 42 +++++++++++-------- .../LifeCycle/LifeCycleManagerTest.java | 17 ++++---- .../nacos/test/config/ConfigAPI_ITCase.java | 3 ++ .../ConfigLongPollReturnChanges_ITCase.java | 4 +- .../test/config/ConfigLongPoll_ITCase.java | 3 ++ .../test/core/auth/ConfigAuth_ITCase.java | 2 + .../test/core/auth/NamingAuth_ITCase.java | 7 +++- 18 files changed, 122 insertions(+), 72 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java index 57c106ec59b..2118834f447 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java @@ -70,8 +70,6 @@ public class NacosConfigService implements ConfigService, LifeCycle { private String encode; private ConfigFilterChainManager configFilterChainManager = new ConfigFilterChainManager(); - private static final ResourceLifeCycleManager RESOURCE_MANAGER = ResourceLifeCycleManager.getInstance(); - public NacosConfigService(Properties properties) throws NacosException { ValidatorUtils.checkInitParam(properties); String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE); @@ -85,7 +83,7 @@ public NacosConfigService(Properties properties) throws NacosException { agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); agent.fetchServerIpList(); worker = new ClientWorker(agent, configFilterChainManager, properties); - RESOURCE_MANAGER.register(this); + ResourceLifeCycleManager.register(this); } private void initNamespace(Properties properties) { @@ -290,6 +288,7 @@ public String getServerStatus() { @Override public void shutDown() throws NacosException{ destroy(); + ResourceLifeCycleManager.deregister(this); } @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java index 76e9cfc9086..cf6c48d0a92 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java @@ -440,9 +440,10 @@ public String getEncode() { @Override public void shutdown() throws NacosException{ - LOGGER.info("do shutdown begin"); + String className = this.getClass().getName(); + LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdown(this.executorService); - LOGGER.info("do shutdown stop"); + LOGGER.info("{} do shutdown stop", className); } @SuppressWarnings("PMD.ClassNamingShouldBeCamelRule") diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index a5c98e9406b..4874cf87af6 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -26,6 +26,7 @@ import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.utils.ContentUtils; import com.alibaba.nacos.common.lifecycle.Closeable; +import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.naming.utils.CollectionUtils; @@ -279,7 +280,6 @@ private void checkLocalConfig(CacheData cacheData) { final String tenant = cacheData.tenant; File path = LocalConfigInfoProcessor.getFailoverFile(this.agent.getName(), dataId, group, tenant); - // 没有 -> 有 if (!cacheData.isUseLocalConfigInfo() && path.exists()) { String content = LocalConfigInfoProcessor.getFailover(this.agent.getName(), dataId, group, tenant); String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); @@ -292,7 +292,7 @@ private void checkLocalConfig(CacheData cacheData) { return; } - // 有 -> 没有。不通知业务监听器,从server拿到配置后通知。 + // If use local config info, then it doesn't notify business listener and notify after getting from server. if (cacheData.isUseLocalConfigInfo() && !path.exists()) { cacheData.setUseLocalConfigInfo(false); LOGGER.warn("[{}] [failover-change] failover file deleted. dataId={}, group={}, tenant={}", this.agent.getName(), @@ -300,7 +300,7 @@ private void checkLocalConfig(CacheData cacheData) { return; } - // 有变更 + // When it changed. if (cacheData.isUseLocalConfigInfo() && path.exists() && cacheData.getLocalConfigInfoVersion() != path.lastModified()) { String content = LocalConfigInfoProcessor.getFailover(this.agent.getName(), dataId, group, tenant); @@ -318,13 +318,13 @@ private String null2defaultGroup(String group) { } public void checkConfigInfo() { - // 分任务 + // Dispatch taskes. int listenerSize = this.cacheMap.get().size(); - // 向上取整为批数 + // Round up the longingTaskCount. int longingTaskCount = (int) Math.ceil(listenerSize / ParamUtil.getPerTaskConfigSize()); if (longingTaskCount > this.currentLongingTaskCount) { for (int i = (int) this.currentLongingTaskCount; i < longingTaskCount; i++) { - // 要判断任务是否在执行 这块需要好好想想。 任务列表现在是无序的。变化过程可能有问题 + // The task list is no order.So it maybe has issues when changing. this.executorService.execute(new LongPollingRunnable(i)); } this.currentLongingTaskCount = longingTaskCount; @@ -332,7 +332,13 @@ public void checkConfigInfo() { } /** - * 从Server获取值变化了的DataID列表。返回的对象里只有dataId和group是有效的。 保证不返回NULL。 + * Fetch the dataId list from server. + * + * @param cacheDatas CacheDatas for config infomations. + * @param inInitializingCacheList initial cache lists. + * @return String include dataId and group (ps: it maybe null). + * + * @throws IOException Exception. */ List checkUpdateDataIds(List cacheDatas, List inInitializingCacheList) throws IOException { StringBuilder sb = new StringBuilder(); @@ -347,7 +353,7 @@ List checkUpdateDataIds(List cacheDatas, List inIniti sb.append(cacheData.getTenant()).append(LINE_SEPARATOR); } if (cacheData.isInitializing()) { - // cacheData 首次出现在cacheMap中&首次check更新 + // It updates when cacheData occours in cacheMap by first time. inInitializingCacheList .add(GroupKey.getKeyTenant(cacheData.dataId, cacheData.group, cacheData.tenant)); } @@ -358,7 +364,13 @@ List checkUpdateDataIds(List cacheDatas, List inIniti } /** - * 从Server获取值变化了的DataID列表。返回的对象里只有dataId和group是有效的。 保证不返回NULL。 + * Fetch the updated dataId list from server. + * + * + * @param probeUpdateString updated attribute string value. + * @param isInitializingCacheList initial cache lists. + * @return The updated dataId list(ps: it maybe null). + * @throws IOException Exception. */ List checkUpdateConfigStr(String probeUpdateString, boolean isInitializingCacheList) throws IOException { @@ -405,7 +417,10 @@ List checkUpdateConfigStr(String probeUpdateString, boolean isInitializi } /** - * 从HTTP响应拿到变化的groupKey。保证不返回NULL。 + * Get the groupKey list from the http response. + * + * @param response Http response. + * @return GroupKey List, (ps: it maybe null). */ private List parseUpdateDataIdResponse(String response) { if (StringUtils.isBlank(response)) { @@ -484,7 +499,7 @@ public void run() { private void init(Properties properties) { - this.timeout = Math.max(NumberUtils.toInt(properties.getProperty(PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT), + this.timeout = Math.max(ConvertUtils.toInt(properties.getProperty(PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT), Constants.CONFIG_LONG_POLL_TIMEOUT), Constants.MIN_CONFIG_LONG_POLL_TIMEOUT); this.taskPenaltyTime = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.CONFIG_RETRY_TIME), Constants.CONFIG_RETRY_TIME); @@ -494,10 +509,11 @@ private void init(Properties properties) { @Override public void shutdown() throws NacosException { - LOGGER.info("do shutdown begin"); + String className = this.getClass().getName(); + LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdown(this.executorService); ThreadUtils.shutdown(this.executor); - LOGGER.info("do shutdown stop"); + LOGGER.info("{} do shutdown stop", className); } class LongPollingRunnable implements Runnable { diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java index e9a4771912f..050b3ba246e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java @@ -18,12 +18,12 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.SystemPropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.client.config.impl.EventDispatcher.ServerlistChangeEvent; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; -import com.alibaba.nacos.client.utils.*; -import com.alibaba.nacos.common.executor.ExecutorFactory; -import com.alibaba.nacos.common.executor.NameThreadFactory; +import com.alibaba.nacos.client.utils.EnvUtil; +import com.alibaba.nacos.client.utils.LogUtils; +import com.alibaba.nacos.client.utils.ParamUtil; +import com.alibaba.nacos.client.utils.TemplateUtils; import com.alibaba.nacos.common.lifecycle.Closeable; import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.StringUtils; @@ -33,9 +33,18 @@ import java.io.IOException; import java.io.StringReader; import java.net.HttpURLConnection; -import java.util.*; -import java.util.concurrent.*; - +import java.util.List; +import java.util.ArrayList; +import java.util.Properties; +import java.util.Iterator; +import java.util.Collections; +import java.util.Random; + +import java.util.concurrent.Callable; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; /** * Serverlist Manager @@ -271,9 +280,10 @@ Iterator iterator() { @Override public void shutdown() throws NacosException{ - LOGGER.info("do shutdown begin"); + String className = this.getClass().getName(); + LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdown(this.executorService); - LOGGER.info("do shutdown stop"); + LOGGER.info("{} do shutdown stop", className); } class GetServerListTask implements Runnable { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index 00cb30bde45..0984bb260db 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -75,9 +75,6 @@ public class NacosNamingService implements NamingService, LifeCycle { private NamingProxy serverProxy; - private static final ResourceLifeCycleManager RESOURCE_MANAGER = ResourceLifeCycleManager.getInstance(); - - public NacosNamingService(String serverList) { Properties properties = new Properties(); properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList); @@ -101,7 +98,7 @@ private void init(Properties properties) { beatReactor = new BeatReactor(serverProxy, initClientBeatThreadCount(properties)); hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, isLoadCacheAtStart(properties), initPollingThreadCount(properties)); - RESOURCE_MANAGER.register(this); + ResourceLifeCycleManager.register(this); } private int initClientBeatThreadCount(Properties properties) { @@ -496,6 +493,7 @@ public BeatReactor getBeatReactor() { @Override public void shutDown() throws NacosException{ destroy(); + ResourceLifeCycleManager.deregister(this); } @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 81cdb4b8f92..90ab5705489 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -106,9 +106,10 @@ public Date addDay(Date date, int num) { @Override public void shutdown() throws NacosException { - NAMING_LOGGER.info("do shutdown begin"); + String className = this.getClass().getName(); + NAMING_LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdown(this.executorService); - NAMING_LOGGER.info("do shutdown stop"); + NAMING_LOGGER.info("{} do shutdown stop", className); } class SwitchRefresher implements Runnable { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java index 3056d6bb4c1..deb765c5059 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java @@ -94,9 +94,10 @@ private String buildKey(String serviceName, String ip, int port) { @Override public void shutdown() throws NacosException{ - NAMING_LOGGER.info("do shutdown begin"); + String className = this.getClass().getName(); + NAMING_LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdown(this.executorService); - NAMING_LOGGER.info("do shutdown stop"); + NAMING_LOGGER.info("{} do shutdown stop", className); } class BeatTask implements Runnable { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java index c2de028fbc7..82f43825dc3 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java @@ -114,9 +114,10 @@ public void serviceChanged(ServiceInfo serviceInfo) { @Override public void shutdown() throws NacosException { - NAMING_LOGGER.info("do shutdown begin"); + String className = this.getClass().getName(); + NAMING_LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdown(this.executor); - NAMING_LOGGER.info("do shutdown stop"); + NAMING_LOGGER.info("{} do shutdown stop", className); } private class Notifier implements Runnable { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java index 9996a0c29b4..f98649fa670 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java @@ -307,9 +307,10 @@ public void refreshOnly(String serviceName, String clusters) { @Override public void shutdown() throws NacosException{ - NAMING_LOGGER.info("do shutdown begin"); + String className = this.getClass().getName(); + NAMING_LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdown(this.executor); - NAMING_LOGGER.info("do shutdown stop"); + NAMING_LOGGER.info("{} do shutdown stop", className); } public class UpdateTask implements Runnable { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java index aacd27717f5..0aaf904cc9f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java @@ -111,9 +111,10 @@ public void run() { @Override public void shutdown() throws NacosException { - NAMING_LOGGER.info("do shutdown begin"); + String className = this.getClass().getName(); + NAMING_LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdown(this.executorService); - NAMING_LOGGER.info("do shutdown stop"); + NAMING_LOGGER.info("{} do shutdown stop", className); } public static class PushPacket { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index 2bf27d22597..c5d1bbb5a3b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -584,9 +584,10 @@ public void setServerPort(int serverPort) { @Override public void shutdown() throws NacosException{ - NAMING_LOGGER.info("do shutdown begin"); + String className = this.getClass().getName(); + NAMING_LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdown(this.executorService); - NAMING_LOGGER.info("do shutdown stop"); + NAMING_LOGGER.info("{} do shutdown stop", className); } } diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java index 4a84202eaa0..9d371b513e4 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java @@ -42,14 +42,15 @@ public final class ResourceLifeCycleManager { /** * Map */ - private Map lockers = new ConcurrentHashMap(8); + private Map lockers; private static final ResourceLifeCycleManager INSTANCE = new ResourceLifeCycleManager(); private static final AtomicBoolean CLOSED = new AtomicBoolean(false); static { - INSTANCE.init(); + INSTANCE.lifeCycleResources = new CopyOnWriteArrayList(); + INSTANCE.lockers = new ConcurrentHashMap(8); ShutdownUtils.addShutdownHook(new Thread(new Runnable() { @Override public void run() { @@ -60,10 +61,6 @@ public void run() { })); } - private void init() { - this.lifeCycleResources = new CopyOnWriteArrayList(); - } - public static ResourceLifeCycleManager getInstance() { return INSTANCE; } @@ -120,13 +117,13 @@ public void destroy(LifeCycle instance) { * @param instance the management life cycle resource instances; * */ - public void deregister(LifeCycle instance) { - if (this.lifeCycleResources.contains(instance)) { - final Object monitor = this.lockers.get(instance); + public static void deregister(LifeCycle instance) { + if (INSTANCE.lifeCycleResources.contains(instance)) { + final Object monitor = INSTANCE.lockers.get(instance); synchronized (monitor) { - if (this.lockers.containsKey(instance)) { - this.lockers.remove(instance); - this.lifeCycleResources.remove(instance); + if (INSTANCE.lockers.containsKey(instance)) { + INSTANCE.lockers.remove(instance); + INSTANCE.lifeCycleResources.remove(instance); } } } @@ -137,14 +134,23 @@ public void deregister(LifeCycle instance) { * * @param instance the management life cycle resource instances. */ - public void register(LifeCycle instance) { - if (!this.lifeCycleResources.contains(instance)) { - synchronized(this) { - if (!this.lockers.containsKey(instance)) { - this.lockers.put(instance, new Object()); - this.lifeCycleResources.add(instance); + public static void register(LifeCycle instance) { + if (!INSTANCE.lifeCycleResources.contains(instance)) { + synchronized(INSTANCE) { + if (!INSTANCE.lockers.containsKey(instance)) { + INSTANCE.lockers.put(instance, new Object()); + INSTANCE.lifeCycleResources.add(instance); } } } } + + /** + * Get the life cycle resource number which registered into the Resource Life Cycle Manager. + * + * @return + */ + public static int getRegisterResourceNum() { + return INSTANCE.lifeCycleResources.size(); + } } diff --git a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java index 2494371b8fc..123830dceb6 100644 --- a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java @@ -31,26 +31,25 @@ public class LifeCycleManagerTest { private Resource res; - private static final ResourceLifeCycleManager RESOURCE_MANAGER = ResourceLifeCycleManager.getInstance(); - @Before public void setup() throws Exception{ this.res = new Resource(0); - RESOURCE_MANAGER.register(this.res); + ResourceLifeCycleManager.register(this.res); this.res.increament(); Assert.assertEquals(this.res.getCounter(), 1); } @After public void cleanup() throws Exception{ - RESOURCE_MANAGER.shutdown(); + ResourceLifeCycleManager.shutdown(); Assert.assertEquals(this.res.getCounter(), 0); // here, double check shutdown called by two times whether the result is ok. - RESOURCE_MANAGER.shutdown(); + ResourceLifeCycleManager.shutdown(); Assert.assertEquals(this.res.getCounter(), 0); // here, check whether the buffer data in resource manager is correct. - RESOURCE_MANAGER.destroy(this.res); + ResourceLifeCycleManager.getInstance().destroy(this.res); Assert.assertEquals(this.res.getCounter(), 0); + Assert.assertEquals(ResourceLifeCycleManager.getRegisterResourceNum(), 0); } @Test @@ -65,10 +64,10 @@ public void testLifeCycleManager() throws Exception{ public void testLifeCycleManager_deregister() throws Exception{ Resource temp = new Resource(0); - RESOURCE_MANAGER.register(temp); + ResourceLifeCycleManager.register(temp); temp.increament(); - RESOURCE_MANAGER.deregister(temp); - RESOURCE_MANAGER.destroy(temp); + ResourceLifeCycleManager.deregister(temp); + ResourceLifeCycleManager.getInstance().destroy(temp); Assert.assertEquals(temp.getCounter(), 1); temp.destroy(); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java index fe6d6971de2..a970ca698a2 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java @@ -28,6 +28,7 @@ import com.alibaba.nacos.client.config.http.MetricsHttpAgent; import com.alibaba.nacos.client.config.http.ServerHttpAgent; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; +import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import com.alibaba.nacos.common.utils.ThreadUtils; import org.junit.After; import org.junit.Assert; @@ -86,6 +87,8 @@ public void cleanup() throws Exception { Assert.assertEquals(true, JSON.parseObject(result.content).getBoolean("data")); NacosFactory.destroyConfigService(iconfig); agent.shutdown(); + // Judge whether the register life cycle resource number equals to zero or not. + Assert.assertEquals(ResourceLifeCycleManager.getRegisterResourceNum(), 0); } catch (Exception e) { e.printStackTrace(); Assert.fail(); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java index d4922dfc90d..cb1c6bbee3c 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java @@ -24,6 +24,7 @@ import com.alibaba.nacos.api.config.PropertyChangeType; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener; +import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -39,7 +40,7 @@ @RunWith(SpringRunner.class) @SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) + webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) public class ConfigLongPollReturnChanges_ITCase { @LocalServerPort @@ -61,6 +62,7 @@ public void init() throws NacosException { public void destroy(){ try { NacosFactory.destroyConfigService(configService); + Assert.assertEquals(ResourceLifeCycleManager.getRegisterResourceNum(), 0); }catch (NacosException ex) { } } diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java index 683e336fac2..dd6df3e330d 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java @@ -22,8 +22,10 @@ import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import com.alibaba.nacos.core.utils.ApplicationUtils; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -63,6 +65,7 @@ public void init() throws NacosException { public void destroy(){ try { NacosFactory.destroyConfigService(configService); + Assert.assertEquals(ResourceLifeCycleManager.getRegisterResourceNum(), 0); }catch (NacosException ex) { } } diff --git a/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java index e9c181c5eee..3894390b48a 100644 --- a/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java @@ -23,6 +23,7 @@ import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener; +import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import com.alibaba.nacos.core.utils.ApplicationUtils; import org.apache.http.HttpStatus; import org.junit.After; @@ -69,6 +70,7 @@ public void destroy(){ super.destroy(); try { NacosFactory.destroyConfigService(iconfig); + Assert.assertEquals(ResourceLifeCycleManager.getRegisterResourceNum(), 0); }catch (NacosException ex) { } diff --git a/test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuth_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuth_ITCase.java index cb01f283080..ef616fb74fa 100644 --- a/test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuth_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuth_ITCase.java @@ -81,6 +81,7 @@ public void writeWithReadPermission() throws Exception { } catch (NacosException ne) { Assert.assertEquals(HttpStatus.SC_FORBIDDEN, ne.getErrCode()); } + NacosFactory.destroyNamingService(namingService); } @Test @@ -97,6 +98,8 @@ public void readWithReadPermission() throws Exception { List list = namingService.getAllInstances("test.1"); Assert.assertEquals(1, list.size()); + NacosFactory.destroyNamingService(namingService1); + NacosFactory.destroyNamingService(namingService); } @Test @@ -111,6 +114,7 @@ public void writeWithWritePermission() throws Exception { TimeUnit.SECONDS.sleep(5L); namingService.deregisterInstance("test.1", "1.2.3.4", 80); + NacosFactory.destroyNamingService(namingService); } @Test @@ -126,7 +130,7 @@ public void readWithWritePermission() throws Exception { List list = namingService.getAllInstances("test.1"); Assert.assertEquals(0, list.size()); - + NacosFactory.destroyNamingService(namingService); } @Test @@ -142,6 +146,7 @@ public void readWriteWithFullPermission() throws Exception { List list = namingService.getAllInstances("test.1"); Assert.assertEquals(1, list.size()); + NacosFactory.destroyNamingService(namingService); } } From dc0c5375a822b699c29f3599670e7f4323e935a1 Mon Sep 17 00:00:00 2001 From: wangwei Date: Wed, 3 Jun 2020 11:25:41 +0800 Subject: [PATCH 039/156] Merge remote-tracking branch 'alibaba/develop' into issue_2749 --- .../nacos/config/server/service/dump/DumpService.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java index 4a6f5a1b603..5a1d8b573d5 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java @@ -131,7 +131,7 @@ protected void dumpOperate(DumpProcessor processor, DumpAllProcessor dumpAllProc .addTask(DumpAllBetaTask.TASK_ID, new DumpAllBetaTask()); Runnable dumpAllTag = () -> dumpAllTaskMgr - .addTask(DumpAllTagTask.TASK_ID, new DumpAllTagTask()); + .addTask(DumpAllTagTask.TASK_ID, new DumpAllTagTask()); Runnable clearConfigHistory = () -> { log.warn("clearConfigHistory start"); @@ -221,10 +221,10 @@ protected void dumpOperate(DumpProcessor processor, DumpAllProcessor dumpAllProc DUMP_ALL_INTERVAL_IN_MINUTE, TimeUnit.MINUTES); ConfigExecutor.scheduleWithFixedDelay(dumpAllBeta, initialDelay, - DUMP_ALL_INTERVAL_IN_MINUTE, TimeUnit.MINUTES); + DUMP_ALL_INTERVAL_IN_MINUTE, TimeUnit.MINUTES); ConfigExecutor.scheduleWithFixedDelay(dumpAllTag, initialDelay, - DUMP_ALL_INTERVAL_IN_MINUTE, TimeUnit.MINUTES); + DUMP_ALL_INTERVAL_IN_MINUTE, TimeUnit.MINUTES); } ConfigExecutor From 09752e354dca2d59448e89ac2e0c6750af8931af Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Wed, 3 Jun 2020 17:35:06 +0800 Subject: [PATCH 040/156] [#1815]adjust this keywords which just includes constructor and set method in the class and fix some unit test cases. --- .../client/config/NacosConfigService.java | 14 +-- .../client/config/http/MetricsHttpAgent.java | 18 +-- .../client/config/http/ServerHttpAgent.java | 96 ++++++++-------- .../client/config/impl/ClientWorker.java | 104 +++++++++--------- .../client/config/impl/ServerListManager.java | 81 +++++++------- .../client/naming/NacosNamingService.java | 18 +-- .../naming/backups/FailoverReactor.java | 2 +- .../nacos/client/naming/beat/BeatReactor.java | 38 +++---- .../client/naming/core/EventDispatcher.java | 6 +- .../nacos/client/naming/core/HostReactor.java | 2 +- .../client/naming/core/PushReceiver.java | 10 +- .../nacos/client/naming/net/NamingProxy.java | 5 +- .../nacos/common/lifecycle/Closeable.java | 15 +++ .../LifeCycle/LifeCycleManagerTest.java | 22 ++-- .../nacos/test/config/ConfigAPI_ITCase.java | 2 +- .../ConfigLongPollReturnChanges_ITCase.java | 2 +- .../test/config/ConfigLongPoll_ITCase.java | 2 +- .../test/core/auth/ConfigAuth_ITCase.java | 2 +- 18 files changed, 223 insertions(+), 216 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java index 2118834f447..f7d94b3508e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java @@ -74,15 +74,15 @@ public NacosConfigService(Properties properties) throws NacosException { ValidatorUtils.checkInitParam(properties); String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE); if (StringUtils.isBlank(encodeTmp)) { - encode = Constants.ENCODE; + this.encode = Constants.ENCODE; } else { - encode = encodeTmp.trim(); + this.encode = encodeTmp.trim(); } initNamespace(properties); - agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); - agent.fetchServerIpList(); - worker = new ClientWorker(agent, configFilterChainManager, properties); + this.agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); + this.agent.fetchServerIpList(); + this.worker = new ClientWorker(this.agent, this.configFilterChainManager, properties); ResourceLifeCycleManager.register(this); } @@ -293,7 +293,7 @@ public void shutDown() throws NacosException{ @Override public void destroy() throws NacosException{ - this.agent.shutdown(); - this.worker.shutdown(); + agent.shutdown(); + worker.shutdown(); } } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java index 8551665a5d0..9b9ae1e4d26 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java @@ -43,7 +43,7 @@ public MetricsHttpAgent(HttpAgent httpAgent) { @Override public void fetchServerIpList() throws NacosException { - this.httpAgent.fetchServerIpList(); + httpAgent.fetchServerIpList(); } @Override @@ -51,7 +51,7 @@ public HttpResult httpGet(String path, List headers, List paramV Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("GET", path, "NA"); HttpResult result; try { - result = this.httpAgent.httpGet(path, headers, paramValues, encoding, readTimeoutMs); + result = httpAgent.httpGet(path, headers, paramValues, encoding, readTimeoutMs); } catch (IOException e) { throw e; } finally { @@ -67,7 +67,7 @@ public HttpResult httpPost(String path, List headers, List param Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("POST", path, "NA"); HttpResult result; try { - result = this.httpAgent.httpPost(path, headers, paramValues, encoding, readTimeoutMs); + result = httpAgent.httpPost(path, headers, paramValues, encoding, readTimeoutMs); } catch (IOException e) { throw e; } finally { @@ -83,7 +83,7 @@ public HttpResult httpDelete(String path, List headers, List par Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("DELETE", path, "NA"); HttpResult result; try { - result = this.httpAgent.httpDelete(path, headers, paramValues, encoding, readTimeoutMs); + result = httpAgent.httpDelete(path, headers, paramValues, encoding, readTimeoutMs); } catch (IOException e) { throw e; @@ -97,27 +97,27 @@ public HttpResult httpDelete(String path, List headers, List par @Override public String getName() { - return this.httpAgent.getName(); + return httpAgent.getName(); } @Override public String getNamespace() { - return this.httpAgent.getNamespace(); + return httpAgent.getNamespace(); } @Override public String getTenant() { - return this.httpAgent.getTenant(); + return httpAgent.getTenant(); } @Override public String getEncode() { - return this.httpAgent.getEncode(); + return httpAgent.getEncode(); } @Override public void shutdown() throws NacosException{ - this.httpAgent.shutdown(); + httpAgent.shutdown(); } } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java index 966dd050e58..45e0b436fd1 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java @@ -83,7 +83,7 @@ public HttpResult httpGet(String path, List headers, List paramV final long endTime = System.currentTimeMillis() + readTimeoutMs; final boolean isSSL = false; injectSecurityInfo(paramValues); - String currentServerAddr = this.serverListMgr.getCurrentServerAddr(); + String currentServerAddr = serverListMgr.getCurrentServerAddr(); int maxRetry = this.maxRetry; do { @@ -99,10 +99,10 @@ public HttpResult httpGet(String path, List headers, List paramV || result.code == HttpURLConnection.HTTP_BAD_GATEWAY || result.code == HttpURLConnection.HTTP_UNAVAILABLE) { LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", - this.serverListMgr.getCurrentServerAddr(), result.code); + serverListMgr.getCurrentServerAddr(), result.code); } else { // Update the currently available server addr - this.serverListMgr.updateCurrentServerAddr(currentServerAddr); + serverListMgr.updateCurrentServerAddr(currentServerAddr); return result; } } catch (ConnectException ce) { @@ -114,14 +114,14 @@ public HttpResult httpGet(String path, List headers, List paramV throw ioe; } - if (this.serverListMgr.getIterator().hasNext()) { - currentServerAddr = this.serverListMgr.getIterator().next(); + if (serverListMgr.getIterator().hasNext()) { + currentServerAddr = serverListMgr.getIterator().next(); } else { maxRetry--; if (maxRetry < 0) { throw new ConnectException("[NACOS HTTP-GET] The maximum number of tolerable server reconnection errors has been reached"); } - this.serverListMgr.refreshCurrentServerAddr(); + serverListMgr.refreshCurrentServerAddr(); } } while (System.currentTimeMillis() <= endTime); @@ -136,7 +136,7 @@ public HttpResult httpPost(String path, List headers, List param final long endTime = System.currentTimeMillis() + readTimeoutMs; boolean isSSL = false; injectSecurityInfo(paramValues); - String currentServerAddr = this.serverListMgr.getCurrentServerAddr(); + String currentServerAddr = serverListMgr.getCurrentServerAddr(); int maxRetry = this.maxRetry; do { @@ -157,7 +157,7 @@ public HttpResult httpPost(String path, List headers, List param currentServerAddr, result.code); } else { // Update the currently available server addr - this.serverListMgr.updateCurrentServerAddr(currentServerAddr); + serverListMgr.updateCurrentServerAddr(currentServerAddr); return result; } } catch (ConnectException ce) { @@ -169,14 +169,14 @@ public HttpResult httpPost(String path, List headers, List param throw ioe; } - if (this.serverListMgr.getIterator().hasNext()) { - currentServerAddr = this.serverListMgr.getIterator().next(); + if (serverListMgr.getIterator().hasNext()) { + currentServerAddr = serverListMgr.getIterator().next(); } else { maxRetry--; if (maxRetry < 0) { throw new ConnectException("[NACOS HTTP-POST] The maximum number of tolerable server reconnection errors has been reached"); } - this.serverListMgr.refreshCurrentServerAddr(); + serverListMgr.refreshCurrentServerAddr(); } } while (System.currentTimeMillis() <= endTime); @@ -191,7 +191,7 @@ public HttpResult httpDelete(String path, List headers, List par final long endTime = System.currentTimeMillis() + readTimeoutMs; boolean isSSL = false; injectSecurityInfo(paramValues); - String currentServerAddr = this.serverListMgr.getCurrentServerAddr(); + String currentServerAddr = serverListMgr.getCurrentServerAddr(); int maxRetry = this.maxRetry; do { @@ -207,10 +207,10 @@ public HttpResult httpDelete(String path, List headers, List par || result.code == HttpURLConnection.HTTP_BAD_GATEWAY || result.code == HttpURLConnection.HTTP_UNAVAILABLE) { LOGGER.error("[NACOS ConnectException] currentServerAddr: {}, httpCode: {}", - this.serverListMgr.getCurrentServerAddr(), result.code); + serverListMgr.getCurrentServerAddr(), result.code); } else { // Update the currently available server addr - this.serverListMgr.updateCurrentServerAddr(currentServerAddr); + serverListMgr.updateCurrentServerAddr(currentServerAddr); return result; } } catch (ConnectException ce) { @@ -224,14 +224,14 @@ public HttpResult httpDelete(String path, List headers, List par throw ioe; } - if (this.serverListMgr.getIterator().hasNext()) { - currentServerAddr = this.serverListMgr.getIterator().next(); + if (serverListMgr.getIterator().hasNext()) { + currentServerAddr = serverListMgr.getIterator().next(); } else { maxRetry--; if (maxRetry < 0) { throw new ConnectException("[NACOS HTTP-DELETE] The maximum number of tolerable server reconnection errors has been reached"); } - this.serverListMgr.refreshCurrentServerAddr(); + serverListMgr.refreshCurrentServerAddr(); } } while (System.currentTimeMillis() <= endTime); @@ -241,8 +241,8 @@ public HttpResult httpDelete(String path, List headers, List par } private String getUrl(String serverAddr, String relativePath) { - String contextPath = this.serverListMgr.getContentPath().startsWith("/") ? - this.serverListMgr.getContentPath() : "/" + this.serverListMgr.getContentPath(); + String contextPath = serverListMgr.getContentPath().startsWith("/") ? + serverListMgr.getContentPath() : "/" + serverListMgr.getContentPath(); return serverAddr + contextPath + relativePath; } @@ -283,7 +283,7 @@ public Thread newThread(Runnable r) { public void run() { securityProxy.login(serverListMgr.getServerUrls()); } - }, 0, securityInfoRefreshIntervalMills, TimeUnit.MILLISECONDS); + }, 0, this.securityInfoRefreshIntervalMills, TimeUnit.MILLISECONDS); } @@ -321,27 +321,27 @@ private void initAkSk(Properties properties) { String ak = properties.getProperty(PropertyKeyConst.ACCESS_KEY); if (StringUtils.isBlank(ak)) { - this.accessKey = SpasAdapter.getAk(); + accessKey = SpasAdapter.getAk(); } else { - this.accessKey = ak; + accessKey = ak; } String sk = properties.getProperty(PropertyKeyConst.SECRET_KEY); if (StringUtils.isBlank(sk)) { - this.secretKey = SpasAdapter.getSk(); + secretKey = SpasAdapter.getSk(); } else { - this.secretKey = sk; + secretKey = sk; } } private void initMaxRetry(Properties properties) { - this.maxRetry = NumberUtils.toInt(String.valueOf(properties.get(PropertyKeyConst.MAX_RETRY)), Constants.MAX_RETRY); + maxRetry = NumberUtils.toInt(String.valueOf(properties.get(PropertyKeyConst.MAX_RETRY)), Constants.MAX_RETRY); } @Override public void fetchServerIpList() throws NacosException { // fetch server address urls list - this.serverListMgr.start(); + serverListMgr.start(); } private List getSpasHeaders(List paramValues) throws IOException { @@ -349,15 +349,15 @@ private List getSpasHeaders(List paramValues) throws IOException // STS 临时凭证鉴权的优先级高于 AK/SK 鉴权 if (STSConfig.getInstance().isSTSOn()) { STSCredential sTSCredential = getSTSCredential(); - this.accessKey = sTSCredential.accessKeyId; - this.secretKey = sTSCredential.accessKeySecret; + accessKey = sTSCredential.accessKeyId; + secretKey = sTSCredential.accessKeySecret; newHeaders.add("Spas-SecurityToken"); newHeaders.add(sTSCredential.securityToken); } - if (StringUtils.isNotEmpty(this.accessKey) && StringUtils.isNotEmpty(secretKey)) { + if (StringUtils.isNotEmpty(accessKey) && StringUtils.isNotEmpty(secretKey)) { newHeaders.add("Spas-AccessKey"); - newHeaders.add(this.accessKey); + newHeaders.add(accessKey); List signHeaders = SpasAdapter.getSignHeaders(paramValues, secretKey); if (signHeaders != null) { newHeaders.addAll(signHeaders); @@ -368,21 +368,21 @@ private List getSpasHeaders(List paramValues) throws IOException private STSCredential getSTSCredential() throws IOException { boolean cacheSecurityCredentials = STSConfig.getInstance().isCacheSecurityCredentials(); - if (cacheSecurityCredentials && this.sTSCredential != null) { + if (cacheSecurityCredentials && sTSCredential != null) { long currentTime = System.currentTimeMillis(); - long expirationTime = this.sTSCredential.expiration.getTime(); + long expirationTime = sTSCredential.expiration.getTime(); int timeToRefreshInMillisecond = STSConfig.getInstance().getTimeToRefreshInMillisecond(); if (expirationTime - currentTime > timeToRefreshInMillisecond) { - return this.sTSCredential; + return sTSCredential; } } String stsResponse = getSTSResponse(); STSCredential stsCredentialTmp = JacksonUtils.toObj(stsResponse, new TypeReference() { }); - this.sTSCredential = stsCredentialTmp; - LOGGER.info("[getSTSCredential] code:{}, accessKeyId:{}, lastUpdated:{}, expiration:{}", this.sTSCredential.getCode(), - this.sTSCredential.getAccessKeyId(), this.sTSCredential.getLastUpdated(), this.sTSCredential.getExpiration()); - return this.sTSCredential; + sTSCredential = stsCredentialTmp; + LOGGER.info("[getSTSCredential] code:{}, accessKeyId:{}, lastUpdated:{}, expiration:{}", sTSCredential.getCode(), + sTSCredential.getAccessKeyId(), sTSCredential.getLastUpdated(), sTSCredential.getExpiration()); + return sTSCredential; } private static String getSTSResponse() throws IOException { @@ -423,29 +423,29 @@ private static String getSTSResponse() throws IOException { @Override public String getName() { - return this.serverListMgr.getName(); + return serverListMgr.getName(); } @Override public String getNamespace() { - return this.serverListMgr.getNamespace(); + return serverListMgr.getNamespace(); } @Override public String getTenant() { - return this.serverListMgr.getTenant(); + return serverListMgr.getTenant(); } @Override public String getEncode() { - return this.encode; + return encode; } @Override public void shutdown() throws NacosException{ String className = this.getClass().getName(); LOGGER.info("{} do shutdown begin", className); - ThreadUtils.shutdownThreadPool(this.executorService, LOGGER); + ThreadUtils.shutdownThreadPool(executorService, LOGGER); LOGGER.info("{} do shutdown stop", className); } @@ -483,12 +483,12 @@ public String getCode() { @Override public String toString() { return "STSCredential{" + - "accessKeyId='" + this.accessKeyId + '\'' + - ", accessKeySecret='" + this.accessKeySecret + '\'' + - ", expiration=" + this.expiration + - ", securityToken='" + this.securityToken + '\'' + - ", lastUpdated=" + this.lastUpdated + - ", code='" + this.code + '\'' + + "accessKeyId='" + accessKeyId + '\'' + + ", accessKeySecret='" + accessKeySecret + '\'' + + ", expiration=" + expiration + + ", securityToken='" + securityToken + '\'' + + ", lastUpdated=" + lastUpdated + + ", code='" + code + '\'' + '}'; } } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index 606a90e63ef..4c8760117e5 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -114,26 +114,26 @@ public void removeTenantListener(String dataId, String group, Listener listener) void removeCache(String dataId, String group) { String groupKey = GroupKey.getKey(dataId, group); - synchronized (this.cacheMap) { - Map copy = new HashMap(this.cacheMap.get()); + synchronized (cacheMap) { + Map copy = new HashMap(cacheMap.get()); copy.remove(groupKey); - this.cacheMap.set(copy); + cacheMap.set(copy); } LOGGER.info("[{}] [unsubscribe] {}", this.agent.getName(), groupKey); - MetricsMonitor.getListenConfigCountMonitor().set(this.cacheMap.get().size()); + MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size()); } void removeCache(String dataId, String group, String tenant) { String groupKey = GroupKey.getKeyTenant(dataId, group, tenant); - synchronized (this.cacheMap) { - Map copy = new HashMap(this.cacheMap.get()); + synchronized (cacheMap) { + Map copy = new HashMap(cacheMap.get()); copy.remove(groupKey); - this.cacheMap.set(copy); + cacheMap.set(copy); } - LOGGER.info("[{}] [unsubscribe] {}", this.agent.getName(), groupKey); + LOGGER.info("[{}] [unsubscribe] {}", agent.getName(), groupKey); - MetricsMonitor.getListenConfigCountMonitor().set(this.cacheMap.get().size()); + MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size()); } public CacheData addCacheDataIfAbsent(String dataId, String group) { @@ -143,9 +143,9 @@ public CacheData addCacheDataIfAbsent(String dataId, String group) { } String key = GroupKey.getKey(dataId, group); - cache = new CacheData(this.configFilterChainManager, this.agent.getName(), dataId, group); + cache = new CacheData(configFilterChainManager, agent.getName(), dataId, group); - synchronized (this.cacheMap) { + synchronized (cacheMap) { CacheData cacheFromMap = getCache(dataId, group); // multiple listeners on the same dataid+group and race condition,so double check again //other listener thread beat me to set to cacheMap @@ -154,18 +154,18 @@ public CacheData addCacheDataIfAbsent(String dataId, String group) { //reset so that server not hang this check cache.setInitializing(true); } else { - int taskId = this.cacheMap.get().size() / (int) ParamUtil.getPerTaskConfigSize(); + int taskId = cacheMap.get().size() / (int) ParamUtil.getPerTaskConfigSize(); cache.setTaskId(taskId); } - Map copy = new HashMap(this.cacheMap.get()); + Map copy = new HashMap(cacheMap.get()); copy.put(key, cache); - this.cacheMap.set(copy); + cacheMap.set(copy); } LOGGER.info("[{}] [subscribe] {}", this.agent.getName(), key); - MetricsMonitor.getListenConfigCountMonitor().set(this.cacheMap.get().size()); + MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size()); return cache; } @@ -176,7 +176,7 @@ public CacheData addCacheDataIfAbsent(String dataId, String group, String tenant return cache; } String key = GroupKey.getKeyTenant(dataId, group, tenant); - synchronized (this.cacheMap) { + synchronized (cacheMap) { CacheData cacheFromMap = getCache(dataId, group, tenant); // multiple listeners on the same dataid+group and race condition,so // double check again @@ -186,9 +186,9 @@ public CacheData addCacheDataIfAbsent(String dataId, String group, String tenant // reset so that server not hang this check cache.setInitializing(true); } else { - cache = new CacheData(this.configFilterChainManager, this.agent.getName(), dataId, group, tenant); + cache = new CacheData(configFilterChainManager, agent.getName(), dataId, group, tenant); // fix issue # 1317 - if (this.enableRemoteSyncConfig) { + if (enableRemoteSyncConfig) { String[] ct = getServerConfig(dataId, group, tenant, 3000L); cache.setContent(ct[0]); } @@ -196,11 +196,11 @@ public CacheData addCacheDataIfAbsent(String dataId, String group, String tenant Map copy = new HashMap(this.cacheMap.get()); copy.put(key, cache); - this.cacheMap.set(copy); + cacheMap.set(copy); } - LOGGER.info("[{}] [subscribe] {}", this.agent.getName(), key); + LOGGER.info("[{}] [subscribe] {}", agent.getName(), key); - MetricsMonitor.getListenConfigCountMonitor().set(this.cacheMap.get().size()); + MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size()); return cache; } @@ -231,10 +231,10 @@ public String[] getServerConfig(String dataId, String group, String tenant, long } else { params = new ArrayList(Arrays.asList("dataId", dataId, "group", group, "tenant", tenant)); } - result = this.agent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, params, this.agent.getEncode(), readTimeout); + result = agent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, params, agent.getEncode(), readTimeout); } catch (IOException e) { String message = String.format( - "[%s] [sub-server] get server config exception, dataId=%s, group=%s, tenant=%s", this.agent.getName(), + "[%s] [sub-server] get server config exception, dataId=%s, group=%s, tenant=%s", agent.getName(), dataId, group, tenant); LOGGER.error(message, e); throw new NacosException(NacosException.SERVER_ERROR, e); @@ -242,7 +242,7 @@ public String[] getServerConfig(String dataId, String group, String tenant, long switch (result.code) { case HttpURLConnection.HTTP_OK: - LocalConfigInfoProcessor.saveSnapshot(this.agent.getName(), dataId, group, tenant, result.content); + LocalConfigInfoProcessor.saveSnapshot(agent.getName(), dataId, group, tenant, result.content); ct[0] = result.content; if (result.headers.containsKey(CONFIG_TYPE)) { ct[1] = result.headers.get(CONFIG_TYPE).get(0); @@ -251,22 +251,22 @@ public String[] getServerConfig(String dataId, String group, String tenant, long } return ct; case HttpURLConnection.HTTP_NOT_FOUND: - LocalConfigInfoProcessor.saveSnapshot(this.agent.getName(), dataId, group, tenant, null); + LocalConfigInfoProcessor.saveSnapshot(agent.getName(), dataId, group, tenant, null); return ct; case HttpURLConnection.HTTP_CONFLICT: { LOGGER.error( "[{}] [sub-server-error] get server config being modified concurrently, dataId={}, group={}, " - + "tenant={}", this.agent.getName(), dataId, group, tenant); + + "tenant={}", agent.getName(), dataId, group, tenant); throw new NacosException(NacosException.CONFLICT, "data being modified, dataId=" + dataId + ",group=" + group + ",tenant=" + tenant); } case HttpURLConnection.HTTP_FORBIDDEN: { - LOGGER.error("[{}] [sub-server-error] no right, dataId={}, group={}, tenant={}", this.agent.getName(), dataId, + LOGGER.error("[{}] [sub-server-error] no right, dataId={}, group={}, tenant={}", agent.getName(), dataId, group, tenant); throw new NacosException(result.code, result.content); } default: { - LOGGER.error("[{}] [sub-server-error] dataId={}, group={}, tenant={}, code={}", this.agent.getName(), dataId, + LOGGER.error("[{}] [sub-server-error] dataId={}, group={}, tenant={}, code={}", agent.getName(), dataId, group, tenant, result.code); throw new NacosException(result.code, "http error, code=" + result.code + ",dataId=" + dataId + ",group=" + group + ",tenant=" + tenant); @@ -278,24 +278,24 @@ private void checkLocalConfig(CacheData cacheData) { final String dataId = cacheData.dataId; final String group = cacheData.group; final String tenant = cacheData.tenant; - File path = LocalConfigInfoProcessor.getFailoverFile(this.agent.getName(), dataId, group, tenant); + File path = LocalConfigInfoProcessor.getFailoverFile(agent.getName(), dataId, group, tenant); if (!cacheData.isUseLocalConfigInfo() && path.exists()) { - String content = LocalConfigInfoProcessor.getFailover(this.agent.getName(), dataId, group, tenant); + String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant); String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); cacheData.setUseLocalConfigInfo(true); cacheData.setLocalConfigInfoVersion(path.lastModified()); cacheData.setContent(content); LOGGER.warn("[{}] [failover-change] failover file created. dataId={}, group={}, tenant={}, md5={}, content={}", - this.agent.getName(), dataId, group, tenant, md5, ContentUtils.truncateContent(content)); + agent.getName(), dataId, group, tenant, md5, ContentUtils.truncateContent(content)); return; } // If use local config info, then it doesn't notify business listener and notify after getting from server. if (cacheData.isUseLocalConfigInfo() && !path.exists()) { cacheData.setUseLocalConfigInfo(false); - LOGGER.warn("[{}] [failover-change] failover file deleted. dataId={}, group={}, tenant={}", this.agent.getName(), + LOGGER.warn("[{}] [failover-change] failover file deleted. dataId={}, group={}, tenant={}", agent.getName(), dataId, group, tenant); return; } @@ -303,13 +303,13 @@ private void checkLocalConfig(CacheData cacheData) { // When it changed. if (cacheData.isUseLocalConfigInfo() && path.exists() && cacheData.getLocalConfigInfoVersion() != path.lastModified()) { - String content = LocalConfigInfoProcessor.getFailover(this.agent.getName(), dataId, group, tenant); + String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant); String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); cacheData.setUseLocalConfigInfo(true); cacheData.setLocalConfigInfoVersion(path.lastModified()); cacheData.setContent(content); LOGGER.warn("[{}] [failover-change] failover file changed. dataId={}, group={}, tenant={}, md5={}, content={}", - this.agent.getName(), dataId, group, tenant, md5, ContentUtils.truncateContent(content)); + agent.getName(), dataId, group, tenant, md5, ContentUtils.truncateContent(content)); } } @@ -319,15 +319,15 @@ private String null2defaultGroup(String group) { public void checkConfigInfo() { // Dispatch taskes. - int listenerSize = this.cacheMap.get().size(); + int listenerSize = cacheMap.get().size(); // Round up the longingTaskCount. int longingTaskCount = (int) Math.ceil(listenerSize / ParamUtil.getPerTaskConfigSize()); - if (longingTaskCount > this.currentLongingTaskCount) { - for (int i = (int) this.currentLongingTaskCount; i < longingTaskCount; i++) { + if (longingTaskCount > currentLongingTaskCount) { + for (int i = (int) currentLongingTaskCount; i < longingTaskCount; i++) { // The task list is no order.So it maybe has issues when changing. - this.executorService.execute(new LongPollingRunnable(i)); + executorService.execute(new LongPollingRunnable(i)); } - this.currentLongingTaskCount = longingTaskCount; + currentLongingTaskCount = longingTaskCount; } } @@ -381,7 +381,7 @@ List checkUpdateConfigStr(String probeUpdateString, boolean isInitializi List headers = new ArrayList(2); headers.add("Long-Pulling-Timeout"); - headers.add("" + this.timeout); + headers.add("" + timeout); // told server do not hang me up if new initializing cacheData added in if (isInitializingCacheList) { @@ -397,20 +397,20 @@ List checkUpdateConfigStr(String probeUpdateString, boolean isInitializi // In order to prevent the server from handling the delay of the client's long task, // increase the client's read timeout to avoid this problem. - long readTimeoutMs = this.timeout + (long) Math.round(this.timeout >> 1); - HttpResult result = this.agent.httpPost(Constants.CONFIG_CONTROLLER_PATH + "/listener", headers, params, - this.agent.getEncode(), readTimeoutMs); + long readTimeoutMs = timeout + (long) Math.round(timeout >> 1); + HttpResult result = agent.httpPost(Constants.CONFIG_CONTROLLER_PATH + "/listener", headers, params, + agent.getEncode(), readTimeoutMs); if (HttpURLConnection.HTTP_OK == result.code) { setHealthServer(true); return parseUpdateDataIdResponse(result.content); } else { setHealthServer(false); - LOGGER.error("[{}] [check-update] get changed dataId error, code: {}", this.agent.getName(), result.code); + LOGGER.error("[{}] [check-update] get changed dataId error, code: {}", agent.getName(), result.code); } } catch (IOException e) { setHealthServer(false); - LOGGER.error("[" + this.agent.getName() + "] [check-update] get changed dataId exception", e); + LOGGER.error("[" + agent.getName() + "] [check-update] get changed dataId exception", e); throw e; } return Collections.emptyList(); @@ -430,7 +430,7 @@ private List parseUpdateDataIdResponse(String response) { try { response = URLDecoder.decode(response, "UTF-8"); } catch (Exception e) { - LOGGER.error("[" + this.agent.getName() + "] [polling-resp] decode modifiedDataIdsString error", e); + LOGGER.error("[" + agent.getName() + "] [polling-resp] decode modifiedDataIdsString error", e); } List updateList = new LinkedList(); @@ -442,14 +442,14 @@ private List parseUpdateDataIdResponse(String response) { String group = keyArr[1]; if (keyArr.length == 2) { updateList.add(GroupKey.getKey(dataId, group)); - LOGGER.info("[{}] [polling-resp] config changed. dataId={}, group={}", this.agent.getName(), dataId, group); + LOGGER.info("[{}] [polling-resp] config changed. dataId={}, group={}", agent.getName(), dataId, group); } else if (keyArr.length == 3) { String tenant = keyArr[2]; updateList.add(GroupKey.getKeyTenant(dataId, group, tenant)); - LOGGER.info("[{}] [polling-resp] config changed. dataId={}, group={}, tenant={}", this.agent.getName(), + LOGGER.info("[{}] [polling-resp] config changed. dataId={}, group={}, tenant={}", agent.getName(), dataId, group, tenant); } else { - LOGGER.error("[{}] [polling-resp] invalid dataIdAndGroup error {}", this.agent.getName(), dataIdAndGroup); + LOGGER.error("[{}] [polling-resp] invalid dataIdAndGroup error {}", agent.getName(), dataIdAndGroup); } } } @@ -511,8 +511,8 @@ private void init(Properties properties) { public void shutdown() throws NacosException { String className = this.getClass().getName(); LOGGER.info("{} do shutdown begin", className); - ThreadUtils.shutdownThreadPool(this.executorService, LOGGER); - ThreadUtils.shutdownThreadPool(this.executor, LOGGER); + ThreadUtils.shutdownThreadPool(executorService, LOGGER); + ThreadUtils.shutdownThreadPool(executor, LOGGER); LOGGER.info("{} do shutdown stop", className); } @@ -531,7 +531,7 @@ public void run() { try { // check failover config for (CacheData cacheData : cacheMap.get().values()) { - if (cacheData.getTaskId() == this.taskId) { + if (cacheData.getTaskId() == taskId) { cacheDatas.add(cacheData); try { checkLocalConfig(cacheData); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java index c230f16f440..b82f3d5d12f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java @@ -59,9 +59,9 @@ public class ServerListManager implements Closeable { private ScheduledExecutorService executorService; public ServerListManager() { - isFixed = false; - isStarted = false; - name = DEFAULT_NAME; + this.isFixed = false; + this.isStarted = false; + this.name = DEFAULT_NAME; } public ServerListManager(List fixed) { @@ -69,8 +69,8 @@ public ServerListManager(List fixed) { } public ServerListManager(List fixed, String namespace) { - isFixed = true; - isStarted = true; + this.isFixed = true; + this.isStarted = true; List serverAddrs = new ArrayList(); for (String serverAddr : fixed) { String[] serverAddrArr = serverAddr.split(":"); @@ -80,21 +80,21 @@ public ServerListManager(List fixed, String namespace) { serverAddrs.add(serverAddr); } } - serverUrls = new ArrayList(serverAddrs); + this.serverUrls = new ArrayList(serverAddrs); if (StringUtils.isBlank(namespace)) { - name = FIXED_NAME + "-" + getFixedNameSuffix(serverAddrs.toArray(new String[serverAddrs.size()])); + this.name = FIXED_NAME + "-" + getFixedNameSuffix(serverAddrs.toArray(new String[serverAddrs.size()])); } else { this.namespace = namespace; - name = FIXED_NAME + "-" + getFixedNameSuffix(serverAddrs.toArray(new String[serverAddrs.size()])) + "-" + this.name = FIXED_NAME + "-" + getFixedNameSuffix(serverAddrs.toArray(new String[serverAddrs.size()])) + "-" + namespace; } } public ServerListManager(String host, int port) { - isFixed = false; - isStarted = false; - name = CUSTOM_NAME + "-" + host + "-" + port; - addressServerUrl = String.format("http://%s:%d/%s/%s", host, port, contentPath, serverListName); + this.isFixed = false; + this.isStarted = false; + this.name = CUSTOM_NAME + "-" + host + "-" + port; + this.addressServerUrl = String.format("http://%s:%d/%s/%s", host, port, this.contentPath, this.serverListName); } public ServerListManager(String endpoint) throws NacosException { @@ -102,8 +102,8 @@ public ServerListManager(String endpoint) throws NacosException { } public ServerListManager(String endpoint, String namespace) throws NacosException { - isFixed = false; - isStarted = false; + this.isFixed = false; + this.isStarted = false; Properties properties = new Properties(); properties.setProperty(PropertyKeyConst.ENDPOINT, endpoint); endpoint = initEndpoint(properties); @@ -113,34 +113,34 @@ public ServerListManager(String endpoint, String namespace) throws NacosExceptio throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank"); } if (StringUtils.isBlank(namespace)) { - name = endpoint; - addressServerUrl = String.format("http://%s:%d/%s/%s", endpoint, endpointPort, contentPath, - serverListName); + this.name = endpoint; + this.addressServerUrl = String.format("http://%s:%d/%s/%s", endpoint, this.endpointPort, this.contentPath, + this.serverListName); } else { if (StringUtils.isBlank(endpoint)) { throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank"); } - name = endpoint + "-" + namespace; + this.name = endpoint + "-" + namespace; this.namespace = namespace; this.tenant = namespace; - addressServerUrl = String.format("http://%s:%d/%s/%s?namespace=%s", endpoint, endpointPort, contentPath, - serverListName, namespace); + this.addressServerUrl = String.format("http://%s:%d/%s/%s?namespace=%s", endpoint, this.endpointPort, this.contentPath, + this.serverListName, namespace); } } public ServerListManager(Properties properties) throws NacosException { - isStarted = false; - serverAddrsStr = properties.getProperty(PropertyKeyConst.SERVER_ADDR); + this.isStarted = false; + this.serverAddrsStr = properties.getProperty(PropertyKeyConst.SERVER_ADDR); String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE); initParam(properties); initExecutor(); if (StringUtils.isNotEmpty(serverAddrsStr)) { - isFixed = true; + this.isFixed = true; List serverAddrs = new ArrayList(); - String[] serverAddrsArr = serverAddrsStr.split(","); + String[] serverAddrsArr = this.serverAddrsStr.split(","); for (String serverAddr: serverAddrsArr) { if (serverAddr.startsWith(HTTPS) || serverAddr.startsWith(HTTP)) { serverAddrs.add(serverAddr); @@ -153,45 +153,44 @@ public ServerListManager(Properties properties) throws NacosException { } } } - serverUrls = serverAddrs; + this.serverUrls = serverAddrs; if (StringUtils.isBlank(namespace)) { - name = FIXED_NAME + "-" + getFixedNameSuffix(serverUrls.toArray(new String[serverUrls.size()])); + this.name = FIXED_NAME + "-" + getFixedNameSuffix(this.serverUrls.toArray(new String[this.serverUrls.size()])); } else { this.namespace = namespace; this.tenant = namespace; - name = FIXED_NAME + "-" + getFixedNameSuffix(serverUrls.toArray(new String[serverUrls.size()])) + "-" + this.name = FIXED_NAME + "-" + getFixedNameSuffix(this.serverUrls.toArray(new String[this.serverUrls.size()])) + "-" + namespace; } } else { if (StringUtils.isBlank(endpoint)) { throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank"); } - isFixed = false; + this.isFixed = false; if (StringUtils.isBlank(namespace)) { - name = endpoint; - addressServerUrl = String.format("http://%s:%d/%s/%s", endpoint, endpointPort, contentPath, - serverListName); + this.name = endpoint; + this.addressServerUrl = String.format("http://%s:%d/%s/%s", this.endpoint, this.endpointPort, this.contentPath, + this.serverListName); } else { this.namespace = namespace; this.tenant = namespace; - name = endpoint + "-" + namespace; - addressServerUrl = String.format("http://%s:%d/%s/%s?namespace=%s", endpoint, endpointPort, - contentPath, serverListName, namespace); + this.name = this.endpoint + "-" + namespace; + this.addressServerUrl = String.format("http://%s:%d/%s/%s?namespace=%s", this.endpoint, this.endpointPort, + this.contentPath, this.serverListName, namespace); } } - } private void initParam(Properties properties) { - endpoint = initEndpoint(properties); + this.endpoint = initEndpoint(properties); String contentPathTmp = properties.getProperty(PropertyKeyConst.CONTEXT_PATH); if (!StringUtils.isBlank(contentPathTmp)) { - contentPath = contentPathTmp; + this.contentPath = contentPathTmp; } String serverListNameTmp = properties.getProperty(PropertyKeyConst.CLUSTER_NAME); if (!StringUtils.isBlank(serverListNameTmp)) { - serverListName = serverListNameTmp; + this.serverListName = serverListNameTmp; } } @@ -205,7 +204,7 @@ public String call() { }); if (StringUtils.isNotBlank(endpointPortTmp)) { - endpointPort = Integer.parseInt(endpointPortTmp); + this.endpointPort = Integer.parseInt(endpointPortTmp); } String endpointTmp = properties.getProperty(PropertyKeyConst.ENDPOINT); @@ -218,7 +217,7 @@ public String call() { if (Boolean.parseBoolean(isUseEndpointRuleParsing)) { String endpointUrl = ParamUtil.parsingEndpointRule(endpointTmp); if (StringUtils.isNotBlank(endpointUrl)) { - serverAddrsStr = ""; + this.serverAddrsStr = ""; } return endpointUrl; } @@ -282,7 +281,7 @@ Iterator iterator() { public void shutdown() throws NacosException{ String className = this.getClass().getName(); LOGGER.info("{} do shutdown begin", className); - ThreadUtils.shutdownThreadPool(this.executorService, LOGGER); + ThreadUtils.shutdownThreadPool(executorService, LOGGER); LOGGER.info("{} do shutdown stop", className); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index 304b4eb4d17..3bf3482befa 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -87,17 +87,17 @@ public NacosNamingService(Properties properties) { private void init(Properties properties) { ValidatorUtils.checkInitParam(properties); - namespace = InitUtils.initNamespaceForNaming(properties); + this.namespace = InitUtils.initNamespaceForNaming(properties); InitUtils.initSerialization(); initServerAddr(properties); InitUtils.initWebRootContext(); initCacheDir(); initLogName(properties); - eventDispatcher = new EventDispatcher(); - serverProxy = new NamingProxy(namespace, endpoint, serverList, properties); - beatReactor = new BeatReactor(serverProxy, initClientBeatThreadCount(properties)); - hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, isLoadCacheAtStart(properties), + this.eventDispatcher = new EventDispatcher(); + this.serverProxy = new NamingProxy(this.namespace, this.endpoint, this.serverList, properties); + this.beatReactor = new BeatReactor(this.serverProxy, initClientBeatThreadCount(properties)); + this.hostReactor = new HostReactor(this.eventDispatcher, this.serverProxy, this.cacheDir, isLoadCacheAtStart(properties), initPollingThreadCount(properties)); ResourceLifeCycleManager.register(this); } @@ -499,9 +499,9 @@ public void shutDown() throws NacosException{ @Override public void destroy() throws NacosException{ - this.beatReactor.shutdown(); - this.eventDispatcher.shutdown(); - this.hostReactor.shutdown(); - this.serverProxy.shutdown(); + beatReactor.shutdown(); + eventDispatcher.shutdown(); + hostReactor.shutdown(); + serverProxy.shutdown(); } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java index 1fddf338d79..a7418fc3c32 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/backups/FailoverReactor.java @@ -108,7 +108,7 @@ public Date addDay(Date date, int num) { public void shutdown() throws NacosException { String className = this.getClass().getName(); NAMING_LOGGER.info("{} do shutdown begin", className); - ThreadUtils.shutdownThreadPool(this.executorService, NAMING_LOGGER); + ThreadUtils.shutdownThreadPool(executorService, NAMING_LOGGER); NAMING_LOGGER.info("{} do shutdown stop", className); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java index 1fef68c64b8..47dcdaa97ed 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java @@ -69,17 +69,17 @@ public void addBeatInfo(String serviceName, BeatInfo beatInfo) { String key = buildKey(serviceName, beatInfo.getIp(), beatInfo.getPort()); BeatInfo existBeat = null; //fix #1733 - if ((existBeat = this.dom2Beat.remove(key)) != null) { + if ((existBeat = dom2Beat.remove(key)) != null) { existBeat.setStopped(true); } - this.dom2Beat.put(key, beatInfo); - this.executorService.schedule(new BeatTask(beatInfo), beatInfo.getPeriod(), TimeUnit.MILLISECONDS); - MetricsMonitor.getDom2BeatSizeMonitor().set(this.dom2Beat.size()); + dom2Beat.put(key, beatInfo); + executorService.schedule(new BeatTask(beatInfo), beatInfo.getPeriod(), TimeUnit.MILLISECONDS); + MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size()); } public void removeBeatInfo(String serviceName, String ip, int port) { NAMING_LOGGER.info("[BEAT] removing beat: {}:{}:{} from beat map.", serviceName, ip, port); - BeatInfo beatInfo = this.dom2Beat.remove(buildKey(serviceName, ip, port)); + BeatInfo beatInfo = dom2Beat.remove(buildKey(serviceName, ip, port)); if (beatInfo == null) { return; } @@ -96,7 +96,7 @@ private String buildKey(String serviceName, String ip, int port) { public void shutdown() throws NacosException{ String className = this.getClass().getName(); NAMING_LOGGER.info("{} do shutdown begin", className); - ThreadUtils.shutdownThreadPool(this.executorService, NAMING_LOGGER); + ThreadUtils.shutdownThreadPool(executorService, NAMING_LOGGER); NAMING_LOGGER.info("{} do shutdown stop", className); } @@ -110,12 +110,12 @@ public BeatTask(BeatInfo beatInfo) { @Override public void run() { - if (this.beatInfo.isStopped()) { + if (beatInfo.isStopped()) { return; } - long nextTime = this.beatInfo.getPeriod(); + long nextTime = beatInfo.getPeriod(); try { - JsonNode result = serverProxy.sendBeat(this.beatInfo, BeatReactor.this.lightBeatEnabled); + JsonNode result = serverProxy.sendBeat(beatInfo, BeatReactor.this.lightBeatEnabled); long interval = result.get("clientBeatInterval").asInt(); boolean lightBeatEnabled = false; if (result.has(CommonParams.LIGHT_BEAT_ENABLED)) { @@ -131,26 +131,26 @@ public void run() { } if (code == NamingResponseCode.RESOURCE_NOT_FOUND) { Instance instance = new Instance(); - instance.setPort(this.beatInfo.getPort()); - instance.setIp(this.beatInfo.getIp()); - instance.setWeight(this.beatInfo.getWeight()); - instance.setMetadata(this.beatInfo.getMetadata()); - instance.setClusterName(this.beatInfo.getCluster()); - instance.setServiceName(this.beatInfo.getServiceName()); + instance.setPort(beatInfo.getPort()); + instance.setIp(beatInfo.getIp()); + instance.setWeight(beatInfo.getWeight()); + instance.setMetadata(beatInfo.getMetadata()); + instance.setClusterName(beatInfo.getCluster()); + instance.setServiceName(beatInfo.getServiceName()); instance.setInstanceId(instance.getInstanceId()); instance.setEphemeral(true); try { - serverProxy.registerService(this.beatInfo.getServiceName(), - NamingUtils.getGroupName(this.beatInfo.getServiceName()), instance); + serverProxy.registerService(beatInfo.getServiceName(), + NamingUtils.getGroupName(beatInfo.getServiceName()), instance); } catch (Exception ignore) { } } } catch (NacosException ne) { NAMING_LOGGER.error("[CLIENT-BEAT] failed to send beat: {}, code: {}, msg: {}", - JacksonUtils.toJson(this.beatInfo), ne.getErrCode(), ne.getErrMsg()); + JacksonUtils.toJson(beatInfo), ne.getErrCode(), ne.getErrMsg()); } - executorService.schedule(new BeatTask(this.beatInfo), nextTime, TimeUnit.MILLISECONDS); + executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS); } } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java index 9be02e7d8fe..5604c72c533 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/EventDispatcher.java @@ -46,7 +46,7 @@ public class EventDispatcher implements Closeable { public EventDispatcher() { - executor = Executors.newSingleThreadExecutor(new ThreadFactory() { + this.executor = Executors.newSingleThreadExecutor(new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r, "com.alibaba.nacos.naming.client.listener"); @@ -56,7 +56,7 @@ public Thread newThread(Runnable r) { } }); - executor.execute(new Notifier()); + this.executor.execute(new Notifier()); } public void addListener(ServiceInfo serviceInfo, String clusters, EventListener listener) { @@ -116,7 +116,7 @@ public void serviceChanged(ServiceInfo serviceInfo) { public void shutdown() throws NacosException { String className = this.getClass().getName(); NAMING_LOGGER.info("{} do shutdown begin", className); - ThreadUtils.shutdownThreadPool(this.executor, NAMING_LOGGER); + ThreadUtils.shutdownThreadPool(executor, NAMING_LOGGER); NAMING_LOGGER.info("{} do shutdown stop", className); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java index acd44e9e92c..a15e4a94adc 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java @@ -309,7 +309,7 @@ public void refreshOnly(String serviceName, String clusters) { public void shutdown() throws NacosException{ String className = this.getClass().getName(); NAMING_LOGGER.info("{} do shutdown begin", className); - ThreadUtils.shutdownThreadPool(this.executor, NAMING_LOGGER); + ThreadUtils.shutdownThreadPool(executor, NAMING_LOGGER); NAMING_LOGGER.info("{} do shutdown stop", className); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java index 2073eb9ab76..7ae6828054e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/PushReceiver.java @@ -73,7 +73,7 @@ public void run() { byte[] buffer = new byte[UDP_MSS]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); - this.udpSocket.receive(packet); + udpSocket.receive(packet); String json = new String(IoUtils.tryDecompress(packet.getData()), "UTF-8").trim(); NAMING_LOGGER.info("received push data: " + json + " from " + packet.getAddress().toString()); @@ -81,7 +81,7 @@ public void run() { PushPacket pushPacket = JacksonUtils.toObj(json, PushPacket.class); String ack; if ("dom".equals(pushPacket.type) || "service".equals(pushPacket.type)) { - this.hostReactor.processServiceJSON(pushPacket.data); + hostReactor.processServiceJSON(pushPacket.data); // send ack to server ack = "{\"type\": \"push-ack\"" @@ -92,7 +92,7 @@ public void run() { ack = "{\"type\": \"dump-ack\"" + ", \"lastRefTime\": \"" + pushPacket.lastRefTime + "\", \"data\":" + "\"" - + StringUtils.escapeJavaScript(JacksonUtils.toJson(this.hostReactor.getServiceInfoMap())) + + StringUtils.escapeJavaScript(JacksonUtils.toJson(hostReactor.getServiceInfoMap())) + "\"}"; } else { // do nothing send ack only @@ -101,7 +101,7 @@ public void run() { + "\", \"data\":" + "\"\"}"; } - this.udpSocket.send(new DatagramPacket(ack.getBytes(Charset.forName("UTF-8")), + udpSocket.send(new DatagramPacket(ack.getBytes(Charset.forName("UTF-8")), ack.getBytes(Charset.forName("UTF-8")).length, packet.getSocketAddress())); } catch (Exception e) { NAMING_LOGGER.error("[NA] error while receiving push data", e); @@ -113,7 +113,7 @@ public void run() { public void shutdown() throws NacosException { String className = this.getClass().getName(); NAMING_LOGGER.info("{} do shutdown begin", className); - ThreadUtils.shutdownThreadPool(this.executorService, NAMING_LOGGER); + ThreadUtils.shutdownThreadPool(executorService, NAMING_LOGGER); NAMING_LOGGER.info("{} do shutdown stop", className); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index 1f28833942b..df25d759360 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -98,8 +98,7 @@ public NamingProxy(String namespaceId, String endpoint, String serverList, Prope this.nacosDomain = serverList; } } - - initRefreshTask(); + this.initRefreshTask(); } private void initRefreshTask() { @@ -586,7 +585,7 @@ public void setServerPort(int serverPort) { public void shutdown() throws NacosException{ String className = this.getClass().getName(); NAMING_LOGGER.info("{} do shutdown begin", className); - ThreadUtils.shutdownThreadPool(this.executorService, NAMING_LOGGER); + ThreadUtils.shutdownThreadPool(executorService, NAMING_LOGGER); NAMING_LOGGER.info("{} do shutdown stop", className); } } diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/Closeable.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/Closeable.java index 6f969b180cc..d6132a8adb8 100644 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/Closeable.java +++ b/common/src/main/java/com/alibaba/nacos/common/lifecycle/Closeable.java @@ -1,3 +1,18 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.alibaba.nacos.common.lifecycle; diff --git a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java index 123830dceb6..7c85c3eda59 100644 --- a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java @@ -35,29 +35,22 @@ public class LifeCycleManagerTest { public void setup() throws Exception{ this.res = new Resource(0); ResourceLifeCycleManager.register(this.res); - this.res.increament(); - Assert.assertEquals(this.res.getCounter(), 1); + Assert.assertEquals(0, this.res.getCounter()); + Assert.assertEquals(1, ResourceLifeCycleManager.getRegisterResourceNum()); } @After public void cleanup() throws Exception{ ResourceLifeCycleManager.shutdown(); - Assert.assertEquals(this.res.getCounter(), 0); - // here, double check shutdown called by two times whether the result is ok. - ResourceLifeCycleManager.shutdown(); - Assert.assertEquals(this.res.getCounter(), 0); - // here, check whether the buffer data in resource manager is correct. - ResourceLifeCycleManager.getInstance().destroy(this.res); - Assert.assertEquals(this.res.getCounter(), 0); - Assert.assertEquals(ResourceLifeCycleManager.getRegisterResourceNum(), 0); + Assert.assertEquals(0, this.res.getCounter()); } @Test public void testLifeCycleManager() throws Exception{ this.res.increament(); - Assert.assertEquals(this.res.getCounter(), 2); + Assert.assertEquals(1, this.res.getCounter()); this.res.increament(); - Assert.assertEquals(this.res.getCounter(), 3); + Assert.assertEquals(2, this.res.getCounter()); } @Test @@ -69,9 +62,10 @@ public void testLifeCycleManager_deregister() throws Exception{ ResourceLifeCycleManager.deregister(temp); ResourceLifeCycleManager.getInstance().destroy(temp); - Assert.assertEquals(temp.getCounter(), 1); + Assert.assertEquals(1, temp.getCounter()); temp.destroy(); - Assert.assertEquals(temp.getCounter(), 0); + Assert.assertEquals(0, temp.getCounter()); + Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); } class Resource implements LifeCycle { diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java index a970ca698a2..401257b7a2c 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java @@ -88,7 +88,7 @@ public void cleanup() throws Exception { NacosFactory.destroyConfigService(iconfig); agent.shutdown(); // Judge whether the register life cycle resource number equals to zero or not. - Assert.assertEquals(ResourceLifeCycleManager.getRegisterResourceNum(), 0); + Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); } catch (Exception e) { e.printStackTrace(); Assert.fail(); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java index cb1c6bbee3c..958b3096597 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java @@ -62,7 +62,7 @@ public void init() throws NacosException { public void destroy(){ try { NacosFactory.destroyConfigService(configService); - Assert.assertEquals(ResourceLifeCycleManager.getRegisterResourceNum(), 0); + Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); }catch (NacosException ex) { } } diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java index dd6df3e330d..9f918e02883 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java @@ -65,7 +65,7 @@ public void init() throws NacosException { public void destroy(){ try { NacosFactory.destroyConfigService(configService); - Assert.assertEquals(ResourceLifeCycleManager.getRegisterResourceNum(), 0); + Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); }catch (NacosException ex) { } } diff --git a/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java index 3894390b48a..a4b51915648 100644 --- a/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java @@ -70,7 +70,7 @@ public void destroy(){ super.destroy(); try { NacosFactory.destroyConfigService(iconfig); - Assert.assertEquals(ResourceLifeCycleManager.getRegisterResourceNum(), 0); + Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); }catch (NacosException ex) { } From 94c41e11d186346bf2b002c6be5a583c654a7ce9 Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Wed, 3 Jun 2020 17:35:49 +0800 Subject: [PATCH 041/156] [#1815]fix unit test cases. --- .../nacos/common/LifeCycle/LifeCycleClassTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java index 463ea1db89b..fab1981deff 100644 --- a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java @@ -33,25 +33,25 @@ public class LifeCycleClassTest { @Before public void setup() throws Exception{ this.res = new Resource(0); - Assert.assertEquals(this.res.getCounter(), 0); + Assert.assertEquals(0, this.res.getCounter()); } @After public void cleanup() throws Exception{ this.res.destroy(); - Assert.assertEquals(this.res.getCounter(), 0); + Assert.assertEquals(0, this.res.getCounter()); } @Test public void testResource_LifeCycleMethod() throws Exception{ this.res.increament(); - Assert.assertEquals(this.res.getCounter(), 1); + Assert.assertEquals(1, this.res.getCounter()); this.res.increament(); - Assert.assertEquals(this.res.getCounter(), 2); + Assert.assertEquals(2, this.res.getCounter()); this.res.increament(); - Assert.assertEquals(this.res.getCounter(), 3); + Assert.assertEquals(3, this.res.getCounter()); this.res.increament(); - Assert.assertEquals(this.res.getCounter(), 4); + Assert.assertEquals(4, this.res.getCounter()); } class Resource implements LifeCycle { From 15fad5d98abf6421231a7d5c997da8c35bb73a3b Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Wed, 3 Jun 2020 19:19:29 +0800 Subject: [PATCH 042/156] [#1815]remove destroyService from Factory Class. --- .../com/alibaba/nacos/api/NacosFactory.java | 21 ------------------- .../nacos/api/config/ConfigFactory.java | 16 -------------- .../nacos/api/naming/NamingFactory.java | 15 ------------- .../nacos/test/config/ConfigAPI_ITCase.java | 2 +- .../ConfigLongPollReturnChanges_ITCase.java | 2 +- .../test/config/ConfigLongPoll_ITCase.java | 2 +- .../nacos/test/core/BaseClusterTest.java | 13 ++++++------ .../test/core/auth/ConfigAuth_ITCase.java | 3 +-- .../test/core/auth/NamingAuth_ITCase.java | 12 +++++------ 9 files changed, 16 insertions(+), 70 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java b/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java index 1872697f3a3..6de6966ad4e 100644 --- a/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java @@ -97,25 +97,4 @@ public static NamingMaintainService createMaintainService(String serverAddr) thr public static NamingMaintainService createMaintainService(Properties properties) throws NacosException { return NamingMaintainFactory.createMaintainService(properties); } - - /** - * Destroy config service instance. - * - * @param configService - * @throws NacosException Exception. - */ - public static void destroyConfigService(ConfigService configService) throws NacosException{ - ConfigFactory.destroyConfigService(configService); - } - - /** - * Destroy naming service instance. - * - * @param namingService - * @throws NacosException Exception. - */ - public static void destroyNamingService(NamingService namingService) throws NacosException{ - NamingFactory.destroyNamingService(namingService); - } - } diff --git a/api/src/main/java/com/alibaba/nacos/api/config/ConfigFactory.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigFactory.java index 65e867d57dc..6e8b90456ce 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/ConfigFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigFactory.java @@ -58,20 +58,4 @@ public static ConfigService createConfigService(String serverAddr) throws NacosE properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr); return createConfigService(properties); } - - /** - * Destroy Config Instance's Resources. - * - * @param configService - * @throws NacosException - */ - public static void destroyConfigService(ConfigService configService) throws NacosException{ - try { - configService.shutDown(); - configService = null; - }catch (Throwable e) { - throw new NacosException(NacosException.RESOURCE_DESTROY_FAILED, e); - } - } - } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java index 570fa6108fb..28c3a45df64 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java @@ -49,19 +49,4 @@ public static NamingService createNamingService(Properties properties) throws Na throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e); } } - - /** - * Destroy Config Instance's Resources. - * - * @param namingService - * @throws NacosException - */ - public static void destroyNamingService(NamingService namingService) throws NacosException{ - try { - namingService.shutDown(); - namingService = null; - }catch (Throwable e) { - throw new NacosException(NacosException.RESOURCE_DESTROY_FAILED, e); - } - } } diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java index 48c6b49eded..d7a2e6fa236 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java @@ -85,7 +85,7 @@ public void cleanup() throws Exception { result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); Assert.assertTrue(JacksonUtils.toObj(result.content).get("data").booleanValue()); - NacosFactory.destroyConfigService(iconfig); + iconfig.shutDown(); agent.shutdown(); // Judge whether the register life cycle resource number equals to zero or not. Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java index 958b3096597..21241927b28 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java @@ -61,7 +61,7 @@ public void init() throws NacosException { @After public void destroy(){ try { - NacosFactory.destroyConfigService(configService); + configService.shutDown(); Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); }catch (NacosException ex) { } diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java index 9f918e02883..1369847a577 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java @@ -64,7 +64,7 @@ public void init() throws NacosException { @After public void destroy(){ try { - NacosFactory.destroyConfigService(configService); + configService.shutDown(); Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); }catch (NacosException ex) { } diff --git a/test/src/test/java/com/alibaba/nacos/test/core/BaseClusterTest.java b/test/src/test/java/com/alibaba/nacos/test/core/BaseClusterTest.java index 0e469139d3f..53f29270087 100644 --- a/test/src/test/java/com/alibaba/nacos/test/core/BaseClusterTest.java +++ b/test/src/test/java/com/alibaba/nacos/test/core/BaseClusterTest.java @@ -157,14 +157,13 @@ public static void after() throws Exception { try { System.out.println("start close : " + context); context.close(); - NacosFactory.destroyConfigService(iconfig7); - NacosFactory.destroyConfigService(iconfig8); - NacosFactory.destroyConfigService(iconfig9); - - NacosFactory.destroyNamingService(inaming7); - NacosFactory.destroyNamingService(inaming8); - NacosFactory.destroyNamingService(inaming9); + iconfig7.shutDown(); + iconfig8.shutDown(); + iconfig9.shutDown(); + inaming7.shutDown(); + inaming8.shutDown(); + inaming9.shutDown(); } catch (Exception ignore) { } finally { System.out.println("finished close : " + context); diff --git a/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java index a4b51915648..b9a769a96b9 100644 --- a/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java @@ -67,9 +67,8 @@ public void init() throws Exception { @After public void destroy(){ - super.destroy(); try { - NacosFactory.destroyConfigService(iconfig); + iconfig.shutDown(); Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); }catch (NacosException ex) { diff --git a/test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuth_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuth_ITCase.java index ef616fb74fa..8c959fbfa4b 100644 --- a/test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuth_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/core/auth/NamingAuth_ITCase.java @@ -81,7 +81,7 @@ public void writeWithReadPermission() throws Exception { } catch (NacosException ne) { Assert.assertEquals(HttpStatus.SC_FORBIDDEN, ne.getErrCode()); } - NacosFactory.destroyNamingService(namingService); + namingService.shutDown(); } @Test @@ -98,8 +98,8 @@ public void readWithReadPermission() throws Exception { List list = namingService.getAllInstances("test.1"); Assert.assertEquals(1, list.size()); - NacosFactory.destroyNamingService(namingService1); - NacosFactory.destroyNamingService(namingService); + namingService1.shutDown(); + namingService.shutDown(); } @Test @@ -114,7 +114,7 @@ public void writeWithWritePermission() throws Exception { TimeUnit.SECONDS.sleep(5L); namingService.deregisterInstance("test.1", "1.2.3.4", 80); - NacosFactory.destroyNamingService(namingService); + namingService.shutDown(); } @Test @@ -130,7 +130,7 @@ public void readWithWritePermission() throws Exception { List list = namingService.getAllInstances("test.1"); Assert.assertEquals(0, list.size()); - NacosFactory.destroyNamingService(namingService); + namingService.shutDown(); } @Test @@ -146,7 +146,7 @@ public void readWriteWithFullPermission() throws Exception { List list = namingService.getAllInstances("test.1"); Assert.assertEquals(1, list.size()); - NacosFactory.destroyNamingService(namingService); + namingService.shutDown(); } } From 8a2921e2cf2c153c3bdd6bbaeba16e8f087aed47 Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Thu, 4 Jun 2020 09:21:44 +0800 Subject: [PATCH 043/156] [#1815]remove no used package name from Factory Class. --- .../main/java/com/alibaba/nacos/api/naming/NamingFactory.java | 1 - 1 file changed, 1 deletion(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java index 28c3a45df64..745ccd275f4 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java @@ -18,7 +18,6 @@ import java.lang.reflect.Constructor; import java.util.Properties; -import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.exception.NacosException; /** From 56eeb35ffcf69d43ef1445a16512183f7e013cae Mon Sep 17 00:00:00 2001 From: lengleng Date: Sat, 6 Jun 2020 17:24:37 +0800 Subject: [PATCH 044/156] :recycle: Refactoring pom. com.alibaba.nacos to ${project.groupId} Uniform variable --- address/pom.xml | 2 +- common/pom.xml | 2 +- config/pom.xml | 2 +- distribution/pom.xml | 12 ++++++------ example/pom.xml | 2 +- naming/pom.xml | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/address/pom.xml b/address/pom.xml index 50643dd9d15..a77be3b0cb4 100644 --- a/address/pom.xml +++ b/address/pom.xml @@ -48,7 +48,7 @@ nacos-naming - com.alibaba.nacos + ${project.groupId} nacos-cmdb diff --git a/common/pom.xml b/common/pom.xml index 2eefb5195a8..d59268a2b33 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -55,7 +55,7 @@ - com.alibaba.nacos + ${project.groupId} nacos-api diff --git a/config/pom.xml b/config/pom.xml index bdd3ca48a31..659c6e9127a 100644 --- a/config/pom.xml +++ b/config/pom.xml @@ -173,7 +173,7 @@ springboot - com.alibaba.nacos + ${project.groupId} nacos-core diff --git a/distribution/pom.xml b/distribution/pom.xml index e3ffb0b6075..f3ebad74f16 100644 --- a/distribution/pom.xml +++ b/distribution/pom.xml @@ -30,7 +30,7 @@ - com.alibaba.nacos + ${project.groupId} nacos-console @@ -39,7 +39,7 @@ release-config - com.alibaba.nacos + ${project.groupId} nacos-config @@ -72,7 +72,7 @@ release-naming - com.alibaba.nacos + ${project.groupId} nacos-naming @@ -131,7 +131,7 @@ release-client - com.alibaba.nacos + ${project.groupId} nacos-client @@ -164,7 +164,7 @@ release-core - com.alibaba.nacos + ${project.groupId} nacos-core @@ -197,7 +197,7 @@ release-nacos - com.alibaba.nacos + ${project.groupId} nacos-console diff --git a/example/pom.xml b/example/pom.xml index 514160e2657..bc4d3c63ae1 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -49,7 +49,7 @@ nacos-core - com.alibaba.nacos + ${project.groupId} nacos-client diff --git a/naming/pom.xml b/naming/pom.xml index 878202896e4..1ef214b5d0e 100644 --- a/naming/pom.xml +++ b/naming/pom.xml @@ -147,7 +147,7 @@ - com.alibaba.nacos + ${project.groupId} nacos-cmdb From bf74511a828e30cf5b3cef44bc8348ec655782c1 Mon Sep 17 00:00:00 2001 From: yangy Date: Sun, 7 Jun 2020 10:06:00 +0800 Subject: [PATCH 045/156] simplify code --- .../alibaba/nacos/console/security/nacos/JwtTokenManager.java | 2 +- .../java/com/alibaba/nacos/console/utils/JwtTokenUtils.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/console/src/main/java/com/alibaba/nacos/console/security/nacos/JwtTokenManager.java b/console/src/main/java/com/alibaba/nacos/console/security/nacos/JwtTokenManager.java index 0ef8fce1fe1..a07738f444f 100644 --- a/console/src/main/java/com/alibaba/nacos/console/security/nacos/JwtTokenManager.java +++ b/console/src/main/java/com/alibaba/nacos/console/security/nacos/JwtTokenManager.java @@ -57,7 +57,7 @@ public String createToken(Authentication authentication) { public String createToken(String userName) { - long now = (new Date()).getTime(); + long now = System.currentTimeMillis(); Date validity; validity = new Date(now + authConfigs.getTokenValidityInSeconds() * 1000L); diff --git a/console/src/main/java/com/alibaba/nacos/console/utils/JwtTokenUtils.java b/console/src/main/java/com/alibaba/nacos/console/utils/JwtTokenUtils.java index 55b32401eef..967a45fd9e1 100644 --- a/console/src/main/java/com/alibaba/nacos/console/utils/JwtTokenUtils.java +++ b/console/src/main/java/com/alibaba/nacos/console/utils/JwtTokenUtils.java @@ -104,7 +104,7 @@ public String createToken(Authentication authentication) { /** * Current time */ - long now = (new Date()).getTime(); + long now = System.currentTimeMillis(); /** * Validity date */ From 5191838d7736fe6065f4a6d335cc4e75b157817c Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sun, 7 Jun 2020 18:16:46 +0800 Subject: [PATCH 046/156] fix: fixed Raft RPC being submitted to the Leader, error could not be returned --- .../service/dump/EmbeddedDumpService.java | 15 +-- .../service/merge/MergeDatumService.java | 7 +- .../nacos/consistency/cp/CPProtocol.java | 3 +- .../core/distributed/raft/JRaftProtocol.java | 2 +- .../core/distributed/raft/JRaftServer.java | 71 ++++++----- .../core/distributed/raft/NacosClosure.java | 113 ++++++++++++------ .../distributed/raft/NacosStateMachine.java | 7 +- .../exception/NoSuchRaftGroupException.java | 2 +- .../raft/processor/AbstractProcessor.java | 15 +-- .../raft/utils/FailoverClosure.java | 7 +- .../raft/utils/FailoverClosureImpl.java | 71 ++++++----- 11 files changed, 170 insertions(+), 143 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/EmbeddedDumpService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/EmbeddedDumpService.java index 36508fd4d97..22425e67ea4 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/EmbeddedDumpService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/EmbeddedDumpService.java @@ -160,17 +160,8 @@ private boolean shouldRetry(Throwable ex) { @Override protected boolean canExecute() { - try { - // if is derby + raft mode, only leader can execute - CPProtocol protocol = protocolManager.getCpProtocol(); - return protocol.isLeader(Constants.CONFIG_MODEL_RAFT_GROUP); - } - catch (NoSuchRaftGroupException e) { - return true; - } - catch (Throwable e) { - // It's impossible to get to this point - throw new RuntimeException(e); - } + // if is derby + raft mode, only leader can execute + CPProtocol protocol = protocolManager.getCpProtocol(); + return protocol.isLeader(Constants.CONFIG_MODEL_RAFT_GROUP); } } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeDatumService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeDatumService.java index dd2f9b88778..723198376ee 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeDatumService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeDatumService.java @@ -26,7 +26,9 @@ import com.alibaba.nacos.config.server.utils.PropertyUtil; import com.alibaba.nacos.config.server.utils.TimeUtils; import com.alibaba.nacos.consistency.cp.CPProtocol; +import com.alibaba.nacos.core.distributed.ProtocolManager; import com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException; +import com.alibaba.nacos.core.utils.ApplicationUtils; import com.alibaba.nacos.core.utils.InetUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,8 +55,6 @@ public class MergeDatumService { static final AtomicInteger FINISHED = new AtomicInteger(); static int total = 0; - private CPProtocol protocol; - @Autowired public MergeDatumService(PersistService persistService) { this.persistService = persistService; @@ -110,7 +110,8 @@ private boolean canExecute() { return true; } try { - return protocol.isLeader(Constants.CONFIG_MODEL_RAFT_GROUP); + ProtocolManager protocolManager = ApplicationUtils.getBean(ProtocolManager.class); + return protocolManager.getCpProtocol().isLeader(Constants.CONFIG_MODEL_RAFT_GROUP); } catch (NoSuchRaftGroupException e) { return true; } catch (Exception e) { diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/cp/CPProtocol.java b/consistency/src/main/java/com/alibaba/nacos/consistency/cp/CPProtocol.java index 9d7a351dfd6..bde4d720db4 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/cp/CPProtocol.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/cp/CPProtocol.java @@ -30,8 +30,7 @@ public interface CPProtocol extends * * @param group business module info * @return is leader - * @throws Exception */ - boolean isLeader(String group) throws Exception; + boolean isLeader(String group); } diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftProtocol.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftProtocol.java index f2c3f05131a..3e3cb8e2d30 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftProtocol.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftProtocol.java @@ -209,7 +209,7 @@ private void injectProtocolMetaData(ProtocolMetaData metaData) { } @Override - public boolean isLeader(String group) throws Exception { + public boolean isLeader(String group) { Node node = raftServer.findNodeByGroup(group); if (node == null) { throw new NoSuchRaftGroupException(group); diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java index 5d23e561b7e..5d8b619f364 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java @@ -19,7 +19,6 @@ import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.common.utils.LoggerUtils; -import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.ThreadUtils; import com.alibaba.nacos.consistency.LogProcessor; import com.alibaba.nacos.consistency.SerializeFactory; @@ -39,11 +38,8 @@ import com.alibaba.nacos.core.distributed.raft.utils.RaftExecutor; import com.alibaba.nacos.core.distributed.raft.utils.RaftOptionsBuilder; import com.alibaba.nacos.core.monitor.MetricsMonitor; -import com.alibaba.nacos.core.notify.NotifyCenter; import com.alibaba.nacos.core.utils.ApplicationUtils; -import com.alibaba.nacos.core.utils.ClassUtils; import com.alibaba.nacos.core.utils.Loggers; -import com.alibaba.nacos.core.utils.TimerContext; import com.alipay.sofa.jraft.CliService; import com.alipay.sofa.jraft.Node; import com.alipay.sofa.jraft.RaftGroupService; @@ -64,8 +60,8 @@ import com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl; import com.alipay.sofa.jraft.util.BytesUtil; import com.alipay.sofa.jraft.util.Endpoint; -import com.google.protobuf.Message; import com.google.common.base.Joiner; +import com.google.protobuf.Message; import org.springframework.util.CollectionUtils; import java.nio.ByteBuffer; @@ -138,8 +134,10 @@ public class JRaftServer { // System.getProperties().setProperty("bolt.channel_write_buf_low_water_mark", String.valueOf(64 * 1024 * 1024)); // System.getProperties().setProperty("bolt.channel_write_buf_high_water_mark", String.valueOf(256 * 1024 * 1024)); - System.getProperties().setProperty("bolt.netty.buffer.low.watermark", String.valueOf(128 * 1024 * 1024)); - System.getProperties().setProperty("bolt.netty.buffer.high.watermark", String.valueOf(256 * 1024 * 1024)); + System.getProperties().setProperty("bolt.netty.buffer.low.watermark", + String.valueOf(128 * 1024 * 1024)); + System.getProperties().setProperty("bolt.netty.buffer.high.watermark", + String.valueOf(256 * 1024 * 1024)); } public JRaftServer() throws Exception { @@ -272,7 +270,8 @@ synchronized void createMultiRaftGroup(Collection processors) { machine.setNode(node); RouteTable.getInstance().updateConfiguration(groupName, configuration); - RaftExecutor.executeByCommon(() -> registerSelfToCluster(groupName, localPeerId, configuration)); + RaftExecutor.executeByCommon( + () -> registerSelfToCluster(groupName, localPeerId, configuration)); // Turn on the leader auto refresh for this group Random random = new Random(); @@ -305,21 +304,25 @@ public void run(Status status, long index, byte[] reqCtx) { } catch (Throwable t) { MetricsMonitor.raftReadIndexFailed(); - future.completeExceptionally(new ConsistencyException("The conformance protocol is temporarily unavailable for reading", t)); + future.completeExceptionally(new ConsistencyException( + "The conformance protocol is temporarily unavailable for reading", + t)); } return; } MetricsMonitor.raftReadIndexFailed(); Loggers.RAFT.error("ReadIndex has error : {}", status.getErrorMsg()); - future.completeExceptionally( - new ConsistencyException("The conformance protocol is temporarily unavailable for reading, " + status.getErrorMsg())); + future.completeExceptionally(new ConsistencyException( + "The conformance protocol is temporarily unavailable for reading, " + + status.getErrorMsg())); } }); return future; } catch (Throwable e) { MetricsMonitor.raftReadFromLeader(); - Loggers.RAFT.warn("Raft linear read failed, go to Leader read logic : {}", e.toString()); + Loggers.RAFT.warn("Raft linear read failed, go to Leader read logic : {}", + e.toString()); // run raft read readFromLeader(request, future); return future; @@ -333,20 +336,25 @@ public void readFromLeader(final GetRequest request, @Override public void accept(Response response, Throwable throwable) { if (Objects.nonNull(throwable)) { - future.completeExceptionally(new ConsistencyException("The conformance protocol is temporarily unavailable for reading", throwable)); + future.completeExceptionally(new ConsistencyException( + "The conformance protocol is temporarily unavailable for reading", + throwable)); return; } if (response.getSuccess()) { future.complete(response); - } else { - future.completeExceptionally( - new ConsistencyException("The conformance protocol is temporarily unavailable for reading, " + response.getErrMsg())); + } + else { + future.completeExceptionally(new ConsistencyException( + "The conformance protocol is temporarily unavailable for reading, " + + response.getErrMsg())); } } }); } - public CompletableFuture commit(final String group, final Message data, final CompletableFuture future) { + public CompletableFuture commit(final String group, final Message data, + final CompletableFuture future) { LoggerUtils .printIfDebugEnabled(Loggers.RAFT, "data requested this time : {}", data); final RaftGroupTuple tuple = findTupleByGroup(group); @@ -374,12 +382,12 @@ public CompletableFuture commit(final String group, final Message data * Add yourself to the Raft cluster * * @param groupId raft group - * @param selfIp local raft node address - * @param conf {@link Configuration} without self info + * @param selfIp local raft node address + * @param conf {@link Configuration} without self info * @return join success */ void registerSelfToCluster(String groupId, PeerId selfIp, Configuration conf) { - for ( ; ; ) { + for (; ; ) { List peerIds = cliService.getPeers(groupId, conf); if (peerIds.contains(selfIp)) { return; @@ -428,14 +436,10 @@ synchronized void shutdown() { public void applyOperation(Node node, Message data, FailoverClosure closure) { final Task task = new Task(); task.setDone(new NacosClosure(data, status -> { - NacosClosure.NStatus nStatus = (NacosClosure.NStatus) status; - if (Objects.nonNull(nStatus.getThrowable())) { - closure.setThrowable(nStatus.getThrowable()); - } - else { - closure.setData(nStatus.getResult()); - } - closure.run(nStatus); + NacosClosure.NacosStatus nacosStatus = (NacosClosure.NacosStatus) status; + closure.setThrowable(nacosStatus.getThrowable()); + closure.setResponse(nacosStatus.getResponse()); + closure.run(nacosStatus); })); task.setData(ByteBuffer.wrap(data.toByteArray())); node.apply(task); @@ -452,10 +456,11 @@ private void invokeToLeader(final String group, final Message request, public void complete(Object o, Throwable ex) { if (Objects.nonNull(ex)) { closure.setThrowable(ex); - closure.run(new Status(RaftError.UNKNOWN, ex.getMessage())); + closure.run( + new Status(RaftError.UNKNOWN, ex.getMessage())); return; } - closure.setData(o); + closure.setResponse((Response) o); closure.run(Status.OK()); } @@ -491,7 +496,8 @@ public void accept(String group, RaftGroupTuple tuple) { RestResult result = maintainService.execute(params); if (result.ok()) { successCnt.incrementAndGet(); - } else { + } + else { Loggers.RAFT.error("Node removal failed : {}", result); } } @@ -513,7 +519,8 @@ void refreshRouteTable(String group) { Configuration oldConf = instance.getConfiguration(groupName); String oldLeader = Optional.ofNullable(instance.selectLeader(groupName)) .orElse(PeerId.emptyPeer()).getEndpoint().toString(); - status = instance.refreshConfiguration(this.cliClientService, groupName, rpcRequestTimeoutMs); + status = instance.refreshConfiguration(this.cliClientService, groupName, + rpcRequestTimeoutMs); if (!status.isOk()) { Loggers.RAFT .error("Fail to refresh route configuration for group : {}, status is : {}", diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/NacosClosure.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/NacosClosure.java index ad808636e30..515b5303a28 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/NacosClosure.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/NacosClosure.java @@ -16,8 +16,10 @@ package com.alibaba.nacos.core.distributed.raft; +import com.alibaba.nacos.consistency.entity.Response; import com.alipay.sofa.jraft.Closure; import com.alipay.sofa.jraft.Status; +import com.alipay.sofa.jraft.error.RaftError; import com.google.protobuf.Message; /** @@ -25,76 +27,113 @@ */ public class NacosClosure implements Closure { - private final Message log; + private final Message message; private final Closure closure; - private Throwable throwable; - private Object object; + private final NacosStatus nacosStatus = new NacosStatus(); - public NacosClosure(Message log, Closure closure) { - this.log = log; + public NacosClosure(Message message, Closure closure) { + this.message = message; this.closure = closure; } @Override public void run(Status status) { - if (closure != null) { - NStatus status1 = new NStatus(status, throwable); - status1.setResult(object); - closure.run(status1); - } - } - - public Object getObject() { - return object; + nacosStatus.setStatus(status); + closure.run(nacosStatus); } - public void setObject(Object object) { - this.object = object; + public void setResponse(Response response) { + this.nacosStatus.setResponse(response); } public void setThrowable(Throwable throwable) { - this.throwable = throwable; + this.nacosStatus.setThrowable(throwable); } - public Closure getClosure() { - return closure; - } - - public Message getLog() { - return log; + public Message getMessage() { + return message; } // Pass the Throwable inside the state machine to the outer layer @SuppressWarnings("PMD.ClassNamingShouldBeCamelRule") - public static class NStatus extends Status { + public static class NacosStatus extends Status { private Status status; - private Object result; + private Response response = null; - private Throwable throwable; + private Throwable throwable = null; - public NStatus(Status status, Throwable throwable) { - super(); + public void setStatus(Status status) { this.status = status; - this.throwable = throwable; } - public Status getStatus() { - return status; + @Override + public void reset() { + status.reset(); } - public void setStatus(Status status) { - this.status = status; + @Override + public boolean isOk() { + return status.isOk(); + } + + @Override + public void setCode(int code) { + status.setCode(code); + } + + @Override + public int getCode() { + return status.getCode(); + } + + @Override + public RaftError getRaftError() { + return status.getRaftError(); + } + + @Override + public void setErrorMsg(String errMsg) { + status.setErrorMsg(errMsg); + } + + @Override + public void setError(int code, String fmt, Object... args) { + status.setError(code, fmt, args); + } + + @Override + public void setError(RaftError error, String fmt, Object... args) { + status.setError(error, fmt, args); + } + + @Override + public String toString() { + return status.toString(); + } + + @Override + public Status copy() { + NacosStatus copy = new NacosStatus(); + copy.status = this.status; + copy.response = this.response; + copy.throwable = this.throwable; + return copy; + } + + @Override + public String getErrorMsg() { + return status.getErrorMsg(); } - public Object getResult() { - return result; + public Response getResponse() { + return response; } - public void setResult(Object result) { - this.result = result; + public void setResponse(Response response) { + this.response = response; } public Throwable getThrowable() { diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/NacosStateMachine.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/NacosStateMachine.java index 937bb3372c9..d62f2159937 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/NacosStateMachine.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/NacosStateMachine.java @@ -32,7 +32,6 @@ import com.alibaba.nacos.core.distributed.raft.utils.JRaftUtils; import com.alibaba.nacos.core.notify.NotifyCenter; import com.alibaba.nacos.core.utils.Loggers; -import com.alibaba.nacos.core.utils.TimerContext; import com.alipay.sofa.jraft.Closure; import com.alipay.sofa.jraft.Iterator; import com.alipay.sofa.jraft.Node; @@ -94,7 +93,7 @@ public void onApply(Iterator iter) { try { if (iter.done() != null) { closure = (NacosClosure) iter.done(); - message = closure.getLog(); + message = closure.getMessage(); } else { final ByteBuffer data = iter.getData(); @@ -246,9 +245,9 @@ private List allPeers() { RouteTable.getInstance().getConfiguration(node.getGroupId()).getPeers()); } - private void postProcessor(Object data, NacosClosure closure) { + private void postProcessor(Response data, NacosClosure closure) { if (Objects.nonNull(closure)) { - closure.setObject(data); + closure.setResponse(data); } } diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/exception/NoSuchRaftGroupException.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/exception/NoSuchRaftGroupException.java index ac24562de1f..6c1407c3d5d 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/exception/NoSuchRaftGroupException.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/exception/NoSuchRaftGroupException.java @@ -19,7 +19,7 @@ /** * @author liaochuntao */ -public class NoSuchRaftGroupException extends Exception { +public class NoSuchRaftGroupException extends RuntimeException { private static final long serialVersionUID = 1755681688785678765L; diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/processor/AbstractProcessor.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/processor/AbstractProcessor.java index 471eaf357a2..b10e65e2fe4 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/processor/AbstractProcessor.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/processor/AbstractProcessor.java @@ -71,13 +71,13 @@ protected void handleRequest(final JRaftServer server, final String group, final } protected void execute(JRaftServer server, final RpcContext asyncCtx, final Message log, final JRaftServer.RaftGroupTuple tuple) { - FailoverClosure closure = new FailoverClosure() { + FailoverClosure closure = new FailoverClosure() { - Object data; + Response data; Throwable ex; @Override - public void setData(Object data) { + public void setResponse(Response data) { this.data = data; } @@ -93,14 +93,7 @@ public void run(Status status) { asyncCtx.sendResponse(Response.newBuilder().setErrMsg(ex.toString()) .setSuccess(false).build()); } else { - ByteString bytes = Objects.nonNull(data) ? ByteString.copyFrom(serializer.serialize(data)) : ByteString.EMPTY; - - Response response = Response.newBuilder() - .setSuccess(true) - .setData(bytes) - .build(); - - asyncCtx.sendResponse(response); + asyncCtx.sendResponse(data); } } }; diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/utils/FailoverClosure.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/utils/FailoverClosure.java index d33ff6daa2b..7f362dd0cc8 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/utils/FailoverClosure.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/utils/FailoverClosure.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.core.distributed.raft.utils; +import com.alibaba.nacos.consistency.entity.Response; import com.alipay.sofa.jraft.Closure; /** @@ -23,14 +24,14 @@ * * @author liaochuntao */ -public interface FailoverClosure extends Closure { +public interface FailoverClosure extends Closure { /** * Set the return interface if needed * - * @param data data + * @param response {@link Response} data */ - void setData(T data); + void setResponse(Response response); /** * Catch exception diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/utils/FailoverClosureImpl.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/utils/FailoverClosureImpl.java index 1ad8ef58c5d..9ec4b3a13d6 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/utils/FailoverClosureImpl.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/utils/FailoverClosureImpl.java @@ -16,11 +16,11 @@ package com.alibaba.nacos.core.distributed.raft.utils; -import com.alibaba.nacos.common.utils.LoggerUtils; +import com.alibaba.nacos.common.utils.Objects; +import com.alibaba.nacos.consistency.entity.Response; import com.alibaba.nacos.consistency.exception.ConsistencyException; -import com.alibaba.nacos.core.utils.Loggers; import com.alipay.sofa.jraft.Status; -import java.util.Objects; + import java.util.concurrent.CompletableFuture; /** @@ -28,39 +28,36 @@ * * @author liaochuntao */ -public class FailoverClosureImpl implements FailoverClosure { - - private final CompletableFuture future; - private volatile T data; - private volatile Throwable throwable; - - public FailoverClosureImpl(final CompletableFuture future) { - this.future = future; - } - - @Override - public void setData(T data) { - this.data = data; - } - - @Override - public void setThrowable(Throwable throwable) { - this.throwable = throwable; - } - - @Override - public void run(Status status) { - if (status.isOk()) { - boolean success = future.complete(data); - LoggerUtils.printIfDebugEnabled(Loggers.RAFT, "future.complete execute {}", success); - return; - } - final Throwable throwable = this.throwable; - if (Objects.nonNull(throwable)) { - future.completeExceptionally(new ConsistencyException(throwable.toString())); - } else { - future.completeExceptionally(new ConsistencyException("operation failure")); - } - } +public class FailoverClosureImpl implements FailoverClosure { + + private final CompletableFuture future; + private volatile Response data; + private volatile Throwable throwable; + + public FailoverClosureImpl(final CompletableFuture future) { + this.future = future; + } + + @Override + public void setResponse(Response data) { + this.data = data; + } + + @Override + public void setThrowable(Throwable throwable) { + this.throwable = throwable; + } + + @Override + public void run(Status status) { + if (status.isOk()) { + future.complete(data); + return; + } + final Throwable throwable = this.throwable; + future.completeExceptionally(Objects.nonNull(throwable) ? + new ConsistencyException(throwable.toString()) : + new ConsistencyException("operation failure")); + } } From d916d221bf52e892cbe854b0f60de32068f40886 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sun, 7 Jun 2020 19:25:10 +0800 Subject: [PATCH 047/156] refactor: add ut --- .../core/distributed/raft/JRaftServer.java | 17 ++++-- .../raft/processor/AbstractProcessorTest.java | 59 +++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 core/src/test/java/com/alibaba/nacos/core/distributed/raft/processor/AbstractProcessorTest.java diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java index 5d8b619f364..3bb81e67625 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/raft/JRaftServer.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.core.distributed.raft; +import com.alibaba.nacos.common.JustForTest; import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.common.utils.LoggerUtils; @@ -140,7 +141,7 @@ public class JRaftServer { String.valueOf(256 * 1024 * 1024)); } - public JRaftServer() throws Exception { + public JRaftServer() { this.conf = new Configuration(); } @@ -555,12 +556,16 @@ CliService getCliService() { return cliService; } - public class RaftGroupTuple { + public static class RaftGroupTuple { - private final LogProcessor processor; - private final Node node; - private final RaftGroupService raftGroupService; - private final NacosStateMachine machine; + private LogProcessor processor; + private Node node; + private RaftGroupService raftGroupService; + private NacosStateMachine machine; + + @JustForTest + public RaftGroupTuple() { + } public RaftGroupTuple(Node node, LogProcessor processor, RaftGroupService raftGroupService, NacosStateMachine machine) { diff --git a/core/src/test/java/com/alibaba/nacos/core/distributed/raft/processor/AbstractProcessorTest.java b/core/src/test/java/com/alibaba/nacos/core/distributed/raft/processor/AbstractProcessorTest.java new file mode 100644 index 00000000000..8ed84b8b200 --- /dev/null +++ b/core/src/test/java/com/alibaba/nacos/core/distributed/raft/processor/AbstractProcessorTest.java @@ -0,0 +1,59 @@ +package com.alibaba.nacos.core.distributed.raft.processor; + +import com.alibaba.nacos.consistency.SerializeFactory; +import com.alibaba.nacos.consistency.entity.Log; +import com.alibaba.nacos.consistency.entity.Response; +import com.alibaba.nacos.core.distributed.raft.JRaftServer; +import com.alibaba.nacos.core.distributed.raft.utils.FailoverClosure; +import com.alipay.sofa.jraft.Node; +import com.alipay.sofa.jraft.Status; +import com.alipay.sofa.jraft.error.RaftError; +import com.alipay.sofa.jraft.rpc.Connection; +import com.alipay.sofa.jraft.rpc.RpcContext; +import com.google.protobuf.Message; +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.atomic.AtomicReference; + +public class AbstractProcessorTest { + + private JRaftServer server = new JRaftServer() { + @Override + public void applyOperation(Node node, Message data, FailoverClosure closure) { + closure.setResponse(Response.newBuilder().setSuccess(false).setErrMsg("Error message transmission").build()); + closure.run(new Status(RaftError.UNKNOWN, "Error message transmission")); + } + }; + + @Test + public void testErrorThroughRPC() { + final AtomicReference reference = new AtomicReference<>(); + + RpcContext context = new RpcContext() { + @Override + public void sendResponse(Object responseObj) { + reference.set((Response) responseObj); + } + + @Override + public Connection getConnection() { + return null; + } + + @Override + public String getRemoteAddress() { + return null; + } + }; + AbstractProcessor processor = new NacosLogProcessor(server, SerializeFactory.getDefault()); + processor.execute(server, context, Log.newBuilder().build(), new JRaftServer.RaftGroupTuple()); + + Response response = reference.get(); + Assert.assertNotNull(response); + + Assert.assertEquals("Error message transmission", response.getErrMsg()); + Assert.assertFalse(response.getSuccess()); + } + +} \ No newline at end of file From 0f1d0abcc189b6c171424b179ee5636c6d9a9f8b Mon Sep 17 00:00:00 2001 From: Brian Huang Date: Sun, 7 Jun 2020 19:55:21 +0800 Subject: [PATCH 048/156] hi.here 'addAll()' call can be replaced with parametrized constructor call,performance will be better --- .../healthcheck/extend/HealthCheckExtendProvider.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/extend/HealthCheckExtendProvider.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/extend/HealthCheckExtendProvider.java index fe64073688b..ef4b0f51512 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/extend/HealthCheckExtendProvider.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/extend/HealthCheckExtendProvider.java @@ -60,10 +60,8 @@ private void loadExtend() { for(HealthCheckType type : HealthCheckType.values()){ origin.add(type.name()); } - Set processorType = new HashSet<>(); - Set healthCheckerType = new HashSet<>(); - processorType.addAll(origin); - healthCheckerType.addAll(origin); + Set processorType = new HashSet<>(origin); + Set healthCheckerType = new HashSet<>(origin); while(processorIt.hasNext()){ HealthCheckProcessor processor = processorIt.next(); From 71217154bb6300babf50ac681671ab3825286895 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sun, 7 Jun 2020 20:34:11 +0800 Subject: [PATCH 049/156] fix: fix task executable judgment logic problems --- .../server/service/dump/EmbeddedDumpService.java | 3 +++ .../config/server/service/merge/MergeDatumService.java | 10 +++------- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/EmbeddedDumpService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/EmbeddedDumpService.java index 22425e67ea4..67cde2d6db3 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/EmbeddedDumpService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/EmbeddedDumpService.java @@ -160,6 +160,9 @@ private boolean shouldRetry(Throwable ex) { @Override protected boolean canExecute() { + if (ApplicationUtils.getStandaloneMode()) { + return true; + } // if is derby + raft mode, only leader can execute CPProtocol protocol = protocolManager.getCpProtocol(); return protocol.isLeader(Constants.CONFIG_MODEL_RAFT_GROUP); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeDatumService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeDatumService.java index 723198376ee..52fa412de21 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeDatumService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/merge/MergeDatumService.java @@ -109,15 +109,11 @@ private boolean canExecute() { if (!PropertyUtil.isEmbeddedStorage()) { return true; } - try { - ProtocolManager protocolManager = ApplicationUtils.getBean(ProtocolManager.class); - return protocolManager.getCpProtocol().isLeader(Constants.CONFIG_MODEL_RAFT_GROUP); - } catch (NoSuchRaftGroupException e) { + if (ApplicationUtils.getStandaloneMode()) { return true; - } catch (Exception e) { - // It's impossible to get to this point - throw new RuntimeException(e); } + ProtocolManager protocolManager = ApplicationUtils.getBean(ProtocolManager.class); + return protocolManager.getCpProtocol().isLeader(Constants.CONFIG_MODEL_RAFT_GROUP); } class MergeAllDataWorker extends Thread { From 795005be2bfce765241670c2df9fbd894e96db2e Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Mon, 8 Jun 2020 11:01:34 +0800 Subject: [PATCH 050/156] fix: where config publish could not trigger horizontal notification in standalone + derby --- .../EmbeddedStoragePersistServiceImpl.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/EmbeddedStoragePersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/EmbeddedStoragePersistServiceImpl.java index 3285285fd2a..1e34da33212 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/EmbeddedStoragePersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/EmbeddedStoragePersistServiceImpl.java @@ -38,18 +38,20 @@ import com.alibaba.nacos.config.server.model.SameConfigPolicy; import com.alibaba.nacos.config.server.model.SubInfo; import com.alibaba.nacos.config.server.model.TenantInfo; +import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent; import com.alibaba.nacos.config.server.service.datasource.DataSourceService; import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource; import com.alibaba.nacos.config.server.service.sql.EmbeddedStorageContextUtils; import com.alibaba.nacos.config.server.utils.LogUtil; import com.alibaba.nacos.config.server.utils.ParamUtils; +import com.alibaba.nacos.config.server.utils.event.EventDispatcher; import com.alibaba.nacos.core.distributed.id.IdGeneratorManager; +import com.alibaba.nacos.core.utils.ApplicationUtils; import com.google.common.base.Joiner; import com.google.common.collect.Lists; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.springframework.context.annotation.Conditional; -import org.springframework.dao.DataIntegrityViolationException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; import org.springframework.transaction.support.TransactionTemplate; @@ -384,6 +386,12 @@ public void insertOrUpdateBeta(final ConfigInfo configInfo, final String betaIps else { updateConfigInfo4Beta(configInfo, betaIps, srcIp, null, time, notify); } + if (ApplicationUtils.getStandaloneMode()) { + EventDispatcher.fireEvent( + new ConfigDataChangeEvent(true, configInfo.getDataId(), + configInfo.getGroup(), configInfo.getTenant(), + time.getTime())); + } } public void insertOrUpdateTag(final ConfigInfo configInfo, final String tag, @@ -396,6 +404,12 @@ public void insertOrUpdateTag(final ConfigInfo configInfo, final String tag, else { updateConfigInfo4Tag(configInfo, tag, srcIp, null, time, notify); } + if (ApplicationUtils.getStandaloneMode()) { + EventDispatcher.fireEvent( + new ConfigDataChangeEvent(false, configInfo.getDataId(), + configInfo.getGroup(), configInfo.getTenant(), tag, + time.getTime())); + } } public void updateMd5(String dataId, String group, String tenant, String md5, @@ -434,6 +448,12 @@ public void insertOrUpdate(String srcIp, String srcUser, ConfigInfo configInfo, else { updateConfigInfo(configInfo, srcIp, srcUser, time, configAdvanceInfo, notify); } + if (ApplicationUtils.getStandaloneMode()) { + EventDispatcher.fireEvent( + new ConfigDataChangeEvent(false, configInfo.getDataId(), + configInfo.getGroup(), configInfo.getTenant(), + time.getTime())); + } } public void insertOrUpdateSub(SubInfo subInfo) { @@ -2610,3 +2630,4 @@ public int tenantInfoCountByTenantId(String tenantId) { } } + From 902d609f1e14a3c09ed180d3a452922632585e3c Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Mon, 8 Jun 2020 23:38:53 +0800 Subject: [PATCH 051/156] perf: optimize configuration change event publishing --- .../nacos/common/utils/CollectionUtils.java | 7 ++ .../server/controller/ConfigController.java | 43 +++++------ .../ConfigChangePublisher.java} | 19 +++-- .../server/service/LongPollingService.java | 18 +++-- .../EmbeddedStoragePersistServiceImpl.java | 21 ------ .../ExternalStoragePersistServiceImpl.java | 71 ++++--------------- .../service/ConfigChangePublisherTest.java | 69 ++++++++++++++++++ pom.xml | 28 ++++++++ ...API_ITCase.java => ConfigAPI_CITCase.java} | 12 ++-- ...ta_ITCase.java => ConfigBeta_CITCase.java} | 9 ++- ... => ConfigExportAndImportAPI_CITCase.java} | 13 ++-- ... ConfigLongPollReturnChanges_CITCase.java} | 6 +- ...TCase.java => ConfigLongPoll_CITCase.java} | 6 +- ... EmbeddedStorageContextUtils_CITCase.java} | 2 +- 14 files changed, 188 insertions(+), 136 deletions(-) rename config/src/main/java/com/alibaba/nacos/config/server/{filter/ToLeader.java => service/ConfigChangePublisher.java} (55%) create mode 100644 config/src/test/java/com/alibaba/nacos/config/server/service/ConfigChangePublisherTest.java rename test/src/test/java/com/alibaba/nacos/test/config/{ConfigAPI_ITCase.java => ConfigAPI_CITCase.java} (99%) rename test/src/test/java/com/alibaba/nacos/test/config/{ConfigBeta_ITCase.java => ConfigBeta_CITCase.java} (98%) rename test/src/test/java/com/alibaba/nacos/test/config/{ConfigExportAndImportAPI_ITCase.java => ConfigExportAndImportAPI_CITCase.java} (97%) rename test/src/test/java/com/alibaba/nacos/test/config/{ConfigLongPollReturnChanges_ITCase.java => ConfigLongPollReturnChanges_CITCase.java} (97%) rename test/src/test/java/com/alibaba/nacos/test/config/{ConfigLongPoll_ITCase.java => ConfigLongPoll_CITCase.java} (94%) rename test/src/test/java/com/alibaba/nacos/test/config/{EmbeddedStorageContextUtils_ITCase.java => EmbeddedStorageContextUtils_CITCase.java} (97%) diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/CollectionUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/CollectionUtils.java index ffe7d70b4fd..fcff8168cc3 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/CollectionUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/CollectionUtils.java @@ -179,6 +179,13 @@ public static boolean sizeIsEmpty(Object object) { } } + public static boolean contains(Collection coll, T target) { + if (isEmpty(coll)) { + return false; + } + return coll.contains(target); + } + /** * Null-safe check if the specified collection is empty. *

diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java index d0fde3e2fbd..3b2374b38e7 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java @@ -21,7 +21,6 @@ import com.alibaba.nacos.config.server.auth.ConfigResourceParser; import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.controller.parameters.SameNamespaceCloneConfigBean; -import com.alibaba.nacos.config.server.filter.ToLeader; import com.alibaba.nacos.config.server.model.ConfigAdvanceInfo; import com.alibaba.nacos.config.server.model.ConfigAllInfo; import com.alibaba.nacos.config.server.model.ConfigInfo; @@ -34,6 +33,7 @@ import com.alibaba.nacos.config.server.result.code.ResultCodeEnum; import com.alibaba.nacos.config.server.service.AggrWhitelist; import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent; +import com.alibaba.nacos.config.server.service.ConfigChangePublisher; import com.alibaba.nacos.config.server.service.ConfigSubService; import com.alibaba.nacos.config.server.service.repository.PersistService; import com.alibaba.nacos.config.server.service.trace.ConfigTraceService; @@ -42,7 +42,6 @@ import com.alibaba.nacos.config.server.utils.RequestUtil; import com.alibaba.nacos.config.server.utils.TimeUtils; import com.alibaba.nacos.config.server.utils.ZipUtils; -import com.alibaba.nacos.config.server.utils.event.EventDispatcher; import com.alibaba.nacos.core.auth.ActionTypes; import com.alibaba.nacos.core.auth.Secured; import com.alibaba.nacos.core.utils.InetUtils; @@ -113,17 +112,16 @@ public ConfigController(ConfigServletInner configServletInner, } /** - * 增加或更新非聚合数据。 + * Adds or updates non-aggregated data. * * @throws NacosException */ @PostMapping - @ToLeader @Secured(action = ActionTypes.WRITE, parser = ConfigResourceParser.class) public Boolean publishConfig(HttpServletRequest request, HttpServletResponse response, - @RequestParam("dataId") String dataId, @RequestParam("group") String group, + @RequestParam(value = "dataId") String dataId, @RequestParam(value = "group") String group, @RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant, - @RequestParam("content") String content, + @RequestParam(value = "content") String content, @RequestParam(value = "tag", required = false) String tag, @RequestParam(value = "appName", required = false) String appName, @RequestParam(value = "src_user", required = false) String srcUser, @@ -165,16 +163,19 @@ public Boolean publishConfig(HttpServletRequest request, HttpServletResponse res if (StringUtils.isBlank(tag)) { persistService.insertOrUpdate(srcIp, srcUser, configInfo, time, configAdvanceInfo, true); + ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, dataId, group, tenant, time.getTime())); } else { persistService .insertOrUpdateTag(configInfo, tag, srcIp, srcUser, time, true); + ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, dataId, group, tenant, tag, time.getTime())); } } else { // beta publish persistService .insertOrUpdateBeta(configInfo, betaIps, srcIp, srcUser, time, true); + ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(true, dataId, group, tenant, time.getTime())); } ConfigTraceService.logPersistenceEvent(dataId, group, tenant, requestIpApp, time.getTime(), InetUtils.getSelfIp(), ConfigTraceService.PERSISTENCE_EVENT_PUB, content); @@ -182,7 +183,7 @@ public Boolean publishConfig(HttpServletRequest request, HttpServletResponse res } /** - * 取数据 + * get configure board infomation fail * * @throws ServletException * @throws IOException @@ -207,7 +208,7 @@ public void getConfig(HttpServletRequest request, HttpServletResponse response, } /** - * 取数据 + * Get the specific configuration information that the console USES * * @throws NacosException */ @@ -226,12 +227,11 @@ public ConfigAllInfo detailConfigInfo(HttpServletRequest request, } /** - * 同步删除某个dataId下面所有的聚合前数据 + * Synchronously delete all pre-aggregation data under a dataId * * @throws NacosException */ @DeleteMapping - @ToLeader @Secured(action = ActionTypes.WRITE, parser = ConfigResourceParser.class) public Boolean deleteConfig(HttpServletRequest request, HttpServletResponse response, @RequestParam("dataId") String dataId, // @@ -255,6 +255,7 @@ public Boolean deleteConfig(HttpServletRequest request, HttpServletResponse resp ConfigTraceService .logPersistenceEvent(dataId, group, tenant, null, time.getTime(), clientIp, ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null); + ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, dataId, group, tenant, tag, time.getTime())); return true; } @@ -266,7 +267,6 @@ public Boolean deleteConfig(HttpServletRequest request, HttpServletResponse resp * @Param [request, response, dataId, group, tenant, tag] */ @DeleteMapping(params = "delType=ids") - @ToLeader @Secured(action = ActionTypes.WRITE, parser = ConfigResourceParser.class) public RestResult deleteConfigs(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "ids") List ids) { @@ -276,6 +276,8 @@ public RestResult deleteConfigs(HttpServletRequest request, .removeConfigInfoByIds(ids, clientIp, null); if (!CollectionUtils.isEmpty(configInfoList)) { for (ConfigInfo configInfo : configInfoList) { + ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, configInfo.getDataId(), + configInfo.getGroup(), configInfo.getTenant(), time.getTime())); ConfigTraceService.logPersistenceEvent(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), null, time.getTime(), clientIp, @@ -299,7 +301,7 @@ public RestResult getConfigAdvanceInfo( } /** - * 比较MD5 + * The client listens for configuration changes */ @PostMapping("/listener") @Secured(action = ActionTypes.READ, parser = ConfigResourceParser.class) @@ -326,7 +328,7 @@ public void listener(HttpServletRequest request, HttpServletResponse response) } /** - * 订阅改配置的客户端信息 + * Subscribe to configured client information */ @GetMapping("/listener") @Secured(action = ActionTypes.READ, parser = ConfigResourceParser.class) @@ -348,7 +350,7 @@ public GroupkeyListenserStatus getListeners(@RequestParam("dataId") String dataI } /** - * 查询配置信息,返回JSON格式。 + * Query the configuration information and return it in JSON format. */ @GetMapping(params = "search=accurate") @Secured(action = ActionTypes.READ, parser = ConfigResourceParser.class) @@ -379,7 +381,8 @@ public Page searchConfig(@RequestParam("dataId") String dataId, } /** - * 模糊查询配置信息。不允许只根据内容模糊查询,即dataId和group都为NULL,但content不是NULL。这种情况下,返回所有配置。 + * Fuzzy query configuration information. Fuzzy queries based only on content are not allowed, that is, + * both dataId and group are NULL, but content is not NULL. In this case, all configurations are returned. */ @GetMapping(params = "search=blur") @Secured(action = ActionTypes.READ, parser = ConfigResourceParser.class) @@ -410,7 +413,6 @@ public Page fuzzySearchConfig(@RequestParam("dataId") String dataId, } @DeleteMapping(params = "beta=true") - @ToLeader @Secured(action = ActionTypes.WRITE, parser = ConfigResourceParser.class) public RestResult stopBeta(@RequestParam(value = "dataId") String dataId, @RequestParam(value = "group") String group, @@ -426,6 +428,7 @@ public RestResult stopBeta(@RequestParam(value = "dataId") String dataI rr.setMessage("remove beta data error"); return rr; } + ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(true, dataId, group, tenant, System.currentTimeMillis())); rr.setCode(200); rr.setData(true); rr.setMessage("stop beta ok"); @@ -504,7 +507,6 @@ public ResponseEntity exportConfig( } @PostMapping(params = "import=true") - @ToLeader @Secured(action = ActionTypes.WRITE, parser = ConfigResourceParser.class) public RestResult> importAndPublishConfig( HttpServletRequest request, @@ -593,7 +595,7 @@ public RestResult> importAndPublishConfig( .batchInsertOrUpdate(configInfoList, srcUser, srcIp, null, time, false, policy); for (ConfigInfo configInfo : configInfoList) { - EventDispatcher.fireEvent( + ConfigChangePublisher.notifyConfigChange( new ConfigDataChangeEvent(false, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), time.getTime())); @@ -607,7 +609,6 @@ public RestResult> importAndPublishConfig( } @PostMapping(params = "clone=true") - @ToLeader @Secured(action = ActionTypes.WRITE, parser = ConfigResourceParser.class) public RestResult> cloneConfig(HttpServletRequest request, @RequestParam(value = "src_user", required = false) String srcUser, @@ -681,7 +682,7 @@ else if (persistService.tenantInfoCountByTenantId(namespace) <= 0) { .batchInsertOrUpdate(configInfoList4Clone, srcUser, srcIp, null, time, false, policy); for (ConfigInfo configInfo : configInfoList4Clone) { - EventDispatcher.fireEvent( + ConfigChangePublisher.notifyConfigChange( new ConfigDataChangeEvent(false, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), time.getTime())); @@ -691,7 +692,7 @@ else if (persistService.tenantInfoCountByTenantId(namespace) <= 0) { InetUtils.getSelfIp(), ConfigTraceService.PERSISTENCE_EVENT_PUB, configInfo.getContent()); } - return ResultBuilder.buildSuccessResult("克隆成功", saveResult); + return ResultBuilder.buildSuccessResult("Clone Completed Successfully", saveResult); } private String processTenant(String tenant) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/filter/ToLeader.java b/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigChangePublisher.java similarity index 55% rename from config/src/main/java/com/alibaba/nacos/config/server/filter/ToLeader.java rename to config/src/main/java/com/alibaba/nacos/config/server/service/ConfigChangePublisher.java index 7cdef916660..69821a11396 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/filter/ToLeader.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigChangePublisher.java @@ -14,14 +14,23 @@ * limitations under the License. */ -package com.alibaba.nacos.config.server.filter; +package com.alibaba.nacos.config.server.service; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; +import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent; +import com.alibaba.nacos.config.server.utils.PropertyUtil; +import com.alibaba.nacos.config.server.utils.event.EventDispatcher; +import com.alibaba.nacos.core.utils.ApplicationUtils; /** * @author liaochuntao */ -@Retention(RetentionPolicy.RUNTIME) -public @interface ToLeader { +public class ConfigChangePublisher { + + public static void notifyConfigChange(ConfigDataChangeEvent event) { + if (PropertyUtil.isEmbeddedStorage() && !ApplicationUtils.getStandaloneMode()) { + return; + } + EventDispatcher.fireEvent(event); + } + } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/LongPollingService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/LongPollingService.java index bb918d180b7..a00c459f2ba 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/LongPollingService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/LongPollingService.java @@ -15,6 +15,8 @@ */ package com.alibaba.nacos.config.server.service; +import com.alibaba.nacos.common.utils.CollectionUtils; +import com.alibaba.nacos.common.utils.ExceptionUtil; import com.alibaba.nacos.config.server.model.SampleResult; import com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent; import com.alibaba.nacos.config.server.monitor.MetricsMonitor; @@ -308,7 +310,7 @@ public void run() { ClientLongPolling clientSub = iter.next(); if (clientSub.clientMd5Map.containsKey(groupKey)) { // 如果beta发布且不在beta列表直接跳过 - if (isBeta && !betaIps.contains(clientSub.ip)) { + if (isBeta && !CollectionUtils.contains(betaIps, clientSub.ip)) { continue; } @@ -329,14 +331,10 @@ public void run() { } } } catch (Throwable t) { - LogUtil.defaultLog.error("data change error:" + t.getMessage(), t.getCause()); + LogUtil.defaultLog.error("data change error: {}", ExceptionUtil.getStackTrace(t)); } } - DataChangeTask(String groupKey) { - this(groupKey, false, null); - } - DataChangeTask(String groupKey, boolean isBeta, List betaIps) { this(groupKey, isBeta, betaIps, null); } @@ -475,6 +473,14 @@ void generateResponse(List changedGroups) { final long timeoutTime; Future asyncTimeoutFuture; + + @Override + public String toString() { + return "ClientLongPolling{" + "clientMd5Map=" + clientMd5Map + ", createTime=" + + createTime + ", ip='" + ip + '\'' + ", appName='" + appName + '\'' + + ", tag='" + tag + '\'' + ", probeRequestSize=" + probeRequestSize + + ", timeoutTime=" + timeoutTime + '}'; + } } void generateResponse(HttpServletRequest request, HttpServletResponse response, List changedGroups) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/EmbeddedStoragePersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/EmbeddedStoragePersistServiceImpl.java index 1e34da33212..35587e8e009 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/EmbeddedStoragePersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/EmbeddedStoragePersistServiceImpl.java @@ -38,15 +38,12 @@ import com.alibaba.nacos.config.server.model.SameConfigPolicy; import com.alibaba.nacos.config.server.model.SubInfo; import com.alibaba.nacos.config.server.model.TenantInfo; -import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent; import com.alibaba.nacos.config.server.service.datasource.DataSourceService; import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource; import com.alibaba.nacos.config.server.service.sql.EmbeddedStorageContextUtils; import com.alibaba.nacos.config.server.utils.LogUtil; import com.alibaba.nacos.config.server.utils.ParamUtils; -import com.alibaba.nacos.config.server.utils.event.EventDispatcher; import com.alibaba.nacos.core.distributed.id.IdGeneratorManager; -import com.alibaba.nacos.core.utils.ApplicationUtils; import com.google.common.base.Joiner; import com.google.common.collect.Lists; import org.apache.commons.collections.CollectionUtils; @@ -386,12 +383,6 @@ public void insertOrUpdateBeta(final ConfigInfo configInfo, final String betaIps else { updateConfigInfo4Beta(configInfo, betaIps, srcIp, null, time, notify); } - if (ApplicationUtils.getStandaloneMode()) { - EventDispatcher.fireEvent( - new ConfigDataChangeEvent(true, configInfo.getDataId(), - configInfo.getGroup(), configInfo.getTenant(), - time.getTime())); - } } public void insertOrUpdateTag(final ConfigInfo configInfo, final String tag, @@ -404,12 +395,6 @@ public void insertOrUpdateTag(final ConfigInfo configInfo, final String tag, else { updateConfigInfo4Tag(configInfo, tag, srcIp, null, time, notify); } - if (ApplicationUtils.getStandaloneMode()) { - EventDispatcher.fireEvent( - new ConfigDataChangeEvent(false, configInfo.getDataId(), - configInfo.getGroup(), configInfo.getTenant(), tag, - time.getTime())); - } } public void updateMd5(String dataId, String group, String tenant, String md5, @@ -448,12 +433,6 @@ public void insertOrUpdate(String srcIp, String srcUser, ConfigInfo configInfo, else { updateConfigInfo(configInfo, srcIp, srcUser, time, configAdvanceInfo, notify); } - if (ApplicationUtils.getStandaloneMode()) { - EventDispatcher.fireEvent( - new ConfigDataChangeEvent(false, configInfo.getDataId(), - configInfo.getGroup(), configInfo.getTenant(), - time.getTime())); - } } public void insertOrUpdateSub(SubInfo subInfo) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/ExternalStoragePersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/ExternalStoragePersistServiceImpl.java index e26837ac677..585c316f91c 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/ExternalStoragePersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/ExternalStoragePersistServiceImpl.java @@ -38,13 +38,10 @@ import com.alibaba.nacos.config.server.model.SameConfigPolicy; import com.alibaba.nacos.config.server.model.SubInfo; import com.alibaba.nacos.config.server.model.TenantInfo; -import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent; import com.alibaba.nacos.config.server.service.datasource.DataSourceService; import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource; -import com.alibaba.nacos.config.server.service.trace.ConfigTraceService; import com.alibaba.nacos.config.server.utils.LogUtil; import com.alibaba.nacos.config.server.utils.ParamUtils; -import com.alibaba.nacos.config.server.utils.event.EventDispatcher; import com.google.common.base.Joiner; import com.google.common.collect.Lists; import org.apache.commons.collections.CollectionUtils; @@ -184,12 +181,6 @@ public void addConfigInfo(final String srcIp, final String srcUser, addConfigTagsRelation(configId, configTags, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant()); insertConfigHistoryAtomic(0, configInfo, srcIp, srcUser, time, "I"); - if (notify) { - EventDispatcher.fireEvent( - new ConfigDataChangeEvent(false, configInfo.getDataId(), - configInfo.getGroup(), configInfo.getTenant(), - time.getTime())); - } } catch (CannotGetJdbcConnectionException e) { LogUtil.fatalLog.error("[db-error] " + e.toString(), e); @@ -349,10 +340,6 @@ public void insertOrUpdateBeta(final ConfigInfo configInfo, updateConfigInfo4Beta(configInfo, betaIps, srcIp, null, time, notify); } - EventDispatcher.fireEvent( - new ConfigDataChangeEvent(true, configInfo.getDataId(), - configInfo.getGroup(), configInfo.getTenant(), - time.getTime())); } public void insertOrUpdateTag(final ConfigInfo configInfo, @@ -364,10 +351,6 @@ public void insertOrUpdateTag(final ConfigInfo configInfo, catch (DataIntegrityViolationException ive) { // 唯一性约束冲突 updateConfigInfo4Tag(configInfo, tag, srcIp, null, time, notify); } - EventDispatcher.fireEvent( - new ConfigDataChangeEvent(false, configInfo.getDataId(), - configInfo.getGroup(), configInfo.getTenant(), tag, - time.getTime())); } /** @@ -406,10 +389,6 @@ public void insertOrUpdate(String srcIp, String srcUser, updateConfigInfo(configInfo, srcIp, srcUser, time, configAdvanceInfo, notify); } - EventDispatcher.fireEvent( - new ConfigDataChangeEvent(false, configInfo.getDataId(), - configInfo.getGroup(), configInfo.getTenant(), - time.getTime())); } /** @@ -452,9 +431,6 @@ public Boolean doInTransaction(TransactionStatus status) { return Boolean.TRUE; } }); - - EventDispatcher.fireEvent(new ConfigDataChangeEvent(false, dataId, group, tenant, - System.currentTimeMillis())); } /** @@ -496,20 +472,6 @@ public List doInTransaction(TransactionStatus status) { } } }); - - if (!CollectionUtils.isEmpty(result)) { - long currentTime = System.currentTimeMillis(); - for (ConfigInfo configInfo : result) { - ConfigTraceService.logPersistenceEvent(configInfo.getDataId(), - configInfo.getGroup(), configInfo.getTenant(), null, currentTime, - srcIp, ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null); - EventDispatcher.fireEvent( - new ConfigDataChangeEvent(false, configInfo.getDataId(), - configInfo.getGroup(), configInfo.getTenant(), - currentTime)); - } - } - return result; } @@ -519,27 +481,21 @@ public List doInTransaction(TransactionStatus status) { public void removeConfigInfo4Beta(final String dataId, final String group, final String tenant) { final String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant; - tjt.execute(new TransactionCallback() { - @Override - public Boolean doInTransaction(TransactionStatus status) { - try { - ConfigInfo configInfo = findConfigInfo4Beta(dataId, group, tenant); - if (configInfo != null) { - jt.update( - "DELETE FROM config_info_beta WHERE data_id=? AND group_id=? AND tenant_id=?", - dataId, group, tenantTmp); - } - } - catch (CannotGetJdbcConnectionException e) { - LogUtil.fatalLog.error("[db-error] " + e.toString(), e); - throw e; + tjt.execute(status -> { + try { + ConfigInfo configInfo = findConfigInfo4Beta(dataId, group, tenant); + if (configInfo != null) { + jt.update( + "DELETE FROM config_info_beta WHERE data_id=? AND group_id=? AND tenant_id=?", + dataId, group, tenantTmp); } - return Boolean.TRUE; } + catch (CannotGetJdbcConnectionException e) { + LogUtil.fatalLog.error("[db-error] " + e.toString(), e); + throw e; + } + return Boolean.TRUE; }); - - EventDispatcher.fireEvent(new ConfigDataChangeEvent(true, dataId, group, tenant, - System.currentTimeMillis())); } // ----------------------- config_aggr_info 表 insert update delete @@ -2688,9 +2644,6 @@ public void removeConfigInfoTag(final String dataId, final String group, LogUtil.fatalLog.error("[db-error] " + e.toString(), e); throw e; } - EventDispatcher.fireEvent( - new ConfigDataChangeEvent(false, dataId, group, tenant, tag, - System.currentTimeMillis())); } /** diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/ConfigChangePublisherTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/ConfigChangePublisherTest.java new file mode 100644 index 00000000000..2cd06b8f6b0 --- /dev/null +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/ConfigChangePublisherTest.java @@ -0,0 +1,69 @@ +package com.alibaba.nacos.config.server.service; + +import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent; +import com.alibaba.nacos.config.server.utils.PropertyUtil; +import com.alibaba.nacos.config.server.utils.event.EventDispatcher; +import com.alibaba.nacos.core.utils.ApplicationUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; + +public class ConfigChangePublisherTest { + + @Test + public void testConfigChangeNotify() { + + AtomicReference reference = new AtomicReference<>(); + + EventDispatcher.addEventListener(new EventDispatcher.AbstractEventListener() { + @Override + public List> interest() { + return Collections.singletonList(ConfigDataChangeEvent.class); + } + + @Override + public void onEvent(EventDispatcher.Event event) { + reference.set((ConfigDataChangeEvent) event); + } + }); + + // nacos is standalone mode and use embedded storage + ApplicationUtils.setIsStandalone(true); + PropertyUtil.setEmbeddedStorage(true); + + ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent("chuntaojun", "chuntaojun", System.currentTimeMillis())); + Assert.assertNotNull(reference.get()); + reference.set(null); + + + // nacos is standalone mode and use external storage + ApplicationUtils.setIsStandalone(true); + PropertyUtil.setEmbeddedStorage(false); + + ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent("chuntaojun", "chuntaojun", System.currentTimeMillis())); + Assert.assertNotNull(reference.get()); + reference.set(null); + + + // nacos is cluster mode and use embedded storage + ApplicationUtils.setIsStandalone(false); + PropertyUtil.setEmbeddedStorage(true); + + ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent("chuntaojun", "chuntaojun", System.currentTimeMillis())); + Assert.assertNull(reference.get()); + reference.set(null); + + + // nacos is cluster mode and use external storage + ApplicationUtils.setIsStandalone(false); + PropertyUtil.setEmbeddedStorage(false); + + ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent("chuntaojun", "chuntaojun", System.currentTimeMillis())); + Assert.assertNotNull(reference.get()); + reference.set(null); + } + +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 0e5903bab54..06cb1596863 100644 --- a/pom.xml +++ b/pom.xml @@ -460,6 +460,34 @@ + + cit-test + + + + maven-failsafe-plugin + ${maven-failsafe-plugin.version} + + @{failsafeArgLine} + -Dnacos.standalone=true + + **/*CITCase.java + + + + + + integration-test + verify + + + + + + + + + it-test diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_CITCase.java similarity index 99% rename from test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java rename to test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_CITCase.java index d725382667c..b3aa749b52b 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_CITCase.java @@ -50,9 +50,9 @@ * @author xiaochun.xxc */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class ConfigAPI_ITCase { +@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos", "server.port=7001"}, + webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +public class ConfigAPI_CITCase { public static final long TIME_OUT = 5000; static ConfigService iconfig = null; @@ -378,7 +378,7 @@ public void nacos_addListener_2() throws Exception { * @author xiaochun.xxc * @since 3.6.8 */ - @Test(timeout = 5 * TIME_OUT) + @Test(timeout = Constants.CONFIG_LONG_POLL_TIMEOUT << 2) public void nacos_addListener_3() throws InterruptedException, NacosException { final AtomicInteger count = new AtomicInteger(0); final String dataId = "nacos_addListener_3"; @@ -386,7 +386,6 @@ public void nacos_addListener_3() throws InterruptedException, NacosException { final String content = "test-abc"; final String newContent = "nacos_addListener_3"; boolean result = iconfig.publishConfig(dataId, group, content); - Thread.sleep(TIME_OUT); Assert.assertTrue(result); Listener ml = new AbstractListener() { @@ -399,9 +398,6 @@ public void receiveConfigInfo(String configInfo) { iconfig.addListener(dataId, group, ml); result = iconfig.publishConfig(dataId, group, newContent); Assert.assertTrue(result); - while (count.get() == 0) { - Thread.sleep(2000); - } // Get enough sleep to ensure that the monitor is triggered only once // during the two long training sessions ThreadUtils.sleep(Constants.CONFIG_LONG_POLL_TIMEOUT << 1); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigBeta_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigBeta_CITCase.java similarity index 98% rename from test/src/test/java/com/alibaba/nacos/test/config/ConfigBeta_ITCase.java rename to test/src/test/java/com/alibaba/nacos/test/config/ConfigBeta_CITCase.java index 5fdee08e6d2..19731048211 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigBeta_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigBeta_CITCase.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.Nacos; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.common.utils.JacksonUtils; +import com.alibaba.nacos.common.utils.ThreadUtils; import com.alibaba.nacos.test.base.Params; import org.junit.Assert; import org.junit.Before; @@ -41,9 +42,9 @@ * @date 2019-07-03 **/ @RunWith(SpringRunner.class) -@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class ConfigBeta_ITCase { +@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos", "server.port=7002"}, + webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +public class ConfigBeta_CITCase { @LocalServerPort private int port; @@ -178,6 +179,8 @@ public void publishBetaConfig_noBetaIps() throws Exception { Assert.assertTrue(response.getStatusCode().is2xxSuccessful()); Assert.assertEquals("true", response.getBody()); + ThreadUtils.sleep(10_000L); + ResponseEntity response1 = request(CONFIG_CONTROLLER_PATH + "/configs?beta=false", Params.newParams() .appendParam("dataId", dataId) diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_CITCase.java similarity index 97% rename from test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java rename to test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_CITCase.java index a1e21e476a0..4db7e4e16a9 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_CITCase.java @@ -41,9 +41,9 @@ * @date 2019/5/23 15:26 */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class ConfigExportAndImportAPI_ITCase { +@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos", "server.port=7003"}, + webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +public class ConfigExportAndImportAPI_CITCase { private static final long TIME_OUT = 2000; private static final String CONFIG_CONTROLLER_PATH = "/v1/cs/configs"; @@ -121,7 +121,7 @@ public void cleanup(){ } } - @Test(timeout = 3*TIME_OUT) + @Test() public void testExportByIds(){ String getDataUrl = "?search=accurate&dataId=&group=&appName=&config_tags=&pageNo=1&pageSize=10&tenant=&namespaceId="; String queryResult = httpClient.get(SERVER_ADDR + CONFIG_CONTROLLER_PATH + getDataUrl, null); @@ -129,8 +129,9 @@ public void testExportByIds(){ JsonNode resultConfigs = resultObj.get("pageItems"); JsonNode config1 = resultConfigs.get(0); JsonNode config2 = resultConfigs.get(1); - String exportByIdsUrl = "?export=true&tenant=&group=&appName=&ids=" + config1.get("id").longValue() - + "," + config2.get("id").longValue(); + String id1 = config1.get("id").asText(); + String id2 = config2.get("id").asText(); + String exportByIdsUrl = "?export=true&tenant=&group=&appName=&ids=" + id1 + "," + id2; System.out.println(exportByIdsUrl); byte[] zipData = httpClient.download(SERVER_ADDR + CONFIG_CONTROLLER_PATH + exportByIdsUrl, null); ZipUtils.UnZipResult unZiped = ZipUtils.unzip(zipData); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_CITCase.java similarity index 97% rename from test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java rename to test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_CITCase.java index c3e2a5ff42b..3885b8838a7 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_CITCase.java @@ -37,9 +37,9 @@ import java.util.concurrent.TimeUnit; @RunWith(SpringRunner.class) -@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class ConfigLongPollReturnChanges_ITCase { +@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos", "server.port=7005"}, + webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +public class ConfigLongPollReturnChanges_CITCase { @LocalServerPort private int port; diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_CITCase.java similarity index 94% rename from test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java rename to test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_CITCase.java index 3ca5df344de..8335f990d38 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_CITCase.java @@ -39,9 +39,9 @@ * @date 2019-06-07 22:24 **/ @RunWith(SpringRunner.class) -@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class ConfigLongPoll_ITCase { +@SpringBootTest(classes = Nacos.class, properties = {"server.servlet.context-path=/nacos", "server.port=7004"}, + webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +public class ConfigLongPoll_CITCase { @LocalServerPort private int port; diff --git a/test/src/test/java/com/alibaba/nacos/test/config/EmbeddedStorageContextUtils_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/EmbeddedStorageContextUtils_CITCase.java similarity index 97% rename from test/src/test/java/com/alibaba/nacos/test/config/EmbeddedStorageContextUtils_ITCase.java rename to test/src/test/java/com/alibaba/nacos/test/config/EmbeddedStorageContextUtils_CITCase.java index f463a84a251..3bbc9bbb105 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/EmbeddedStorageContextUtils_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/EmbeddedStorageContextUtils_CITCase.java @@ -29,7 +29,7 @@ /** * @author liaochuntao */ -public class EmbeddedStorageContextUtils_ITCase { +public class EmbeddedStorageContextUtils_CITCase { @Test public void test_multi_thread_sql_contexts() throws Exception { From 769fe0254f827472b7f78317fb654c99e5f427a1 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Mon, 8 Jun 2020 23:39:50 +0800 Subject: [PATCH 052/156] ci: modify travis.yml --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index b8a83104ac6..fc6fa7748bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,6 +29,7 @@ before_install: script: - mvn -B clean package apache-rat:check findbugs:findbugs -Dmaven.test.skip=true - mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U + - mvn clean install -Pcit-test - mvn clean package -Pit-test after_success: - mvn clean package -Pit-test From 8f7fb0f65a51cf959017ee2140ae5a62c8b5cdd4 Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Tue, 9 Jun 2020 14:10:47 +0800 Subject: [PATCH 053/156] [#1815]remove no used package name from Factory Class and adjust test cases. --- .../nacos/client/naming/core/HostReactor.java | 15 +++++++++++++-- .../common/LifeCycle/LifeCycleManagerTest.java | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java index a15e4a94adc..b6d1ccef476 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java @@ -29,8 +29,19 @@ import com.alibaba.nacos.common.utils.ThreadUtils; import org.apache.commons.lang3.StringUtils; -import java.util.*; -import java.util.concurrent.*; +import java.util.HashSet; +import java.util.Map; +import java.util.HashMap; +import java.util.Set; +import java.util.List; +import java.util.ArrayList; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; + import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; diff --git a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java index 7c85c3eda59..0f1c6f62962 100644 --- a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java @@ -65,7 +65,7 @@ public void testLifeCycleManager_deregister() throws Exception{ Assert.assertEquals(1, temp.getCounter()); temp.destroy(); Assert.assertEquals(0, temp.getCounter()); - Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); + Assert.assertEquals(1, ResourceLifeCycleManager.getRegisterResourceNum()); } class Resource implements LifeCycle { From 2efe3175971e273399afc6cf116100e715c57eda Mon Sep 17 00:00:00 2001 From: zhouhailin Date: Tue, 9 Jun 2020 23:02:18 +0800 Subject: [PATCH 054/156] =?UTF-8?q?FIX=20ISSUE=20#3007=20:=20MaxHistory?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=90=8D=E7=A7=B0=E5=92=8CNacos=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E7=9A=84=E5=91=BD=E5=90=8D=E9=A3=8E=E6=A0=BC=E4=B8=8D?= =?UTF-8?q?=E7=BB=9F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../META-INF/logback/nacos-included.xml | 2 +- .../META-INF/logback/config-included.xml | 24 ++++----- .../main/resources/META-INF/logback/nacos.xml | 12 ++--- distribution/conf/nacos-logback.xml | 50 +++++++++---------- .../META-INF/logback/naming-included.xml | 24 ++++----- 5 files changed, 56 insertions(+), 56 deletions(-) diff --git a/address/src/main/resources/META-INF/logback/nacos-included.xml b/address/src/main/resources/META-INF/logback/nacos-included.xml index 44f88e2c072..ed8f8a1e3ce 100644 --- a/address/src/main/resources/META-INF/logback/nacos-included.xml +++ b/address/src/main/resources/META-INF/logback/nacos-included.xml @@ -11,7 +11,7 @@ ${LOG_HOME}/nacos-address.log.%d{yyyy-MM-dd}.%i 2GB - 15 + 15 7GB true diff --git a/config/src/main/resources/META-INF/logback/config-included.xml b/config/src/main/resources/META-INF/logback/config-included.xml index bdb8f306292..eedf9335f1e 100644 --- a/config/src/main/resources/META-INF/logback/config-included.xml +++ b/config/src/main/resources/META-INF/logback/config-included.xml @@ -9,7 +9,7 @@ ${LOG_HOME}/config-dump.log.%d{yyyy-MM-dd}.%i 2GB - 15 + 15 7GB true @@ -25,7 +25,7 @@ ${LOG_HOME}/config-pull.log.%d{yyyy-MM-dd}.%i 20MB - 15 + 15 128MB true @@ -41,7 +41,7 @@ ${LOG_HOME}/config-fatal.log.%d{yyyy-MM-dd}.%i 20MB - 15 + 15 128MB true @@ -57,7 +57,7 @@ ${LOG_HOME}/config-memory.log.%d{yyyy-MM-dd}.%i 20MB - 15 + 15 128MB true @@ -73,7 +73,7 @@ ${LOG_HOME}/config-pull-check.log.%d{yyyy-MM-dd}.%i 1GB - 15 + 15 3GB true @@ -90,7 +90,7 @@ ${LOG_HOME}/config-acl.log.%d{yyyy-MM-dd}.%i 50MB - 15 + 15 512MB true @@ -107,7 +107,7 @@ ${LOG_HOME}/config-client-request.log.%d{yyyy-MM-dd}.%i 2GB - 15 + 15 7GB true @@ -124,7 +124,7 @@ ${LOG_HOME}/config-sdk-request.log.%d{yyyy-MM-dd}.%i 1GB - 15 + 15 3GB true @@ -141,7 +141,7 @@ ${LOG_HOME}/config-trace.log.%d{yyyy-MM-dd}.%i 2GB - 15 + 15 7GB true @@ -158,7 +158,7 @@ ${LOG_HOME}/config-notify.log.%d{yyyy-MM-dd}.%i 1GB - 15 + 15 3GB true @@ -175,7 +175,7 @@ ${LOG_HOME}/config-app.log.%d{yyyy-MM-dd}.%i 20MB - 15 + 15 128MB true @@ -192,7 +192,7 @@ ${LOG_HOME}/config-server.log.%d{yyyy-MM-dd}.%i 50MB - 15 + 15 512MB true diff --git a/core/src/main/resources/META-INF/logback/nacos.xml b/core/src/main/resources/META-INF/logback/nacos.xml index 6d47222cf9d..b67b58a0b6d 100644 --- a/core/src/main/resources/META-INF/logback/nacos.xml +++ b/core/src/main/resources/META-INF/logback/nacos.xml @@ -25,7 +25,7 @@ ${LOG_HOME}/nacos.log.%d{yyyy-MM-dd}.%i 50MB - 15 + 15 512MB true @@ -42,7 +42,7 @@ ${LOG_HOME}/core-auth.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -59,7 +59,7 @@ ${LOG_HOME}/protocol-raft.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -76,7 +76,7 @@ ${LOG_HOME}/protocol-distro.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -93,7 +93,7 @@ ${LOG_HOME}/nacos-cluster.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -110,7 +110,7 @@ ${LOG_HOME}/alipay-jraft.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true diff --git a/distribution/conf/nacos-logback.xml b/distribution/conf/nacos-logback.xml index 27da0022bfa..56795c79027 100644 --- a/distribution/conf/nacos-logback.xml +++ b/distribution/conf/nacos-logback.xml @@ -11,7 +11,7 @@ ${nacos.home}/logs/cmdb-main.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -35,7 +35,7 @@ ${LOG_HOME}/naming-server.log.%d{yyyy-MM-dd}.%i 1GB - 7 + 7 7GB true @@ -59,7 +59,7 @@ ${LOG_HOME}/naming-raft.log.%d{yyyy-MM-dd}.%i 1GB - 7 + 7 3GB true @@ -84,7 +84,7 @@ ${LOG_HOME}/naming-distro.log.%d{yyyy-MM-dd}.%i 1GB - 7 + 7 3GB true @@ -108,7 +108,7 @@ ${LOG_HOME}/naming-event.log.%d{yyyy-MM-dd}.%i 1GB - 7 + 7 3GB true @@ -132,7 +132,7 @@ ${LOG_HOME}/naming-push.log.%d{yyyy-MM-dd}.%i 1GB - 7 + 7 3GB true @@ -148,7 +148,7 @@ ${LOG_HOME}/naming-rt.log.%d{yyyy-MM-dd}.%i 1GB - 7 + 7 3GB true @@ -165,7 +165,7 @@ ${LOG_HOME}/naming-performance.log.%d{yyyy-MM-dd}.%i 1GB - 7 + 7 3GB true @@ -183,7 +183,7 @@ ${LOG_HOME}/config-dump.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -199,7 +199,7 @@ ${LOG_HOME}/config-pull.log.%d{yyyy-MM-dd}.%i 20MB - 7 + 7 128MB true @@ -215,7 +215,7 @@ ${LOG_HOME}/config-fatal.log.%d{yyyy-MM-dd}.%i 20MB - 7 + 7 128MB true @@ -231,7 +231,7 @@ ${LOG_HOME}/config-memory.log.%d{yyyy-MM-dd}.%i 20MB - 7 + 7 128MB true @@ -247,7 +247,7 @@ ${LOG_HOME}/config-pull-check.log.%d{yyyy-MM-dd}.%i 1GB - 7 + 7 3GB true @@ -264,7 +264,7 @@ ${LOG_HOME}/config-client-request.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -281,7 +281,7 @@ ${LOG_HOME}/config-trace.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -298,7 +298,7 @@ ${LOG_HOME}/config-notify.log.%d{yyyy-MM-dd}.%i 1GB - 7 + 7 3GB true @@ -315,7 +315,7 @@ ${LOG_HOME}/config-server.log.%d{yyyy-MM-dd}.%i 50MB - 7 + 7 512MB true @@ -332,7 +332,7 @@ ${LOG_HOME}/nacos.log.%d{yyyy-MM-dd}.%i 50MB - 7 + 7 512MB true @@ -349,7 +349,7 @@ ${LOG_HOME}/nacos-address.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -366,7 +366,7 @@ ${LOG_HOME}/istio-main.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -383,7 +383,7 @@ ${LOG_HOME}/core-auth.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -400,7 +400,7 @@ ${LOG_HOME}/protocol-raft.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -417,7 +417,7 @@ ${LOG_HOME}/protocol-distro.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -434,7 +434,7 @@ ${LOG_HOME}/nacos-cluster.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true @@ -451,7 +451,7 @@ ${LOG_HOME}/alipay-jraft.log.%d{yyyy-MM-dd}.%i 2GB - 7 + 7 7GB true diff --git a/naming/src/main/resources/META-INF/logback/naming-included.xml b/naming/src/main/resources/META-INF/logback/naming-included.xml index 81258d44fdc..84f20f67ccd 100644 --- a/naming/src/main/resources/META-INF/logback/naming-included.xml +++ b/naming/src/main/resources/META-INF/logback/naming-included.xml @@ -11,7 +11,7 @@ ${LOG_HOME}/naming-server.log.%d{yyyy-MM-dd}.%i 2GB - 15 + 15 7GB true @@ -27,7 +27,7 @@ ${LOG_HOME}/naming-raft.log.%d{yyyy-MM-dd}.%i 20MB - 15 + 15 128MB true @@ -43,7 +43,7 @@ ${LOG_HOME}/naming-event.log.%d{yyyy-MM-dd}.%i 20MB - 15 + 15 128MB true @@ -59,7 +59,7 @@ ${LOG_HOME}/naming-push.log.%d{yyyy-MM-dd}.%i 20MB - 15 + 15 128MB true @@ -75,7 +75,7 @@ ${LOG_HOME}/naming-rt.log.%d{yyyy-MM-dd}.%i 1GB - 15 + 15 3GB true @@ -92,7 +92,7 @@ ${LOG_HOME}/naming-performance.log.%d{yyyy-MM-dd}.%i 50MB - 15 + 15 512MB true @@ -109,7 +109,7 @@ ${LOG_HOME}/naming-router.log.%d{yyyy-MM-dd}.%i 2GB - 15 + 15 7GB true @@ -126,7 +126,7 @@ ${LOG_HOME}/naming-cache.log.%d{yyyy-MM-dd}.%i 1GB - 15 + 15 3GB true @@ -143,7 +143,7 @@ ${LOG_HOME}/naming-device.log.%d{yyyy-MM-dd}.%i 2GB - 15 + 15 7GB true @@ -160,7 +160,7 @@ ${LOG_HOME}/naming-tag.log.%d{yyyy-MM-dd}.%i 1GB - 15 + 15 3GB true @@ -177,7 +177,7 @@ ${LOG_HOME}/naming-debug.log.%d{yyyy-MM-dd}.%i 20MB - 15 + 15 128MB true @@ -194,7 +194,7 @@ ${LOG_HOME}/naming-distro.log.%d{yyyy-MM-dd}.%i 1GB - 7 + 7 3GB true From d21bbf4688e367665babd5036c7d2a9b8bb7c854 Mon Sep 17 00:00:00 2001 From: huqingfeng <1397649957@qq.com> Date: Wed, 10 Jun 2020 17:42:17 +0800 Subject: [PATCH 055/156] a power of two size for hashmap --- .../java/com/alibaba/nacos/client/naming/net/NamingProxy.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index 73554285d6e..47e1f08509b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -197,7 +197,7 @@ public void registerService(String serviceName, String groupName, Instance insta NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance: {}", namespaceId, serviceName, instance); - final Map params = new HashMap(9); + final Map params = new HashMap(16); params.put(CommonParams.NAMESPACE_ID, namespaceId); params.put(CommonParams.SERVICE_NAME, serviceName); params.put(CommonParams.GROUP_NAME, groupName); From 6d24d37c711648da1e149ae1265e7322ed9a4c08 Mon Sep 17 00:00:00 2001 From: baolongcheng Date: Wed, 10 Jun 2020 21:24:09 +0800 Subject: [PATCH 056/156] =?UTF-8?q?=E4=BF=AE=E9=92=88=E5=AF=B9mysql5.6?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=BD=BF=E7=94=A8innodb=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E5=BC=95=E6=93=8E=EF=BC=8Cpermissions=E8=A1=A8resouce=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E9=95=BF=E5=BA=A6=E8=BF=87=E9=95=BF=EF=BC=8C=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=BB=BA=E8=A1=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- distribution/conf/nacos-mysql.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distribution/conf/nacos-mysql.sql b/distribution/conf/nacos-mysql.sql index 5e88447adbc..d048cebf493 100644 --- a/distribution/conf/nacos-mysql.sql +++ b/distribution/conf/nacos-mysql.sql @@ -192,7 +192,7 @@ CREATE TABLE `roles` ( CREATE TABLE `permissions` ( `role` varchar(50) NOT NULL, - `resource` varchar(512) NOT NULL, + `resource` varchar(255) NOT NULL, `action` varchar(8) NOT NULL, UNIQUE INDEX `uk_role_permission` (`role`,`resource`,`action`) USING BTREE ); From 990a01b5888e353bca442e3ea246bef8c6e56056 Mon Sep 17 00:00:00 2001 From: allo Date: Wed, 10 Jun 2020 23:54:07 +0800 Subject: [PATCH 057/156] Update application.properties spelling err correct --- distribution/conf/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distribution/conf/application.properties b/distribution/conf/application.properties index 87dc5455af1..0e0722bcb95 100644 --- a/distribution/conf/application.properties +++ b/distribution/conf/application.properties @@ -13,7 +13,7 @@ server.port=8848 #*************** Config Module Related Configurations ***************# -### If user MySQL as datasource: +### If use MySQL as datasource: # spring.datasource.platform=mysql ### Count of DB: From fa6bf9951d3484fd46b29c5aa71f543bc0e6ff7e Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Thu, 11 Jun 2020 09:34:15 +0800 Subject: [PATCH 058/156] remove extra blank lines --- .../nacos/common/http/HttpClientManager.java | 1 - .../client/DefaultAsyncHttpClientRequest.java | 2 -- .../http/client/DefaultClientHttpResponse.java | 2 -- .../http/client/DefaultHttpClientRequest.java | 1 - .../common/http/client/HttpClientRequest.java | 1 - .../http/client/NacosAsyncRestTemplate.java | 15 --------------- .../common/http/client/NacosRestTemplate.java | 9 --------- .../common/http/handler/ResponseHandler.java | 1 - .../alibaba/nacos/common/http/param/Header.java | 4 ---- .../com/alibaba/nacos/common/utils/IoUtils.java | 3 --- .../alibaba/nacos/common/utils/JacksonUtils.java | 4 +--- .../alibaba/nacos/common/http/HttpUtilsTest.java | 9 --------- .../common/NacosAsyncRestTemplate_ITCase.java | 6 ------ .../test/common/NacosRestTemplate_ITCase.java | 9 --------- 14 files changed, 1 insertion(+), 66 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java index 55efdbc519e..6371ebef6c9 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientManager.java @@ -104,5 +104,4 @@ public static void shutdown() { logger.warn("[HttpClientManager] Destruction of the end"); } - } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultAsyncHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultAsyncHttpClientRequest.java index e3947877f5e..35f2319a633 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultAsyncHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultAsyncHttpClientRequest.java @@ -16,7 +16,6 @@ package com.alibaba.nacos.common.http.client; - import com.alibaba.nacos.common.http.Callback; import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.http.handler.ResponseHandler; @@ -76,7 +75,6 @@ public void cancelled() { } - @Override public void close() throws IOException { this.asyncClient.close(); diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java index 37911865142..8f721e960f6 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java @@ -20,11 +20,9 @@ import org.apache.http.HttpResponse; import org.apache.http.client.utils.HttpClientUtils; - import java.io.IOException; import java.io.InputStream; - /** * ApacheClientHttpResponse implementation {@link HttpClientResponse} * diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java index 1facf19daff..52bb63c00eb 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java @@ -59,7 +59,6 @@ public HttpClientResponse execute(URI uri, String httpMethod, RequestHttpEntity return new DefaultClientHttpResponse(response); } - static HttpRequestBase build(URI uri, String method, RequestHttpEntity requestHttpEntity) throws Exception { Header headers = requestHttpEntity.getHeaders(); BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method); diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java index ab6999b601d..b85df955a2a 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/HttpClientRequest.java @@ -30,7 +30,6 @@ */ public interface HttpClientRequest extends Closeable { - /** * execute http request * @param uri http url diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java index 32c28da7757..9b91ed91a28 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosAsyncRestTemplate.java @@ -48,7 +48,6 @@ public NacosAsyncRestTemplate(AsyncHttpClientRequest clientRequest) { this.clientRequest = clientRequest; } - /** * async http get * URL request params are expanded using the given query {@link Query}. @@ -64,7 +63,6 @@ public NacosAsyncRestTemplate(AsyncHttpClientRequest clientRequest) { * @throws Exception ex */ public void get(String url, Header header, Query query, Type responseType, Callback callback) throws Exception { - execute(url, HttpMethod.GET, new RequestHttpEntity(header, query), responseType, callback); } @@ -84,7 +82,6 @@ public void get(String url, Header header, Query query, Type responseType, C */ public void get(String url, Header header, Map paramValues, Type responseType, Callback callback) throws Exception { - execute(url, HttpMethod.GET, new RequestHttpEntity(header, Query.newInstance().initParams(paramValues)), responseType, callback); } @@ -107,7 +104,6 @@ public void get(String url, Header header, Map paramValues, */ public void getLarge(String url, Header header, Query query, Object body, Type responseType, Callback callback) throws Exception { - execute(url, HttpMethod.GET_LARGE, new RequestHttpEntity(header, query, body), responseType, callback); } @@ -128,7 +124,6 @@ public void getLarge(String url, Header header, Query query, Object body, */ public void delete(String url, Header header, Query query, Type responseType, Callback callback) throws Exception { - execute(url, HttpMethod.DELETE, new RequestHttpEntity(header, query), responseType, callback); } @@ -151,7 +146,6 @@ public void delete(String url, Header header, Query query, */ public void put(String url, Header header, Query query, Object body, Type responseType, Callback callback) throws Exception { - execute(url, HttpMethod.PUT, new RequestHttpEntity(header, query, body), responseType, callback); } @@ -175,12 +169,10 @@ public void put(String url, Header header, Query query, Object body, */ public void putJson(String url, Header header, Map paramValues, String body, Type responseType, Callback callback) throws Exception { - execute(url, HttpMethod.PUT, new RequestHttpEntity( header.setContentType(MediaType.APPLICATION_JSON), Query.newInstance().initParams(paramValues), body), responseType, callback); - } /** @@ -202,7 +194,6 @@ public void putJson(String url, Header header, Map paramValu */ public void putFrom(String url, Header header, Query query, Map bodyValues, Type responseType, Callback callback) throws Exception { - execute(url, HttpMethod.PUT, new RequestHttpEntity( header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues), responseType, callback); @@ -227,7 +218,6 @@ public void putFrom(String url, Header header, Query query, Map void putFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType, Callback callback) throws Exception { - execute(url, HttpMethod.PUT, new RequestHttpEntity( header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), Query.newInstance().initParams(paramValues), bodyValues), responseType, callback); @@ -251,7 +241,6 @@ public void putFrom(String url, Header header, Map paramValu */ public void post(String url, Header header, Query query, Object body, Type responseType, Callback callback) throws Exception { - execute(url, HttpMethod.POST, new RequestHttpEntity( header, query, body), responseType, callback); } @@ -275,7 +264,6 @@ public void post(String url, Header header, Query query, Object body, */ public void postJson(String url, Header header, Map paramValues, String body, Type responseType, Callback callback) throws Exception { - execute(url, HttpMethod.POST, new RequestHttpEntity( header.setContentType(MediaType.APPLICATION_JSON), Query.newInstance().initParams(paramValues), body), @@ -301,7 +289,6 @@ public void postJson(String url, Header header, Map paramVal */ public void postFrom(String url, Header header, Query query, Map bodyValues, Type responseType, Callback callback) throws Exception { - execute(url, HttpMethod.POST, new RequestHttpEntity( header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues), responseType, callback); @@ -326,7 +313,6 @@ public void postFrom(String url, Header header, Query query, Map void postFrom(String url, Header header, Map paramValues, Map bodyValues, Type responseType, Callback callback) throws Exception { - execute(url, HttpMethod.POST, new RequestHttpEntity( header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), Query.newInstance().initParams(paramValues), @@ -336,7 +322,6 @@ public void postFrom(String url, Header header, Map paramVal private void execute(String url, String httpMethod, RequestHttpEntity requestEntity, Type responseType, Callback callback) throws Exception { - URI uri = HttpUtils.buildUri(url, requestEntity.getQuery()); if (logger.isDebugEnabled()) { logger.debug("HTTP " + httpMethod + " " + url); diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java index c5141d10487..4de20b3406b 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/NacosRestTemplate.java @@ -80,7 +80,6 @@ public HttpRestResult get(String url, Header header, Query query, Type re */ public HttpRestResult get(String url, Header header, Map paramValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity(header, Query.newInstance().initParams(paramValues)); - return execute(url, HttpMethod.GET, requestHttpEntity, responseType); } @@ -156,7 +155,6 @@ public HttpRestResult putJson(String url, Header header, Map HttpRestResult putFrom(String url, Header header, Map HttpRestResult postJson(String url, Header header, Map HttpRestResult postJson(String url, Header header, Map HttpRestResult postFrom(String url, Header header, Query query, Map bodyValues, Type responseType) throws Exception { RequestHttpEntity requestHttpEntity = new RequestHttpEntity( header.setContentType(MediaType.APPLICATION_FORM_URLENCODED), query, bodyValues); - return execute(url, HttpMethod.POST, requestHttpEntity, responseType); } @@ -290,11 +285,9 @@ public HttpRestResult postFrom(String url, Header header, Map HttpRestResult execute(String url, String httpMethod, RequestHttpEntity requestEntity, Type responseType) throws Exception { URI uri = HttpUtils.buildUri(url, requestEntity.getQuery()); @@ -319,6 +312,4 @@ public void close() throws Exception{ requestClient.close(); } - - } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java b/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java index 9047a79bf7d..6bd7d1ed1b0 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/handler/ResponseHandler.java @@ -85,5 +85,4 @@ private static HttpRestResult convert(RestResult restResult) { return httpRestResult; } - } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java index ecc9cd87447..786efeffbdf 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java @@ -122,14 +122,10 @@ private String analysisCharset(String contentType) { return charset; } - - - public void clear() { header.clear(); } - @Override public String toString() { return "Header{" + diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java index 51182308583..8b73233f62c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java @@ -168,7 +168,6 @@ public static void cleanDirectory(File directory) throws IOException { } public static void copy(byte[] in, OutputStream out) throws IOException { - try { out.write(in); } finally { @@ -176,9 +175,7 @@ public static void copy(byte[] in, OutputStream out) throws IOException { out.close(); } catch (IOException io) { } - } - } static public long copy(InputStream input, OutputStream output) throws IOException { diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java index e74ff7d68a1..c384fa7e7c3 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java @@ -77,14 +77,12 @@ public static T toObj(byte[] json, Type cls) { } catch (Exception e) { throw new NacosDeserializationException(e); } - } - + } public static T toObj(InputStream inputStream, Class tClass) throws Exception { return mapper.readValue(inputStream, tClass); } - public static T toObj(byte[] json, TypeReference typeReference) { try { return toObj(StringUtils.newString4UTF8(json), typeReference); diff --git a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java index 0bac86939c8..59682b5c5bb 100644 --- a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java @@ -15,15 +15,6 @@ */ package com.alibaba.nacos.common.http; - -import org.junit.Test; - public class HttpUtilsTest { - @Test - public void test_url_encode() throws Exception { - - } - - } diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java index b8e467023c9..bd318b92353 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosAsyncRestTemplate_ITCase.java @@ -132,7 +132,6 @@ public void test_url_get() throws Exception { System.out.println(restResult.getHeader()); Assert.assertTrue(restResult.ok()); Assert.assertEquals(restResult.getData().get("dom"), "app-test"); - } @Test @@ -148,7 +147,6 @@ public void test_url_by_map() throws Exception{ System.out.println(restResult.getHeader()); Assert.assertTrue(restResult.ok()); Assert.assertEquals(restResult.getData().get("dom"), "app-test"); - } @Test @@ -165,10 +163,6 @@ public void test_url_delete() throws Exception{ System.out.println(restResult.getData()); System.out.println(restResult.getHeader()); Assert.assertTrue(restResult.ok()); - } - - - } diff --git a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java index c45b673d569..1e2a8c0a806 100644 --- a/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/common/NacosRestTemplate_ITCase.java @@ -53,12 +53,10 @@ public class NacosRestTemplate_ITCase { private NacosRestTemplate nacosRestTemplate = HttpClientManager.getNacosRestTemplate(); - private final String INSTANCE_PATH = "/nacos/v1/ns"; private final String CONFIG_PATH = "/nacos/v1/cs"; private String IP = null; - @Before public void init() throws NacosException { IP = String.format("http://localhost:%d", port); @@ -132,11 +130,4 @@ public void test_url_delete() throws Exception{ System.out.println(restResult); } - - - - - - - } From eb39830b6c821dec817228d873c84cefc95e10bf Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Thu, 11 Jun 2020 09:42:44 +0800 Subject: [PATCH 059/156] [#1815]remove lifeCycle, lifeCycleManager, and adjust some related codes. --- .../nacos/api/exception/NacosException.java | 6 - .../client/config/NacosConfigService.java | 13 +- .../nacos/client/config/http/HttpAgent.java | 2 +- .../client/config/http/MetricsHttpAgent.java | 4 +- .../client/config/http/ServerHttpAgent.java | 2 +- .../client/config/impl/ClientWorker.java | 3 +- .../client/naming/NacosNamingService.java | 11 +- .../nacos/common/lifecycle/LifeCycle.java | 35 ---- .../lifecycle/ResourceLifeCycleManager.java | 156 ------------------ .../common/LifeCycle/LifeCycleClassTest.java | 82 --------- .../LifeCycle/LifeCycleManagerTest.java | 97 ----------- .../nacos/test/config/ConfigAPI_ITCase.java | 5 +- .../ConfigExportAndImportAPI_ITCase.java | 2 +- .../ConfigLongPollReturnChanges_ITCase.java | 2 - .../test/config/ConfigLongPoll_ITCase.java | 4 - .../test/core/auth/ConfigAuth_ITCase.java | 3 - 16 files changed, 10 insertions(+), 417 deletions(-) delete mode 100644 common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java delete mode 100644 common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java delete mode 100644 common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java delete mode 100644 common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java index 45594761f57..d13e1157dab 100644 --- a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java +++ b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java @@ -145,10 +145,4 @@ public String toString() { public static final int OVER_THRESHOLD = 503; public static final int RESOURCE_NOT_FOUND = -404; - - /** - * resource instance destroy failed. - */ - public static final int RESOURCE_DESTROY_FAILED = 410; - } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java index f7d94b3508e..16b761762fa 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java @@ -34,8 +34,6 @@ import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.ValidatorUtils; -import com.alibaba.nacos.common.lifecycle.LifeCycle; -import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -52,7 +50,7 @@ * @author Nacos */ @SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") -public class NacosConfigService implements ConfigService, LifeCycle { +public class NacosConfigService implements ConfigService { private static final Logger LOGGER = LogUtils.logger(NacosConfigService.class); @@ -81,9 +79,8 @@ public NacosConfigService(Properties properties) throws NacosException { initNamespace(properties); this.agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); - this.agent.fetchServerIpList(); + this.agent.start(); this.worker = new ClientWorker(this.agent, this.configFilterChainManager, properties); - ResourceLifeCycleManager.register(this); } private void initNamespace(Properties properties) { @@ -287,12 +284,6 @@ public String getServerStatus() { @Override public void shutDown() throws NacosException{ - destroy(); - ResourceLifeCycleManager.deregister(this); - } - - @Override - public void destroy() throws NacosException{ agent.shutdown(); worker.shutdown(); } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java index 5f80cf5f901..82adf6d481a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/HttpAgent.java @@ -35,7 +35,7 @@ public interface HttpAgent extends Closeable { * @return Nothing. * @throws NacosException on get ip list error. */ - void fetchServerIpList() throws NacosException; + void start() throws NacosException; /** * invoke http get method diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java index 9b9ae1e4d26..c37adfb4629 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java @@ -42,8 +42,8 @@ public MetricsHttpAgent(HttpAgent httpAgent) { } @Override - public void fetchServerIpList() throws NacosException { - httpAgent.fetchServerIpList(); + public void start() throws NacosException { + httpAgent.start(); } @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java index 45e0b436fd1..5e97a311a88 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java @@ -339,7 +339,7 @@ private void initMaxRetry(Properties properties) { } @Override - public void fetchServerIpList() throws NacosException { + public void start() throws NacosException { // fetch server address urls list serverListMgr.start(); } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index 4c8760117e5..e30e584db6a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -26,7 +26,6 @@ import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.utils.ContentUtils; import com.alibaba.nacos.common.lifecycle.Closeable; -import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.naming.utils.CollectionUtils; @@ -499,7 +498,7 @@ public void run() { private void init(Properties properties) { - this.timeout = Math.max(ConvertUtils.toInt(properties.getProperty(PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT), + this.timeout = Math.max(NumberUtils.toInt(properties.getProperty(PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT), Constants.CONFIG_LONG_POLL_TIMEOUT), Constants.MIN_CONFIG_LONG_POLL_TIMEOUT); this.taskPenaltyTime = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.CONFIG_RETRY_TIME), Constants.CONFIG_RETRY_TIME); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index 3bf3482befa..eefa75bce80 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -35,8 +35,6 @@ import com.alibaba.nacos.client.naming.utils.InitUtils; import com.alibaba.nacos.client.naming.utils.UtilAndComs; import com.alibaba.nacos.client.utils.ValidatorUtils; -import com.alibaba.nacos.common.lifecycle.LifeCycle; -import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; @@ -52,7 +50,7 @@ * @author nkorange */ @SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") -public class NacosNamingService implements NamingService, LifeCycle { +public class NacosNamingService implements NamingService { /** * Each Naming service should have different namespace. @@ -99,7 +97,6 @@ private void init(Properties properties) { this.beatReactor = new BeatReactor(this.serverProxy, initClientBeatThreadCount(properties)); this.hostReactor = new HostReactor(this.eventDispatcher, this.serverProxy, this.cacheDir, isLoadCacheAtStart(properties), initPollingThreadCount(properties)); - ResourceLifeCycleManager.register(this); } private int initClientBeatThreadCount(Properties properties) { @@ -493,12 +490,6 @@ public BeatReactor getBeatReactor() { @Override public void shutDown() throws NacosException{ - destroy(); - ResourceLifeCycleManager.deregister(this); - } - - @Override - public void destroy() throws NacosException{ beatReactor.shutdown(); eventDispatcher.shutdown(); hostReactor.shutdown(); diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java deleted file mode 100644 index 3e06a841bf9..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/LifeCycle.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.nacos.common.lifecycle; - -import com.alibaba.nacos.api.exception.NacosException; - -/** - * - * The lifecycle interface for generic service. Classes are need to implement - * this interface have a defined life cycle defined by the methods of this interface. - * - * @author zongtanghu - */ -public interface LifeCycle { - - /** - * Destroy the service. - * - * @throws NacosException An NacosException occours when destroy service. - */ - public void destroy() throws NacosException; -} diff --git a/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java b/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java deleted file mode 100644 index 9d371b513e4..00000000000 --- a/common/src/main/java/com/alibaba/nacos/common/lifecycle/ResourceLifeCycleManager.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.nacos.common.lifecycle; - -import com.alibaba.nacos.common.utils.ShutdownUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * A class to manage every instances' life cycle which register into it. - * @author zongtanghu - * - */ -public final class ResourceLifeCycleManager { - - private static final Logger LOGGER = LoggerFactory.getLogger(ResourceLifeCycleManager.class); - - /** - * - */ - private List lifeCycleResources; - - /** - * Map - */ - private Map lockers; - - private static final ResourceLifeCycleManager INSTANCE = new ResourceLifeCycleManager(); - - private static final AtomicBoolean CLOSED = new AtomicBoolean(false); - - static { - INSTANCE.lifeCycleResources = new CopyOnWriteArrayList(); - INSTANCE.lockers = new ConcurrentHashMap(8); - ShutdownUtils.addShutdownHook(new Thread(new Runnable() { - @Override - public void run() { - LOGGER.warn("[LifeCycleManager] Start destroying Every Instance"); - shutdown(); - LOGGER.warn("[LifeCycleManager] Destruction of the end"); - } - })); - } - - public static ResourceLifeCycleManager getInstance() { - return INSTANCE; - } - - /** - * Destroy all of the life cycle resources which are managed by ResourceLifeCycleManager. - * - */ - public static void shutdown() { - if (!CLOSED.compareAndSet(false, true)) { - return; - } - List instances = INSTANCE.lifeCycleResources; - for (LifeCycle instance : instances) { - INSTANCE.destroy(instance); - } - } - - /** - * Destroy the life cycle resource instance. - * - * @param instance the life cycle resource instance which is need to be destroyed. - */ - public void destroy(LifeCycle instance) { - final Object monitor = this.lockers.get(instance); - if (monitor == null) { - return; - } - synchronized (monitor) { - try { - // the life cycle resources which managed are do stop method. - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Life cycle resources do stop"); - } - - // here, double check if lockers contains or not - if (!this.lockers.containsKey(instance)) { - return; - } - - instance.destroy(); - this.lifeCycleResources.remove(instance); - this.lockers.remove(instance); - } catch (Exception e) { - LOGGER.error("An exception occurred during resource do stop : {}", e); - } - } - } - - - /** - * Cancel the specified lifecycle resource instance management. - * - * @param instance the management life cycle resource instances; - * - */ - public static void deregister(LifeCycle instance) { - if (INSTANCE.lifeCycleResources.contains(instance)) { - final Object monitor = INSTANCE.lockers.get(instance); - synchronized (monitor) { - if (INSTANCE.lockers.containsKey(instance)) { - INSTANCE.lockers.remove(instance); - INSTANCE.lifeCycleResources.remove(instance); - } - } - } - } - - /** - * Register the life cycle resource instances into the lifeCycleResources lists. - * - * @param instance the management life cycle resource instances. - */ - public static void register(LifeCycle instance) { - if (!INSTANCE.lifeCycleResources.contains(instance)) { - synchronized(INSTANCE) { - if (!INSTANCE.lockers.containsKey(instance)) { - INSTANCE.lockers.put(instance, new Object()); - INSTANCE.lifeCycleResources.add(instance); - } - } - } - } - - /** - * Get the life cycle resource number which registered into the Resource Life Cycle Manager. - * - * @return - */ - public static int getRegisterResourceNum() { - return INSTANCE.lifeCycleResources.size(); - } -} diff --git a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java deleted file mode 100644 index fab1981deff..00000000000 --- a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleClassTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.nacos.common.LifeCycle; - -import com.alibaba.nacos.common.lifecycle.LifeCycle; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -/** - * Test serveral cases for lifecycle class instance. - * - * @author zongtanghu - */ -public class LifeCycleClassTest { - - private Resource res; - - @Before - public void setup() throws Exception{ - this.res = new Resource(0); - Assert.assertEquals(0, this.res.getCounter()); - } - - @After - public void cleanup() throws Exception{ - this.res.destroy(); - Assert.assertEquals(0, this.res.getCounter()); - } - - @Test - public void testResource_LifeCycleMethod() throws Exception{ - this.res.increament(); - Assert.assertEquals(1, this.res.getCounter()); - this.res.increament(); - Assert.assertEquals(2, this.res.getCounter()); - this.res.increament(); - Assert.assertEquals(3, this.res.getCounter()); - this.res.increament(); - Assert.assertEquals(4, this.res.getCounter()); - } - - class Resource implements LifeCycle { - - private int counter; - - public Resource(int counter) { - this.counter = counter; - } - - public int getCounter() { - return counter; - } - - public void setCounter(int counter) { - this.counter = counter; - } - - public void increament() { - this.counter++; - } - - @Override - public void destroy() { - this.counter = 0; - } - } -} diff --git a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java b/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java deleted file mode 100644 index 0f1c6f62962..00000000000 --- a/common/src/test/java/com/alibaba/nacos/common/LifeCycle/LifeCycleManagerTest.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.nacos.common.LifeCycle; - -import com.alibaba.nacos.common.lifecycle.LifeCycle; -import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -/** - * Test serveral cases for lifecycle Manager class instance. - * - * @author zongtanghu - */ -public class LifeCycleManagerTest { - - private Resource res; - @Before - public void setup() throws Exception{ - this.res = new Resource(0); - ResourceLifeCycleManager.register(this.res); - Assert.assertEquals(0, this.res.getCounter()); - Assert.assertEquals(1, ResourceLifeCycleManager.getRegisterResourceNum()); - } - - @After - public void cleanup() throws Exception{ - ResourceLifeCycleManager.shutdown(); - Assert.assertEquals(0, this.res.getCounter()); - } - - @Test - public void testLifeCycleManager() throws Exception{ - this.res.increament(); - Assert.assertEquals(1, this.res.getCounter()); - this.res.increament(); - Assert.assertEquals(2, this.res.getCounter()); - } - - @Test - public void testLifeCycleManager_deregister() throws Exception{ - - Resource temp = new Resource(0); - ResourceLifeCycleManager.register(temp); - temp.increament(); - ResourceLifeCycleManager.deregister(temp); - ResourceLifeCycleManager.getInstance().destroy(temp); - - Assert.assertEquals(1, temp.getCounter()); - temp.destroy(); - Assert.assertEquals(0, temp.getCounter()); - Assert.assertEquals(1, ResourceLifeCycleManager.getRegisterResourceNum()); - } - - class Resource implements LifeCycle { - - private int counter; - - public Resource(int counter) { - this.counter = counter; - } - - - public int getCounter() { - return counter; - } - - public void setCounter(int counter) { - this.counter = counter; - } - - public void increament() { - this.counter++; - } - - @Override - public void destroy() { - this.counter = 0; - } - } -} diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java index d7a2e6fa236..11650d7ab12 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java @@ -28,7 +28,6 @@ import com.alibaba.nacos.client.config.http.ServerHttpAgent; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.common.utils.JacksonUtils; -import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import com.alibaba.nacos.common.utils.ThreadUtils; import org.junit.After; import org.junit.Assert; @@ -74,7 +73,7 @@ public void setUp() throws Exception { properties.put(PropertyKeyConst.CONTEXT_PATH, "/nacos"); iconfig = NacosFactory.createConfigService(properties); agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); - agent.fetchServerIpList(); + agent.start(); } @After @@ -87,8 +86,6 @@ public void cleanup() throws Exception { Assert.assertTrue(JacksonUtils.toObj(result.content).get("data").booleanValue()); iconfig.shutDown(); agent.shutdown(); - // Judge whether the register life cycle resource number equals to zero or not. - Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); Assert.assertTrue(JacksonUtils.toObj(result.content).get("data").booleanValue()); } catch (Exception e) { e.printStackTrace(); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java index 7534e963def..404d39cca47 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigExportAndImportAPI_ITCase.java @@ -65,7 +65,7 @@ public void setUp() throws Exception { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1"+":"+port); agent = new MetricsHttpAgent(new ServerHttpAgent(properties)); - agent.fetchServerIpList(); + agent.start(); Map prarm = new HashMap<>(7); prarm.put("dataId", "testNoAppname1.yml"); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java index 21241927b28..c133d03120d 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPollReturnChanges_ITCase.java @@ -24,7 +24,6 @@ import com.alibaba.nacos.api.config.PropertyChangeType; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener; -import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -62,7 +61,6 @@ public void init() throws NacosException { public void destroy(){ try { configService.shutDown(); - Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); }catch (NacosException ex) { } } diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java index 1369847a577..519f455a73e 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigLongPoll_ITCase.java @@ -22,10 +22,7 @@ import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; -import com.alibaba.nacos.core.utils.ApplicationUtils; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -65,7 +62,6 @@ public void init() throws NacosException { public void destroy(){ try { configService.shutDown(); - Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); }catch (NacosException ex) { } } diff --git a/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java index b9a769a96b9..54c8ee67217 100644 --- a/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/core/auth/ConfigAuth_ITCase.java @@ -23,8 +23,6 @@ import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener; -import com.alibaba.nacos.common.lifecycle.ResourceLifeCycleManager; -import com.alibaba.nacos.core.utils.ApplicationUtils; import org.apache.http.HttpStatus; import org.junit.After; import org.junit.Assert; @@ -69,7 +67,6 @@ public void init() throws Exception { public void destroy(){ try { iconfig.shutDown(); - Assert.assertEquals(0, ResourceLifeCycleManager.getRegisterResourceNum()); }catch (NacosException ex) { } From 9b873797ca07b81220c4f4baa021353353a945a9 Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Thu, 11 Jun 2020 10:35:50 +0800 Subject: [PATCH 060/156] [#1815]fix ut. --- .../java/com/alibaba/nacos/test/config/ConfigAPI_CITCase.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_CITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_CITCase.java index 79853f8ae02..1ae67b691e1 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_CITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_CITCase.java @@ -84,8 +84,6 @@ public void cleanup() throws Exception { result = agent.httpDelete(CONFIG_CONTROLLER_PATH + "/", null, params, agent.getEncode(), TIME_OUT); Assert.assertEquals(HttpURLConnection.HTTP_OK, result.code); Assert.assertTrue(JacksonUtils.toObj(result.content).get("data").booleanValue()); - iconfig.shutDown(); - agent.shutdown(); Assert.assertTrue(JacksonUtils.toObj(result.content).get("data").booleanValue()); } catch (Exception e) { e.printStackTrace(); From d2b980677f1ebce427b47457a51c4a82763f5dac Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Thu, 11 Jun 2020 11:33:38 +0800 Subject: [PATCH 061/156] [#1815]remove code comments. --- .../com/alibaba/nacos/client/config/http/ServerHttpAgent.java | 1 - 1 file changed, 1 deletion(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java index 5e97a311a88..dc5178263d9 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java @@ -340,7 +340,6 @@ private void initMaxRetry(Properties properties) { @Override public void start() throws NacosException { - // fetch server address urls list serverListMgr.start(); } From e7796b39352f4d17c4a2185d4739be34edd0e9dc Mon Sep 17 00:00:00 2001 From: zongtanghu Date: Thu, 11 Jun 2020 12:42:47 +0800 Subject: [PATCH 062/156] [#1815]adjust codes for instance class attribute init. --- .../client/config/impl/ServerListManager.java | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java index b82f3d5d12f..855fcaa2934 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java @@ -56,7 +56,15 @@ public class ServerListManager implements Closeable { private static final Logger LOGGER = LogUtils.logger(ServerListManager.class); private static final String HTTPS = "https://"; private static final String HTTP = "http://"; - private ScheduledExecutorService executorService; + private ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread t = new Thread(r); + t.setName("com.alibaba.nacos.client.ServerListManager"); + t.setDaemon(true); + return t; + } + }); public ServerListManager() { this.isFixed = false; @@ -107,7 +115,6 @@ public ServerListManager(String endpoint, String namespace) throws NacosExceptio Properties properties = new Properties(); properties.setProperty(PropertyKeyConst.ENDPOINT, endpoint); endpoint = initEndpoint(properties); - initExecutor(); if (StringUtils.isBlank(endpoint)) { throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank"); @@ -135,7 +142,6 @@ public ServerListManager(Properties properties) throws NacosException { this.serverAddrsStr = properties.getProperty(PropertyKeyConst.SERVER_ADDR); String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE); initParam(properties); - initExecutor(); if (StringUtils.isNotEmpty(serverAddrsStr)) { this.isFixed = true; @@ -225,19 +231,6 @@ public String call() { return StringUtils.isNotBlank(endpointTmp) ? endpointTmp : ""; } - private void initExecutor() { - // init executorService - this.executorService = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - Thread t = new Thread(r); - t.setName("com.alibaba.nacos.client.ServerListManager"); - t.setDaemon(true); - return t; - } - }); - } - public synchronized void start() throws NacosException { if (isStarted || isFixed) { From 95272feb2827d4434636a1a30f5b5658e1ba15cc Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Thu, 11 Jun 2020 17:38:34 +0800 Subject: [PATCH 063/156] adjust some codes --- .../common/constant/HttpHeaderConsts.java | 3 ++ .../client/DefaultClientHttpResponse.java | 3 +- .../http/client/DefaultHttpClientRequest.java | 2 +- .../nacos/common/http/param/Header.java | 14 +++++--- .../nacos/common/http/param/MediaType.java | 4 +-- .../alibaba/nacos/common/utils/IoUtils.java | 11 ------- .../nacos/common/utils/JacksonUtils.java | 32 ++++++++----------- .../nacos/common/http/HttpUtilsTest.java | 20 ------------ 8 files changed, 30 insertions(+), 59 deletions(-) delete mode 100644 common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java diff --git a/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java b/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java index 18d5236ce11..baa43b84331 100644 --- a/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java +++ b/common/src/main/java/com/alibaba/nacos/common/constant/HttpHeaderConsts.java @@ -27,5 +27,8 @@ public interface HttpHeaderConsts { String REQUEST_SOURCE_HEADER = "Request-Source"; String CONTENT_TYPE = "Content-Type"; String CONTENT_LENGTH = "Content-Length"; + String ACCEPT_CHARSET = "Accept-Charset"; + String ACCEPT_ENCODING = "Accept-Encoding"; + String CONTENT_ENCODING = "Content-Encoding"; } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java index 8f721e960f6..957c763f7e6 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultClientHttpResponse.java @@ -72,8 +72,7 @@ public void close() { if (this.response != null) { HttpClientUtils.closeQuietly(response); } - } - catch (Exception ex) { + } catch (Exception ex) { // ignore } } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java index 52bb63c00eb..e36c22d4ba7 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/DefaultHttpClientRequest.java @@ -41,7 +41,7 @@ @SuppressWarnings({"unchecked", "rawtypes", "resource"}) public class DefaultHttpClientRequest implements HttpClientRequest { - private static final Logger logger = LoggerFactory.getLogger(NacosRestTemplate.class); + private static final Logger logger = LoggerFactory.getLogger(DefaultHttpClientRequest.class); private final CloseableHttpClient client; diff --git a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java index 786efeffbdf..239fe95d64c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/param/Header.java @@ -39,9 +39,9 @@ public class Header { private Header() { header = new LinkedHashMap(); addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_JSON); - addParam("Accept-Charset", "UTF-8"); - addParam("Accept-Encoding", "gzip"); - addParam("Content-Encoding", "gzip"); + addParam(HttpHeaderConsts.ACCEPT_CHARSET, "UTF-8"); + addParam(HttpHeaderConsts.ACCEPT_ENCODING, "gzip"); + addParam(HttpHeaderConsts.CONTENT_ENCODING, "gzip"); } public static Header newInstance() { @@ -104,8 +104,12 @@ public void addAll(Map params) { } public String getCharset() { - String value = getValue(HttpHeaderConsts.CONTENT_TYPE); - return (StringUtils.isNotBlank(value) ? analysisCharset(value) : Constants.ENCODE); + String acceptCharset = getValue(HttpHeaderConsts.ACCEPT_CHARSET); + if (acceptCharset == null) { + String contentType = getValue(HttpHeaderConsts.CONTENT_TYPE); + acceptCharset = StringUtils.isNotBlank(contentType) ? analysisCharset(contentType) : Constants.ENCODE; + } + return acceptCharset; } private String analysisCharset(String contentType) { diff --git a/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java b/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java index a5246f811b7..8f3c411f2c8 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/param/MediaType.java @@ -25,7 +25,7 @@ private MediaType() {} public static final String APPLICATION_ATOM_XML = "application/atom+xml"; - public static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded;charset=utf-8"; + public static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded"; public static final String APPLICATION_OCTET_STREAM = "application/octet-stream"; @@ -35,7 +35,7 @@ private MediaType() {} public static final String APPLICATION_XML = "application/xml"; - public static final String APPLICATION_JSON = "application/json;charset=UTF-8"; + public static final String APPLICATION_JSON = "application/json"; public static final String MULTIPART_FORM_DATA = "multipart/form-data"; diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java index 8b73233f62c..105635fe4e7 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java @@ -167,17 +167,6 @@ public static void cleanDirectory(File directory) throws IOException { } } - public static void copy(byte[] in, OutputStream out) throws IOException { - try { - out.write(in); - } finally { - try { - out.close(); - } catch (IOException io) { - } - } - } - static public long copy(InputStream input, OutputStream output) throws IOException { byte[] buffer = new byte[1024]; int bytesRead; diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java index c384fa7e7c3..b2d6ba9ba46 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java @@ -47,8 +47,7 @@ public final class JacksonUtils { public static String toJson(Object obj) { try { return mapper.writeValueAsString(obj); - } - catch (JsonProcessingException e) { + } catch (JsonProcessingException e) { throw new NacosSerializationException(obj.getClass(), e); } } @@ -56,8 +55,7 @@ public static String toJson(Object obj) { public static byte[] toJsonBytes(Object obj) { try { return ByteUtils.toBytes(mapper.writeValueAsString(obj)); - } - catch (JsonProcessingException e) { + } catch (JsonProcessingException e) { throw new NacosSerializationException(obj.getClass(), e); } } @@ -65,8 +63,7 @@ public static byte[] toJsonBytes(Object obj) { public static T toObj(byte[] json, Class cls) { try { return toObj(StringUtils.newString4UTF8(json), cls); - } - catch (Exception e) { + } catch (Exception e) { throw new NacosDeserializationException(cls, e); } } @@ -79,8 +76,12 @@ public static T toObj(byte[] json, Type cls) { } } - public static T toObj(InputStream inputStream, Class tClass) throws Exception { - return mapper.readValue(inputStream, tClass); + public static T toObj(InputStream inputStream, Class tClass) { + try { + return mapper.readValue(inputStream, tClass); + } catch (IOException e) { + throw new NacosDeserializationException(e); + } } public static T toObj(byte[] json, TypeReference typeReference) { @@ -94,8 +95,7 @@ public static T toObj(byte[] json, TypeReference typeReference) { public static T toObj(String json, Class cls) { try { return mapper.readValue(json, cls); - } - catch (IOException e) { + } catch (IOException e) { throw new NacosDeserializationException(cls, e); } } @@ -103,8 +103,7 @@ public static T toObj(String json, Class cls) { public static T toObj(String json, Type type) { try { return mapper.readValue(json, mapper.constructType(type)); - } - catch (IOException e) { + } catch (IOException e) { throw new NacosDeserializationException(e); } } @@ -112,8 +111,7 @@ public static T toObj(String json, Type type) { public static T toObj(String json, TypeReference typeReference) { try { return mapper.readValue(json, typeReference); - } - catch (IOException e) { + } catch (IOException e) { throw new NacosDeserializationException(typeReference.getClass(), e); } } @@ -121,8 +119,7 @@ public static T toObj(String json, TypeReference typeReference) { public static T toObj(InputStream inputStream, Type type) { try { return mapper.readValue(inputStream, mapper.constructType(type)); - } - catch (IOException e) { + } catch (IOException e) { throw new NacosDeserializationException(type, e); } } @@ -130,8 +127,7 @@ public static T toObj(InputStream inputStream, Type type) { public static JsonNode toObj(String json) { try { return mapper.readTree(json); - } - catch (IOException e) { + } catch (IOException e) { throw new NacosDeserializationException(e); } } diff --git a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java deleted file mode 100644 index 59682b5c5bb..00000000000 --- a/common/src/test/java/com/alibaba/nacos/common/http/HttpUtilsTest.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.nacos.common.http; - -public class HttpUtilsTest { - -} From 315a6c36c50f77b0d05ca11da5cad7234d99d9bb Mon Sep 17 00:00:00 2001 From: horizonzy <1060026287@qq.com> Date: Thu, 11 Jun 2020 21:44:33 +0800 Subject: [PATCH 064/156] 1.update beatinfo when received instance modify info 2.add the unit test of this case --- .../client/naming/NacosNamingService.java | 19 ++----- .../nacos/client/naming/beat/BeatReactor.java | 15 ++++- .../nacos/client/naming/core/HostReactor.java | 26 +++++++-- .../client/naming/core/HostReactorTest.java | 57 +++++++++++++++++-- 4 files changed, 89 insertions(+), 28 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index eefa75bce80..0077776d42c 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -95,8 +95,8 @@ private void init(Properties properties) { this.eventDispatcher = new EventDispatcher(); this.serverProxy = new NamingProxy(this.namespace, this.endpoint, this.serverList, properties); this.beatReactor = new BeatReactor(this.serverProxy, initClientBeatThreadCount(properties)); - this.hostReactor = new HostReactor(this.eventDispatcher, this.serverProxy, this.cacheDir, isLoadCacheAtStart(properties), - initPollingThreadCount(properties)); + this.hostReactor = new HostReactor(this.eventDispatcher, this.serverProxy, beatReactor, this.cacheDir, + isLoadCacheAtStart(properties), initPollingThreadCount(properties)); } private int initClientBeatThreadCount(Properties properties) { @@ -189,21 +189,10 @@ public void registerInstance(String serviceName, Instance instance) throws Nacos @Override public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException { - if (instance.isEphemeral()) { - BeatInfo beatInfo = new BeatInfo(); - beatInfo.setServiceName(NamingUtils.getGroupedName(serviceName, groupName)); - beatInfo.setIp(instance.getIp()); - beatInfo.setPort(instance.getPort()); - beatInfo.setCluster(instance.getClusterName()); - beatInfo.setWeight(instance.getWeight()); - beatInfo.setMetadata(instance.getMetadata()); - beatInfo.setScheduled(false); - beatInfo.setPeriod(instance.getInstanceHeartBeatInterval()); - + BeatInfo beatInfo = beatReactor.buildBeatInfo(instance); beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo); } - serverProxy.registerService(NamingUtils.getGroupedName(serviceName, groupName), groupName, instance); } @@ -489,7 +478,7 @@ public BeatReactor getBeatReactor() { } @Override - public void shutDown() throws NacosException{ + public void shutDown() throws NacosException { beatReactor.shutdown(); eventDispatcher.shutdown(); hostReactor.shutdown(); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java index 47dcdaa97ed..c339afb3fd2 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java @@ -87,7 +87,20 @@ public void removeBeatInfo(String serviceName, String ip, int port) { MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size()); } - private String buildKey(String serviceName, String ip, int port) { + public BeatInfo buildBeatInfo(Instance instance) { + BeatInfo beatInfo = new BeatInfo(); + beatInfo.setServiceName(instance.getServiceName()); + beatInfo.setIp(instance.getIp()); + beatInfo.setPort(instance.getPort()); + beatInfo.setCluster(instance.getClusterName()); + beatInfo.setWeight(instance.getWeight()); + beatInfo.setMetadata(instance.getMetadata()); + beatInfo.setScheduled(false); + beatInfo.setPeriod(instance.getInstanceHeartBeatInterval()); + return beatInfo; + } + + public String buildKey(String serviceName, String ip, int port) { return serviceName + Constants.NAMING_INSTANCE_ID_SPLITTER + ip + Constants.NAMING_INSTANCE_ID_SPLITTER + port; } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java index b6d1ccef476..036517a6afd 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/HostReactor.java @@ -20,6 +20,8 @@ import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.naming.backups.FailoverReactor; +import com.alibaba.nacos.client.naming.beat.BeatInfo; +import com.alibaba.nacos.client.naming.beat.BeatReactor; import com.alibaba.nacos.client.naming.cache.DiskCache; import com.alibaba.nacos.client.naming.net.NamingProxy; import com.alibaba.nacos.client.naming.utils.UtilAndComs; @@ -64,6 +66,8 @@ public class HostReactor implements Closeable { private EventDispatcher eventDispatcher; + private BeatReactor beatReactor; + private NamingProxy serverProxy; private FailoverReactor failoverReactor; @@ -72,11 +76,11 @@ public class HostReactor implements Closeable { private ScheduledExecutorService executor; - public HostReactor(EventDispatcher eventDispatcher, NamingProxy serverProxy, String cacheDir) { - this(eventDispatcher, serverProxy, cacheDir, false, UtilAndComs.DEFAULT_POLLING_THREAD_COUNT); + public HostReactor(EventDispatcher eventDispatcher, NamingProxy serverProxy, BeatReactor beatReactor, String cacheDir) { + this(eventDispatcher, serverProxy, beatReactor, cacheDir, false, UtilAndComs.DEFAULT_POLLING_THREAD_COUNT); } - public HostReactor(EventDispatcher eventDispatcher, NamingProxy serverProxy, String cacheDir, + public HostReactor(EventDispatcher eventDispatcher, NamingProxy serverProxy, BeatReactor beatReactor, String cacheDir, boolean loadCacheAtStart, int pollingThreadCount) { // init executorService this.executor = new ScheduledThreadPoolExecutor(pollingThreadCount, new ThreadFactory() { @@ -88,8 +92,8 @@ public Thread newThread(Runnable r) { return thread; } }); - this.eventDispatcher = eventDispatcher; + this.beatReactor = beatReactor; this.serverProxy = serverProxy; this.cacheDir = cacheDir; if (loadCacheAtStart) { @@ -187,6 +191,7 @@ public ServiceInfo processServiceJSON(String json) { if (modHosts.size() > 0) { changed = true; + updateBeatInfo(modHosts); NAMING_LOGGER.info("modified ips(" + modHosts.size() + ") service: " + serviceInfo.getKey() + " -> " + JacksonUtils.toJson(modHosts)); } @@ -218,6 +223,16 @@ public ServiceInfo processServiceJSON(String json) { return serviceInfo; } + private void updateBeatInfo(Set modHosts) { + for (Instance instance : modHosts) { + String key = beatReactor.buildKey(instance.getServiceName(), instance.getIp(), instance.getPort()); + if (beatReactor.dom2Beat.containsKey(key) && instance.isEphemeral()) { + BeatInfo beatInfo = beatReactor.buildBeatInfo(instance); + beatReactor.addBeatInfo(instance.getServiceName(), beatInfo); + } + } + } + private ServiceInfo getServiceInfo0(String serviceName, String clusters) { String key = ServiceInfo.getKey(serviceName, clusters); @@ -272,7 +287,6 @@ public ServiceInfo getServiceInfo(final String serviceName, final String cluster } - public void scheduleUpdateIfAbsent(String serviceName, String clusters) { if (futureMap.get(ServiceInfo.getKey(serviceName, clusters)) != null) { return; @@ -317,7 +331,7 @@ public void refreshOnly(String serviceName, String clusters) { } @Override - public void shutdown() throws NacosException{ + public void shutdown() throws NacosException { String className = this.getClass().getName(); NAMING_LOGGER.info("{} do shutdown begin", className); ThreadUtils.shutdownThreadPool(executor, NAMING_LOGGER); diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/core/HostReactorTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/core/HostReactorTest.java index 2462a32b3a8..2df457e8efb 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/core/HostReactorTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/core/HostReactorTest.java @@ -16,20 +16,23 @@ package com.alibaba.nacos.client.naming.core; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.pojo.Instance; +import com.alibaba.nacos.api.naming.pojo.ServiceInfo; +import com.alibaba.nacos.client.naming.beat.BeatInfo; +import com.alibaba.nacos.client.naming.beat.BeatReactor; +import com.alibaba.nacos.client.naming.net.NamingProxy; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import java.util.HashMap; + import static org.junit.Assert.*; import static org.mockito.Mockito.when; -import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.api.naming.pojo.Instance; -import com.alibaba.nacos.api.naming.pojo.ServiceInfo; -import com.alibaba.nacos.client.naming.net.NamingProxy; - @RunWith(MockitoJUnitRunner.class) public class HostReactorTest { @@ -43,15 +46,31 @@ public class HostReactorTest { private HostReactor hostReactor; + private BeatReactor beatReactor; + @Before public void setUp() throws Exception { - hostReactor = new HostReactor(eventDispatcher, namingProxy, CACHE_DIR); + beatReactor = new BeatReactor(namingProxy); + BeatInfo beatInfo = new BeatInfo(); + beatInfo.setServiceName("testName"); + beatInfo.setIp("1.1.1.1"); + beatInfo.setPort(1234); + beatInfo.setCluster("clusterName"); + beatInfo.setWeight(1); + beatInfo.setMetadata(new HashMap()); + beatInfo.setScheduled(false); + beatInfo.setPeriod(1000L); + beatReactor.addBeatInfo("testName", beatInfo); + hostReactor = new HostReactor(eventDispatcher, namingProxy, beatReactor, CACHE_DIR); } @Test public void testProcessServiceJSON() { ServiceInfo actual = hostReactor.processServiceJSON(EXAMPLE); assertServiceInfo(actual); + hostReactor.processServiceJSON(CHANGE_DATA_EXAMPLE); + BeatInfo actualBeatInfo = beatReactor.dom2Beat.get(beatReactor.buildKey("testName", "1.1.1.1", 1234)); + assertEquals(2.0, actualBeatInfo.getWeight(), 0.0); } @Test @@ -105,4 +124,30 @@ private void assertInstance(Instance actual) { + "\t\"allIPs\": false,\n" + "\t\"valid\": true\n" + "}"; + + //the weight changed from 1.0 to 2.0 + private static final String CHANGE_DATA_EXAMPLE = "{\n" + + "\t\"name\": \"testName\",\n" + + "\t\"clusters\": \"testClusters\",\n" + + "\t\"cacheMillis\": 1000,\n" + + "\t\"hosts\": [{\n" + + "\t\t\"ip\": \"1.1.1.1\",\n" + + "\t\t\"port\": 1234,\n" + + "\t\t\"weight\": 2.0,\n" + + "\t\t\"healthy\": true,\n" + + "\t\t\"enabled\": true,\n" + + "\t\t\"ephemeral\": true,\n" + + "\t\t\"clusterName\": \"testClusters\",\n" + + "\t\t\"serviceName\": \"testName\",\n" + + "\t\t\"metadata\": {},\n" + + "\t\t\"instanceHeartBeatInterval\": 5000,\n" + + "\t\t\"instanceHeartBeatTimeOut\": 15000,\n" + + "\t\t\"ipDeleteTimeout\": 30000,\n" + + "\t\t\"instanceIdGenerator\": \"simple\"\n" + + "\t}],\n" + + "\t\"lastRefTime\": 0,\n" + + "\t\"checksum\": \"\",\n" + + "\t\"allIPs\": false,\n" + + "\t\"valid\": true\n" + + "}"; } From e44c86a1a0113baf243742ba0bf3cafcbb94bd3c Mon Sep 17 00:00:00 2001 From: KomachiSion <263976490@qq.com> Date: Fri, 12 Jun 2020 15:30:33 +0800 Subject: [PATCH 065/156] For #2873, unified copyrights for nacos-address. --- address/pom.xml | 25 +++++++++++-------- .../AddressServerGeneratorManager.java | 2 +- .../component/AddressServerManager.java | 2 +- .../constant/AddressServerConstants.java | 2 +- .../AddressServerClusterController.java | 2 +- .../controller/ServerListController.java | 2 +- .../alibaba/nacos/address/misc/Loggers.java | 2 +- .../util/AddressServerParamCheckUtil.java | 2 +- .../META-INF/logback/nacos-included.xml | 16 ++++++++++++ .../META-INF/nacos-default.properties | 16 ++++++++++++ .../src/main/resources/application.properties | 16 ++++++++++++ .../address/AddressServerControllerTests.java | 2 +- .../nacos/address/ParamCheckUtilTests.java | 2 +- .../nacos/address/SimpleHttpTestUtils.java | 2 +- 14 files changed, 72 insertions(+), 21 deletions(-) diff --git a/address/pom.xml b/address/pom.xml index a77be3b0cb4..4db6e94b863 100644 --- a/address/pom.xml +++ b/address/pom.xml @@ -1,16 +1,19 @@ + ~ Copyright 1999-2018 Alibaba Group Holding Ltd. + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> diff --git a/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java b/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java index 90e4008ad50..a0dda0fdffb 100644 --- a/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java +++ b/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 the original author or authors. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/address/src/main/java/com/alibaba/nacos/address/component/AddressServerManager.java b/address/src/main/java/com/alibaba/nacos/address/component/AddressServerManager.java index c3a6d01a804..43be032528e 100644 --- a/address/src/main/java/com/alibaba/nacos/address/component/AddressServerManager.java +++ b/address/src/main/java/com/alibaba/nacos/address/component/AddressServerManager.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 the original author or authors. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/address/src/main/java/com/alibaba/nacos/address/constant/AddressServerConstants.java b/address/src/main/java/com/alibaba/nacos/address/constant/AddressServerConstants.java index c60881b92ec..c12984ab3de 100644 --- a/address/src/main/java/com/alibaba/nacos/address/constant/AddressServerConstants.java +++ b/address/src/main/java/com/alibaba/nacos/address/constant/AddressServerConstants.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 the original author or authors. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/address/src/main/java/com/alibaba/nacos/address/controller/AddressServerClusterController.java b/address/src/main/java/com/alibaba/nacos/address/controller/AddressServerClusterController.java index c917201d25b..49f0748a3be 100644 --- a/address/src/main/java/com/alibaba/nacos/address/controller/AddressServerClusterController.java +++ b/address/src/main/java/com/alibaba/nacos/address/controller/AddressServerClusterController.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 the original author or authors. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/address/src/main/java/com/alibaba/nacos/address/controller/ServerListController.java b/address/src/main/java/com/alibaba/nacos/address/controller/ServerListController.java index 263fc897aca..c75adb7b6bb 100644 --- a/address/src/main/java/com/alibaba/nacos/address/controller/ServerListController.java +++ b/address/src/main/java/com/alibaba/nacos/address/controller/ServerListController.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 the original author or authors. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/address/src/main/java/com/alibaba/nacos/address/misc/Loggers.java b/address/src/main/java/com/alibaba/nacos/address/misc/Loggers.java index dd707de100b..a450ff1982f 100644 --- a/address/src/main/java/com/alibaba/nacos/address/misc/Loggers.java +++ b/address/src/main/java/com/alibaba/nacos/address/misc/Loggers.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 the original author or authors. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/address/src/main/java/com/alibaba/nacos/address/util/AddressServerParamCheckUtil.java b/address/src/main/java/com/alibaba/nacos/address/util/AddressServerParamCheckUtil.java index ec4e991d31b..f4f26b71d2a 100644 --- a/address/src/main/java/com/alibaba/nacos/address/util/AddressServerParamCheckUtil.java +++ b/address/src/main/java/com/alibaba/nacos/address/util/AddressServerParamCheckUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 the original author or authors. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/address/src/main/resources/META-INF/logback/nacos-included.xml b/address/src/main/resources/META-INF/logback/nacos-included.xml index ed8f8a1e3ce..14324efe205 100644 --- a/address/src/main/resources/META-INF/logback/nacos-included.xml +++ b/address/src/main/resources/META-INF/logback/nacos-included.xml @@ -1,4 +1,20 @@ + + diff --git a/address/src/main/resources/META-INF/nacos-default.properties b/address/src/main/resources/META-INF/nacos-default.properties index e69de29bb2d..49021f5d040 100644 --- a/address/src/main/resources/META-INF/nacos-default.properties +++ b/address/src/main/resources/META-INF/nacos-default.properties @@ -0,0 +1,16 @@ +# +# Copyright 1999-2018 Alibaba Group Holding Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + diff --git a/address/src/main/resources/application.properties b/address/src/main/resources/application.properties index 8a6c9ff9b65..6acfc72c546 100644 --- a/address/src/main/resources/application.properties +++ b/address/src/main/resources/application.properties @@ -1,2 +1,18 @@ +# +# Copyright 1999-2018 Alibaba Group Holding Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + server.port=8080 server.servlet.context-path=/ diff --git a/address/src/test/java/com/alibaba/nacos/address/AddressServerControllerTests.java b/address/src/test/java/com/alibaba/nacos/address/AddressServerControllerTests.java index 78461de964d..a7c5d7d20c1 100644 --- a/address/src/test/java/com/alibaba/nacos/address/AddressServerControllerTests.java +++ b/address/src/test/java/com/alibaba/nacos/address/AddressServerControllerTests.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 the original author or authors. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/address/src/test/java/com/alibaba/nacos/address/ParamCheckUtilTests.java b/address/src/test/java/com/alibaba/nacos/address/ParamCheckUtilTests.java index 395bfcac429..ef9eaebd9ec 100644 --- a/address/src/test/java/com/alibaba/nacos/address/ParamCheckUtilTests.java +++ b/address/src/test/java/com/alibaba/nacos/address/ParamCheckUtilTests.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 the original author or authors. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/address/src/test/java/com/alibaba/nacos/address/SimpleHttpTestUtils.java b/address/src/test/java/com/alibaba/nacos/address/SimpleHttpTestUtils.java index 12648ec7ce1..cae710fbd25 100644 --- a/address/src/test/java/com/alibaba/nacos/address/SimpleHttpTestUtils.java +++ b/address/src/test/java/com/alibaba/nacos/address/SimpleHttpTestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 the original author or authors. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 4deb71885996c1b7bf4b6078a9287727a372b1a8 Mon Sep 17 00:00:00 2001 From: KomachiSion <263976490@qq.com> Date: Fri, 12 Jun 2020 15:31:20 +0800 Subject: [PATCH 066/156] For #2873, unified copyrights for nacos-api. --- api/pom.xml | 25 +++++++++++-------- .../nacos/api/SystemPropertyKeyConst.java | 2 +- .../alibaba/nacos/api/config/ConfigType.java | 2 +- .../nacos/api/config/PropertyChangeType.java | 2 +- api/src/main/resources/application.properties | 18 ++++++++++++- pom.xml | 2 -- 6 files changed, 34 insertions(+), 17 deletions(-) diff --git a/api/pom.xml b/api/pom.xml index 7cd60209eec..d07087aae3f 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -1,16 +1,19 @@ + ~ Copyright 1999-2018 Alibaba Group Holding Ltd. + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> diff --git a/api/src/main/java/com/alibaba/nacos/api/SystemPropertyKeyConst.java b/api/src/main/java/com/alibaba/nacos/api/SystemPropertyKeyConst.java index 580a241b05d..bad044cf938 100644 --- a/api/src/main/java/com/alibaba/nacos/api/SystemPropertyKeyConst.java +++ b/api/src/main/java/com/alibaba/nacos/api/SystemPropertyKeyConst.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 the original author or authors. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/api/src/main/java/com/alibaba/nacos/api/config/ConfigType.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigType.java index be063af838d..66cdb958f5d 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/ConfigType.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigType.java @@ -1,7 +1,7 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. * - * Licensed under the Apache License, Version 2.0 = the "License""); + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * diff --git a/api/src/main/java/com/alibaba/nacos/api/config/PropertyChangeType.java b/api/src/main/java/com/alibaba/nacos/api/config/PropertyChangeType.java index 93c47dfd493..a0d683e7c05 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/PropertyChangeType.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/PropertyChangeType.java @@ -1,7 +1,7 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. * - * Licensed under the Apache License, Version 2.0 = the "License""); + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * diff --git a/api/src/main/resources/application.properties b/api/src/main/resources/application.properties index e5683df88cb..5d11f83de6a 100644 --- a/api/src/main/resources/application.properties +++ b/api/src/main/resources/application.properties @@ -1 +1,17 @@ -version=${project.version} \ No newline at end of file +# +# Copyright 1999-2018 Alibaba Group Holding Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +version=${project.version} diff --git a/pom.xml b/pom.xml index 06cb1596863..77982c5ed47 100644 --- a/pom.xml +++ b/pom.xml @@ -307,8 +307,6 @@ REPORTING-BUGS.md README.md .github/**/* - src/main/resources/** - src/test/** bin/* conf/* doc/* From 079009b5a57176065150774c534cbddc3f8dc394 Mon Sep 17 00:00:00 2001 From: KomachiSion <263976490@qq.com> Date: Fri, 12 Jun 2020 15:33:41 +0800 Subject: [PATCH 067/156] For #2873, unified copyrights for nacos-client. --- client/pom.xml | 25 +++++++++++-------- .../alibaba/nacos/client/identify/Base64.java | 11 ++++---- .../client/naming/utils/CollectionUtils.java | 21 ++++++++-------- .../nacos/client/naming/utils/JvmRandom.java | 25 ++++++++----------- .../client/naming/utils/RandomUtils.java | 25 ++++++++----------- .../nacos/client/naming/utils/SignUtil.java | 2 +- .../nacos/client/utils/TemplateUtils.java | 2 +- .../src/main/resources/application.properties | 18 ++++++++++++- client/src/main/resources/nacos-log4j2.xml | 16 ++++++++++++ client/src/main/resources/nacos-logback.xml | 16 ++++++++++++ .../alibaba/nacos/client/BeatReactorTest.java | 16 ++++++++++++ .../com/alibaba/nacos/client/ConfigTest.java | 2 -- .../client/config/common/GroupKeyTest.java | 2 +- .../client/utils/ValidatorUtilsTest.java | 18 ++++++++++++- 14 files changed, 136 insertions(+), 63 deletions(-) diff --git a/client/pom.xml b/client/pom.xml index 49d845a1c1d..8b8614af450 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -1,16 +1,19 @@ + ~ Copyright 1999-2018 Alibaba Group Holding Ltd. + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> diff --git a/client/src/main/java/com/alibaba/nacos/client/identify/Base64.java b/client/src/main/java/com/alibaba/nacos/client/identify/Base64.java index 1829b0c148f..3c6366bf0e3 100644 --- a/client/src/main/java/com/alibaba/nacos/client/identify/Base64.java +++ b/client/src/main/java/com/alibaba/nacos/client/identify/Base64.java @@ -1,10 +1,9 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/CollectionUtils.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/CollectionUtils.java index 65f9e3227bc..664fdd204a6 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/CollectionUtils.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/CollectionUtils.java @@ -1,18 +1,17 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.alibaba.nacos.client.naming.utils; diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/JvmRandom.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/JvmRandom.java index 362c1747c1b..5fd093773c3 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/JvmRandom.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/JvmRandom.java @@ -1,20 +1,17 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at + * Copyright 1999-2018 Alibaba Group Holding Ltd. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.alibaba.nacos.client.naming.utils; diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/RandomUtils.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/RandomUtils.java index 7601faf0145..836114d7d0d 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/RandomUtils.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/RandomUtils.java @@ -1,20 +1,17 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at + * Copyright 1999-2018 Alibaba Group Holding Ltd. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.alibaba.nacos.client.naming.utils; diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/SignUtil.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/SignUtil.java index e77f6929a1f..fc59a69904f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/SignUtil.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/SignUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 the original author or authors. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/client/src/main/java/com/alibaba/nacos/client/utils/TemplateUtils.java b/client/src/main/java/com/alibaba/nacos/client/utils/TemplateUtils.java index dac97544cca..ab7cdf1ec95 100644 --- a/client/src/main/java/com/alibaba/nacos/client/utils/TemplateUtils.java +++ b/client/src/main/java/com/alibaba/nacos/client/utils/TemplateUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 the original author or authors. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/client/src/main/resources/application.properties b/client/src/main/resources/application.properties index e5683df88cb..5d11f83de6a 100644 --- a/client/src/main/resources/application.properties +++ b/client/src/main/resources/application.properties @@ -1 +1,17 @@ -version=${project.version} \ No newline at end of file +# +# Copyright 1999-2018 Alibaba Group Holding Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +version=${project.version} diff --git a/client/src/main/resources/nacos-log4j2.xml b/client/src/main/resources/nacos-log4j2.xml index 5e242380d22..baff4ce6658 100644 --- a/client/src/main/resources/nacos-log4j2.xml +++ b/client/src/main/resources/nacos-log4j2.xml @@ -1,4 +1,20 @@ + + + + nacos diff --git a/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java b/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java index dbce5b4e3f6..66abfa6093d 100644 --- a/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/BeatReactorTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.client; import com.alibaba.nacos.api.exception.NacosException; diff --git a/client/src/test/java/com/alibaba/nacos/client/ConfigTest.java b/client/src/test/java/com/alibaba/nacos/client/ConfigTest.java index f3f18edd076..ad518c80a3d 100644 --- a/client/src/test/java/com/alibaba/nacos/client/ConfigTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/ConfigTest.java @@ -1,5 +1,4 @@ /* - * * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,6 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ package com.alibaba.nacos.client; diff --git a/client/src/test/java/com/alibaba/nacos/client/config/common/GroupKeyTest.java b/client/src/test/java/com/alibaba/nacos/client/config/common/GroupKeyTest.java index 98d3009cfee..0a300c745af 100644 --- a/client/src/test/java/com/alibaba/nacos/client/config/common/GroupKeyTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/config/common/GroupKeyTest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/client/src/test/java/com/alibaba/nacos/client/utils/ValidatorUtilsTest.java b/client/src/test/java/com/alibaba/nacos/client/utils/ValidatorUtilsTest.java index fa551d80655..8f910e15a7d 100644 --- a/client/src/test/java/com/alibaba/nacos/client/utils/ValidatorUtilsTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/utils/ValidatorUtilsTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.client.utils; import org.junit.Test; @@ -86,4 +102,4 @@ public void test_server_addrs_k8s_err() { ValidatorUtils.checkServerAddr(serverAddrs); } -} \ No newline at end of file +} From cb0ffe43697dc72cbb18eb29885d6b2e999a2637 Mon Sep 17 00:00:00 2001 From: KomachiSion <263976490@qq.com> Date: Fri, 12 Jun 2020 15:34:10 +0800 Subject: [PATCH 068/156] For #2873, unified copyrights for nacos-cmdb. --- cmdb/pom.xml | 25 +++++++++++-------- .../src/main/resources/application.properties | 18 ++++++++++++- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/cmdb/pom.xml b/cmdb/pom.xml index 09f1b9eec78..ea6a361a952 100644 --- a/cmdb/pom.xml +++ b/cmdb/pom.xml @@ -1,16 +1,19 @@ + ~ Copyright 1999-2018 Alibaba Group Holding Ltd. + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> diff --git a/cmdb/src/main/resources/application.properties b/cmdb/src/main/resources/application.properties index d92634331aa..906c51ce652 100644 --- a/cmdb/src/main/resources/application.properties +++ b/cmdb/src/main/resources/application.properties @@ -1,6 +1,22 @@ +# +# Copyright 1999-2018 Alibaba Group Holding Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + server.port=8848 server.servlet.context-path=/nacos nacos.cmdb.dumpTaskInterval=3600 nacos.cmdb.eventTaskInterval=10 -nacos.cmdb.loadDataAtStart=true \ No newline at end of file +nacos.cmdb.loadDataAtStart=true From f42dd3436d62a3ed3a7ad40e402339c9b43f1b24 Mon Sep 17 00:00:00 2001 From: KomachiSion <263976490@qq.com> Date: Fri, 12 Jun 2020 15:44:51 +0800 Subject: [PATCH 069/156] For #2873, unified copyrights for nacos-common. --- common/license | 15 ----------- common/pom.xml | 25 +++++++++++-------- .../alibaba/nacos/common/utils/MapUtils.java | 2 -- .../nacos/common/utils/MD5UtilsTest.java | 2 +- .../nacos/common/utils/MapUtilsTest.java | 18 ++++++++++++- resources/copyright | 14 +++++++++++ 6 files changed, 46 insertions(+), 30 deletions(-) delete mode 100644 common/license create mode 100644 resources/copyright diff --git a/common/license b/common/license deleted file mode 100644 index e2ccce1fa96..00000000000 --- a/common/license +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ \ No newline at end of file diff --git a/common/pom.xml b/common/pom.xml index d59268a2b33..2557a0d6155 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -1,16 +1,19 @@ + ~ Copyright 1999-2018 Alibaba Group Holding Ltd. + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/MapUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/MapUtils.java index 97044ea21a2..97e0737b11c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/MapUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/MapUtils.java @@ -1,5 +1,4 @@ /* - * * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,6 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ package com.alibaba.nacos.common.utils; diff --git a/common/src/test/java/com/alibaba/nacos/common/utils/MD5UtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/utils/MD5UtilsTest.java index ffe76c1fb90..0f3eee7738b 100644 --- a/common/src/test/java/com/alibaba/nacos/common/utils/MD5UtilsTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/utils/MD5UtilsTest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2019 Alibaba Group Holding Ltd. + * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/common/src/test/java/com/alibaba/nacos/common/utils/MapUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/utils/MapUtilsTest.java index 40d59fe02a7..8ade9b8b771 100644 --- a/common/src/test/java/com/alibaba/nacos/common/utils/MapUtilsTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/utils/MapUtilsTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.common.utils; import org.junit.Assert; @@ -47,4 +63,4 @@ public void test_map() { Assert.assertTrue(map.containsKey("key-map")); } -} \ No newline at end of file +} diff --git a/resources/copyright b/resources/copyright new file mode 100644 index 00000000000..b7cc70113ff --- /dev/null +++ b/resources/copyright @@ -0,0 +1,14 @@ + +Copyright 1999-2018 Alibaba Group Holding Ltd. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. From 8aaa83360503806e71aaf6c7b57bcf8b8aeb43e9 Mon Sep 17 00:00:00 2001 From: KomachiSion <263976490@qq.com> Date: Fri, 12 Jun 2020 15:45:35 +0800 Subject: [PATCH 070/156] For #2873, unified copyrights for nacos-config. --- config/pom.xml | 25 +++++---- .../model/event/ConfigDataChangeEvent.java | 2 - .../server/model/event/ConfigDumpEvent.java | 2 - .../model/event/LocalDataChangeEvent.java | 2 - .../service/datasource/DataSourceService.java | 2 - .../service/datasource/DynamicDataSource.java | 2 - .../ExternalDataSourceServiceImpl.java | 2 - .../LocalDataSourceServiceImpl.java | 2 - .../service/dump/DumpConfigHandler.java | 2 - .../service/repository/RowMapperManager.java | 2 - .../nacos/config/server/utils/DiskUtil.java | 2 - .../META-INF/logback/config-included.xml | 16 ++++++ .../src/main/resources/META-INF/nacos-db.sql | 16 ++++++ config/src/main/resources/META-INF/schema.sql | 16 ++++++ .../service/ConfigChangePublisherTest.java | 18 +++++- .../server/service/RowMapperManagerTest.java | 18 +++++- .../server/service/dump/DumpServiceTest.java | 16 ++++++ config/src/test/resources/jdbc.properties | 16 ++++++ config/src/test/resources/log4j.properties | 56 ++++++++++++------- config/src/test/resources/user.properties | 18 +++++- 20 files changed, 181 insertions(+), 54 deletions(-) diff --git a/config/pom.xml b/config/pom.xml index 659c6e9127a..d72793eef7b 100644 --- a/config/pom.xml +++ b/config/pom.xml @@ -1,16 +1,19 @@ + ~ Copyright 1999-2018 Alibaba Group Holding Ltd. + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> diff --git a/config/src/main/java/com/alibaba/nacos/config/server/model/event/ConfigDataChangeEvent.java b/config/src/main/java/com/alibaba/nacos/config/server/model/event/ConfigDataChangeEvent.java index b34287ea268..badd540f645 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/model/event/ConfigDataChangeEvent.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/model/event/ConfigDataChangeEvent.java @@ -1,5 +1,4 @@ /* - * * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,6 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ package com.alibaba.nacos.config.server.model.event; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/model/event/ConfigDumpEvent.java b/config/src/main/java/com/alibaba/nacos/config/server/model/event/ConfigDumpEvent.java index 6d0752a1c60..decc40cbdf4 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/model/event/ConfigDumpEvent.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/model/event/ConfigDumpEvent.java @@ -1,5 +1,4 @@ /* - * * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,6 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ package com.alibaba.nacos.config.server.model.event; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/model/event/LocalDataChangeEvent.java b/config/src/main/java/com/alibaba/nacos/config/server/model/event/LocalDataChangeEvent.java index dccb71bcb82..9faf2a14a8a 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/model/event/LocalDataChangeEvent.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/model/event/LocalDataChangeEvent.java @@ -1,5 +1,4 @@ /* - * * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,6 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ package com.alibaba.nacos.config.server.model.event; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/DataSourceService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/DataSourceService.java index 1b0bfbcbabe..e581326eb43 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/DataSourceService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/DataSourceService.java @@ -1,5 +1,4 @@ /* - * * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,6 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ package com.alibaba.nacos.config.server.service.datasource; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/DynamicDataSource.java b/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/DynamicDataSource.java index 35fec62f165..4da0a5b9b73 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/DynamicDataSource.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/DynamicDataSource.java @@ -1,5 +1,4 @@ /* - * * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,6 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ package com.alibaba.nacos.config.server.service.datasource; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/ExternalDataSourceServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/ExternalDataSourceServiceImpl.java index 1c80a39199a..43d7da6abb6 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/ExternalDataSourceServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/ExternalDataSourceServiceImpl.java @@ -1,5 +1,4 @@ /* - * * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,6 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ package com.alibaba.nacos.config.server.service.datasource; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/LocalDataSourceServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/LocalDataSourceServiceImpl.java index d8a9cf9ac32..8a5da114fe2 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/LocalDataSourceServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/datasource/LocalDataSourceServiceImpl.java @@ -1,5 +1,4 @@ /* - * * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,6 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ package com.alibaba.nacos.config.server.service.datasource; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpConfigHandler.java b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpConfigHandler.java index c93c092da77..916eaa62542 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpConfigHandler.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpConfigHandler.java @@ -1,5 +1,4 @@ /* - * * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,6 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ package com.alibaba.nacos.config.server.service.dump; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/RowMapperManager.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/RowMapperManager.java index cbd70fa5baa..f22be140401 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/RowMapperManager.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/RowMapperManager.java @@ -1,5 +1,4 @@ /* - * * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,6 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ package com.alibaba.nacos.config.server.service.repository; diff --git a/config/src/main/java/com/alibaba/nacos/config/server/utils/DiskUtil.java b/config/src/main/java/com/alibaba/nacos/config/server/utils/DiskUtil.java index ff99f4348ec..6cde6cc043d 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/utils/DiskUtil.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/utils/DiskUtil.java @@ -1,5 +1,4 @@ /* - * * Copyright 1999-2018 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +12,6 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ package com.alibaba.nacos.config.server.utils; diff --git a/config/src/main/resources/META-INF/logback/config-included.xml b/config/src/main/resources/META-INF/logback/config-included.xml index eedf9335f1e..ef4c58ea282 100644 --- a/config/src/main/resources/META-INF/logback/config-included.xml +++ b/config/src/main/resources/META-INF/logback/config-included.xml @@ -1,4 +1,20 @@ + + diff --git a/config/src/main/resources/META-INF/nacos-db.sql b/config/src/main/resources/META-INF/nacos-db.sql index f7953e5a131..563d7610fe7 100644 --- a/config/src/main/resources/META-INF/nacos-db.sql +++ b/config/src/main/resources/META-INF/nacos-db.sql @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + /******************************************/ /* 数据库全名 = nacos_config */ /* 表名称 = config_info */ diff --git a/config/src/main/resources/META-INF/schema.sql b/config/src/main/resources/META-INF/schema.sql index 06d0b000b5d..d7bf05810a9 100644 --- a/config/src/main/resources/META-INF/schema.sql +++ b/config/src/main/resources/META-INF/schema.sql @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + CREATE SCHEMA nacos AUTHORIZATION nacos; CREATE TABLE config_info ( diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/ConfigChangePublisherTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/ConfigChangePublisherTest.java index 2cd06b8f6b0..56027506aee 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/ConfigChangePublisherTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/ConfigChangePublisherTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.config.server.service; import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent; @@ -66,4 +82,4 @@ public void onEvent(EventDispatcher.Event event) { reference.set(null); } -} \ No newline at end of file +} diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/RowMapperManagerTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/RowMapperManagerTest.java index 004bbd7eff7..7ace73db4b5 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/RowMapperManagerTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/RowMapperManagerTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.config.server.service; import com.alibaba.nacos.config.server.service.repository.RowMapperManager; @@ -15,4 +31,4 @@ public void test_user_mapper() { Assert.assertEquals(ClassUtils.resolveGenericTypeByInterface(mapper.getClass()).getSimpleName(), User.class.getSimpleName()); } -} \ No newline at end of file +} diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java index acc5e3dd59a..33caf254786 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.config.server.service.dump; import org.junit.Test; diff --git a/config/src/test/resources/jdbc.properties b/config/src/test/resources/jdbc.properties index e69de29bb2d..49021f5d040 100755 --- a/config/src/test/resources/jdbc.properties +++ b/config/src/test/resources/jdbc.properties @@ -0,0 +1,16 @@ +# +# Copyright 1999-2018 Alibaba Group Holding Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + diff --git a/config/src/test/resources/log4j.properties b/config/src/test/resources/log4j.properties index a6e39c43eef..601248f52fb 100644 --- a/config/src/test/resources/log4j.properties +++ b/config/src/test/resources/log4j.properties @@ -1,20 +1,36 @@ -log4j.rootLogger=DEBUG, ServerDailyRollingFile,stdout -log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender -log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd_HH -log4j.appender.ServerDailyRollingFile.File=${webapp.root}/WEB-INF/logs/nacos-server.log -log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout -log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=[%p] [%t] %d{MM-dd HH:mm:ss,SSS} [%c{1}] - %m%n -log4j.appender.ServerDailyRollingFile.Append=true - -log4j.logger.opLog=INFO, opFile -log4j.appender.opFile=org.apache.log4j.DailyRollingFileAppender -log4j.appender.opFile.DatePattern='.'yyyy-MM-dd_HH -log4j.appender.opFile.File=${webapp.root}/WEB-INF/logs/operation.log -log4j.appender.opFile.layout=org.apache.log4j.PatternLayout -log4j.appender.opFile.layout.ConversionPattern=[%p] [%t] %d{MM-dd HH:mm:ss,SSS} [%c{1}] - %m%n -log4j.appender.opFile.Append=true - -log4j.logger.com.taobao.config = warn -log4j.logger.org.apache.http.wire=warn -log4j.logger.java.sql = warn -log4j.logger.com.ibatis.common.jdbc=warn \ No newline at end of file +# +# Copyright 1999-2018 Alibaba Group Holding Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +log4j.rootLogger=DEBUG, ServerDailyRollingFile,stdout +log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender +log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd_HH +log4j.appender.ServerDailyRollingFile.File=${webapp.root}/WEB-INF/logs/nacos-server.log +log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout +log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=[%p] [%t] %d{MM-dd HH:mm:ss,SSS} [%c{1}] - %m%n +log4j.appender.ServerDailyRollingFile.Append=true + +log4j.logger.opLog=INFO, opFile +log4j.appender.opFile=org.apache.log4j.DailyRollingFileAppender +log4j.appender.opFile.DatePattern='.'yyyy-MM-dd_HH +log4j.appender.opFile.File=${webapp.root}/WEB-INF/logs/operation.log +log4j.appender.opFile.layout=org.apache.log4j.PatternLayout +log4j.appender.opFile.layout.ConversionPattern=[%p] [%t] %d{MM-dd HH:mm:ss,SSS} [%c{1}] - %m%n +log4j.appender.opFile.Append=true + +log4j.logger.com.taobao.config = warn +log4j.logger.org.apache.http.wire=warn +log4j.logger.java.sql = warn +log4j.logger.com.ibatis.common.jdbc=warn diff --git a/config/src/test/resources/user.properties b/config/src/test/resources/user.properties index ab26fdd51bb..669c95b3243 100755 --- a/config/src/test/resources/user.properties +++ b/config/src/test/resources/user.properties @@ -1 +1,17 @@ -admin=admin \ No newline at end of file +# +# Copyright 1999-2018 Alibaba Group Holding Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +admin=admin From 1577eeb57c2a98d7db2da815c149536bd42cea6d Mon Sep 17 00:00:00 2001 From: KomachiSion <263976490@qq.com> Date: Fri, 12 Jun 2020 15:46:28 +0800 Subject: [PATCH 071/156] For #2873, unified copyrights for nacos-consistency. --- consistency/pom.xml | 27 ++++++++++--------- .../nacos/consistency/entity/Data.java | 26 ++++++++++++++---- .../nacos/consistency/entity/GetRequest.java | 16 +++++++++++ .../entity/GetRequestOrBuilder.java | 16 +++++++++++ .../alibaba/nacos/consistency/entity/Log.java | 16 +++++++++++ .../consistency/entity/LogOrBuilder.java | 16 +++++++++++ .../nacos/consistency/entity/Response.java | 16 +++++++++++ .../consistency/entity/ResponseOrBuilder.java | 16 +++++++++++ .../consistency/ProtocolMetaDataTest.java | 16 +++++++++++ .../consistency/SerializeFactoryTest.java | 18 ++++++++++++- 10 files changed, 164 insertions(+), 19 deletions(-) diff --git a/consistency/pom.xml b/consistency/pom.xml index c4cd3cd9090..2d47dcfb195 100644 --- a/consistency/pom.xml +++ b/consistency/pom.xml @@ -1,18 +1,19 @@ + ~ Copyright 1999-2018 Alibaba Group Holding Ltd. + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/Data.java b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/Data.java index b0c2d0061af..f95df7d5fe4 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/Data.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/Data.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + // Generated by the protocol buffer compiler. DO NOT EDIT! // source: Data.proto @@ -16,27 +32,27 @@ public static void registerAllExtensions( } static final com.google.protobuf.Descriptors.Descriptor internal_static_Log_descriptor; - static final + static final com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internal_static_Log_fieldAccessorTable; static final com.google.protobuf.Descriptors.Descriptor internal_static_Log_ExtendInfoEntry_descriptor; - static final + static final com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internal_static_Log_ExtendInfoEntry_fieldAccessorTable; static final com.google.protobuf.Descriptors.Descriptor internal_static_GetRequest_descriptor; - static final + static final com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internal_static_GetRequest_fieldAccessorTable; static final com.google.protobuf.Descriptors.Descriptor internal_static_GetRequest_ExtendInfoEntry_descriptor; - static final + static final com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internal_static_GetRequest_ExtendInfoEntry_fieldAccessorTable; static final com.google.protobuf.Descriptors.Descriptor internal_static_Response_descriptor; - static final + static final com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internal_static_Response_fieldAccessorTable; diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/GetRequest.java b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/GetRequest.java index 9b12f952c2e..e582b326799 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/GetRequest.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/GetRequest.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + // Generated by the protocol buffer compiler. DO NOT EDIT! // source: Data.proto diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/GetRequestOrBuilder.java b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/GetRequestOrBuilder.java index c63a0437bec..172c0abd1a8 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/GetRequestOrBuilder.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/GetRequestOrBuilder.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + // Generated by the protocol buffer compiler. DO NOT EDIT! // source: Data.proto diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/Log.java b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/Log.java index 15d78ab7578..f54d5fdf189 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/Log.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/Log.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + // Generated by the protocol buffer compiler. DO NOT EDIT! // source: Data.proto diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/LogOrBuilder.java b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/LogOrBuilder.java index 46c1b6003e5..9a8cfa159ed 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/LogOrBuilder.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/LogOrBuilder.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + // Generated by the protocol buffer compiler. DO NOT EDIT! // source: Data.proto diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/Response.java b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/Response.java index 9cbc29ceac0..eff188d6f8a 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/Response.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/Response.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + // Generated by the protocol buffer compiler. DO NOT EDIT! // source: Data.proto diff --git a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/ResponseOrBuilder.java b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/ResponseOrBuilder.java index 67ecc2ec3ca..20c3c58eaf2 100644 --- a/consistency/src/main/java/com/alibaba/nacos/consistency/entity/ResponseOrBuilder.java +++ b/consistency/src/main/java/com/alibaba/nacos/consistency/entity/ResponseOrBuilder.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + // Generated by the protocol buffer compiler. DO NOT EDIT! // source: Data.proto diff --git a/consistency/src/test/java/com/alibaba/nacos/consistency/ProtocolMetaDataTest.java b/consistency/src/test/java/com/alibaba/nacos/consistency/ProtocolMetaDataTest.java index 146286b260f..353501f4cb7 100644 --- a/consistency/src/test/java/com/alibaba/nacos/consistency/ProtocolMetaDataTest.java +++ b/consistency/src/test/java/com/alibaba/nacos/consistency/ProtocolMetaDataTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.consistency; import java.time.LocalDateTime; diff --git a/consistency/src/test/java/com/alibaba/nacos/consistency/SerializeFactoryTest.java b/consistency/src/test/java/com/alibaba/nacos/consistency/SerializeFactoryTest.java index 6aea133e9f9..39a8c43d3df 100644 --- a/consistency/src/test/java/com/alibaba/nacos/consistency/SerializeFactoryTest.java +++ b/consistency/src/test/java/com/alibaba/nacos/consistency/SerializeFactoryTest.java @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.alibaba.nacos.consistency; import org.junit.Assert; @@ -60,4 +76,4 @@ public void test_set_serialize() { System.out.println(result); } -} \ No newline at end of file +} From 89406abc23882a2714e3afefc0e534ed72fcba7f Mon Sep 17 00:00:00 2001 From: KomachiSion <263976490@qq.com> Date: Fri, 12 Jun 2020 15:55:04 +0800 Subject: [PATCH 072/156] For #2873, unified copyrights for nacos-console. --- console/pom.xml | 25 +- .../META-INF/nacos-default.properties | 16 + .../src/main/resources/META-INF/schema.sql | 16 + .../src/main/resources/application.properties | 16 + .../static/console-fe/build/copy-dist.js | 3 + .../static/console-fe/build/copyFile.js | 5 +- .../console-fe/build/webpack.base.conf.js | 3 + .../console-fe/build/webpack.dev.conf.js | 3 + .../console-fe/build/webpack.prod.conf.js | 3 + .../console-fe/public/css/bootstrap.css | 3 + .../console-fe/public/css/codemirror.css | 3 + .../console-fe/public/css/font-awesome.css | 3 + .../static/console-fe/public/css/icon.css | 5 +- .../static/console-fe/public/css/merge.css | 3 + .../static/console-fe/public/index.html | 6 +- .../public/js/codemirror.addone.fullscreen.js | 5 +- .../public/js/codemirror.addone.json-lint.js | 5 +- .../public/js/codemirror.addone.lint.js | 5 +- .../static/console-fe/public/js/codemirror.js | 3 + .../public/js/codemirror.lib.clike-lint.js | 5 +- .../public/js/codemirror.lib.json-lint.js | 9 +- .../static/console-fe/public/js/javascript.js | 3 + .../static/console-fe/public/js/merge.js | 3 + .../public/js/vs/editor/editor.main.js | 2 +- .../public/js/vs/editor/editor.main.nls.de.js | 5 +- .../public/js/vs/editor/editor.main.nls.es.js | 5 +- .../public/js/vs/editor/editor.main.nls.fr.js | 5 +- .../public/js/vs/editor/editor.main.nls.hu.js | 5 +- .../public/js/vs/editor/editor.main.nls.it.js | 5 +- .../public/js/vs/editor/editor.main.nls.ja.js | 5 +- .../public/js/vs/editor/editor.main.nls.js | 5 +- .../public/js/vs/editor/editor.main.nls.ko.js | 5 +- .../js/vs/editor/editor.main.nls.pt-br.js | 5 +- .../public/js/vs/editor/editor.main.nls.ru.js | 5 +- .../public/js/vs/editor/editor.main.nls.tr.js | 5 +- .../js/vs/editor/editor.main.nls.zh-cn.js | 5 +- .../js/vs/editor/editor.main.nls.zh-tw.js | 5 +- .../js/vs/language/typescript/src/worker.js | 5 +- .../static/console-fe/public/js/xml.js | 3 + .../src/components/BatchHandle/BatchHandle.js | 3 + .../src/components/BatchHandle/index.js | 3 + .../src/components/BatchHandle/index.scss | 5 +- .../src/components/CloneDialog/CloneDialog.js | 3 + .../src/components/CloneDialog/index.js | 3 + .../src/components/CloneDialog/index.scss | 5 +- .../components/DeleteDialog/DeleteDialog.js | 3 + .../src/components/DeleteDialog/index.js | 3 + .../src/components/DeleteDialog/index.scss | 5 +- .../DiffEditorDialog/DiffEditorDialog.js | 3 + .../src/components/DiffEditorDialog/index.js | 3 + .../components/DiffEditorDialog/index.scss | 5 +- .../EditorNameSpace/EditorNameSpace.js | 3 + .../src/components/EditorNameSpace/index.js | 3 + .../src/components/EditorNameSpace/index.scss | 5 +- .../components/ExportDialog/ExportDialog.js | 3 + .../src/components/ExportDialog/index.js | 3 + .../src/components/ExportDialog/index.scss | 5 +- .../components/ImportDialog/ImportDialog.js | 3 + .../src/components/ImportDialog/index.js | 3 + .../src/components/ImportDialog/index.scss | 5 +- .../components/MonacoEditor/MonacoEditor.tsx | 3 + .../src/components/MonacoEditor/constant.ts | 3 + .../src/components/MonacoEditor/index.scss | 5 +- .../src/components/MonacoEditor/index.tsx | 3 + .../components/NameSpaceList/NameSpaceList.js | 3 + .../src/components/NameSpaceList/index.js | 3 + .../src/components/NameSpaceList/index.scss | 5 +- .../components/NewNameSpace/NewNameSpace.js | 3 + .../src/components/NewNameSpace/index.js | 3 + .../src/components/NewNameSpace/index.scss | 5 +- .../src/components/RegionGroup/RegionGroup.js | 3 + .../src/components/RegionGroup/index.js | 3 + .../src/components/RegionGroup/index.scss | 5 +- .../src/components/ShowCodeing/ShowCodeing.js | 9 +- .../ShowCodeing/ShowServiceCodeing.js | 13 +- .../src/components/ShowCodeing/index.js | 3 + .../src/components/ShowCodeing/index.scss | 5 +- .../components/SuccessDialog/SuccessDialog.js | 3 + .../src/components/SuccessDialog/index.js | 3 + .../src/components/SuccessDialog/index.scss | 5 +- .../resources/static/console-fe/src/config.js | 3 + .../static/console-fe/src/constants.js | 3 + .../static/console-fe/src/globalLib.js | 3 + .../resources/static/console-fe/src/index.js | 3 + .../static/console-fe/src/index.scss | 5 +- .../static/console-fe/src/layouts/Header.js | 3 + .../console-fe/src/layouts/MainLayout.js | 3 + .../static/console-fe/src/layouts/index.scss | 5 +- .../static/console-fe/src/layouts/menu.js | 16 + .../resources/static/console-fe/src/lib.js | 3 + .../static/console-fe/src/locales/en-US.js | 3 + .../static/console-fe/src/locales/index.js | 3 + .../static/console-fe/src/locales/zh-CN.js | 3 + .../PermissionsManagement/NewPermissions.js | 3 + .../PermissionsManagement.js | 3 + .../PermissionsManagement.scss | 7 +- .../PermissionsManagement/index.js | 3 + .../RolesManagement/NewRole.js | 3 + .../RolesManagement/RolesManagement.js | 3 + .../RolesManagement/RolesManagement.scss | 7 +- .../AuthorityControl/RolesManagement/index.js | 3 + .../UserManagement/NewUser.js | 3 + .../UserManagement/PasswordReset.js | 3 + .../UserManagement/UserManagement.js | 3 + .../UserManagement/UserManagement.scss | 5 +- .../AuthorityControl/UserManagement/index.js | 3 + .../src/pages/AuthorityControl/authority.scss | 5 +- .../ClusterNodeList/ClusterNodeList.js | 3 + .../ClusterNodeList/ClusterNodeList.scss | 5 +- .../ClusterNodeList/index.js | 3 + .../ConfigDetail/ConfigDetail.js | 3 + .../ConfigDetail/index.js | 3 + .../ConfigDetail/index.scss | 5 +- .../ConfigEditor/ConfigEditor.js | 3 + .../ConfigEditor/NewConfigEditor.js | 3 + .../ConfigEditor/index.js | 3 + .../ConfigEditor/index.scss | 5 +- .../ConfigRollback/ConfigRollback.js | 3 + .../ConfigRollback/index.js | 3 + .../ConfigRollback/index.scss | 5 +- .../ConfigSync/ConfigSync.js | 3 + .../ConfigSync/index.js | 3 + .../ConfigSync/index.scss | 5 +- .../ConfigurationManagement.js | 3 + .../ConfigurationManagement/DashboardCard.js | 3 + .../ConfigurationManagement/index.js | 3 + .../ConfigurationManagement/index.scss | 5 +- .../HistoryDetail/HistoryDetail.js | 3 + .../HistoryDetail/index.js | 3 + .../HistoryDetail/index.scss | 5 +- .../HistoryRollback/HistoryRollback.js | 3 + .../HistoryRollback/index.js | 3 + .../HistoryRollback/index.scss | 5 +- .../ListeningToQuery/ListeningToQuery.js | 3 + .../ListeningToQuery/index.js | 3 + .../ListeningToQuery/index.scss | 5 +- .../NewConfig/NewConfig.js | 3 + .../NewConfig/index.js | 3 + .../NewConfig/index.scss | 5 +- .../console-fe/src/pages/Login/Login.jsx | 16 + .../console-fe/src/pages/Login/index.jsx | 3 + .../console-fe/src/pages/Login/index.scss | 16 + .../src/pages/NameSpace/NameSpace.js | 3 + .../console-fe/src/pages/NameSpace/index.js | 3 + .../console-fe/src/pages/NameSpace/index.scss | 5 +- .../ServiceDetail/EditClusterDialog.js | 3 + .../ServiceDetail/EditInstanceDialog.js | 3 + .../ServiceDetail/EditServiceDialog.js | 3 + .../ServiceDetail/InstanceTable.js | 3 + .../ServiceDetail/ServiceDetail.js | 3 + .../ServiceDetail/ServiceDetail.scss | 7 +- .../ServiceDetail/constant.js | 3 + .../ServiceManagement/ServiceDetail/index.js | 3 + .../ServiceList/ServiceList.js | 3 + .../ServiceList/ServiceList.scss | 5 +- .../ServiceManagement/ServiceList/index.js | 3 + .../SubscriberList/SubscriberList.js | 3 + .../SubscriberList/SubscriberList.scss | 16 + .../ServiceManagement/SubscriberList/index.js | 3 + .../console-fe/src/pages/Welcome/Welcome.js | 3 + .../console-fe/src/pages/Welcome/index.js | 3 + .../console-fe/src/reducers/authority.js | 3 + .../static/console-fe/src/reducers/base.js | 3 + .../console-fe/src/reducers/configuration.js | 3 + .../static/console-fe/src/reducers/index.js | 3 + .../static/console-fe/src/reducers/locale.js | 3 + .../console-fe/src/reducers/namespace.js | 3 + .../console-fe/src/reducers/subscribers.js | 3 + .../static/console-fe/src/utils/nacosutil.js | 3 + .../static/console-fe/src/utils/request.js | 16 + .../console-fe/src/utils/validateContent.js | 16 + .../sample/configurationManagement.spec.js | 652 +++++++++--------- console/src/main/resources/static/index.html | 6 +- console/src/main/resources/static/login.html | 16 + 174 files changed, 1062 insertions(+), 398 deletions(-) diff --git a/console/pom.xml b/console/pom.xml index 8ee3fabb476..ff047553815 100644 --- a/console/pom.xml +++ b/console/pom.xml @@ -1,16 +1,19 @@ + ~ Copyright 1999-2018 Alibaba Group Holding Ltd. + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> diff --git a/console/src/main/resources/META-INF/nacos-default.properties b/console/src/main/resources/META-INF/nacos-default.properties index b86d3b48bcb..40c32c761d6 100644 --- a/console/src/main/resources/META-INF/nacos-default.properties +++ b/console/src/main/resources/META-INF/nacos-default.properties @@ -1,3 +1,19 @@ +# +# Copyright 1999-2018 Alibaba Group Holding Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + # Console Default Properties spring.mvc.view.prefix=/jsp/ diff --git a/console/src/main/resources/META-INF/schema.sql b/console/src/main/resources/META-INF/schema.sql index f0a19931bcb..fd6bf470830 100644 --- a/console/src/main/resources/META-INF/schema.sql +++ b/console/src/main/resources/META-INF/schema.sql @@ -1,3 +1,19 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + CREATE SCHEMA nacos AUTHORIZATION nacos; CREATE TABLE config_info ( diff --git a/console/src/main/resources/application.properties b/console/src/main/resources/application.properties index f2ba17b2867..96e24c9047b 100644 --- a/console/src/main/resources/application.properties +++ b/console/src/main/resources/application.properties @@ -1,3 +1,19 @@ +# +# Copyright 1999-2018 Alibaba Group Holding Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + #*************** Spring Boot Related Configurations ***************# ### Default web context path: server.servlet.contextPath=/nacos diff --git a/console/src/main/resources/static/console-fe/build/copy-dist.js b/console/src/main/resources/static/console-fe/build/copy-dist.js index daf957ef0b1..8cd01b8b3b3 100644 --- a/console/src/main/resources/static/console-fe/build/copy-dist.js +++ b/console/src/main/resources/static/console-fe/build/copy-dist.js @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/console/src/main/resources/static/console-fe/build/copyFile.js b/console/src/main/resources/static/console-fe/build/copyFile.js index e078ed5aa6b..fd056cc110f 100644 --- a/console/src/main/resources/static/console-fe/build/copyFile.js +++ b/console/src/main/resources/static/console-fe/build/copyFile.js @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -39,4 +42,4 @@ copyList.forEach(_fileName => { const readStream = fs.createReadStream(srcFileName); const writeStream = fs.createWriteStream(destFileName); readStream.pipe(writeStream); -}); \ No newline at end of file +}); diff --git a/console/src/main/resources/static/console-fe/build/webpack.base.conf.js b/console/src/main/resources/static/console-fe/build/webpack.base.conf.js index d4da1ef0e19..ad1f7f95900 100644 --- a/console/src/main/resources/static/console-fe/build/webpack.base.conf.js +++ b/console/src/main/resources/static/console-fe/build/webpack.base.conf.js @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/console/src/main/resources/static/console-fe/build/webpack.dev.conf.js b/console/src/main/resources/static/console-fe/build/webpack.dev.conf.js index 210e08ab3d2..5f5ebf85d44 100644 --- a/console/src/main/resources/static/console-fe/build/webpack.dev.conf.js +++ b/console/src/main/resources/static/console-fe/build/webpack.dev.conf.js @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/console/src/main/resources/static/console-fe/build/webpack.prod.conf.js b/console/src/main/resources/static/console-fe/build/webpack.prod.conf.js index 0d67a70f7ae..86f68e51c29 100644 --- a/console/src/main/resources/static/console-fe/build/webpack.prod.conf.js +++ b/console/src/main/resources/static/console-fe/build/webpack.prod.conf.js @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/console/src/main/resources/static/console-fe/public/css/bootstrap.css b/console/src/main/resources/static/console-fe/public/css/bootstrap.css index ce3ef2335f3..72451b285e1 100644 --- a/console/src/main/resources/static/console-fe/public/css/bootstrap.css +++ b/console/src/main/resources/static/console-fe/public/css/bootstrap.css @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/console/src/main/resources/static/console-fe/public/css/codemirror.css b/console/src/main/resources/static/console-fe/public/css/codemirror.css index b54156be5c6..3ebb1bf1ab3 100644 --- a/console/src/main/resources/static/console-fe/public/css/codemirror.css +++ b/console/src/main/resources/static/console-fe/public/css/codemirror.css @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/console/src/main/resources/static/console-fe/public/css/font-awesome.css b/console/src/main/resources/static/console-fe/public/css/font-awesome.css index 09f8a2e5851..4fec53fb9c4 100644 --- a/console/src/main/resources/static/console-fe/public/css/font-awesome.css +++ b/console/src/main/resources/static/console-fe/public/css/font-awesome.css @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/console/src/main/resources/static/console-fe/public/css/icon.css b/console/src/main/resources/static/console-fe/public/css/icon.css index 60cc705cea3..4577a45dce1 100644 --- a/console/src/main/resources/static/console-fe/public/css/icon.css +++ b/console/src/main/resources/static/console-fe/public/css/icon.css @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -259,4 +262,4 @@ .icon-alitomcat:before { content: "\e607" !important; -} \ No newline at end of file +} diff --git a/console/src/main/resources/static/console-fe/public/css/merge.css b/console/src/main/resources/static/console-fe/public/css/merge.css index 1305bd07a97..0038672eabb 100644 --- a/console/src/main/resources/static/console-fe/public/css/merge.css +++ b/console/src/main/resources/static/console-fe/public/css/merge.css @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/console/src/main/resources/static/console-fe/public/index.html b/console/src/main/resources/static/console-fe/public/index.html index cde191100a2..f076d8a626a 100644 --- a/console/src/main/resources/static/console-fe/public/index.html +++ b/console/src/main/resources/static/console-fe/public/index.html @@ -1,10 +1,12 @@ - + + diff --git a/console/src/main/resources/static/console-fe/public/js/codemirror.addone.fullscreen.js b/console/src/main/resources/static/console-fe/public/js/codemirror.addone.fullscreen.js index 32afce68cd5..e38b6a0ed22 100644 --- a/console/src/main/resources/static/console-fe/public/js/codemirror.addone.fullscreen.js +++ b/console/src/main/resources/static/console-fe/public/js/codemirror.addone.fullscreen.js @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -48,4 +51,4 @@ window.scrollTo(info.scrollLeft, info.scrollTop); cm.refresh(); } -}); \ No newline at end of file +}); diff --git a/console/src/main/resources/static/console-fe/public/js/codemirror.addone.json-lint.js b/console/src/main/resources/static/console-fe/public/js/codemirror.addone.json-lint.js index 684673ef68d..5628e0da62b 100644 --- a/console/src/main/resources/static/console-fe/public/js/codemirror.addone.json-lint.js +++ b/console/src/main/resources/static/console-fe/public/js/codemirror.addone.json-lint.js @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -38,4 +41,4 @@ CodeMirror.registerHelper("lint", "json", function(text) { return found; }); -}); \ No newline at end of file +}); diff --git a/console/src/main/resources/static/console-fe/public/js/codemirror.addone.lint.js b/console/src/main/resources/static/console-fe/public/js/codemirror.addone.lint.js index d7363bfe468..927caf8f28f 100644 --- a/console/src/main/resources/static/console-fe/public/js/codemirror.addone.lint.js +++ b/console/src/main/resources/static/console-fe/public/js/codemirror.addone.lint.js @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -245,4 +248,4 @@ CodeMirror.defineExtension("performLint", function() { if (this.state.lint) startLinting(this); }); -}); \ No newline at end of file +}); diff --git a/console/src/main/resources/static/console-fe/public/js/codemirror.js b/console/src/main/resources/static/console-fe/public/js/codemirror.js index 00872a7c26c..559b4ea1ad5 100644 --- a/console/src/main/resources/static/console-fe/public/js/codemirror.js +++ b/console/src/main/resources/static/console-fe/public/js/codemirror.js @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/console/src/main/resources/static/console-fe/public/js/codemirror.lib.clike-lint.js b/console/src/main/resources/static/console-fe/public/js/codemirror.lib.clike-lint.js index 8c6e921bbf7..5ea9b185121 100644 --- a/console/src/main/resources/static/console-fe/public/js/codemirror.lib.clike-lint.js +++ b/console/src/main/resources/static/console-fe/public/js/codemirror.lib.clike-lint.js @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -796,4 +799,4 @@ CodeMirror.defineMode("clike", function(config, parserConfig) { } }); -}); \ No newline at end of file +}); diff --git a/console/src/main/resources/static/console-fe/public/js/codemirror.lib.json-lint.js b/console/src/main/resources/static/console-fe/public/js/codemirror.lib.json-lint.js index d395030b1de..92989879dd2 100644 --- a/console/src/main/resources/static/console-fe/public/js/codemirror.lib.json-lint.js +++ b/console/src/main/resources/static/console-fe/public/js/codemirror.lib.json-lint.js @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -30,7 +33,7 @@ case 1: // replace escaped characters with actual character .replace(/\\v/g,'\v') .replace(/\\f/g,'\f') .replace(/\\b/g,'\b'); - + break; case 2:this.$ = Number(yytext); break; @@ -354,7 +357,7 @@ next:function () { if (this._input === "") { return this.EOF; } else { - this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(), + this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(), {text: "", token: null, line: this.yylineno}); } }, @@ -442,4 +445,4 @@ exports.main = function commonjsMain(args) { if (typeof module !== 'undefined' && require.main === module) { exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require("system").args); } -} \ No newline at end of file +} diff --git a/console/src/main/resources/static/console-fe/public/js/javascript.js b/console/src/main/resources/static/console-fe/public/js/javascript.js index 388753ce4fc..b6b45102c20 100644 --- a/console/src/main/resources/static/console-fe/public/js/javascript.js +++ b/console/src/main/resources/static/console-fe/public/js/javascript.js @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/console/src/main/resources/static/console-fe/public/js/merge.js b/console/src/main/resources/static/console-fe/public/js/merge.js index 66c6ba32240..ad59c3c0e9f 100644 --- a/console/src/main/resources/static/console-fe/public/js/merge.js +++ b/console/src/main/resources/static/console-fe/public/js/merge.js @@ -1,9 +1,12 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at + * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/console/src/main/resources/static/console-fe/public/js/vs/editor/editor.main.js b/console/src/main/resources/static/console-fe/public/js/vs/editor/editor.main.js index df6110da5f1..ced7382a44b 100644 --- a/console/src/main/resources/static/console-fe/public/js/vs/editor/editor.main.js +++ b/console/src/main/resources/static/console-fe/public/js/vs/editor/editor.main.js @@ -66,4 +66,4 @@ define("vs/language/typescript/src/monaco.contribution",["require","exports","vs *-----------------------------------------------------------------------------*/ define("vs/basic-languages/src/monaco.contribution",["require","exports","vs/editor/edcore.main"],function(s,e){"use strict";function i(e){var i=o[e].module;return new l.Promise(function(a,t,o){s([i],function(s){l.languages.setMonarchTokensProvider(e,s.language),l.languages.setLanguageConfiguration(e,s.conf),a(void 0)},t)})}function a(s){return n[s]||(n[s]=i(s)),n[s]}function t(s){var e=s.id;o[e]=s,l.languages.register(s),l.languages.onLanguage(e,function(){a(e)})}Object.defineProperty(e,"__esModule",{value:!0});var l="undefined"==typeof monaco?self.monaco:monaco,o={},n={};e.loadLanguage=a,t({id:"bat",extensions:[".bat",".cmd"],aliases:["Batch","bat"],module:"./bat"}),t({id:"coffeescript",extensions:[".coffee"],aliases:["CoffeeScript","coffeescript","coffee"],mimetypes:["text/x-coffeescript","text/coffeescript"],module:"./coffee"}),t({id:"c",extensions:[".c",".h"],aliases:["C","c"],module:"./cpp"}),t({id:"cpp",extensions:[".cpp",".cc",".cxx",".hpp",".hh",".hxx"],aliases:["C++","Cpp","cpp"],module:"./cpp"}),t({id:"csharp",extensions:[".cs",".csx"],aliases:["C#","csharp"],module:"./csharp"}),t({id:"dockerfile",extensions:[".dockerfile"],filenames:["Dockerfile"],aliases:["Dockerfile"],module:"./dockerfile"}),t({id:"fsharp",extensions:[".fs",".fsi",".ml",".mli",".fsx",".fsscript"],aliases:["F#","FSharp","fsharp"],module:"./fsharp"}),t({id:"go",extensions:[".go"],aliases:["Go"],module:"./go"}),t({id:"handlebars",extensions:[".handlebars",".hbs"],aliases:["Handlebars","handlebars"],mimetypes:["text/x-handlebars-template"],module:"./handlebars"}),t({id:"html",extensions:[".html",".htm",".shtml",".xhtml",".mdoc",".jsp",".asp",".aspx",".jshtm"],aliases:["HTML","htm","html","xhtml"],mimetypes:["text/html","text/x-jshtm","text/template","text/ng-template"],module:"./html"}),t({id:"ini",extensions:[".ini",".properties",".gitconfig"],filenames:["config",".gitattributes",".gitconfig",".editorconfig"],aliases:["Ini","ini"],module:"./ini"}),t({id:"pug",extensions:[".jade",".pug"],aliases:["Pug","Jade","jade"],module:"./pug"}),t({id:"java",extensions:[".java",".jav"],aliases:["Java","java"],mimetypes:["text/x-java-source","text/x-java"],module:"./java"}),t({id:"lua",extensions:[".lua"],aliases:["Lua","lua"],module:"./lua"}),t({id:"markdown",extensions:[".md",".markdown",".mdown",".mkdn",".mkd",".mdwn",".mdtxt",".mdtext"],aliases:["Markdown","markdown"],module:"./markdown"}),t({id:"msdax",extensions:[".dax",".msdax"],aliases:["DAX","MSDAX"],module:"./msdax"}),t({id:"objective-c",extensions:[".m"],aliases:["Objective-C"],module:"./objective-c"}),t({id:"postiats",extensions:[".dats",".sats",".hats"],aliases:["ATS","ATS/Postiats"],module:"./postiats"}),t({id:"php",extensions:[".php",".php4",".php5",".phtml",".ctp"],aliases:["PHP","php"],mimetypes:["application/x-php"],module:"./php"}),t({id:"powershell",extensions:[".ps1",".psm1",".psd1"],aliases:["PowerShell","powershell","ps","ps1"],module:"./powershell"}),t({id:"python",extensions:[".py",".rpy",".pyw",".cpy",".gyp",".gypi"],aliases:["Python","py"],firstLine:"^#!/.*\\bpython[0-9.-]*\\b",module:"./python"}),t({id:"r",extensions:[".r",".rhistory",".rprofile",".rt"],aliases:["R","r"],module:"./r"}),t({id:"razor",extensions:[".cshtml"],aliases:["Razor","razor"],mimetypes:["text/x-cshtml"],module:"./razor"}),t({id:"ruby",extensions:[".rb",".rbx",".rjs",".gemspec",".pp"],filenames:["rakefile"],aliases:["Ruby","rb"],module:"./ruby"}),t({id:"swift",aliases:["Swift","swift"],extensions:[".swift"],mimetypes:["text/swift"],module:"./swift"}),t({id:"sql",extensions:[".sql"],aliases:["SQL"],module:"./sql"}),t({id:"vb",extensions:[".vb"],aliases:["Visual Basic","vb"],module:"./vb"}),t({id:"xml",extensions:[".xml",".dtd",".ascx",".csproj",".config",".wxi",".wxl",".wxs",".xaml",".svg",".svgz"],firstLine:"(\\<\\?xml.*)|(\\