From 1f8a64b11dac741aabb78a3f170d8897dac46588 Mon Sep 17 00:00:00 2001 From: horizonzy <1060026287@qq.com> Date: Mon, 7 Sep 2020 15:15:47 +0800 Subject: [PATCH 01/21] replace the deprecated api com.fasterxml.jackson.databind.node.ObjectNode#put(java.lang.String, com.fasterxml.jackson.databind.JsonNode) --- .../alibaba/nacos/naming/controllers/ApiController.java | 6 +++--- .../nacos/naming/controllers/InstanceController.java | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ApiController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ApiController.java index bc0e915af..dfce95127 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/ApiController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/ApiController.java @@ -88,8 +88,8 @@ public class ApiController extends InstanceController { } if (CollectionUtils.isEmpty(domSet)) { - result.put("doms", JacksonUtils.transferToJsonNode(new HashSet<>())); result.put("count", 0); + result.set("doms", JacksonUtils.transferToJsonNode(new HashSet<>())); return result; } @@ -99,8 +99,8 @@ public class ApiController extends InstanceController { } } - result.put("doms", JacksonUtils.transferToJsonNode(doms)); result.put("count", doms.size()); + result.set("doms", JacksonUtils.transferToJsonNode(doms)); return result; } @@ -116,8 +116,8 @@ public class ApiController extends InstanceController { count += doms.get(namespaceId).size(); } - result.put("doms", JacksonUtils.transferToJsonNode(doms)); result.put("count", count); + result.set("doms", JacksonUtils.transferToJsonNode(doms)); return result; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java index 287cb13b2..02f0b4a31 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java @@ -301,8 +301,8 @@ public class InstanceController { result.put("clusterName", cluster); result.put("weight", instance.getWeight()); result.put("healthy", instance.isHealthy()); - result.put("metadata", JacksonUtils.transferToJsonNode(instance.getMetadata())); result.put("instanceId", instance.getInstanceId()); + result.set("metadata", JacksonUtils.transferToJsonNode(instance.getMetadata())); return result; } } @@ -568,7 +568,6 @@ public class InstanceController { result.put("dom", NamingUtils.getServiceName(serviceName)); } - result.put("hosts", JacksonUtils.createEmptyArrayNode()); result.put("name", serviceName); result.put("cacheMillis", cacheMillis); result.put("lastRefTime", System.currentTimeMillis()); @@ -576,7 +575,8 @@ public class InstanceController { result.put("useSpecifiedURL", false); result.put("clusters", clusters); result.put("env", env); - result.put("metadata", JacksonUtils.transferToJsonNode(service.getMetadata())); + result.set("hosts", JacksonUtils.createEmptyArrayNode()); + result.set("metadata", JacksonUtils.transferToJsonNode(service.getMetadata())); return result; } @@ -637,7 +637,7 @@ public class InstanceController { ipObj.put("healthy", entry.getKey()); ipObj.put("marked", instance.isMarked()); ipObj.put("instanceId", instance.getInstanceId()); - ipObj.put("metadata", JacksonUtils.transferToJsonNode(instance.getMetadata())); + ipObj.set("metadata", JacksonUtils.transferToJsonNode(instance.getMetadata())); ipObj.put("enabled", instance.isEnabled()); ipObj.put("weight", instance.getWeight()); ipObj.put("clusterName", instance.getClusterName()); From f423d39d1020e6fe7934e4759455463838ea73bb Mon Sep 17 00:00:00 2001 From: yanlinly Date: Tue, 8 Sep 2020 10:00:46 +0800 Subject: [PATCH 02/21] Revert "[#3368]Cancel empty Long polling thread to improve performance. (#ISSUE3432)" (#3778) This reverts commit 95c8bf242b1f8895b77c519fb7dfdbc144e986c0. --- .../client/config/impl/ClientWorker.java | 47 +------ .../listener/impl/ClientWorkerTest.java | 132 ------------------ 2 files changed, 2 insertions(+), 177 deletions(-) delete mode 100644 client/src/test/java/com/alibaba/nacos/client/config/listener/impl/ClientWorkerTest.java 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 fab00f032..93578dda9 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 @@ -32,7 +32,6 @@ import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.TenantUtil; import com.alibaba.nacos.common.http.HttpRestResult; import com.alibaba.nacos.common.lifecycle.Closeable; -import com.alibaba.nacos.common.utils.ConcurrentHashSet; import com.alibaba.nacos.common.utils.ConvertUtils; import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.common.utils.StringUtils; @@ -166,7 +165,6 @@ public class ClientWorker implements Closeable { copy.remove(groupKey); cacheMap.set(copy); } - reMakeCacheDataTaskId(); LOGGER.info("[{}] [unsubscribe] {}", this.agent.getName(), groupKey); MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size()); @@ -179,38 +177,11 @@ public class ClientWorker implements Closeable { copy.remove(groupKey); cacheMap.set(copy); } - reMakeCacheDataTaskId(); LOGGER.info("[{}] [unsubscribe] {}", agent.getName(), groupKey); MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size()); } - /** - * Remake cacheData taskId. - */ - private void reMakeCacheDataTaskId() { - int listenerSize = cacheMap.get().size(); - int remakeTaskId = (int) Math.ceil(listenerSize / ParamUtil.getPerTaskConfigSize()); - if (remakeTaskId < (int) currentLongingTaskCount) { - for (int i = 0; i < remakeTaskId; i++) { - int count = 0; - for (String key : cacheMap.get().keySet()) { - if (count == ParamUtil.getPerTaskConfigSize()) { - break; - } - CacheData cacheData = cacheMap.get().get(key); - cacheData.setTaskId(i); - synchronized (cacheMap) { - Map copy = new HashMap(this.cacheMap.get()); - copy.put(key, cacheData); - cacheMap.set(copy); - } - count++; - } - } - } - } - /** * Add cache data if absent. * @@ -277,8 +248,6 @@ public class ClientWorker implements Closeable { cache.setInitializing(true); } else { cache = new CacheData(configFilterChainManager, agent.getName(), dataId, group, tenant); - int taskId = cacheMap.get().size() / (int) ParamUtil.getPerTaskConfigSize(); - cache.setTaskId(taskId); // fix issue # 1317 if (enableRemoteSyncConfig) { String[] ct = getServerConfig(dataId, group, tenant, 3000L); @@ -425,16 +394,11 @@ public class ClientWorker implements Closeable { int longingTaskCount = (int) Math.ceil(listenerSize / ParamUtil.getPerTaskConfigSize()); if (longingTaskCount > currentLongingTaskCount) { for (int i = (int) currentLongingTaskCount; i < longingTaskCount; i++) { - taskIdSet.add(i); // The task list is no order.So it maybe has issues when changing. executorService.execute(new LongPollingRunnable(i)); } - } else if (longingTaskCount < currentLongingTaskCount) { - for (int i = longingTaskCount; i < (int) currentLongingTaskCount; i++) { - taskIdSet.remove(i); - } + currentLongingTaskCount = longingTaskCount; } - currentLongingTaskCount = longingTaskCount; } /** @@ -692,9 +656,7 @@ public class ClientWorker implements Closeable { } inInitializingCacheList.clear(); - if (taskIdSet.contains(taskId)) { - executorService.execute(this); - } + executorService.execute(this); } catch (Throwable e) { @@ -723,11 +685,6 @@ public class ClientWorker implements Closeable { private final AtomicReference> cacheMap = new AtomicReference>( new HashMap()); - /** - * Store the running taskId. - */ - private final ConcurrentHashSet taskIdSet = new ConcurrentHashSet(); - private final HttpAgent agent; private final ConfigFilterChainManager configFilterChainManager; diff --git a/client/src/test/java/com/alibaba/nacos/client/config/listener/impl/ClientWorkerTest.java b/client/src/test/java/com/alibaba/nacos/client/config/listener/impl/ClientWorkerTest.java deleted file mode 100644 index 792212418..000000000 --- a/client/src/test/java/com/alibaba/nacos/client/config/listener/impl/ClientWorkerTest.java +++ /dev/null @@ -1,132 +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.listener.impl; - -import com.alibaba.nacos.api.config.listener.Listener; -import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager; -import com.alibaba.nacos.client.config.http.MetricsHttpAgent; -import com.alibaba.nacos.client.config.impl.ClientWorker; -import com.alibaba.nacos.client.utils.ParamUtil; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.Arrays; -import java.util.List; -import java.util.Properties; -import java.util.concurrent.Executor; -import java.util.concurrent.ScheduledExecutorService; - -import static org.mockito.Mockito.mock; - -public class ClientWorkerTest { - - @Mock - ScheduledExecutorService scheduledExecutorService; - - private ClientWorker clientWorker; - - private List listeners; - - private final String dataId = "data"; - - private final String group = "group"; - - private final String currentLongingTaskCount = "currentLongingTaskCount"; - - @Before - public void init() { - MockitoAnnotations.initMocks(this); - clientWorker = new ClientWorker(mock(MetricsHttpAgent.class), mock(ConfigFilterChainManager.class), - mock(Properties.class)); - try { - Field executorServiceField = clientWorker.getClass().getDeclaredField("executorService"); - executorServiceField.setAccessible(true); - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.setInt(executorServiceField, executorServiceField.getModifiers() & ~Modifier.FINAL); - executorServiceField.set(clientWorker, scheduledExecutorService); - Listener listener = new Listener() { - @Override - public Executor getExecutor() { - return null; - } - - @Override - public void receiveConfigInfo(String configInfo) { - - } - }; - listeners = Arrays.asList(listener); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - - @Test - public void testAddLongPollNumberThreads() { - try { - for (int i = 0; i < ParamUtil.getPerTaskConfigSize(); i++) { - clientWorker.addTenantListeners(dataId + i, group, listeners); - } - Field currentLongingTaskCountField = clientWorker.getClass().getDeclaredField(currentLongingTaskCount); - currentLongingTaskCountField.setAccessible(true); - Assert.assertEquals(currentLongingTaskCount, (int) currentLongingTaskCountField.getDouble(clientWorker), 1); - for (int i = (int) ParamUtil.getPerTaskConfigSize(); i < ParamUtil.getPerTaskConfigSize() * 2; i++) { - clientWorker.addTenantListeners(dataId + i, group, listeners); - } - Assert.assertEquals(currentLongingTaskCount, (int) currentLongingTaskCountField.getDouble(clientWorker), 2); - } catch (NacosException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } - } - - @Test - public void testReduceLongPollNumberThreads() { - try { - for (int i = 0; i < ParamUtil.getPerTaskConfigSize() * 3; i++) { - clientWorker.addTenantListeners(dataId + i, group, listeners); - } - Field currentLongingTaskCountField = clientWorker.getClass().getDeclaredField(currentLongingTaskCount); - currentLongingTaskCountField.setAccessible(true); - Assert.assertEquals(currentLongingTaskCount, (int) currentLongingTaskCountField.getDouble(clientWorker), 3); - - for (int i = (int) ParamUtil.getPerTaskConfigSize(); i < ParamUtil.getPerTaskConfigSize() * 2; i++) { - clientWorker.removeTenantListener(dataId + i, group, listeners.get(0)); - } - Assert.assertEquals(currentLongingTaskCount, (int) currentLongingTaskCountField.getDouble(clientWorker), 2); - } catch (NacosException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } - } - -} From f42cb4716d1c7a8cb112c3558705609c7709904e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= <263976490@qq.com> Date: Tue, 8 Sep 2020 11:30:19 +0800 Subject: [PATCH 03/21] [ISSUE #3658] Some enhance refactor for naming distro (#3765) * Some enhance refactor for naming distro * Remove null code --- .../distro/DistroConsistencyServiceImpl.java | 21 ----- .../ephemeral/distro/DistroHttpData.java | 4 +- .../ephemeral/distro/DistroHttpRegistry.java | 77 +++++++++++++++++++ .../naming/controllers/DistroController.java | 5 +- .../DistroConsistencyServiceImplTest.java | 11 --- 5 files changed, 81 insertions(+), 37 deletions(-) create mode 100644 naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroHttpRegistry.java diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java index 7f679fec2..26cfac16d 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImpl.java @@ -19,7 +19,6 @@ package com.alibaba.nacos.naming.consistency.ephemeral.distro; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.common.utils.Objects; -import com.alibaba.nacos.core.utils.ApplicationUtils; import com.alibaba.nacos.naming.cluster.ServerStatus; import com.alibaba.nacos.naming.cluster.transport.Serializer; import com.alibaba.nacos.consistency.DataOperation; @@ -28,16 +27,10 @@ import com.alibaba.nacos.naming.consistency.KeyBuilder; import com.alibaba.nacos.naming.consistency.RecordListener; import com.alibaba.nacos.naming.consistency.ephemeral.EphemeralConsistencyService; import com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKey; -import com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKeyTaskFailedHandler; -import com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpDelayTaskProcessor; -import com.alibaba.nacos.naming.consistency.ephemeral.distro.component.DistroDataStorageImpl; -import com.alibaba.nacos.naming.consistency.ephemeral.distro.component.DistroHttpAgent; import com.alibaba.nacos.core.distributed.distro.DistroProtocol; -import com.alibaba.nacos.core.distributed.distro.component.DistroComponentHolder; import com.alibaba.nacos.core.distributed.distro.component.DistroDataProcessor; import com.alibaba.nacos.core.distributed.distro.entity.DistroData; import com.alibaba.nacos.core.distributed.distro.entity.DistroKey; -import com.alibaba.nacos.core.distributed.distro.task.DistroTaskEngineHolder; import com.alibaba.nacos.naming.core.DistroMapper; import com.alibaba.nacos.naming.core.Instances; import com.alibaba.nacos.naming.core.Service; @@ -102,20 +95,6 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService this.switchDomain = switchDomain; this.globalConfig = globalConfig; this.distroProtocol = distroProtocol; - registerDistroComponent(); - } - - private void registerDistroComponent() { - DistroComponentHolder componentHolder = ApplicationUtils.getBean(DistroComponentHolder.class); - DistroTaskEngineHolder taskEngineHolder = ApplicationUtils.getBean(DistroTaskEngineHolder.class); - componentHolder.registerDataStorage(KeyBuilder.INSTANCE_LIST_KEY_PREFIX, - new DistroDataStorageImpl(dataStore, distroMapper)); - componentHolder.registerTransportAgent(KeyBuilder.INSTANCE_LIST_KEY_PREFIX, new DistroHttpAgent()); - componentHolder.registerFailedTaskHandler(KeyBuilder.INSTANCE_LIST_KEY_PREFIX, - new DistroHttpCombinedKeyTaskFailedHandler(globalConfig, taskEngineHolder)); - taskEngineHolder.registerNacosTaskProcessor(KeyBuilder.INSTANCE_LIST_KEY_PREFIX, - new DistroHttpDelayTaskProcessor(globalConfig, taskEngineHolder)); - componentHolder.registerDataProcessor(this); } @PostConstruct diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroHttpData.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroHttpData.java index 7f22ed4d3..314770313 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroHttpData.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroHttpData.java @@ -33,8 +33,8 @@ public class DistroHttpData extends DistroData { private Object deserializedContent; - public DistroHttpData(DistroKey distroKey, byte[] content, Object deserializedContent) { - super(distroKey, content); + public DistroHttpData(DistroKey distroKey, Object deserializedContent) { + setDistroKey(distroKey); this.deserializedContent = deserializedContent; } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroHttpRegistry.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroHttpRegistry.java new file mode 100644 index 000000000..16c81fdab --- /dev/null +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroHttpRegistry.java @@ -0,0 +1,77 @@ +/* + * 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.naming.consistency.ephemeral.distro; + +import com.alibaba.nacos.core.distributed.distro.component.DistroComponentHolder; +import com.alibaba.nacos.core.distributed.distro.task.DistroTaskEngineHolder; +import com.alibaba.nacos.naming.consistency.KeyBuilder; +import com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKeyTaskFailedHandler; +import com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpDelayTaskProcessor; +import com.alibaba.nacos.naming.consistency.ephemeral.distro.component.DistroDataStorageImpl; +import com.alibaba.nacos.naming.consistency.ephemeral.distro.component.DistroHttpAgent; +import com.alibaba.nacos.naming.core.DistroMapper; +import com.alibaba.nacos.naming.misc.GlobalConfig; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +/** + * Distro http registry. + * + * @author xiweng.yy + */ +@Component +public class DistroHttpRegistry { + + private final DistroComponentHolder componentHolder; + + private final DistroTaskEngineHolder taskEngineHolder; + + private final DataStore dataStore; + + private final DistroMapper distroMapper; + + private final GlobalConfig globalConfig; + + private final DistroConsistencyServiceImpl consistencyService; + + public DistroHttpRegistry(DistroComponentHolder componentHolder, DistroTaskEngineHolder taskEngineHolder, + DataStore dataStore, DistroMapper distroMapper, GlobalConfig globalConfig, + DistroConsistencyServiceImpl consistencyService) { + this.componentHolder = componentHolder; + this.taskEngineHolder = taskEngineHolder; + this.dataStore = dataStore; + this.distroMapper = distroMapper; + this.globalConfig = globalConfig; + this.consistencyService = consistencyService; + } + + /** + * Register necessary component to distro protocol for HTTP implement. + */ + @PostConstruct + public void doRegister() { + componentHolder.registerDataStorage(KeyBuilder.INSTANCE_LIST_KEY_PREFIX, + new DistroDataStorageImpl(dataStore, distroMapper)); + componentHolder.registerTransportAgent(KeyBuilder.INSTANCE_LIST_KEY_PREFIX, new DistroHttpAgent()); + componentHolder.registerFailedTaskHandler(KeyBuilder.INSTANCE_LIST_KEY_PREFIX, + new DistroHttpCombinedKeyTaskFailedHandler(globalConfig, taskEngineHolder)); + taskEngineHolder.registerNacosTaskProcessor(KeyBuilder.INSTANCE_LIST_KEY_PREFIX, + new DistroHttpDelayTaskProcessor(globalConfig, taskEngineHolder)); + componentHolder.registerDataProcessor(consistencyService); + } +} 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 b1e89d92b..7052b71a1 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 @@ -84,8 +84,7 @@ public class DistroController { .isDefaultInstanceEphemeral()) { serviceManager.createEmptyService(namespaceId, serviceName, true); } - DistroHttpData distroHttpData = new DistroHttpData(createDistroKey(entry.getKey()), null, - entry.getValue()); + DistroHttpData distroHttpData = new DistroHttpData(createDistroKey(entry.getKey()), entry.getValue()); distroProtocol.onReceive(distroHttpData); } } @@ -101,7 +100,7 @@ public class DistroController { */ @PutMapping("/checksum") public ResponseEntity syncChecksum(@RequestParam String source, @RequestBody Map dataMap) { - DistroHttpData distroHttpData = new DistroHttpData(createDistroKey(source), null, dataMap); + DistroHttpData distroHttpData = new DistroHttpData(createDistroKey(source), dataMap); distroProtocol.onVerify(distroHttpData); return ResponseEntity.ok("ok"); } diff --git a/naming/src/test/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImplTest.java b/naming/src/test/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImplTest.java index ffaa82a05..4a76dc1b4 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImplTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DistroConsistencyServiceImplTest.java @@ -24,9 +24,7 @@ import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.consistency.KeyBuilder; import com.alibaba.nacos.naming.consistency.RecordListener; import com.alibaba.nacos.core.distributed.distro.DistroProtocol; -import com.alibaba.nacos.core.distributed.distro.component.DistroComponentHolder; import com.alibaba.nacos.core.distributed.distro.entity.DistroKey; -import com.alibaba.nacos.core.distributed.distro.task.DistroTaskEngineHolder; import com.alibaba.nacos.naming.core.Instances; import com.alibaba.nacos.naming.misc.GlobalConfig; import org.junit.After; @@ -41,7 +39,6 @@ import java.util.concurrent.ConcurrentLinkedQueue; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -68,20 +65,12 @@ public class DistroConsistencyServiceImplTest extends BaseTest { @Mock private RecordListener recordListener; - @Mock - private DistroComponentHolder distroComponentHolder; - - @Mock - private DistroTaskEngineHolder distroTaskEngineHolder; - private Map> listeners; private Instances instances; @Before public void setUp() throws Exception { - doReturn(distroComponentHolder).when(context).getBean(DistroComponentHolder.class); - doReturn(distroTaskEngineHolder).when(context).getBean(DistroTaskEngineHolder.class); when(globalConfig.getTaskDispatchPeriod()).thenReturn(2000); distroConsistencyService = new DistroConsistencyServiceImpl(distroMapper, dataStore, serializer, switchDomain, globalConfig, distroProtocol); From 3dc0f243f88ddde9a06189d068af20999b473b63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BB=B6?= <1060026287@qq.com> Date: Wed, 9 Sep 2020 16:40:07 +0800 Subject: [PATCH 04/21] [ISSUE #3687] check serviceName's format(groupName@@serviceName) in server and client (#3767) * 1.in server, check serviceName's format 'groupName@@serviceName', groupName and serviceName can't be blank 2.in client, check 'groupName@@serviceName', groupName and serviceName can't be blank * ignore the check to groupName * check split's length instead of exception to check argument * 1.add some notes 2.remove unnecessary code * modify the notes --- .../nacos/api/naming/utils/NamingUtils.java | 3 ++ .../controllers/InstanceController.java | 40 ++++++++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java b/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java index 911219ae3..9faa8b40b 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/utils/NamingUtils.java @@ -28,6 +28,9 @@ import com.alibaba.nacos.api.utils.StringUtils; public class NamingUtils { public static String getGroupedName(final String serviceName, final String groupName) { + if (StringUtils.isBlank(serviceName)) { + throw new IllegalArgumentException("Param 'serviceName' is illegal, serviceName is blank"); + } final String resultGroupedName = groupName + Constants.SERVICE_INFO_SPLITER + serviceName; return resultGroupedName.intern(); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java index 02f0b4a31..5f1ae15a3 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java @@ -38,6 +38,7 @@ import com.alibaba.nacos.naming.push.ClientInfo; import com.alibaba.nacos.naming.push.DataSource; import com.alibaba.nacos.naming.push.PushService; import com.alibaba.nacos.naming.web.CanDistro; +import com.alibaba.nacos.naming.web.DistroFilter; import com.alibaba.nacos.naming.web.NamingResourceParser; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -115,9 +116,10 @@ public class InstanceController { @Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE) public String register(HttpServletRequest request) throws Exception { - final String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); final String namespaceId = WebUtils .optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); + final String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); + checkServiceNameFormat(serviceName); final Instance instance = parseInstance(request); @@ -139,6 +141,7 @@ public class InstanceController { Instance instance = getIpAddress(request); String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); + checkServiceNameFormat(serviceName); Service service = serviceManager.getService(namespaceId, serviceName); if (service == null) { @@ -161,9 +164,10 @@ public class InstanceController { @PutMapping @Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE) public String update(HttpServletRequest request) throws Exception { - final String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); final String namespaceId = WebUtils .optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); + final String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); + checkServiceNameFormat(serviceName); final Instance instance = parseInstance(request); String agent = WebUtils.getUserAgent(request); @@ -190,8 +194,9 @@ public class InstanceController { @PatchMapping @Secured(parser = NamingResourceParser.class, action = ActionTypes.WRITE) public String patch(HttpServletRequest request) throws Exception { - String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); + String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); + checkServiceNameFormat(serviceName); String ip = WebUtils.required(request, "ip"); String port = WebUtils.required(request, "port"); String cluster = WebUtils.optional(request, CommonParams.CLUSTER_NAME, StringUtils.EMPTY); @@ -242,8 +247,9 @@ public class InstanceController { public ObjectNode list(HttpServletRequest request) throws Exception { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); - String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); + checkServiceNameFormat(serviceName); + String agent = WebUtils.getUserAgent(request); String clusters = WebUtils.optional(request, "clusters", StringUtils.EMPTY); String clientIP = WebUtils.optional(request, "clientIP", StringUtils.EMPTY); @@ -274,6 +280,7 @@ public class InstanceController { String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); + checkServiceNameFormat(serviceName); String cluster = WebUtils.optional(request, CommonParams.CLUSTER_NAME, UtilsAndCommons.DEFAULT_CLUSTER_NAME); String ip = WebUtils.required(request, "ip"); int port = Integer.parseInt(WebUtils.required(request, "port")); @@ -344,8 +351,9 @@ public class InstanceController { ip = clientBeat.getIp(); port = clientBeat.getPort(); } - String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID); + String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); + checkServiceNameFormat(serviceName); Loggers.SRV_LOG.debug("[CLIENT-BEAT] full arguments: beat: {}, serviceName: {}", clientBeat, serviceName); Instance instance = serviceManager.getInstance(namespaceId, serviceName, clusterName, ip, port); @@ -413,7 +421,7 @@ public class InstanceController { namespaceId = Constants.DEFAULT_NAMESPACE_ID; serviceName = key; } - + checkServiceNameFormat(serviceName); Service service = serviceManager.getService(namespaceId, serviceName); if (service == null) { @@ -433,6 +441,26 @@ public class InstanceController { return result; } + /** + * check combineServiceName format. the serviceName can't be blank. some relational logic in {@link + * DistroFilter#doFilter}, it will handle combineServiceName in some case, you should know it. + *
+     * serviceName = "@@"; the length = 0; illegal
+     * serviceName = "group@@"; the length = 1; illegal
+     * serviceName = "@@serviceName"; the length = 2; legal
+     * serviceName = "group@@serviceName"; the length = 2; legal
+     * 
+ * + * @param combineServiceName such as: groupName@@serviceName + */ + private void checkServiceNameFormat(String combineServiceName) { + String[] split = combineServiceName.split(Constants.SERVICE_INFO_SPLITER); + if (split.length <= 1) { + throw new IllegalArgumentException( + "Param 'serviceName' is illegal, it should be format as 'groupName@@serviceName"); + } + } + private Instance parseInstance(HttpServletRequest request) throws Exception { String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME); From baa699a3f147488a6e26955a8761fbd23e03d8de Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Wed, 9 Sep 2020 17:01:18 +0800 Subject: [PATCH 05/21] [ISSUE#3790] Supplement http response Content-Encoding processing. (#3791) * bug: fix issue #3790, Supplement http response Content-Encoding processing * bug: fix issue #3790, Supplement http response Content-Encoding processing. * bug: fix issue #3790, Supplement http response Content-Encoding processing. --- .../client/response/JdkHttpClientResponse.java | 14 ++++++++++++++ .../alibaba/nacos/common/http/param/Header.java | 1 - .../com/alibaba/nacos/common/utils/IoUtils.java | 6 +++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/response/JdkHttpClientResponse.java b/common/src/main/java/com/alibaba/nacos/common/http/client/response/JdkHttpClientResponse.java index f4ad0794d..90fef3445 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/response/JdkHttpClientResponse.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/response/JdkHttpClientResponse.java @@ -16,9 +16,11 @@ package com.alibaba.nacos.common.http.client.response; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.utils.IoUtils; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; @@ -36,6 +38,8 @@ public class JdkHttpClientResponse implements HttpClientResponse { private Header responseHeader; + private static final String CONTENT_ENCODING = "gzip"; + public JdkHttpClientResponse(HttpURLConnection conn) { this.conn = conn; } @@ -51,8 +55,18 @@ public class JdkHttpClientResponse implements HttpClientResponse { @Override public InputStream getBody() throws IOException { + Header headers = getHeaders(); InputStream errorStream = this.conn.getErrorStream(); this.responseStream = (errorStream != null ? errorStream : this.conn.getInputStream()); + String contentEncoding = headers.getValue(HttpHeaderConsts.CONTENT_ENCODING); + // Used to process http content_encoding, when content_encoding is GZIP, use GZIPInputStream + if (CONTENT_ENCODING.equals(contentEncoding)) { + byte[] bytes = IoUtils.tryDecompress(this.responseStream); + if (bytes == null) { + throw new IOException("decompress http response error"); + } + return new ByteArrayInputStream(bytes); + } return this.responseStream; } 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 bebba34ea..c581d7b26 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 @@ -45,7 +45,6 @@ public class Header { addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_JSON); addParam(HttpHeaderConsts.ACCEPT_CHARSET, "UTF-8"); addParam(HttpHeaderConsts.ACCEPT_ENCODING, "gzip"); - addParam(HttpHeaderConsts.CONTENT_ENCODING, "gzip"); } public static Header newInstance() { 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 0f949ba42..c38de6dad 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 @@ -50,9 +50,9 @@ public class IoUtils { * * @param raw compress stream * @return byte array after decompress - * @throws Exception exception + * @throws IOException exception */ - public static byte[] tryDecompress(InputStream raw) throws Exception { + public static byte[] tryDecompress(InputStream raw) throws IOException { GZIPInputStream gis = null; ByteArrayOutputStream out = null; try { @@ -60,7 +60,7 @@ public class IoUtils { out = new ByteArrayOutputStream(); copy(gis, out); return out.toByteArray(); - } catch (Exception e) { + } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { From a41d209d570ccf5deed6c6c608861b1638ded2f5 Mon Sep 17 00:00:00 2001 From: S2W Date: Wed, 9 Sep 2020 17:03:25 +0800 Subject: [PATCH 06/21] Update ConvertUtils.java (#3789) --- .../main/java/com/alibaba/nacos/common/utils/ConvertUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/ConvertUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/ConvertUtils.java index 14432eced..628d79bc3 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/ConvertUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/ConvertUtils.java @@ -126,7 +126,7 @@ public final class ConvertUtils { * @return the boolean value of the string, {@code false} if no match or the String is null */ public static boolean toBoolean(final String str) { - return toBooleanObject(str) == Boolean.TRUE; + return Boolean.TRUE.equals(toBooleanObject(str)); } /** From af2151e49c359b3fc7c9dd27ac9adbfb71d1ccec Mon Sep 17 00:00:00 2001 From: Marcus Date: Mon, 14 Sep 2020 11:55:08 +0800 Subject: [PATCH 07/21] fix ConvertUtils can not handle FormatException. --- .../nacos/common/utils/ConvertUtils.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/ConvertUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/ConvertUtils.java index 628d79bc3..50670e411 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/ConvertUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/ConvertUtils.java @@ -51,7 +51,11 @@ public final class ConvertUtils { if (StringUtils.isBlank(val)) { return defaultValue; } - return Integer.parseInt(val); + try { + return Integer.parseInt(val); + } catch (NumberFormatException exception) { + return defaultValue; + } } /** @@ -77,7 +81,11 @@ public final class ConvertUtils { if (StringUtils.isBlank(val)) { return defaultValue; } - return Long.parseLong(val); + try { + return Long.parseLong(val); + } catch (NumberFormatException exception) { + return defaultValue; + } } /** @@ -92,7 +100,11 @@ public final class ConvertUtils { if (StringUtils.isBlank(val)) { return defaultValue; } - return Boolean.parseBoolean(val); + try { + return Boolean.parseBoolean(val); + } catch (NumberFormatException exception) { + return defaultValue; + } } // The following utility functions are extracted from org.apache.commons.lang3 From 5e4429f0e4af12c9da141280155395f63cbb3e37 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Tue, 15 Sep 2020 09:18:20 +0800 Subject: [PATCH 08/21] [ISSUE#3192] naming module replace http client (#3763) * naming module replace http client * refactor: naming module replace http client. * refactor: naming module replace http client. * refactor: Add apache http client Factory. * refactor: naming module replace http client. * fix code style * refactor: Add http client config * refactor: naming module HttpClientManager change * refactor: naming module HttpClientManager change * refactor: naming module replace http client. * fix code style * refactor: fix JDK http client Use error problem. * refactor: Query And Header entity init Add non-empty judgment * Enhance the asynchronous http delete request method to support body passing parameters. * refactor: apache http client set MaxConnTotal and maxConnPerRoute. --- .../http/AbstractApacheHttpClientFactory.java | 10 +- .../http/AbstractHttpClientFactory.java | 9 +- .../nacos/common/http/BaseHttpClient.java | 3 +- .../nacos/common/http/BaseHttpMethod.java | 36 ++ .../nacos/common/http/HttpClientConfig.java | 115 +++- .../alibaba/nacos/common/http/HttpUtils.java | 17 +- .../http/client/NacosAsyncRestTemplate.java | 21 + .../common/http/client/NacosRestTemplate.java | 20 + .../request/DefaultHttpClientRequest.java | 2 +- .../client/request/JdkHttpClientRequest.java | 2 +- .../nacos/common/http/param/Header.java | 15 +- .../nacos/common/http/param/Query.java | 8 +- .../nacos/common/model/RequestHttpEntity.java | 3 +- .../nacos/common/utils/HttpMethod.java | 12 +- .../distro/task/load/DistroLoadDataTask.java | 2 +- .../consistency/persistent/raft/RaftCore.java | 147 +++-- .../persistent/raft/RaftPeerSet.java | 31 +- .../persistent/raft/RaftProxy.java | 20 +- .../nacos/naming/core/SubscribeManager.java | 8 +- .../healthcheck/ClientBeatCheckTask.java | 26 +- .../naming/healthcheck/HealthCheckCommon.java | 6 +- .../alibaba/nacos/naming/misc/HttpClient.java | 598 ++++-------------- .../nacos/naming/misc/HttpClientManager.java | 149 +++++ .../nacos/naming/misc/NamingProxy.java | 98 +-- .../naming/misc/ServerStatusSynchronizer.java | 23 +- .../misc/ServiceStatusSynchronizer.java | 25 +- .../nacos/naming/web/DistroFilter.java | 6 +- 27 files changed, 764 insertions(+), 648 deletions(-) create mode 100644 naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClientManager.java diff --git a/common/src/main/java/com/alibaba/nacos/common/http/AbstractApacheHttpClientFactory.java b/common/src/main/java/com/alibaba/nacos/common/http/AbstractApacheHttpClientFactory.java index 1d3c32e39..5d70ef14c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/AbstractApacheHttpClientFactory.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/AbstractApacheHttpClientFactory.java @@ -30,9 +30,15 @@ public abstract class AbstractApacheHttpClientFactory extends AbstractHttpClient @Override public final NacosRestTemplate createNacosRestTemplate() { + final HttpClientConfig originalRequestConfig = buildHttpClientConfig(); final RequestConfig requestConfig = getRequestConfig(); - return new NacosRestTemplate(assignLogger(), - new DefaultHttpClientRequest(HttpClients.custom().setDefaultRequestConfig(requestConfig).build())); + return new NacosRestTemplate(assignLogger(), new DefaultHttpClientRequest( + HttpClients.custom().setDefaultRequestConfig(requestConfig) + .setUserAgent(originalRequestConfig.getUserAgent()) + .setMaxConnTotal(originalRequestConfig.getMaxConnTotal()) + .setMaxConnPerRoute(originalRequestConfig.getMaxConnPerRoute()) + .setConnectionTimeToLive(originalRequestConfig.getConnTimeToLive(), + originalRequestConfig.getConnTimeToLiveTimeUnit()).build())); } } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/AbstractHttpClientFactory.java b/common/src/main/java/com/alibaba/nacos/common/http/AbstractHttpClientFactory.java index dd85b8744..54b0fd715 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/AbstractHttpClientFactory.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/AbstractHttpClientFactory.java @@ -67,15 +67,20 @@ public abstract class AbstractHttpClientFactory implements HttpClientFactory { @Override public NacosAsyncRestTemplate createNacosAsyncRestTemplate() { - RequestConfig requestConfig = getRequestConfig(); + final HttpClientConfig originalRequestConfig = buildHttpClientConfig(); + final RequestConfig requestConfig = getRequestConfig(); return new NacosAsyncRestTemplate(assignLogger(), new DefaultAsyncHttpClientRequest( - HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig).build())); + HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig) + .setMaxConnTotal(originalRequestConfig.getMaxConnTotal()) + .setMaxConnPerRoute(originalRequestConfig.getMaxConnPerRoute()) + .setUserAgent(originalRequestConfig.getUserAgent()).build())); } protected RequestConfig getRequestConfig() { HttpClientConfig httpClientConfig = buildHttpClientConfig(); return RequestConfig.custom().setConnectTimeout(httpClientConfig.getConTimeOutMillis()) .setSocketTimeout(httpClientConfig.getReadTimeOutMillis()) + .setConnectionRequestTimeout(httpClientConfig.getConnectionRequestTimeout()) .setMaxRedirects(httpClientConfig.getMaxRedirects()).build(); } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpClient.java b/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpClient.java index 7881af077..18bd1782d 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpClient.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/BaseHttpClient.java @@ -16,7 +16,6 @@ package com.alibaba.nacos.common.http; -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.Query; @@ -103,7 +102,7 @@ public abstract class BaseHttpClient { final BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method); final HttpRequestBase httpRequestBase = httpMethod.init(url); HttpUtils.initRequestHeader(httpRequestBase, header); - HttpUtils.initRequestEntity(httpRequestBase, body, header.getValue(HttpHeaderConsts.CONTENT_TYPE)); + HttpUtils.initRequestEntity(httpRequestBase, body, header); return httpRequestBase; } 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 4ccb2407b..b53cfa36d 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 @@ -84,6 +84,16 @@ public enum BaseHttpMethod { } }, + /** + * delete Large request. + */ + DELETE_LARGE(HttpMethod.DELETE_LARGE) { + @Override + protected HttpRequestBase createRequest(String url) { + return new HttpDeleteWithEntity(url); + } + }, + /** * head request. */ @@ -155,6 +165,10 @@ public enum BaseHttpMethod { /** * get Large implemented. + *

+ * Mainly used for GET request parameters are relatively large, can not be placed on the URL, so it needs to be + * placed in the body. + *

*/ public static class HttpGetWithEntity extends HttpEntityEnclosingRequestBase { @@ -171,4 +185,26 @@ public enum BaseHttpMethod { } } + /** + * delete Large implemented. + *

+ * Mainly used for DELETE request parameters are relatively large, can not be placed on the URL, so it needs to be + * placed in the body. + *

+ */ + public static class HttpDeleteWithEntity extends HttpEntityEnclosingRequestBase { + + public static final String METHOD_NAME = "DELETE"; + + public HttpDeleteWithEntity(String url) { + super(); + setURI(URI.create(url)); + } + + @Override + public String getMethod() { + return METHOD_NAME; + } + } + } diff --git a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientConfig.java b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientConfig.java index cc5b9993b..ae50ab485 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/HttpClientConfig.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/HttpClientConfig.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.common.http; +import java.util.concurrent.TimeUnit; + /** * http client config build. * @@ -23,16 +25,62 @@ package com.alibaba.nacos.common.http; */ public class HttpClientConfig { + /** + * connect time out. + */ private final int conTimeOutMillis; + /** + * read time out. + */ private final int readTimeOutMillis; + /** + * connTimeToLive. + */ + private final long connTimeToLive; + + /** + * connTimeToLiveTimeUnit. + */ + private final TimeUnit connTimeToLiveTimeUnit; + + /** + * connectionRequestTimeout. + */ + private final int connectionRequestTimeout; + + /** + * max redirect. + */ private final int maxRedirects; - public HttpClientConfig(int conTimeOutMillis, int readTimeOutMillis, int maxRedirects) { + /** + * max connect total. + */ + private final int maxConnTotal; + + /** + * Assigns maximum connection per route value. + */ + private final int maxConnPerRoute; + + /** + * user agent. + */ + private final String userAgent; + + public HttpClientConfig(int conTimeOutMillis, int readTimeOutMillis, long connTimeToLive, TimeUnit timeUnit, + int connectionRequestTimeout, int maxRedirects, int maxConnTotal, int maxConnPerRoute, String userAgent) { this.conTimeOutMillis = conTimeOutMillis; this.readTimeOutMillis = readTimeOutMillis; + this.connTimeToLive = connTimeToLive; + this.connTimeToLiveTimeUnit = timeUnit; + this.connectionRequestTimeout = connectionRequestTimeout; this.maxRedirects = maxRedirects; + this.maxConnTotal = maxConnTotal; + this.maxConnPerRoute = maxConnPerRoute; + this.userAgent = userAgent; } public int getConTimeOutMillis() { @@ -43,10 +91,34 @@ public class HttpClientConfig { return readTimeOutMillis; } + public long getConnTimeToLive() { + return connTimeToLive; + } + + public TimeUnit getConnTimeToLiveTimeUnit() { + return connTimeToLiveTimeUnit; + } + + public int getConnectionRequestTimeout() { + return connectionRequestTimeout; + } + public int getMaxRedirects() { return maxRedirects; } + public int getMaxConnTotal() { + return maxConnTotal; + } + + public int getMaxConnPerRoute() { + return maxConnPerRoute; + } + + public String getUserAgent() { + return userAgent; + } + public static HttpClientConfigBuilder builder() { return new HttpClientConfigBuilder(); } @@ -57,8 +129,20 @@ public class HttpClientConfig { private int readTimeOutMillis = -1; + private long connTimeToLive = -1; + + private TimeUnit connTimeToLiveTimeUnit = TimeUnit.MILLISECONDS; + + private int connectionRequestTimeout = -1; + private int maxRedirects = 50; + private int maxConnTotal = 0; + + private int maxConnPerRoute = 0; + + private String userAgent; + public HttpClientConfigBuilder setConTimeOutMillis(int conTimeOutMillis) { this.conTimeOutMillis = conTimeOutMillis; return this; @@ -69,13 +153,40 @@ public class HttpClientConfig { return this; } + public HttpClientConfigBuilder setConnectionTimeToLive(long connTimeToLive, TimeUnit connTimeToLiveTimeUnit) { + this.connTimeToLive = connTimeToLive; + this.connTimeToLiveTimeUnit = connTimeToLiveTimeUnit; + return this; + } + + public HttpClientConfigBuilder setConnectionRequestTimeout(int connectionRequestTimeout) { + this.connectionRequestTimeout = connectionRequestTimeout; + return this; + } + public HttpClientConfigBuilder setMaxRedirects(int maxRedirects) { this.maxRedirects = maxRedirects; return this; } + public HttpClientConfigBuilder setMaxConnTotal(int maxConnTotal) { + this.maxConnTotal = maxConnTotal; + return this; + } + + public HttpClientConfigBuilder setMaxConnPerRoute(int maxConnPerRoute) { + this.maxConnPerRoute = maxConnPerRoute; + return this; + } + + public HttpClientConfigBuilder setUserAgent(String userAgent) { + this.userAgent = userAgent; + return this; + } + public HttpClientConfig build() { - return new HttpClientConfig(conTimeOutMillis, readTimeOutMillis, maxRedirects); + return new HttpClientConfig(conTimeOutMillis, readTimeOutMillis, connTimeToLive, connTimeToLiveTimeUnit, + connectionRequestTimeout, maxRedirects, maxConnTotal, maxConnPerRoute, userAgent); } } } 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 97f6f5360..7f5238a6a 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,15 +16,17 @@ package com.alibaba.nacos.common.http; -import com.alibaba.nacos.common.http.handler.RequestHandler; +import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.http.param.Header; import com.alibaba.nacos.common.http.param.Query; +import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpRequestBase; +import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicNameValuePair; @@ -70,17 +72,22 @@ public final class HttpUtils { * * @param requestBase requestBase {@link HttpRequestBase} * @param body body - * @param mediaType mediaType {@link ContentType} + * @param header request header * @throws Exception exception */ - public static void initRequestEntity(HttpRequestBase requestBase, Object body, String mediaType) throws Exception { + public static void initRequestEntity(HttpRequestBase requestBase, Object body, Header header) throws Exception { if (body == null) { return; } if (requestBase instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestBase; - ContentType contentType = ContentType.create(mediaType); - StringEntity entity = new StringEntity(RequestHandler.parse(body), contentType); + ContentType contentType = ContentType.create(header.getValue(HttpHeaderConsts.CONTENT_TYPE), header.getCharset()); + HttpEntity entity; + if (body instanceof byte[]) { + entity = new ByteArrayEntity((byte[]) body, contentType); + } else { + entity = new StringEntity(body instanceof String ? (String) body : JacksonUtils.toJson(body), contentType); + } request.setEntity(entity); } } 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 082400113..ad9ff06cb 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 @@ -105,6 +105,27 @@ public class NacosAsyncRestTemplate extends AbstractNacosRestTemplate { execute(url, HttpMethod.DELETE, new RequestHttpEntity(header, query), responseType, callback); } + /** + * async http delete large request, when the parameter exceeds the URL limit, you can use this method to put the + * parameter into the body pass. + * + *

{@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 body body + * @param responseType return type + * @param callback callback {@link Callback#onReceive(com.alibaba.nacos.common.model.RestResult)} + */ + public void delete(String url, Header header, String body, Type responseType, Callback callback) { + execute(url, HttpMethod.DELETE_LARGE, + new RequestHttpEntity(header.setContentType(MediaType.APPLICATION_JSON), Query.EMPTY, body), + responseType, callback); + } + /** * async http put Create a new resource by PUTting the given body to http request. * 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 f7b3693a5..1d60c2e4c 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 @@ -427,6 +427,26 @@ public class NacosRestTemplate extends AbstractNacosRestTemplate { return execute(url, httpMethod, requestHttpEntity, responseType); } + /** + * Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns + * the response as {@link HttpRestResult}. + * + * @param url url + * @param config HttpClientConfig + * @param header http header param + * @param query http query param + * @param body http body param + * @param httpMethod http method + * @param responseType return type + * @return {@link HttpRestResult} + * @throws Exception ex + */ + public HttpRestResult exchange(String url, HttpClientConfig config, Header header, Query query, + Object body, String httpMethod, Type responseType) throws Exception { + RequestHttpEntity requestHttpEntity = new RequestHttpEntity(config, header, query, body); + return execute(url, httpMethod, requestHttpEntity, responseType); + } + /** * Set the request interceptors that this accessor should use. * diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/request/DefaultHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/request/DefaultHttpClientRequest.java index 2a84c390b..d59010532 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/request/DefaultHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/request/DefaultHttpClientRequest.java @@ -65,7 +65,7 @@ public class DefaultHttpClientRequest implements HttpClientRequest { && requestHttpEntity.getBody() instanceof Map) { HttpUtils.initRequestFromEntity(httpRequestBase, (Map) requestHttpEntity.getBody(), headers.getCharset()); } else { - HttpUtils.initRequestEntity(httpRequestBase, requestHttpEntity.getBody(), headers.getValue(HttpHeaderConsts.CONTENT_TYPE)); + HttpUtils.initRequestEntity(httpRequestBase, requestHttpEntity.getBody(), headers); } replaceDefaultConfig(httpRequestBase, requestHttpEntity.getHttpClientConfig()); return httpRequestBase; diff --git a/common/src/main/java/com/alibaba/nacos/common/http/client/request/JdkHttpClientRequest.java b/common/src/main/java/com/alibaba/nacos/common/http/client/request/JdkHttpClientRequest.java index 7649b4eb5..fd921158c 100644 --- a/common/src/main/java/com/alibaba/nacos/common/http/client/request/JdkHttpClientRequest.java +++ b/common/src/main/java/com/alibaba/nacos/common/http/client/request/JdkHttpClientRequest.java @@ -90,7 +90,7 @@ public class JdkHttpClientRequest implements HttpClientRequest { conn.setConnectTimeout(this.httpClientConfig.getConTimeOutMillis()); conn.setReadTimeout(this.httpClientConfig.getReadTimeOutMillis()); conn.setRequestMethod(httpMethod); - if (body != null) { + if (body != null && !"".equals(body)) { String contentType = headers.getValue(HttpHeaderConsts.CONTENT_TYPE); String bodyStr = JacksonUtils.toJson(body); if (MediaType.APPLICATION_FORM_URLENCODED.equals(contentType)) { 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 c581d7b26..fcaf31c72 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 @@ -18,6 +18,7 @@ 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.MapUtils; import com.alibaba.nacos.common.utils.StringUtils; import java.util.ArrayList; @@ -129,8 +130,10 @@ public class Header { * @param params parameters */ public void addAll(Map params) { - for (Map.Entry entry : params.entrySet()) { - addParam(entry.getKey(), entry.getValue()); + if (MapUtils.isNotEmpty(params)) { + for (Map.Entry entry : params.entrySet()) { + addParam(entry.getKey(), entry.getValue()); + } } } @@ -142,9 +145,11 @@ public class Header { * @param headers original response header */ public void setOriginalResponseHeader(Map> headers) { - this.originalResponseHeader.putAll(headers); - for (Map.Entry> entry : this.originalResponseHeader.entrySet()) { - addParam(entry.getKey(), entry.getValue().get(0)); + if (MapUtils.isNotEmpty(headers)) { + this.originalResponseHeader.putAll(headers); + for (Map.Entry> entry : this.originalResponseHeader.entrySet()) { + addParam(entry.getKey(), entry.getValue().get(0)); + } } } 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 1b2413343..4c4029e42 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 @@ -16,6 +16,8 @@ package com.alibaba.nacos.common.http.param; +import com.alibaba.nacos.common.utils.MapUtils; + import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.LinkedHashMap; @@ -68,8 +70,10 @@ public class Query { * @return this query */ public Query initParams(Map params) { - for (Map.Entry entry : params.entrySet()) { - addParam(entry.getKey(), entry.getValue()); + if (MapUtils.isNotEmpty(params)) { + for (Map.Entry entry : params.entrySet()) { + addParam(entry.getKey(), entry.getValue()); + } } return this; } 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 b2c03253d..2240002e6 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 @@ -26,7 +26,6 @@ import java.util.Map; * Represents an HTTP request , consisting of headers and body. * * @author mai.jh - * @date 2020/5/23 */ public class RequestHttpEntity { @@ -36,7 +35,7 @@ public class RequestHttpEntity { private final Query query; - private Object body; + private final Object body; public RequestHttpEntity(Header header, Query query) { this(null, header, query); diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/HttpMethod.java b/common/src/main/java/com/alibaba/nacos/common/utils/HttpMethod.java index 688433fac..c605bd9b9 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/HttpMethod.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/HttpMethod.java @@ -26,8 +26,10 @@ public class HttpMethod { public static final String GET = "GET"; - // this is only use in nacos, Custom request type, essentially a get request - + /** + * this is only use in nacos, Custom request type, essentially a GET request, Mainly used for GET request parameters + * are relatively large,can not be placed on the URL, so it needs to be placed in the body. + */ public static final String GET_LARGE = "GET-LARGE"; public static final String HEAD = "HEAD"; @@ -40,6 +42,12 @@ public class HttpMethod { public static final String DELETE = "DELETE"; + /** + * this is only use in nacos, Custom request type, essentially a DELETE request, Mainly used for DELETE request + * parameters are relatively large, can not be placed on the URL, so it needs to be placed in the body. + */ + public static final String DELETE_LARGE = "DELETE_LARGE"; + public static final String OPTIONS = "OPTIONS"; public static final String TRACE = "TRACE"; diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/distro/task/load/DistroLoadDataTask.java b/core/src/main/java/com/alibaba/nacos/core/distributed/distro/task/load/DistroLoadDataTask.java index b919b6d70..35f0ec7ae 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/distro/task/load/DistroLoadDataTask.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/distro/task/load/DistroLoadDataTask.java @@ -109,7 +109,7 @@ public class DistroLoadDataTask implements Runnable { return true; } } catch (Exception e) { - Loggers.DISTRO.error("[DISTRO-INIT] load snapshot {} from {} failed.", resourceType, each.getAddress()); + Loggers.DISTRO.error("[DISTRO-INIT] load snapshot {} from {} failed.", resourceType, each.getAddress(), e); } } return false; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java index e9d409591..96e12900a 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java @@ -18,11 +18,13 @@ package com.alibaba.nacos.naming.consistency.persistent.raft; import com.alibaba.nacos.common.executor.ExecutorFactory; import com.alibaba.nacos.common.executor.NameThreadFactory; +import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.JacksonUtils; +import com.alibaba.nacos.consistency.DataOperation; import com.alibaba.nacos.core.utils.ApplicationUtils; import com.alibaba.nacos.core.utils.ClassUtils; import com.alibaba.nacos.naming.NamingApp; -import com.alibaba.nacos.consistency.DataOperation; import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.consistency.KeyBuilder; import com.alibaba.nacos.naming.consistency.RecordListener; @@ -41,8 +43,6 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; -import com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.Response; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.javatuples.Pair; @@ -55,7 +55,6 @@ import javax.annotation.PostConstruct; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -223,25 +222,28 @@ public class RaftCore { continue; } final String url = buildUrl(server, API_ON_PUB); - HttpClient.asyncHttpPostLarge(url, Arrays.asList("key=" + key), content, - new AsyncCompletionHandler() { - @Override - public Integer onCompleted(Response response) throws Exception { - if (response.getStatusCode() != HttpURLConnection.HTTP_OK) { - Loggers.RAFT - .warn("[RAFT] failed to publish data to peer, datumId={}, peer={}, http code={}", - datum.key, server, response.getStatusCode()); - return 1; - } - latch.countDown(); - return 0; - } - - @Override - public STATE onContentWriteCompleted() { - return STATE.CONTINUE; - } - }); + HttpClient.asyncHttpPostLarge(url, Arrays.asList("key", key), content, new Callback() { + @Override + public void onReceive(RestResult result) { + if (!result.ok()) { + Loggers.RAFT + .warn("[RAFT] failed to publish data to peer, datumId={}, peer={}, http code={}", + datum.key, server, result.getCode()); + return; + } + latch.countDown(); + } + + @Override + public void onError(Throwable throwable) { + Loggers.RAFT.error("[RAFT] failed to publish data to peer", throwable); + } + + @Override + public void onCancel() { + + } + }); } @@ -287,21 +289,29 @@ public class RaftCore { for (final String server : peers.allServersWithoutMySelf()) { String url = buildUrl(server, API_ON_DEL); - HttpClient.asyncHttpDeleteLarge(url, null, json.toString(), new AsyncCompletionHandler() { + HttpClient.asyncHttpDeleteLarge(url, null, json.toString(), new Callback() { @Override - public Integer onCompleted(Response response) throws Exception { - if (response.getStatusCode() != HttpURLConnection.HTTP_OK) { + public void onReceive(RestResult result) { + if (!result.ok()) { Loggers.RAFT .warn("[RAFT] failed to delete data from peer, datumId={}, peer={}, http code={}", - key, server, response.getStatusCode()); - return 1; + key, server, result.getCode()); + return; } RaftPeer local = peers.local(); local.resetLeaderDue(); - - return 0; + } + + @Override + public void onError(Throwable throwable) { + Loggers.RAFT.error("[RAFT] failed to delete data from peer", throwable); + } + + @Override + public void onCancel() { + } }); } @@ -458,22 +468,30 @@ public class RaftCore { for (final String server : peers.allServersWithoutMySelf()) { final String url = buildUrl(server, API_VOTE); try { - HttpClient.asyncHttpPost(url, null, params, new AsyncCompletionHandler() { + HttpClient.asyncHttpPost(url, null, params, new Callback() { @Override - public Integer onCompleted(Response response) throws Exception { - if (response.getStatusCode() != HttpURLConnection.HTTP_OK) { - Loggers.RAFT - .error("NACOS-RAFT vote failed: {}, url: {}", response.getResponseBody(), url); - return 1; + public void onReceive(RestResult result) { + if (!result.ok()) { + Loggers.RAFT.error("NACOS-RAFT vote failed: {}, url: {}", result.getCode(), url); + return; } - RaftPeer peer = JacksonUtils.toObj(response.getResponseBody(), RaftPeer.class); + RaftPeer peer = JacksonUtils.toObj(result.getData(), RaftPeer.class); Loggers.RAFT.info("received approve from peer: {}", JacksonUtils.toJson(peer)); peers.decideLeader(peer); - return 0; + } + + @Override + public void onError(Throwable throwable) { + Loggers.RAFT.error("error while sending vote to server: {}", server, throwable); + } + + @Override + public void onCancel() { + } }); } catch (Exception e) { @@ -605,28 +623,32 @@ public class RaftCore { if (Loggers.RAFT.isDebugEnabled()) { Loggers.RAFT.debug("send beat to server " + server); } - HttpClient.asyncHttpPostLarge(url, null, compressedBytes, new AsyncCompletionHandler() { + HttpClient.asyncHttpPostLarge(url, null, compressedBytes, new Callback() { @Override - public Integer onCompleted(Response response) throws Exception { - if (response.getStatusCode() != HttpURLConnection.HTTP_OK) { - Loggers.RAFT.error("NACOS-RAFT beat failed: {}, peer: {}", response.getResponseBody(), - server); + public void onReceive(RestResult result) { + if (!result.ok()) { + Loggers.RAFT.error("NACOS-RAFT beat failed: {}, peer: {}", result.getCode(), server); MetricsMonitor.getLeaderSendBeatFailedException().increment(); - return 1; + return; } - peers.update(JacksonUtils.toObj(response.getResponseBody(), RaftPeer.class)); + peers.update(JacksonUtils.toObj(result.getData(), RaftPeer.class)); if (Loggers.RAFT.isDebugEnabled()) { Loggers.RAFT.debug("receive beat response from: {}", url); } - return 0; } @Override - public void onThrowable(Throwable t) { - Loggers.RAFT.error("NACOS-RAFT error while sending heart-beat to peer: {} {}", server, t); + public void onError(Throwable throwable) { + Loggers.RAFT.error("NACOS-RAFT error while sending heart-beat to peer: {} {}", server, + throwable); MetricsMonitor.getLeaderSendBeatFailedException().increment(); } + + @Override + public void onCancel() { + + } }); } catch (Exception e) { Loggers.RAFT.error("error while sending heart-beat to peer: {} {}", server, e); @@ -746,15 +768,15 @@ public class RaftCore { // update datum entry String url = buildUrl(remote.ip, API_GET) + "?keys=" + URLEncoder.encode(keys, "UTF-8"); - HttpClient.asyncHttpGet(url, null, null, new AsyncCompletionHandler() { + HttpClient.asyncHttpGet(url, null, null, new Callback() { @Override - public Integer onCompleted(Response response) throws Exception { - if (response.getStatusCode() != HttpURLConnection.HTTP_OK) { - return 1; + public void onReceive(RestResult result) { + if (!result.ok()) { + return; } List datumList = JacksonUtils - .toObj(response.getResponseBody(), new TypeReference>() { + .toObj(result.getData(), new TypeReference>() { }); for (JsonNode datumJson : datumList) { @@ -823,9 +845,24 @@ public class RaftCore { OPERATE_LOCK.unlock(); } } - TimeUnit.MILLISECONDS.sleep(200); - return 0; + try { + TimeUnit.MILLISECONDS.sleep(200); + } catch (InterruptedException e) { + Loggers.RAFT.error("[RAFT-BEAT] Interrupted error ", e); + } + return; } + + @Override + public void onError(Throwable throwable) { + Loggers.RAFT.error("[RAFT-BEAT] failed to sync datum from leader", throwable); + } + + @Override + public void onCancel() { + + } + }); batch.clear(); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftPeerSet.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftPeerSet.java index 68c4b7945..c988049a8 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftPeerSet.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftPeerSet.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.naming.consistency.persistent.raft; +import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.core.cluster.Member; @@ -26,8 +28,6 @@ import com.alibaba.nacos.core.utils.ApplicationUtils; import com.alibaba.nacos.naming.misc.HttpClient; import com.alibaba.nacos.naming.misc.Loggers; import com.alibaba.nacos.naming.misc.NetUtils; -import com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.Response; import org.apache.commons.collections.SortedBag; import org.apache.commons.collections.bag.TreeBag; import org.apache.commons.lang3.StringUtils; @@ -35,7 +35,6 @@ import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; -import java.net.HttpURLConnection; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; @@ -219,20 +218,28 @@ public class RaftPeerSet extends MemberChangeListener { if (!Objects.equals(peer, candidate) && peer.state == RaftPeer.State.LEADER) { try { String url = RaftCore.buildUrl(peer.ip, RaftCore.API_GET_PEER); - HttpClient.asyncHttpGet(url, null, params, new AsyncCompletionHandler() { + HttpClient.asyncHttpGet(url, null, params, new Callback() { @Override - public Integer onCompleted(Response response) throws Exception { - if (response.getStatusCode() != HttpURLConnection.HTTP_OK) { + public void onReceive(RestResult result) { + if (!result.ok()) { Loggers.RAFT - .error("[NACOS-RAFT] get peer failed: {}, peer: {}", response.getResponseBody(), + .error("[NACOS-RAFT] get peer failed: {}, peer: {}", result.getCode(), peer.ip); peer.state = RaftPeer.State.FOLLOWER; - return 1; + return; } - - update(JacksonUtils.toObj(response.getResponseBody(), RaftPeer.class)); - - return 0; + + update(JacksonUtils.toObj(result.getData(), RaftPeer.class)); + } + + @Override + public void onError(Throwable throwable) { + + } + + @Override + public void onCancel() { + } }); } catch (Exception e) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java index 50aac9bf2..7240014ca 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftProxy.java @@ -16,13 +16,13 @@ package com.alibaba.nacos.naming.consistency.persistent.raft; +import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.core.utils.ApplicationUtils; import com.alibaba.nacos.naming.misc.HttpClient; import com.alibaba.nacos.naming.misc.UtilsAndCommons; import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; -import java.net.HttpURLConnection; import java.util.Map; /** @@ -48,9 +48,9 @@ public class RaftProxy { } String url = "http://" + server + ApplicationUtils.getContextPath() + api; - HttpClient.HttpResult result = HttpClient.httpGet(url, null, params); - if (result.code != HttpURLConnection.HTTP_OK) { - throw new IllegalStateException("leader failed, caused by: " + result.content); + RestResult result = HttpClient.httpGet(url, null, params); + if (!result.ok()) { + throw new IllegalStateException("leader failed, caused by: " + result.getMessage()); } } @@ -69,7 +69,7 @@ public class RaftProxy { server = server + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort(); } String url = "http://" + server + ApplicationUtils.getContextPath() + api; - HttpClient.HttpResult result; + RestResult result; switch (method) { case GET: result = HttpClient.httpGet(url, null, params); @@ -84,8 +84,8 @@ public class RaftProxy { throw new RuntimeException("unsupported method:" + method); } - if (result.code != HttpURLConnection.HTTP_OK) { - throw new IllegalStateException("leader failed, caused by: " + result.content); + if (!result.ok()) { + throw new IllegalStateException("leader failed, caused by: " + result.getMessage()); } } @@ -106,9 +106,9 @@ public class RaftProxy { } String url = "http://" + server + ApplicationUtils.getContextPath() + api; - HttpClient.HttpResult result = HttpClient.httpPostLarge(url, headers, content); - if (result.code != HttpURLConnection.HTTP_OK) { - throw new IllegalStateException("leader failed, caused by: " + result.content); + RestResult result = HttpClient.httpPostLarge(url, headers, content); + if (!result.ok()) { + throw new IllegalStateException("leader failed, caused by: " + result.getMessage()); } } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java index 1cd1651c1..d0bfe745c 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/SubscribeManager.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.naming.core; import com.alibaba.nacos.api.naming.CommonParams; +import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.core.cluster.Member; import com.alibaba.nacos.core.cluster.ServerMemberManager; @@ -31,7 +32,6 @@ import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -97,13 +97,13 @@ public class SubscribeManager { continue; } - HttpClient.HttpResult result = HttpClient.httpGet( + RestResult result = HttpClient.httpGet( "http://" + server.getAddress() + ApplicationUtils.getContextPath() + UtilsAndCommons.NACOS_NAMING_CONTEXT + SUBSCRIBER_ON_SYNC_URL, new ArrayList<>(), paramValues); - if (HttpURLConnection.HTTP_OK == result.code) { - Subscribers subscribers = JacksonUtils.toObj(result.content, Subscribers.class); + if (!result.ok()) { + Subscribers subscribers = JacksonUtils.toObj(result.getData(), Subscribers.class); subscriberList.addAll(subscribers.getSubscribers()); } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java index 253e664dc..022280438 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/ClientBeatCheckTask.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.naming.healthcheck; +import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.core.utils.ApplicationUtils; import com.alibaba.nacos.naming.consistency.KeyBuilder; @@ -31,10 +33,7 @@ import com.alibaba.nacos.naming.misc.SwitchDomain; import com.alibaba.nacos.naming.misc.UtilsAndCommons; import com.alibaba.nacos.naming.push.PushService; import com.fasterxml.jackson.annotation.JsonIgnore; -import com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.Response; -import java.net.HttpURLConnection; import java.util.List; /** @@ -140,15 +139,26 @@ public class ClientBeatCheckTask implements Runnable { + UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance?" + request.toUrl(); // delete instance asynchronously: - HttpClient.asyncHttpDelete(url, null, null, new AsyncCompletionHandler() { + HttpClient.asyncHttpDelete(url, null, null, new Callback() { @Override - public Object onCompleted(Response response) throws Exception { - if (response.getStatusCode() != HttpURLConnection.HTTP_OK) { + public void onReceive(RestResult result) { + if (!result.ok()) { Loggers.SRV_LOG .error("[IP-DEAD] failed to delete ip automatically, ip: {}, caused {}, resp code: {}", - instance.toJson(), response.getResponseBody(), response.getStatusCode()); + instance.toJson(), result.getMessage(), result.getCode()); } - return null; + } + + @Override + public void onError(Throwable throwable) { + Loggers.SRV_LOG + .error("[IP-DEAD] failed to delete ip automatically, ip: {}, error: {}", instance.toJson(), + throwable); + } + + @Override + public void onCancel() { + } }); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HealthCheckCommon.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HealthCheckCommon.java index 41a345fc9..6df6b3a9d 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HealthCheckCommon.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/HealthCheckCommon.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.naming.healthcheck; +import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.core.cluster.Member; import com.alibaba.nacos.core.cluster.ServerMemberManager; @@ -34,7 +35,6 @@ import com.alibaba.nacos.naming.push.PushService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.net.HttpURLConnection; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; @@ -92,11 +92,11 @@ public class HealthCheckCommon { JacksonUtils.toJson(list)); } - HttpClient.HttpResult httpResult = HttpClient.httpPost( + RestResult httpResult = HttpClient.httpPost( "http://" + server.getAddress() + ApplicationUtils.getContextPath() + UtilsAndCommons.NACOS_NAMING_CONTEXT + "/api/healthCheckResult", null, params); - if (httpResult.code != HttpURLConnection.HTTP_OK) { + if (!httpResult.ok()) { Loggers.EVT_LOG.warn("[HEALTH-CHECK-SYNC] failed to send result to {}, result: {}", server, JacksonUtils.toJson(list)); } 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 7ce2daf3e..a93dbd909 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 @@ -17,49 +17,24 @@ package com.alibaba.nacos.naming.misc; import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.http.HttpClientConfig; +import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate; +import com.alibaba.nacos.common.http.client.NacosRestTemplate; +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.RestResult; import com.alibaba.nacos.common.utils.HttpMethod; -import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.VersionUtils; import com.alibaba.nacos.core.utils.ApplicationUtils; -import com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.AsyncHttpClient; -import com.ning.http.client.AsyncHttpClientConfig; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.http.HeaderElement; -import org.apache.http.HttpEntity; -import org.apache.http.HttpHeaders; -import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.entity.UrlEncodedFormEntity; -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; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.message.BasicNameValuePair; -import java.io.IOException; -import java.io.InputStream; -import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; -import java.net.URI; -import java.net.URL; -import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.zip.GZIPInputStream; /** * Http Client. @@ -72,34 +47,11 @@ public class HttpClient { private static final int CON_TIME_OUT_MILLIS = 5000; - private static AsyncHttpClient asyncHttpClient; + private static final NacosRestTemplate SYNC_NACOS_REST_TEMPLATE = HttpClientManager.getNacosRestTemplate(); - private static CloseableHttpClient postClient; + private static final NacosRestTemplate APACHE_SYNC_NACOS_REST_TEMPLATE = HttpClientManager.getApacheRestTemplate(); - static { - AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder(); - builder.setMaximumConnectionsTotal(-1); - builder.setMaximumConnectionsPerHost(128); - builder.setAllowPoolingConnection(true); - builder.setFollowRedirects(false); - builder.setIdleConnectionTimeoutInMs(TIME_OUT_MILLIS); - builder.setConnectionTimeoutInMs(CON_TIME_OUT_MILLIS); - builder.setCompressionEnabled(true); - builder.setIOThreadMultiplier(1); - builder.setMaxRequestRetry(0); - builder.setUserAgent(UtilsAndCommons.SERVER_VERSION); - - asyncHttpClient = new AsyncHttpClient(builder.build()); - - HttpClientBuilder builder2 = HttpClients.custom(); - builder2.setUserAgent(UtilsAndCommons.SERVER_VERSION); - builder2.setConnectionTimeToLive(CON_TIME_OUT_MILLIS, TimeUnit.MILLISECONDS); - builder2.setMaxConnPerRoute(-1); - builder2.setMaxConnTotal(-1); - builder2.disableAutomaticRetries(); - - postClient = builder2.build(); - } + private static final NacosAsyncRestTemplate ASYNC_REST_TEMPLATE = HttpClientManager.getAsyncRestTemplate(); /** * Request http delete method. @@ -107,11 +59,11 @@ public class HttpClient { * @param url url * @param headers headers * @param paramValues params - * @return {@link HttpResult} as response + * @return {@link RestResult} as response */ - public static HttpResult httpDelete(String url, List headers, Map paramValues) { + public static RestResult httpDelete(String url, List headers, Map paramValues) { return request(url, headers, paramValues, StringUtils.EMPTY, CON_TIME_OUT_MILLIS, TIME_OUT_MILLIS, "UTF-8", - "DELETE"); + HttpMethod.DELETE); } /** @@ -120,11 +72,11 @@ public class HttpClient { * @param url url * @param headers headers * @param paramValues params - * @return {@link HttpResult} as response + * @return {@link RestResult} as response */ - public static HttpResult httpGet(String url, List headers, Map paramValues) { + public static RestResult httpGet(String url, List headers, Map paramValues) { return request(url, headers, paramValues, StringUtils.EMPTY, CON_TIME_OUT_MILLIS, TIME_OUT_MILLIS, "UTF-8", - "GET"); + HttpMethod.GET); } /** @@ -138,39 +90,31 @@ public class HttpClient { * @param readTimeout timeout of request * @param encoding charset of request * @param method http method - * @return {@link HttpResult} as response + * @return {@link RestResult} as response */ - public static HttpResult request(String url, List headers, Map paramValues, String body, - int connectTimeout, int readTimeout, String encoding, String method) { - HttpURLConnection conn = null; + public static RestResult request(String url, List headers, Map paramValues, + String body, int connectTimeout, int readTimeout, String encoding, String method) { + Header header = Header.newInstance(); + if (CollectionUtils.isNotEmpty(headers)) { + header.addAll(headers); + } + header.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + header.addParam(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.version); + header.addParam(HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION); + header.addParam(HttpHeaderConsts.REQUEST_SOURCE_HEADER, ApplicationUtils.getLocalAddress()); + header.addParam(HttpHeaderConsts.ACCEPT_CHARSET, encoding); + + HttpClientConfig httpClientConfig = HttpClientConfig.builder().setConTimeOutMillis(connectTimeout) + .setReadTimeOutMillis(readTimeout).build(); + Query query = Query.newInstance().initParams(paramValues); + query.addParam("encoding", "UTF-8"); + query.addParam("nofix", "1"); try { - String encodedContent = encodingParams(paramValues, encoding); - url += StringUtils.isBlank(encodedContent) ? StringUtils.EMPTY : ("?" + encodedContent); - - conn = (HttpURLConnection) new URL(url).openConnection(); - conn.setConnectTimeout(connectTimeout); - conn.setReadTimeout(readTimeout); - conn.setRequestMethod(method); - - setHeaders(conn, headers, encoding); - - if (StringUtils.isNotBlank(body)) { - conn.setDoOutput(true); - byte[] b = body.getBytes(); - conn.setRequestProperty("Content-Length", String.valueOf(b.length)); - conn.getOutputStream().write(b, 0, b.length); - conn.getOutputStream().flush(); - conn.getOutputStream().close(); - } - - conn.connect(); - - return getResult(conn); + return SYNC_NACOS_REST_TEMPLATE + .exchange(url, httpClientConfig, header, query, body, method, String.class); } catch (Exception e) { Loggers.SRV_LOG.warn("Exception while request: {}, caused: {}", url, e); - return new HttpResult(500, e.toString(), Collections.emptyMap()); - } finally { - IoUtils.closeQuietly(conn); + return RestResult.builder().withCode(500).withMsg(e.toString()).build(); } } @@ -180,11 +124,11 @@ public class HttpClient { * @param url url * @param headers headers * @param paramValues params - * @param handler callback after request execute + * @param callback callback after request execute */ public static void asyncHttpGet(String url, List headers, Map paramValues, - AsyncCompletionHandler handler) throws Exception { - asyncHttpRequest(url, headers, paramValues, handler, HttpMethod.GET); + Callback callback) throws Exception { + asyncHttpRequest(url, headers, paramValues, callback, HttpMethod.GET); } /** @@ -193,11 +137,11 @@ public class HttpClient { * @param url url * @param headers headers * @param paramValues params - * @param handler callback after request execute + * @param callback callback after request execute */ public static void asyncHttpPost(String url, List headers, Map paramValues, - AsyncCompletionHandler handler) throws Exception { - asyncHttpRequest(url, headers, paramValues, handler, HttpMethod.POST); + Callback callback) throws Exception { + asyncHttpRequest(url, headers, paramValues, callback, HttpMethod.POST); } /** @@ -206,11 +150,11 @@ public class HttpClient { * @param url url * @param headers headers * @param paramValues params - * @param handler callback after request execute + * @param callback callback after request execute */ public static void asyncHttpDelete(String url, List headers, Map paramValues, - AsyncCompletionHandler handler) throws Exception { - asyncHttpRequest(url, headers, paramValues, handler, HttpMethod.DELETE); + Callback callback) throws Exception { + asyncHttpRequest(url, headers, paramValues, callback, HttpMethod.DELETE); } /** @@ -223,124 +167,84 @@ public class HttpClient { * @throws Exception exception when request */ public static void asyncHttpRequest(String url, List headers, Map paramValues, - AsyncCompletionHandler handler, String method) throws Exception { - if (!MapUtils.isEmpty(paramValues)) { - String encodedContent = encodingParams(paramValues, "UTF-8"); - url += (null == encodedContent) ? "" : ("?" + encodedContent); - } + Callback callback, String method) throws Exception { - AsyncHttpClient.BoundRequestBuilder builder; + Query query = Query.newInstance().initParams(paramValues); + query.addParam("encoding", "UTF-8"); + query.addParam("nofix", "1"); + + Header header = Header.newInstance(); + if (CollectionUtils.isNotEmpty(headers)) { + header.addAll(headers); + } + header.addParam(HttpHeaderConsts.ACCEPT_CHARSET, "UTF-8"); switch (method) { case HttpMethod.GET: - builder = asyncHttpClient.prepareGet(url); + ASYNC_REST_TEMPLATE.get(url, header, query, String.class, callback); break; case HttpMethod.POST: - builder = asyncHttpClient.preparePost(url); + ASYNC_REST_TEMPLATE.postForm(url, header, paramValues, String.class, callback); break; case HttpMethod.PUT: - builder = asyncHttpClient.preparePut(url); + ASYNC_REST_TEMPLATE.putForm(url, header, paramValues, String.class, callback); break; case HttpMethod.DELETE: - builder = asyncHttpClient.prepareDelete(url); + ASYNC_REST_TEMPLATE.delete(url, header, query, String.class, callback); break; default: throw new RuntimeException("not supported method:" + method); } - - if (!CollectionUtils.isEmpty(headers)) { - for (String header : headers) { - builder.setHeader(header.split("=")[0], header.split("=")[1]); - } - } - - builder.setHeader("Accept-Charset", "UTF-8"); - - if (handler != null) { - builder.execute(handler); - } else { - builder.execute(); - } } /** * Request http post method by async with large body. * - * @param url url - * @param headers headers - * @param content full request content - * @param handler callback after request execute + * @param url url + * @param headers headers + * @param content full request content + * @param callback callback after request execute */ - public static void asyncHttpPostLarge(String url, List headers, String content, - AsyncCompletionHandler handler) throws Exception { - asyncHttpPostLarge(url, headers, content.getBytes(), handler); + public static void asyncHttpPostLarge(String url, List headers, String content, Callback callback) + throws Exception { + asyncHttpPostLarge(url, headers, content.getBytes(), callback); } /** * Request http post method by async with large body. * - * @param url url - * @param headers headers - * @param content full request content - * @param handler callback after request execute + * @param url url + * @param headers headers + * @param content full request content + * @param callback callback after request execute */ - public static void asyncHttpPostLarge(String url, List headers, byte[] content, - AsyncCompletionHandler handler) throws Exception { - AsyncHttpClient.BoundRequestBuilder builder = asyncHttpClient.preparePost(url); - - if (!CollectionUtils.isEmpty(headers)) { - for (String header : headers) { - builder.setHeader(header.split("=")[0], header.split("=")[1]); - } - } - - builder.setBody(content); - - builder.setHeader("Content-Type", "application/json; charset=UTF-8"); - builder.setHeader("Accept-Charset", "UTF-8"); - builder.setHeader("Accept-Encoding", "gzip"); - builder.setHeader("Content-Encoding", "gzip"); - - if (handler != null) { - builder.execute(handler); - } else { - builder.execute(); + public static void asyncHttpPostLarge(String url, List headers, byte[] content, Callback callback) + throws Exception { + Header header = Header.newInstance(); + if (CollectionUtils.isNotEmpty(headers)) { + header.addAll(headers); } + ASYNC_REST_TEMPLATE.post(url, header, Query.EMPTY, content, String.class, callback); } /** * Request http delete method by async with large body. * - * @param url url - * @param headers headers - * @param content full request content - * @param handler callback after request execute + * @param url url + * @param headers headers + * @param content full request content + * @param callback callback after request execute */ - public static void asyncHttpDeleteLarge(String url, List headers, String content, - AsyncCompletionHandler handler) throws Exception { - AsyncHttpClient.BoundRequestBuilder builder = asyncHttpClient.prepareDelete(url); - - if (!CollectionUtils.isEmpty(headers)) { - for (String header : headers) { - builder.setHeader(header.split("=")[0], header.split("=")[1]); - } - } - - builder.setBody(content.getBytes()); - - builder.setHeader("Content-Type", "application/json; charset=UTF-8"); - builder.setHeader("Accept-Charset", "UTF-8"); - builder.setHeader("Accept-Encoding", "gzip"); - builder.setHeader("Content-Encoding", "gzip"); - - if (handler != null) { - builder.execute(handler); - } else { - builder.execute(); + public static void asyncHttpDeleteLarge(String url, List headers, String content, Callback callback) + throws Exception { + Header header = Header.newInstance(); + if (CollectionUtils.isNotEmpty(headers)) { + header.addAll(headers); } + ASYNC_REST_TEMPLATE.delete(url, header, content, String.class, callback); } - public static HttpResult httpPost(String url, List headers, Map paramValues) { + public static RestResult httpPost(String url, List headers, Map paramValues) { return httpPost(url, headers, paramValues, "UTF-8"); } @@ -351,109 +255,40 @@ public class HttpClient { * @param headers headers * @param paramValues params * @param encoding charset - * @return {@link HttpResult} as response + * @return {@link RestResult} as response */ - public static HttpResult httpPost(String url, List headers, Map paramValues, + public static RestResult httpPost(String url, List headers, Map paramValues, String encoding) { try { - - HttpPost httpost = new HttpPost(url); - - RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) - .setConnectTimeout(5000).setSocketTimeout(5000).setRedirectsEnabled(true).setMaxRedirects(5) - .build(); - httpost.setConfig(requestConfig); - - List nvps = new ArrayList(); - - for (Map.Entry entry : paramValues.entrySet()) { - nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + Header header = Header.newInstance(); + if (CollectionUtils.isNotEmpty(headers)) { + header.addAll(headers); } + header.addParam(HttpHeaderConsts.ACCEPT_CHARSET, encoding); - httpost.setEntity(new UrlEncodedFormEntity(nvps, encoding)); - HttpResponse response = postClient.execute(httpost); - HttpEntity entity = response.getEntity(); - - String charset = encoding; - if (entity.getContentType() != null) { - - HeaderElement[] headerElements = entity.getContentType().getElements(); - - if (headerElements != null && headerElements.length > 0 && headerElements[0] != null - && headerElements[0].getParameterByName("charset") != null) { - charset = headerElements[0].getParameterByName("charset").getValue(); - } - } - - return new HttpResult(response.getStatusLine().getStatusCode(), - IoUtils.toString(entity.getContent(), charset), Collections.emptyMap()); + HttpClientConfig httpClientConfig = HttpClientConfig.builder().setConTimeOutMillis(5000).setReadTimeOutMillis(5000) + .setConnectionRequestTimeout(5000).setMaxRedirects(5).build(); + return APACHE_SYNC_NACOS_REST_TEMPLATE.postForm(url, httpClientConfig, header, paramValues, String.class); } catch (Throwable e) { - return new HttpResult(500, e.toString(), Collections.emptyMap()); + return RestResult.builder().withCode(500).withMsg(e.toString()).build(); } } /** * Request http put method by async with large body. * - * @param url url - * @param headers headers - * @param content full request content - * @param handler callback after request execute + * @param url url + * @param headers headers + * @param content full request content + * @param callback callback after request execute */ public static void asyncHttpPutLarge(String url, Map headers, byte[] content, - AsyncCompletionHandler handler) throws Exception { - AsyncHttpClient.BoundRequestBuilder builder = asyncHttpClient.preparePut(url); - - if (!headers.isEmpty()) { - for (String headerKey : headers.keySet()) { - builder.setHeader(headerKey, headers.get(headerKey)); - } - } - - builder.setBody(content); - - builder.setHeader("Content-Type", "application/json; charset=UTF-8"); - builder.setHeader("Accept-Charset", "UTF-8"); - builder.setHeader("Accept-Encoding", "gzip"); - builder.setHeader("Content-Encoding", "gzip"); - - if (handler != null) { - builder.execute(handler); - } else { - builder.execute(); - } - } - - /** - * Request http get method by async with large body. - * - * @param url url - * @param headers headers - * @param content full request content - * @param handler callback after request execute - */ - public static void asyncHttpGetLarge(String url, Map headers, byte[] content, - AsyncCompletionHandler handler) throws Exception { - AsyncHttpClient.BoundRequestBuilder builder = asyncHttpClient.prepareGet(url); - - if (!headers.isEmpty()) { - for (String headerKey : headers.keySet()) { - builder.setHeader(headerKey, headers.get(headerKey)); - } - } - - builder.setBody(content); - - builder.setHeader("Content-Type", "application/json; charset=UTF-8"); - builder.setHeader("Accept-Charset", "UTF-8"); - builder.setHeader("Accept-Encoding", "gzip"); - builder.setHeader("Content-Encoding", "gzip"); - - if (handler != null) { - builder.execute(handler); - } else { - builder.execute(); + Callback callback) throws Exception { + Header header = Header.newInstance(); + if (MapUtils.isNotEmpty(headers)) { + header.addAll(headers); } + ASYNC_REST_TEMPLATE.put(url, header, Query.EMPTY, content, String.class, callback); } /** @@ -462,29 +297,17 @@ public class HttpClient { * @param url url * @param headers headers * @param content full request content - * @return {@link HttpResult} as response + * @return {@link RestResult} as response */ - public static HttpResult httpPutLarge(String url, Map headers, byte[] content) { - HttpClientBuilder builder = HttpClients.custom().setUserAgent(UtilsAndCommons.SERVER_VERSION) - .setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); - try (CloseableHttpClient httpClient = builder.build();) { - - HttpPut httpPut = new HttpPut(url); - for (Map.Entry entry : headers.entrySet()) { - httpPut.setHeader(entry.getKey(), entry.getValue()); - } - httpPut.setEntity(new ByteArrayEntity(content, ContentType.APPLICATION_JSON)); - - HttpResponse response = httpClient.execute(httpPut); - HttpEntity entity = response.getEntity(); - - HeaderElement[] headerElements = entity.getContentType().getElements(); - String charset = headerElements[0].getParameterByName("charset").getValue(); - - return new HttpResult(response.getStatusLine().getStatusCode(), - IoUtils.toString(entity.getContent(), charset), Collections.emptyMap()); + public static RestResult httpPutLarge(String url, Map headers, byte[] content) { + Header header = Header.newInstance(); + if (MapUtils.isNotEmpty(headers)) { + header.addAll(headers); + } + try { + return APACHE_SYNC_NACOS_REST_TEMPLATE.put(url, header, Query.EMPTY, content, String.class); } catch (Exception e) { - return new HttpResult(500, e.toString(), Collections.emptyMap()); + return RestResult.builder().withCode(500).withMsg(e.toString()).build(); } } @@ -494,33 +317,17 @@ public class HttpClient { * @param url url * @param headers headers * @param content full request content - * @return {@link HttpResult} as response + * @return {@link RestResult} as response */ - public static HttpResult httpGetLarge(String url, Map headers, String content) { - HttpClientBuilder builder = HttpClients.custom(); - builder.setUserAgent(UtilsAndCommons.SERVER_VERSION); - builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); - try (CloseableHttpClient httpClient = builder.build();) { - - HttpGetWithEntity httpGetWithEntity = new HttpGetWithEntity(); - httpGetWithEntity.setURI(new URI(url)); - - for (Map.Entry entry : headers.entrySet()) { - httpGetWithEntity.setHeader(entry.getKey(), entry.getValue()); - } - - httpGetWithEntity.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8"))); - - HttpResponse response = httpClient.execute(httpGetWithEntity); - HttpEntity entity = response.getEntity(); - - HeaderElement[] headerElements = entity.getContentType().getElements(); - String charset = headerElements[0].getParameterByName("charset").getValue(); - - return new HttpResult(response.getStatusLine().getStatusCode(), - IoUtils.toString(entity.getContent(), charset), Collections.emptyMap()); + public static RestResult httpGetLarge(String url, Map headers, String content) { + Header header = Header.newInstance(); + if (MapUtils.isNotEmpty(headers)) { + header.addAll(headers); + } + try { + return APACHE_SYNC_NACOS_REST_TEMPLATE.getLarge(url, header, Query.EMPTY, content, String.class); } catch (Exception e) { - return new HttpResult(500, e.toString(), Collections.emptyMap()); + return RestResult.builder().withCode(500).withMsg(e.toString()).build(); } } @@ -530,126 +337,20 @@ public class HttpClient { * @param url url * @param headers headers * @param content full request content - * @return {@link HttpResult} as response + * @return {@link RestResult} as response */ - public static HttpResult httpPostLarge(String url, Map headers, String content) { - HttpClientBuilder builder = HttpClients.custom(); - builder.setUserAgent(UtilsAndCommons.SERVER_VERSION); - builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS); - try (CloseableHttpClient httpClient = builder.build();) { - - HttpPost httpost = new HttpPost(url); - - for (Map.Entry entry : headers.entrySet()) { - httpost.setHeader(entry.getKey(), entry.getValue()); - } - - httpost.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8"))); - HttpResponse response = httpClient.execute(httpost); - HttpEntity entity = response.getEntity(); - - HeaderElement[] headerElements = entity.getContentType().getElements(); - String charset = headerElements[0].getParameterByName("charset").getValue(); - - return new HttpResult(response.getStatusLine().getStatusCode(), - IoUtils.toString(entity.getContent(), charset), Collections.emptyMap()); + public static RestResult httpPostLarge(String url, Map headers, String content) { + Header header = Header.newInstance(); + if (MapUtils.isNotEmpty(headers)) { + header.addAll(headers); + } + try { + return APACHE_SYNC_NACOS_REST_TEMPLATE.postJson(url, header, content, String.class); } catch (Exception e) { - return new HttpResult(500, e.toString(), Collections.emptyMap()); + return RestResult.builder().withCode(500).withMsg(e.toString()).build(); } } - private static HttpResult getResult(HttpURLConnection conn) throws IOException { - int respCode = conn.getResponseCode(); - - InputStream inputStream; - if (HttpURLConnection.HTTP_OK == respCode) { - inputStream = conn.getInputStream(); - } else { - inputStream = conn.getErrorStream(); - } - - Map respHeaders = new HashMap(conn.getHeaderFields().size()); - for (Map.Entry> entry : conn.getHeaderFields().entrySet()) { - respHeaders.put(entry.getKey(), entry.getValue().get(0)); - } - - String gzipEncoding = "gzip"; - - if (gzipEncoding.equals(respHeaders.get(HttpHeaders.CONTENT_ENCODING))) { - inputStream = new GZIPInputStream(inputStream); - } - - return new HttpResult(respCode, IoUtils.toString(inputStream, getCharset(conn)), respHeaders); - } - - private static String getCharset(HttpURLConnection conn) { - String contentType = conn.getContentType(); - if (StringUtils.isEmpty(contentType)) { - return "UTF-8"; - } - - String[] values = contentType.split(";"); - if (values.length == 0) { - return "UTF-8"; - } - - String charset = "UTF-8"; - for (String value : values) { - value = value.trim(); - - if (value.toLowerCase().startsWith("charset=")) { - charset = value.substring("charset=".length()); - } - } - - return charset; - } - - private static void setHeaders(HttpURLConnection conn, List headers, String encoding) { - if (null != headers) { - for (Iterator iter = headers.iterator(); iter.hasNext(); ) { - conn.addRequestProperty(iter.next(), iter.next()); - } - } - - conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding); - conn.addRequestProperty("Accept-Charset", encoding); - conn.addRequestProperty(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.version); - conn.addRequestProperty(HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION); - conn.addRequestProperty(HttpHeaderConsts.REQUEST_SOURCE_HEADER, ApplicationUtils.getLocalAddress()); - } - - /** - * Encoding parameters. - * - * @param params parameters - * @param encoding charset - * @return parameters string - * @throws UnsupportedEncodingException unsupported encodin exception - */ - public static String encodingParams(Map params, String encoding) - throws UnsupportedEncodingException { - StringBuilder sb = new StringBuilder(); - if (null == params || params.isEmpty()) { - return null; - } - - params.put("encoding", encoding); - params.put("nofix", "1"); - - for (Map.Entry entry : params.entrySet()) { - if (StringUtils.isEmpty(entry.getValue())) { - continue; - } - - sb.append(entry.getKey()).append("="); - sb.append(URLEncoder.encode(entry.getValue(), encoding)); - sb.append("&"); - } - - return sb.toString(); - } - /** * Translate parameter map. * @@ -664,33 +365,4 @@ public class HttpClient { } return map; } - - public static class HttpResult { - - public final int code; - - public final String content; - - private final Map respHeaders; - - public HttpResult(int code, String content, Map respHeaders) { - this.code = code; - this.content = content; - this.respHeaders = respHeaders; - } - - public String getHeader(String name) { - return respHeaders.get(name); - } - } - - public static class HttpGetWithEntity extends HttpEntityEnclosingRequestBase { - - public static final String METHOD_NAME = "GET"; - - @Override - public String getMethod() { - return METHOD_NAME; - } - } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClientManager.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClientManager.java new file mode 100644 index 000000000..bd1e3153c --- /dev/null +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/HttpClientManager.java @@ -0,0 +1,149 @@ +/* + * 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.naming.misc; + +import com.alibaba.nacos.common.http.AbstractApacheHttpClientFactory; +import com.alibaba.nacos.common.http.AbstractHttpClientFactory; +import com.alibaba.nacos.common.http.HttpClientBeanHolder; +import com.alibaba.nacos.common.http.HttpClientConfig; +import com.alibaba.nacos.common.http.HttpClientFactory; +import com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate; +import com.alibaba.nacos.common.http.client.NacosRestTemplate; +import com.alibaba.nacos.common.utils.ExceptionUtil; +import com.alibaba.nacos.common.utils.ThreadUtils; +import org.slf4j.Logger; + +import java.util.concurrent.TimeUnit; + +import static com.alibaba.nacos.naming.misc.Loggers.SRV_LOG; + +/** + * http Manager. + * + * @author mai.jh + */ +public class HttpClientManager { + + private static final int TIME_OUT_MILLIS = 10000; + + private static final int CON_TIME_OUT_MILLIS = 5000; + + private static final HttpClientFactory SYNC_HTTP_CLIENT_FACTORY = new SyncHttpClientFactory(); + + private static final HttpClientFactory ASYNC_HTTP_CLIENT_FACTORY = new AsyncHttpClientFactory(); + + private static final HttpClientFactory APACHE_SYNC_HTTP_CLIENT_FACTORY = new ApacheSyncHttpClientFactory(); + + private static final NacosRestTemplate NACOS_REST_TEMPLATE; + + private static final NacosRestTemplate APACHE_NACOS_REST_TEMPLATE; + + private static final NacosAsyncRestTemplate NACOS_ASYNC_REST_TEMPLATE; + + static { + // build nacos rest template + NACOS_REST_TEMPLATE = HttpClientBeanHolder.getNacosRestTemplate(SYNC_HTTP_CLIENT_FACTORY); + APACHE_NACOS_REST_TEMPLATE = HttpClientBeanHolder.getNacosRestTemplate(APACHE_SYNC_HTTP_CLIENT_FACTORY); + NACOS_ASYNC_REST_TEMPLATE = HttpClientBeanHolder.getNacosAsyncRestTemplate(ASYNC_HTTP_CLIENT_FACTORY); + + ThreadUtils.addShutdownHook(new Runnable() { + @Override + public void run() { + shutdown(); + } + }); + } + + public static NacosRestTemplate getNacosRestTemplate() { + return NACOS_REST_TEMPLATE; + } + + /** + * Use apache http client to achieve. + * @return NacosRestTemplate + */ + public static NacosRestTemplate getApacheRestTemplate() { + return APACHE_NACOS_REST_TEMPLATE; + } + + public static NacosAsyncRestTemplate getAsyncRestTemplate() { + return NACOS_ASYNC_REST_TEMPLATE; + } + + private static void shutdown() { + SRV_LOG.warn("[NamingServerHttpClientManager] Start destroying HTTP-Client"); + try { + HttpClientBeanHolder.shutdownNacostSyncRest(SYNC_HTTP_CLIENT_FACTORY.getClass().getName()); + HttpClientBeanHolder.shutdownNacostSyncRest(APACHE_SYNC_HTTP_CLIENT_FACTORY.getClass().getName()); + HttpClientBeanHolder.shutdownNacosAsyncRest(ASYNC_HTTP_CLIENT_FACTORY.getClass().getName()); + } catch (Exception ex) { + SRV_LOG.error("[NamingServerHttpClientManager] An exception occurred when the HTTP client was closed : {}", + ExceptionUtil.getStackTrace(ex)); + } + SRV_LOG.warn("[NamingServerHttpClientManager] Destruction of the end"); + } + + private static class AsyncHttpClientFactory extends AbstractHttpClientFactory { + + @Override + protected HttpClientConfig buildHttpClientConfig() { + return HttpClientConfig.builder().setConTimeOutMillis(CON_TIME_OUT_MILLIS) + .setReadTimeOutMillis(TIME_OUT_MILLIS) + .setUserAgent(UtilsAndCommons.SERVER_VERSION) + .setMaxConnTotal(-1) + .setMaxConnPerRoute(128) + .setMaxRedirects(0).build(); + } + + @Override + protected Logger assignLogger() { + return SRV_LOG; + } + } + + private static class SyncHttpClientFactory extends AbstractHttpClientFactory { + + @Override + protected HttpClientConfig buildHttpClientConfig() { + return HttpClientConfig.builder().setConTimeOutMillis(CON_TIME_OUT_MILLIS) + .setReadTimeOutMillis(TIME_OUT_MILLIS) + .setMaxRedirects(0).build(); + } + + @Override + protected Logger assignLogger() { + return SRV_LOG; + } + } + + private static class ApacheSyncHttpClientFactory extends AbstractApacheHttpClientFactory { + + @Override + protected HttpClientConfig buildHttpClientConfig() { + return HttpClientConfig.builder() + .setConnectionTimeToLive(500, TimeUnit.MILLISECONDS) + .setMaxConnTotal(Runtime.getRuntime().availableProcessors() * 2) + .setMaxConnPerRoute(Runtime.getRuntime().availableProcessors()) + .setMaxRedirects(0).build(); + } + + @Override + protected Logger assignLogger() { + return SRV_LOG; + } + } +} diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java index 1225af7f1..cb6414061 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/NamingProxy.java @@ -17,11 +17,11 @@ package com.alibaba.nacos.naming.misc; import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.VersionUtils; import com.alibaba.nacos.core.utils.ApplicationUtils; -import com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.Response; import org.apache.commons.lang3.StringUtils; import java.io.IOException; @@ -69,28 +69,32 @@ public class NamingProxy { headers.put(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.version); headers.put(HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION); - headers.put("Connection", "Keep-Alive"); + headers.put(HttpHeaderConsts.CONNECTION, "Keep-Alive"); HttpClient.asyncHttpPutLarge( "http://" + server + ApplicationUtils.getContextPath() + UtilsAndCommons.NACOS_NAMING_CONTEXT + TIMESTAMP_SYNC_URL + "?source=" + NetUtils.localServer(), headers, checksums, - new AsyncCompletionHandler() { + new Callback() { @Override - public Object onCompleted(Response response) throws Exception { - if (HttpURLConnection.HTTP_OK != response.getStatusCode()) { + public void onReceive(RestResult result) { + if (!result.ok()) { Loggers.DISTRO.error("failed to req API: {}, code: {}, msg: {}", "http://" + server + ApplicationUtils.getContextPath() + UtilsAndCommons.NACOS_NAMING_CONTEXT + TIMESTAMP_SYNC_URL, - response.getStatusCode(), response.getResponseBody()); + result.getCode(), result.getMessage()); } - return null; } - + @Override - public void onThrowable(Throwable t) { + public void onError(Throwable throwable) { Loggers.DISTRO .error("failed to req API:" + "http://" + server + ApplicationUtils.getContextPath() - + UtilsAndCommons.NACOS_NAMING_CONTEXT + TIMESTAMP_SYNC_URL, t); + + UtilsAndCommons.NACOS_NAMING_CONTEXT + TIMESTAMP_SYNC_URL, throwable); + } + + @Override + public void onCancel() { + } }); } catch (Exception e) { @@ -110,17 +114,17 @@ public class NamingProxy { Map params = new HashMap<>(8); params.put("keys", StringUtils.join(keys, ",")); - HttpClient.HttpResult result = HttpClient.httpGetLarge( + RestResult result = HttpClient.httpGetLarge( "http://" + server + ApplicationUtils.getContextPath() + UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL, new HashMap<>(8), JacksonUtils.toJson(params)); - if (HttpURLConnection.HTTP_OK == result.code) { - return result.content.getBytes(); + if (result.ok()) { + return result.getData().getBytes(); } throw new IOException("failed to req API: " + "http://" + server + ApplicationUtils.getContextPath() - + UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL + ". code: " + result.code + " msg: " - + result.content); + + UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL + ". code: " + result.getCode() + " msg: " + + result.getMessage()); } /** @@ -133,17 +137,17 @@ public class NamingProxy { public static byte[] getAllData(String server) throws Exception { Map params = new HashMap<>(8); - HttpClient.HttpResult result = HttpClient.httpGet( + RestResult result = HttpClient.httpGet( "http://" + server + ApplicationUtils.getContextPath() + UtilsAndCommons.NACOS_NAMING_CONTEXT + ALL_DATA_GET_URL, new ArrayList<>(), params); - if (HttpURLConnection.HTTP_OK == result.code) { - return result.content.getBytes(); + if (result.ok()) { + return result.getData().getBytes(); } throw new IOException("failed to req API: " + "http://" + server + ApplicationUtils.getContextPath() - + UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL + ". code: " + result.code + " msg: " - + result.content); + + UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL + ". code: " + result.getCode() + " msg: " + + result.getMessage()); } /** @@ -158,23 +162,23 @@ public class NamingProxy { headers.put(HttpHeaderConsts.CLIENT_VERSION_HEADER, VersionUtils.version); headers.put(HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION); - headers.put("Accept-Encoding", "gzip,deflate,sdch"); - headers.put("Connection", "Keep-Alive"); - headers.put("Content-Encoding", "gzip"); + headers.put(HttpHeaderConsts.ACCEPT_ENCODING, "gzip,deflate,sdch"); + headers.put(HttpHeaderConsts.CONNECTION, "Keep-Alive"); + headers.put(HttpHeaderConsts.CONTENT_ENCODING, "gzip"); try { - HttpClient.HttpResult result = HttpClient.httpPutLarge( + RestResult result = HttpClient.httpPutLarge( "http://" + curServer + ApplicationUtils.getContextPath() + UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_ON_SYNC_URL, headers, data); - if (HttpURLConnection.HTTP_OK == result.code) { + if (result.ok()) { return true; } - if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) { + if (HttpURLConnection.HTTP_NOT_MODIFIED == result.getCode()) { return true; } throw new IOException("failed to req API:" + "http://" + curServer + ApplicationUtils.getContextPath() - + UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_ON_SYNC_URL + ". code:" + result.code + " msg: " - + result.content); + + UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_ON_SYNC_URL + ". code:" + result.getCode() + " msg: " + + result.getData()); } catch (Exception e) { Loggers.SRV_LOG.warn("NamingProxy", e); } @@ -196,7 +200,7 @@ public class NamingProxy { HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION, "Accept-Encoding", "gzip,deflate,sdch", "Connection", "Keep-Alive", "Content-Encoding", "gzip"); - HttpClient.HttpResult result; + RestResult result; if (!curServer.contains(UtilsAndCommons.IP_PORT_SPLITER)) { curServer = curServer + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort(); @@ -204,17 +208,17 @@ public class NamingProxy { result = HttpClient.httpGet("http://" + curServer + api, headers, params); - if (HttpURLConnection.HTTP_OK == result.code) { - return result.content; + if (result.ok()) { + return result.getData(); } - if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) { + if (HttpURLConnection.HTTP_NOT_MODIFIED == result.getCode()) { return StringUtils.EMPTY; } throw new IOException( - "failed to req API:" + "http://" + curServer + api + ". code:" + result.code + " msg: " - + result.content); + "failed to req API:" + "http://" + curServer + api + ". code:" + result.getCode() + " msg: " + + result.getMessage()); } catch (Exception e) { Loggers.SRV_LOG.warn("NamingProxy", e); } @@ -238,7 +242,7 @@ public class NamingProxy { HttpHeaderConsts.USER_AGENT_HEADER, UtilsAndCommons.SERVER_VERSION, "Accept-Encoding", "gzip,deflate,sdch", "Connection", "Keep-Alive", "Content-Encoding", "gzip"); - HttpClient.HttpResult result; + RestResult result; if (!curServer.contains(UtilsAndCommons.IP_PORT_SPLITER)) { curServer = curServer + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort(); @@ -254,17 +258,17 @@ public class NamingProxy { + "/api/" + api, headers, params); } - if (HttpURLConnection.HTTP_OK == result.code) { - return result.content; + if (result.ok()) { + return result.getData(); } - if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) { + if (HttpURLConnection.HTTP_NOT_MODIFIED == result.getCode()) { return StringUtils.EMPTY; } throw new IOException("failed to req API:" + "http://" + curServer + ApplicationUtils.getContextPath() - + UtilsAndCommons.NACOS_NAMING_CONTEXT + "/api/" + api + ". code:" + result.code + " msg: " - + result.content); + + UtilsAndCommons.NACOS_NAMING_CONTEXT + "/api/" + api + ". code:" + result.getCode() + " msg: " + + result.getMessage()); } catch (Exception e) { Loggers.SRV_LOG.warn("NamingProxy", e); } @@ -288,7 +292,7 @@ public class NamingProxy { UtilsAndCommons.SERVER_VERSION, "Accept-Encoding", "gzip,deflate,sdch", "Connection", "Keep-Alive", "Content-Encoding", "gzip"); - HttpClient.HttpResult result; + RestResult result; if (!curServer.contains(UtilsAndCommons.IP_PORT_SPLITER)) { curServer = curServer + UtilsAndCommons.IP_PORT_SPLITER + ApplicationUtils.getPort(); @@ -304,17 +308,17 @@ public class NamingProxy { + path, headers, params); } - if (HttpURLConnection.HTTP_OK == result.code) { - return result.content; + if (result.ok()) { + return result.getData(); } - if (HttpURLConnection.HTTP_NOT_MODIFIED == result.code) { + if (HttpURLConnection.HTTP_NOT_MODIFIED == result.getCode()) { return StringUtils.EMPTY; } throw new IOException("failed to req API:" + "http://" + curServer + ApplicationUtils.getContextPath() - + UtilsAndCommons.NACOS_NAMING_CONTEXT + path + ". code:" + result.code + " msg: " - + result.content); + + UtilsAndCommons.NACOS_NAMING_CONTEXT + path + ". code:" + result.getCode() + " msg: " + + result.getMessage()); } catch (Exception e) { Loggers.SRV_LOG.warn("NamingProxy", e); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/ServerStatusSynchronizer.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/ServerStatusSynchronizer.java index bb1243891..cb8cf3f10 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/ServerStatusSynchronizer.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/ServerStatusSynchronizer.java @@ -16,12 +16,11 @@ package com.alibaba.nacos.naming.misc; +import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.core.utils.ApplicationUtils; -import com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.Response; import org.springframework.util.StringUtils; -import java.net.HttpURLConnection; import java.util.HashMap; import java.util.Map; @@ -52,15 +51,23 @@ public class ServerStatusSynchronizer implements Synchronizer { } try { - HttpClient.asyncHttpGet(url, null, params, new AsyncCompletionHandler() { + HttpClient.asyncHttpGet(url, null, params, new Callback() { @Override - public Integer onCompleted(Response response) throws Exception { - if (response.getStatusCode() != HttpURLConnection.HTTP_OK) { + public void onReceive(RestResult result) { + if (!result.ok()) { Loggers.SRV_LOG.warn("[STATUS-SYNCHRONIZE] failed to request serverStatus, remote server: {}", serverIP); - return 1; } - return 0; + } + + @Override + public void onError(Throwable throwable) { + Loggers.SRV_LOG.warn("[STATUS-SYNCHRONIZE] failed to request serverStatus, remote server: {}", serverIP, throwable); + } + + @Override + public void onCancel() { + } }); } catch (Exception e) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/ServiceStatusSynchronizer.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/ServiceStatusSynchronizer.java index c65fa93c2..940018259 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/ServiceStatusSynchronizer.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/ServiceStatusSynchronizer.java @@ -16,13 +16,12 @@ package com.alibaba.nacos.naming.misc; +import com.alibaba.nacos.common.http.Callback; +import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.core.utils.ApplicationUtils; -import com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.Response; import org.apache.commons.lang3.StringUtils; -import java.net.HttpURLConnection; import java.util.HashMap; import java.util.Map; @@ -53,16 +52,24 @@ public class ServiceStatusSynchronizer implements Synchronizer { } try { - HttpClient.asyncHttpPostLarge(url, null, JacksonUtils.toJson(params), new AsyncCompletionHandler() { + HttpClient.asyncHttpPostLarge(url, null, JacksonUtils.toJson(params), new Callback() { @Override - public Integer onCompleted(Response response) throws Exception { - if (response.getStatusCode() != HttpURLConnection.HTTP_OK) { + public void onReceive(RestResult result) { + if (!result.ok()) { Loggers.SRV_LOG.warn("[STATUS-SYNCHRONIZE] failed to request serviceStatus, remote server: {}", serverIP); - - return 1; + } - return 0; + } + + @Override + public void onError(Throwable throwable) { + Loggers.SRV_LOG.warn("[STATUS-SYNCHRONIZE] failed to request serviceStatus, remote server: " + serverIP, throwable); + } + + @Override + public void onCancel() { + } }); } catch (Exception e) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java index 579731e45..371182dd5 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/web/DistroFilter.java @@ -19,6 +19,7 @@ package com.alibaba.nacos.naming.web; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.common.constant.HttpHeaderConsts; +import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.utils.ExceptionUtil; import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.core.code.ControllerMethodsCache; @@ -139,11 +140,12 @@ public class DistroFilter implements Filter { final String body = IoUtils.toString(req.getInputStream(), Charsets.UTF_8.name()); final Map paramsValue = HttpClient.translateParameterMap(req.getParameterMap()); - HttpClient.HttpResult result = HttpClient + RestResult result = HttpClient .request("http://" + targetServer + req.getRequestURI(), headerList, paramsValue, body, PROXY_CONNECT_TIMEOUT, PROXY_READ_TIMEOUT, Charsets.UTF_8.name(), req.getMethod()); + String data = result.ok() ? result.getData() : result.getMessage(); try { - WebUtils.response(resp, result.content, result.code); + WebUtils.response(resp, data, result.getCode()); } catch (Exception ignore) { Loggers.SRV_LOG.warn("[DISTRO-FILTER] request failed: " + distroMapper.mapSrv(groupedServiceName) + urlString); From 565557c5c5b5d22cbcedfdcdad4289e5a4461a96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= <263976490@qq.com> Date: Tue, 15 Sep 2020 14:50:57 +0800 Subject: [PATCH 09/21] Fix NullPointerException when no subscriber for slow event (#3835) --- .../alibaba/nacos/common/notify/DefaultSharePublisher.java | 4 ++++ .../com/alibaba/nacos/common/notify/NotifyCenterTest.java | 1 + 2 files changed, 5 insertions(+) diff --git a/common/src/main/java/com/alibaba/nacos/common/notify/DefaultSharePublisher.java b/common/src/main/java/com/alibaba/nacos/common/notify/DefaultSharePublisher.java index 3028423f7..a2fa996d1 100644 --- a/common/src/main/java/com/alibaba/nacos/common/notify/DefaultSharePublisher.java +++ b/common/src/main/java/com/alibaba/nacos/common/notify/DefaultSharePublisher.java @@ -96,6 +96,10 @@ public class DefaultSharePublisher extends DefaultPublisher { // Get for Map, the algorithm is O(1). Set subscribers = subMappings.get(slowEventType); + if (null == subscribers) { + LOGGER.debug("[NotifyCenter] No subscribers for slow event {}", slowEventType.getName()); + return; + } // Notification single event subscriber for (Subscriber subscriber : subscribers) { diff --git a/common/src/test/java/com/alibaba/nacos/common/notify/NotifyCenterTest.java b/common/src/test/java/com/alibaba/nacos/common/notify/NotifyCenterTest.java index a7163938d..d1abd7c1e 100644 --- a/common/src/test/java/com/alibaba/nacos/common/notify/NotifyCenterTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/notify/NotifyCenterTest.java @@ -132,6 +132,7 @@ public class NotifyCenterTest { return ExpireEvent.class; } + @Override public boolean ignoreExpireEvent() { return true; } From 2ded5a24fc68a733df7620211f76a3958914ba05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= <263976490@qq.com> Date: Wed, 16 Sep 2020 09:14:03 +0800 Subject: [PATCH 10/21] Set mediaType charset as utf8 (#3837) --- .../alibaba/nacos/common/http/param/MediaType.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 ecb05abb1..4838f9613 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 @@ -28,7 +28,7 @@ public final class 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"; @@ -36,14 +36,14 @@ public final class MediaType { public static final String APPLICATION_XHTML_XML = "application/xhtml+xml"; - public static final String APPLICATION_XML = "application/xml"; + public static final String APPLICATION_XML = "application/xml;charset=UTF-8"; - public static final String APPLICATION_JSON = "application/json"; + 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 MULTIPART_FORM_DATA = "multipart/form-data;charset=UTF-8"; - public static final String TEXT_HTML = "text/html"; + public static final String TEXT_HTML = "text/html;charset=UTF-8"; - public static final String TEXT_PLAIN = "text/plain"; + public static final String TEXT_PLAIN = "text/plain;charset=UTF-8"; } From 700c90d15fdb626a1470b88299051b82060519ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= <263976490@qq.com> Date: Wed, 16 Sep 2020 12:33:33 +0800 Subject: [PATCH 11/21] Fix code style problem in DiskUtils (#3842) --- .../alibaba/nacos/core/utils/DiskUtils.java | 141 +++++++----------- style/nacos-code-style-for-idea.xml | 1 + 2 files changed, 57 insertions(+), 85 deletions(-) diff --git a/core/src/main/java/com/alibaba/nacos/core/utils/DiskUtils.java b/core/src/main/java/com/alibaba/nacos/core/utils/DiskUtils.java index cd9c33e5d..5a7c4b255 100644 --- a/core/src/main/java/com/alibaba/nacos/core/utils/DiskUtils.java +++ b/core/src/main/java/com/alibaba/nacos/core/utils/DiskUtils.java @@ -91,75 +91,48 @@ public final class DiskUtils { } /** - * Creates a new empty file in the specified directory, using the given - * prefix and suffix strings to generate its name. The resulting - * {@code Path} is associated with the same {@code FileSystem} as the given - * directory. + * Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its + * name. The resulting {@code Path} is associated with the same {@code FileSystem} as the given directory. * *

The details as to how the name of the file is constructed is - * implementation dependent and therefore not specified. Where possible - * the {@code prefix} and {@code suffix} are used to construct candidate - * names in the same manner as the {@link - * java.io.File#createTempFile(String,String,File)} method. + * implementation dependent and therefore not specified. Where possible the {@code prefix} and {@code suffix} are + * used to construct candidate names in the same manner as the {@link java.io.File#createTempFile(String, String, File)} + * method. * - * - * @param dir - * the path to directory in which to create the file - * @param prefix - * the prefix string to be used in generating the file's name; - * may be {@code null} - * @param suffix - * the suffix string to be used in generating the file's name; - * may be {@code null}, in which case "{@code .tmp}" is used - * - * @return the path to the newly created file that did not exist before - * this method was invoked - * - * @throws IllegalArgumentException - * if the prefix or suffix parameters cannot be used to generate - * a candidate file name - * @throws UnsupportedOperationException - * if the array contains an attribute that cannot be set atomically - * when creating the directory - * @throws IOException - * if an I/O error occurs or {@code dir} does not exist - * @throws SecurityException - * In the case of the default provider, and a security manager is - * installed, the {@link SecurityManager#checkWrite(String) checkWrite} - * method is invoked to check write access to the file. + * @param dir the path to directory in which to create the file + * @param prefix the prefix string to be used in generating the file's name; may be {@code null} + * @param suffix the suffix string to be used in generating the file's name; may be {@code null}, in which case + * "{@code .tmp}" is used + * @return the path to the newly created file that did not exist before this method was invoked + * @throws IllegalArgumentException if the prefix or suffix parameters cannot be used to generate a candidate + * file name + * @throws UnsupportedOperationException if the array contains an attribute that cannot be set atomically when + * creating the directory + * @throws IOException if an I/O error occurs or {@code dir} does not exist + * @throws SecurityException In the case of the default provider, and a security manager is installed, + * the {@link SecurityManager#checkWrite(String) checkWrite} method is invoked + * to check write access to the file. */ public static File createTmpFile(String dir, String prefix, String suffix) throws IOException { return Files.createTempFile(Paths.get(dir), prefix, suffix).toFile(); } /** - * Creates an empty file in the default temporary-file directory, using - * the given prefix and suffix to generate its name. The resulting {@code - * Path} is associated with the default {@code FileSystem}. + * Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its + * name. The resulting {@code Path} is associated with the default {@code FileSystem}. * - * @param prefix - * the prefix string to be used in generating the file's name; - * may be {@code null} - * @param suffix - * the suffix string to be used in generating the file's name; - * may be {@code null}, in which case "{@code .tmp}" is used - * - * @return the path to the newly created file that did not exist before - * this method was invoked - * - * @throws IllegalArgumentException - * if the prefix or suffix parameters cannot be used to generate - * a candidate file name - * @throws UnsupportedOperationException - * if the array contains an attribute that cannot be set atomically - * when creating the directory - * @throws IOException - * if an I/O error occurs or the temporary-file directory does not - * exist - * @throws SecurityException - * In the case of the default provider, and a security manager is - * installed, the {@link SecurityManager#checkWrite(String) checkWrite} - * method is invoked to check write access to the file. + * @param prefix the prefix string to be used in generating the file's name; may be {@code null} + * @param suffix the suffix string to be used in generating the file's name; may be {@code null}, in which case + * "{@code .tmp}" is used + * @return the path to the newly created file that did not exist before this method was invoked + * @throws IllegalArgumentException if the prefix or suffix parameters cannot be used to generate a candidate + * file name + * @throws UnsupportedOperationException if the array contains an attribute that cannot be set atomically when + * creating the directory + * @throws IOException if an I/O error occurs or the temporary-file directory does not exist + * @throws SecurityException In the case of the default provider, and a security manager is installed, + * the {@link SecurityManager#checkWrite(String) checkWrite} method is invoked + * to check write access to the file. */ public static File createTmpFile(String prefix, String suffix) throws IOException { return Files.createTempFile(prefix, suffix).toFile(); @@ -380,8 +353,8 @@ public final class DiskUtils { */ public static void compress(final String rootDir, final String sourceDir, final String outputFile, final Checksum checksum) throws IOException { - try (final FileOutputStream fos = new FileOutputStream( - outputFile); final CheckedOutputStream cos = new CheckedOutputStream(fos, checksum); + try (final FileOutputStream fos = new FileOutputStream(outputFile); + final CheckedOutputStream cos = new CheckedOutputStream(fos, checksum); final ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(cos))) { compressDirectoryToZipFile(rootDir, sourceDir, zos); zos.flush(); @@ -401,8 +374,8 @@ public final class DiskUtils { compressDirectoryToZipFile(rootDir, child, zos); } else { zos.putNextEntry(new ZipEntry(child)); - try (final FileInputStream fis = new FileInputStream( - file); final BufferedInputStream bis = new BufferedInputStream(fis)) { + try (final FileInputStream fis = new FileInputStream(file); + final BufferedInputStream bis = new BufferedInputStream(fis)) { IOUtils.copy(bis, zos); } } @@ -421,16 +394,16 @@ public final class DiskUtils { */ public static void decompress(final String sourceFile, final String outputDir, final Checksum checksum) throws IOException { - try (final FileInputStream fis = new FileInputStream( - sourceFile); final CheckedInputStream cis = new CheckedInputStream(fis, checksum); + try (final FileInputStream fis = new FileInputStream(sourceFile); + final CheckedInputStream cis = new CheckedInputStream(fis, checksum); final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(cis))) { ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { final String fileName = entry.getName(); final File entryFile = new File(Paths.get(outputDir, fileName).toString()); FileUtils.forceMkdir(entryFile.getParentFile()); - try (final FileOutputStream fos = new FileOutputStream( - entryFile); final BufferedOutputStream bos = new BufferedOutputStream(fos)) { + try (final FileOutputStream fos = new FileOutputStream(entryFile); + final BufferedOutputStream bos = new BufferedOutputStream(fos)) { IOUtils.copy(zis, bos); bos.flush(); fos.getFD().sync(); @@ -447,11 +420,10 @@ public final class DiskUtils { /** * Returns an Iterator for the lines in a File. *

- * This method opens an InputStream for the file. - * When you have finished with the iterator you should close the stream - * to free internal resources. This can be done by calling the - * {@link org.apache.commons.io.LineIterator#close()} or - * {@link org.apache.commons.io.LineIterator#closeQuietly(org.apache.commons.io.LineIterator)} method. + * This method opens an InputStream for the file. When you have finished with the iterator you should + * close the stream to free internal resources. This can be done by calling the {@link + * org.apache.commons.io.LineIterator#close()} or {@link org.apache.commons.io.LineIterator#closeQuietly(org.apache.commons.io.LineIterator)} + * method. *

* The recommended usage pattern is: *
@@ -466,12 +438,11 @@ public final class DiskUtils {
      * }
      * 
*

- * If an exception occurs during the creation of the iterator, the - * underlying stream is closed. + * If an exception occurs during the creation of the iterator, the underlying stream is closed. *

* - * @param file the file to open for input, must not be null - * @param encoding the encoding to use, null means platform default + * @param file the file to open for input, must not be null + * @param encoding the encoding to use, null means platform default * @return an Iterator of the lines in the file, never null * @throws IOException in case of an I/O error (file closed) * @since 1.2 @@ -483,20 +454,20 @@ public final class DiskUtils { /** * Returns an Iterator for the lines in a File using the default encoding for the VM. * - * @param file the file to open for input, must not be null + * @param file the file to open for input, must not be null * @return an Iterator of the lines in the file, never null * @throws IOException in case of an I/O error (file closed) - * @since 1.3 * @see #lineIterator(File, String) + * @since 1.3 */ public static LineIterator lineIterator(File file) throws IOException { return new LineIterator(FileUtils.lineIterator(file, null)); } public static class LineIterator implements AutoCloseable { - + private final org.apache.commons.io.LineIterator target; - + /** * Constructs an iterator of the lines for a Reader. * @@ -505,7 +476,7 @@ public final class DiskUtils { LineIterator(org.apache.commons.io.LineIterator target) { this.target = target; } - + public boolean hasNext() { return target.hasNext(); } @@ -513,20 +484,20 @@ public final class DiskUtils { public String next() { return target.next(); } - + public String nextLine() { return target.nextLine(); } - + @Override public void close() { target.close(); } - + public void remove() { target.remove(); } - + public void forEachRemaining(Consumer action) { target.forEachRemaining(action); } diff --git a/style/nacos-code-style-for-idea.xml b/style/nacos-code-style-for-idea.xml index 28846f8ae..6bd63a113 100644 --- a/style/nacos-code-style-for-idea.xml +++ b/style/nacos-code-style-for-idea.xml @@ -139,6 +139,7 @@