From ea214e1d50ea45e437c89aa7986962397b1ed6ce Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sat, 27 Apr 2019 22:32:28 +0800 Subject: [PATCH 01/12] feat(client): add feature of service curd --- .../alibaba/nacos/api/common/Constants.java | 2 + .../nacos/api/naming/NamingService.java | 126 ++++++++++++++++++ .../nacos/api/selector/NoneSelector.java | 28 ++++ .../client/naming/NacosNamingService.java | 106 ++++++++++++++- .../nacos/client/naming/net/NamingProxy.java | 47 +++++++ .../client/naming/utils/UtilAndComs.java | 2 + .../client/naming/NacosNamingServiceTest.java | 84 ++++++++++++ update_git_info.sh | 19 +++ 8 files changed, 410 insertions(+), 4 deletions(-) create mode 100644 api/src/main/java/com/alibaba/nacos/api/selector/NoneSelector.java create mode 100644 client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java create mode 100755 update_git_info.sh diff --git a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java index 49d7722dc..842416a5b 100644 --- a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java +++ b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java @@ -103,6 +103,8 @@ public class Constants { public static final int FLOW_CONTROL_INTERVAL = 1000; + public static final float PROTECT_THRESHOLD = 0.0F; + public static final String LINE_SEPARATOR = Character.toString((char)1); public static final String WORD_SEPARATOR = Character.toString((char)2); diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java index 06cb467da..1a30bbe3a 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java @@ -19,10 +19,12 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.listener.EventListener; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; +import com.alibaba.nacos.api.naming.pojo.Service; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.selector.AbstractSelector; import java.util.List; +import java.util.Map; /** * Naming Service @@ -408,6 +410,130 @@ public interface NamingService { */ Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException; + /** + * create service to Nacos + * + * @param serviceName name of service + * @throws NacosException + */ + void createService(String serviceName) throws NacosException; + + /** + * create service to Nacos + * + * @param serviceName name of service + * @param groupName group of service + * @throws NacosException + */ + void createService(String serviceName, String groupName) throws NacosException; + + /** + * create service to Nacos + * + * @param serviceName name of service + * @param groupName group of service + * @param protectThreshold protectThreshold of service + * @throws NacosException + */ + void createService(String serviceName, String groupName, Float protectThreshold) throws NacosException; + + /** + * create service to Nacos + * + * @param serviceName name of service + * @param groupName group of service + * @param protectThreshold protectThreshold of service + * @param expression expression of selector + * @throws NacosException + */ + void createService(String serviceName, String groupName, Float protectThreshold, String expression) throws NacosException; + + /** + * create service to Nacos + * + * @param service name of service + * @param selector selector + * @throws NacosException + */ + void createService(Service service, AbstractSelector selector) throws NacosException; + + /** + * delete service from Nacos + * + * @param serviceName name of service + * @return if delete service success return true + * @throws NacosException + */ + boolean deleteService(String serviceName) throws NacosException; + + /** + * delete service from Nacos + * + * @param serviceName name of service + * @param groupName group of service + * @return if delete service success return true + * @throws NacosException + */ + boolean deleteService(String serviceName, String groupName) throws NacosException; + + /** + * update service to Nacos + * + * @param serviceName name of service + * @param groupName group of service + * @throws NacosException + */ + void updateService(String serviceName, String groupName) throws NacosException; + + /** + * update service to Nacos + * + * @param serviceName name of service + * @param groupName group of service + * @param protectThreshold protectThreshold of service + * @throws NacosException + */ + void updateService(String serviceName, String groupName, Float protectThreshold) throws NacosException; + + /** + * update service to Nacos with with selector's expression + * + * @param serviceName name of service + * @param groupName group of service + * @param protectThreshold protectThreshold of service + * @param expression expression of selector + * @throws NacosException + */ + void updateService(String serviceName, String groupName, Float protectThreshold, String expression) throws NacosException; + + /** + * update service to Nacos + * + * @param serviceName name of service + * @param groupName group of service + * @param protectThreshold protectThreshold of service + * @param metadata metadata of service + * @throws NacosException + */ + void updateService(String serviceName, String groupName, Float protectThreshold, Map metadata) throws NacosException; + + /** + * update service to Nacos + * + * @param service {@link Service} pojo of service + * @throws NacosException + */ + void updateService(Service service) throws NacosException; + + /** + * update service to Nacos with selector + * + * @param service {@link Service} pojo of service + * @param selector {@link AbstractSelector} pojo of selector + * @throws NacosException + */ + void updateService(Service service, AbstractSelector selector) throws NacosException; + /** * Subscribe service to receive events of instances alteration * diff --git a/api/src/main/java/com/alibaba/nacos/api/selector/NoneSelector.java b/api/src/main/java/com/alibaba/nacos/api/selector/NoneSelector.java new file mode 100644 index 000000000..8da7c48c6 --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/selector/NoneSelector.java @@ -0,0 +1,28 @@ +/* + * 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.api.selector; + +/** + * @author liaochuntao + * @since 1.0.0 + */ +public class NoneSelector extends AbstractSelector { + + public NoneSelector() { + this.setType(SelectorType.none.name()); + } +} diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index 09b6da76e..a84da05fc 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -23,9 +23,12 @@ import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.listener.EventListener; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; +import com.alibaba.nacos.api.naming.pojo.Service; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.api.selector.AbstractSelector; +import com.alibaba.nacos.api.selector.ExpressionSelector; +import com.alibaba.nacos.api.selector.NoneSelector; import com.alibaba.nacos.client.identify.CredentialService; import com.alibaba.nacos.client.naming.beat.BeatInfo; import com.alibaba.nacos.client.naming.beat.BeatReactor; @@ -42,10 +45,7 @@ import com.alibaba.nacos.client.utils.TemplateUtils; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.math.NumberUtils; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Properties; +import java.util.*; import java.util.concurrent.Callable; /** @@ -479,6 +479,104 @@ public class NacosNamingService implements NamingService { } } + @Override + public void createService(String serviceName) throws NacosException { + createService(serviceName, Constants.DEFAULT_GROUP); + } + + @Override + public void createService(String serviceName, String groupName) throws NacosException { + createService(serviceName, groupName, Constants.PROTECT_THRESHOLD); + } + + @Override + public void createService(String serviceName, String groupName, Float protectThreshold) throws NacosException { + NoneSelector selector = new NoneSelector(); + Service service = new Service(); + service.setName(serviceName); + service.setGroupName(groupName); + service.setProtectThreshold(protectThreshold); + + createService(service, selector); + } + + @Override + public void createService(String serviceName, String groupName, Float protectThreshold, String expression) throws NacosException { + Service service = new Service(); + service.setName(serviceName); + service.setGroupName(groupName); + service.setProtectThreshold(protectThreshold); + + ExpressionSelector selector = new ExpressionSelector(); + selector.setExpression(expression); + + createService(service, selector); + } + + @Override + public void createService(Service service, AbstractSelector selector) throws NacosException { + serverProxy.createService(service, selector); + } + + @Override + public boolean deleteService(String serviceName) throws NacosException { + return deleteService(serviceName, Constants.DEFAULT_GROUP); + } + + @Override + public boolean deleteService(String serviceName, String groupName) throws NacosException { + return serverProxy.deleteService(serviceName, groupName); + } + + @Override + public void updateService(String serviceName, String groupName) throws NacosException { + updateService(serviceName, groupName, Constants.PROTECT_THRESHOLD); + } + + @Override + public void updateService(String serviceName, String groupName, Float protectThreshold) throws NacosException { + Service service = new Service(); + service.setName(serviceName); + service.setGroupName(groupName); + service.setProtectThreshold(protectThreshold); + + updateService(service, new NoneSelector()); + } + + @Override + public void updateService(String serviceName, String groupName, Float protectThreshold, String expression) throws NacosException { + Service service = new Service(); + service.setName(serviceName); + service.setGroupName(groupName); + service.setProtectThreshold(protectThreshold); + + ExpressionSelector selector = new ExpressionSelector(); + selector.setExpression(expression); + + updateService(service, selector); + } + + @Override + public void updateService(String serviceName, String groupName, Float protectThreshold, Map metadata) throws NacosException { + Service service = new Service(); + service.setName(serviceName); + service.setGroupName(groupName); + service.setProtectThreshold(protectThreshold); + service.setMetadata(metadata); + + updateService(service, new NoneSelector()); + } + + @Override + public void updateService(Service service) throws NacosException { + updateService(service, new NoneSelector()); + } + + @Override + public void updateService(Service service, AbstractSelector selector) throws NacosException { + serverProxy.updateService(service, selector); + } + @Override public void subscribe(String serviceName, EventListener listener) throws NacosException { subscribe(serviceName, new ArrayList(), listener); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index 5aa070aa6..f60732936 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -24,6 +24,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.CommonParams; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; +import com.alibaba.nacos.api.naming.pojo.Service; import com.alibaba.nacos.api.selector.AbstractSelector; import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.SelectorType; @@ -203,6 +204,52 @@ public class NamingProxy { reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.DELETE); } + public void createService(Service service, AbstractSelector selector) throws NacosException { + + NAMING_LOGGER.info("[CREATE-SERVICE] {} creating service : {}", + namespaceId, service); + + final Map params = new HashMap(6); + params.put(CommonParams.NAMESPACE_ID, namespaceId); + params.put(CommonParams.SERVICE_NAME, service.getName()); + params.put(CommonParams.GROUP_NAME, service.getGroupName()); + params.put("protectThreshold", String.valueOf(service.getProtectThreshold())); + params.put("metadata", JSON.toJSONString(service.getMetadata())); + params.put("selector", JSON.toJSONString(selector)); + + reqAPI(UtilAndComs.NACOS_URL_SERVICE, params, HttpMethod.POST); + + } + + public boolean deleteService(String serviceName, String groupName) throws NacosException { + NAMING_LOGGER.info("[DELETE-SERVICE] {} deleting service : {} with groupName : {}", + namespaceId, serviceName, groupName); + + final Map params = new HashMap(6); + params.put(CommonParams.NAMESPACE_ID, namespaceId); + params.put(CommonParams.SERVICE_NAME, serviceName); + params.put(CommonParams.GROUP_NAME, groupName); + + String result = reqAPI(UtilAndComs.NACOS_URL_SERVICE, params, HttpMethod.DELETE); + NAMING_LOGGER.info(result); + return "ok".equals(result); + } + + public void updateService(Service service, AbstractSelector selector) throws NacosException { + NAMING_LOGGER.info("[UPDATE-SERVICE] {} updating service : {}", + namespaceId, service); + + final Map params = new HashMap(6); + params.put(CommonParams.NAMESPACE_ID, namespaceId); + params.put(CommonParams.SERVICE_NAME, service.getName()); + params.put(CommonParams.GROUP_NAME, service.getGroupName()); + params.put("protectThreshold", String.valueOf(service.getProtectThreshold())); + params.put("metadata", JSON.toJSONString(service.getMetadata())); + params.put("selector", JSON.toJSONString(selector)); + + reqAPI(UtilAndComs.NACOS_URL_SERVICE, params, HttpMethod.PUT); + } + public String queryList(String serviceName, String clusters, int udpPort, boolean healthyOnly) throws NacosException { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java index 3035b0e00..5c7af569a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/UtilAndComs.java @@ -28,6 +28,8 @@ public class UtilAndComs { public static String NACOS_URL_INSTANCE = NACOS_URL_BASE + "/instance"; + public static String NACOS_URL_SERVICE = NACOS_URL_BASE + "/service"; + public static final String ENCODING = "UTF-8"; public static final String ENV_LIST_KEY = "envList"; diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java new file mode 100644 index 000000000..e384c1089 --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java @@ -0,0 +1,84 @@ +package com.alibaba.nacos.client.naming; + +import com.alibaba.nacos.api.NacosFactory; +import com.alibaba.nacos.api.PropertyKeyConst; +import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.pojo.Service; +import com.alibaba.nacos.api.selector.ExpressionSelector; +import com.alibaba.nacos.api.selector.NoneSelector; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; + + +public class NacosNamingServiceTest { + + private NamingService nameService; + + @Before + public void before() throws NacosException { + Properties properties = new Properties(); + properties.put(PropertyKeyConst.SERVER_ADDR, "11.160.165.126:8848"); + + nameService = NacosFactory.createNamingService(properties); + } + + @Test + public void createService() { + Service service = new Service(); + service.setName("nacos-api"); + service.setGroupName(Constants.DEFAULT_GROUP); + service.setProtectThreshold(1.0f); + Map metadata = new HashMap(); + metadata.put("nacos-1", "this is a test metadata"); + service.setMetadata(metadata); + + ExpressionSelector selector = new ExpressionSelector(); + selector.setExpression("CONSUMER.label.A=PROVIDER.label.A &CONSUMER.label.B=PROVIDER.label.B"); + + try { + nameService.createService(service, new NoneSelector()); + } catch (NacosException e) { + NAMING_LOGGER.error(e.getErrMsg()); + } + } + + @Test + public void deleteService() { + try { + Assert.assertTrue(nameService.deleteService("nacos-api")); + } catch (NacosException e) { + NAMING_LOGGER.error(e.getErrMsg()); + } + } + + @Test + public void updateService() { + Service service = new Service(); + service.setName("nacos-api"); + service.setGroupName(Constants.DEFAULT_GROUP); + service.setProtectThreshold(1.0f); + Map metadata = new HashMap(); + metadata.put("nacos-1", "nacos-3-update"); + service.setMetadata(metadata); + + try { + nameService.updateService(service); + } catch (NacosException e) { + NAMING_LOGGER.error(e.getErrMsg()); + } + } + + @Test + public void registerInstance() throws NacosException { + nameService.registerInstance("nacos-api", "127.0.0.1", 8009); + } +} diff --git a/update_git_info.sh b/update_git_info.sh new file mode 100755 index 000000000..4bc72c3de --- /dev/null +++ b/update_git_info.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +git filter-branch --env-filter ' + +OLD_EMAIL="liaochuntao@live.com" +CORRECT_NAME="chuntaojun" +CORRECT_EMAIL="liaochuntao@live.com" + +if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ] +then + export GIT_COMMITTER_NAME="$CORRECT_NAME" + export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL" +fi +if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ] +then + export GIT_AUTHOR_NAME="$CORRECT_NAME" + export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" +fi +' --tag-name-filter cat -- --branches --tags From 39b3820150c07309851f2315ff49f9f9f80b5c99 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sat, 27 Apr 2019 22:36:32 +0800 Subject: [PATCH 02/12] refactor(client): delete unused file --- update_git_info.sh | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100755 update_git_info.sh diff --git a/update_git_info.sh b/update_git_info.sh deleted file mode 100755 index 4bc72c3de..000000000 --- a/update_git_info.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh - -git filter-branch --env-filter ' - -OLD_EMAIL="liaochuntao@live.com" -CORRECT_NAME="chuntaojun" -CORRECT_EMAIL="liaochuntao@live.com" - -if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ] -then - export GIT_COMMITTER_NAME="$CORRECT_NAME" - export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL" -fi -if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ] -then - export GIT_AUTHOR_NAME="$CORRECT_NAME" - export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" -fi -' --tag-name-filter cat -- --branches --tags From f2c046a39571ccc6ca320d2df902f509024e4543 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Tue, 30 Apr 2019 17:46:10 +0800 Subject: [PATCH 03/12] refactor(client): Service operation function module split --- .../com/alibaba/nacos/api/NacosFactory.java | 24 ++ .../nacos/api/naming/MaintainFactory.java | 52 ++++ .../nacos/api/naming/MaintainService.java | 146 ++++++++++ .../nacos/api/naming/NamingService.java | 124 --------- .../client/naming/NacosMaintainService.java | 163 ++++++++++++ .../client/naming/NacosNamingService.java | 250 ++---------------- .../nacos/client/naming/net/NamingProxy.java | 15 ++ .../client/naming/utils/NamingUtils.java | 135 ++++++++++ .../naming/NacosMaintainServiceTest.java | 90 +++++++ .../client/naming/NacosNamingServiceTest.java | 38 +-- .../server/service/dump/DumpServiceTest.java | 24 ++ .../ephemeral/distro/TaskDispatcherTest.java | 1 + 12 files changed, 677 insertions(+), 385 deletions(-) create mode 100644 api/src/main/java/com/alibaba/nacos/api/naming/MaintainFactory.java create mode 100644 api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java create mode 100644 client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java create mode 100644 client/src/main/java/com/alibaba/nacos/client/naming/utils/NamingUtils.java create mode 100644 client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java create mode 100644 config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java diff --git a/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java b/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java index 6020468cd..165965e49 100644 --- a/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java @@ -20,6 +20,8 @@ import java.util.Properties; import com.alibaba.nacos.api.config.ConfigFactory; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.MaintainFactory; +import com.alibaba.nacos.api.naming.MaintainService; import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.api.naming.NamingService; @@ -74,4 +76,26 @@ public class NacosFactory { return NamingFactory.createNamingService(properties); } + /** + * Create maintain service + * + * @param serverAddr + * @return MaintainService + * @throws NacosException Exception + */ + public static MaintainService createMaintainService(String serverAddr) throws NacosException { + return MaintainFactory.createMaintainService(serverAddr); + } + + /** + * Create maintain service + * + * @param properties + * @return MaintainService + * @throws NacosException Exception + */ + public static MaintainService createMaintainService(Properties properties) throws NacosException { + return MaintainFactory.createMaintainService(properties); + } + } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainFactory.java b/api/src/main/java/com/alibaba/nacos/api/naming/MaintainFactory.java new file mode 100644 index 000000000..fdf634424 --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/naming/MaintainFactory.java @@ -0,0 +1,52 @@ +/* + * 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.api.naming; + +import com.alibaba.nacos.api.exception.NacosException; + +import java.lang.reflect.Constructor; +import java.util.Properties; + +/** + * @author liaochuntao + * @since 1.0.0 + */ +public class MaintainFactory { + + public static MaintainService createMaintainService(String serverList) throws NacosException { + try { + Class driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosMaintainService"); + Constructor constructor = driverImplClass.getConstructor(String.class); + MaintainService vendorImpl = (MaintainService)constructor.newInstance(serverList); + return vendorImpl; + } catch (Throwable e) { + throw new NacosException(-400, e.getMessage()); + } + } + + public static MaintainService createMaintainService(Properties properties) throws NacosException { + try { + Class driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosMaintainService"); + Constructor constructor = driverImplClass.getConstructor(Properties.class); + MaintainService vendorImpl = (MaintainService)constructor.newInstance(properties); + return vendorImpl; + } catch (Throwable e) { + throw new NacosException(-400, e.getMessage()); + } + } + +} diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java b/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java new file mode 100644 index 000000000..afa60986e --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java @@ -0,0 +1,146 @@ +/* + * 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.api.naming; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.pojo.Service; +import com.alibaba.nacos.api.selector.AbstractSelector; + +import java.util.Map; + +/** + * Operations related to Nacos + * + * @author liaochuntao + * @since 1.0.0 + */ +public interface MaintainService { + + /** + * + * @param serviceName + * @return + * @throws NacosException + */ + Service selectOneService(String serviceName) throws NacosException; + + /** + * + * @param serviceName + * @param groupName + * @return + * @throws NacosException + */ + Service selectOneService(String serviceName, String groupName) throws NacosException; + + /** + * create service to Nacos + * + * @param serviceName name of service + * @throws NacosException + */ + void createService(String serviceName) throws NacosException; + + /** + * create service to Nacos + * + * @param serviceName name of service + * @param groupName group of service + * @throws NacosException + */ + void createService(String serviceName, String groupName) throws NacosException; + + /** + * create service to Nacos + * + * @param serviceName name of service + * @param groupName group of service + * @param protectThreshold protectThreshold of service + * @throws NacosException + */ + void createService(String serviceName, String groupName, Float protectThreshold) throws NacosException; + + /** + * create service to Nacos + * + * @param serviceName name of service + * @param groupName group of service + * @param protectThreshold protectThreshold of service + * @param expression expression of selector + * @throws NacosException + */ + void createService(String serviceName, String groupName, Float protectThreshold, String expression) throws NacosException; + + /** + * create service to Nacos + * + * @param service name of service + * @param selector selector + * @throws NacosException + */ + void createService(Service service, AbstractSelector selector) throws NacosException; + + /** + * delete service from Nacos + * + * @param serviceName name of service + * @return if delete service success return true + * @throws NacosException + */ + boolean deleteService(String serviceName) throws NacosException; + + /** + * delete service from Nacos + * + * @param serviceName name of service + * @param groupName group of service + * @return if delete service success return true + * @throws NacosException + */ + boolean deleteService(String serviceName, String groupName) throws NacosException; + + /** + * update service to Nacos + * + * @param serviceName name of service + * @param groupName group of service + * @param protectThreshold protectThreshold of service + * @throws NacosException + */ + void updateService(String serviceName, String groupName, Float protectThreshold) throws NacosException; + + /** + * update service to Nacos + * + * @param serviceName name of service + * @param groupName group of service + * @param protectThreshold protectThreshold of service + * @param metadata metadata of service + * @throws NacosException + */ + void updateService(String serviceName, String groupName, Float protectThreshold, Map metadata) throws NacosException; + + /** + * update service to Nacos with selector + * + * @param service {@link Service} pojo of service + * @param selector {@link AbstractSelector} pojo of selector + * @throws NacosException + */ + void updateService(Service service, AbstractSelector selector) throws NacosException; + +} diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java index 1a30bbe3a..e82c7202d 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java @@ -410,130 +410,6 @@ public interface NamingService { */ Instance selectOneHealthyInstance(String serviceName, String groupName, List clusters, boolean subscribe) throws NacosException; - /** - * create service to Nacos - * - * @param serviceName name of service - * @throws NacosException - */ - void createService(String serviceName) throws NacosException; - - /** - * create service to Nacos - * - * @param serviceName name of service - * @param groupName group of service - * @throws NacosException - */ - void createService(String serviceName, String groupName) throws NacosException; - - /** - * create service to Nacos - * - * @param serviceName name of service - * @param groupName group of service - * @param protectThreshold protectThreshold of service - * @throws NacosException - */ - void createService(String serviceName, String groupName, Float protectThreshold) throws NacosException; - - /** - * create service to Nacos - * - * @param serviceName name of service - * @param groupName group of service - * @param protectThreshold protectThreshold of service - * @param expression expression of selector - * @throws NacosException - */ - void createService(String serviceName, String groupName, Float protectThreshold, String expression) throws NacosException; - - /** - * create service to Nacos - * - * @param service name of service - * @param selector selector - * @throws NacosException - */ - void createService(Service service, AbstractSelector selector) throws NacosException; - - /** - * delete service from Nacos - * - * @param serviceName name of service - * @return if delete service success return true - * @throws NacosException - */ - boolean deleteService(String serviceName) throws NacosException; - - /** - * delete service from Nacos - * - * @param serviceName name of service - * @param groupName group of service - * @return if delete service success return true - * @throws NacosException - */ - boolean deleteService(String serviceName, String groupName) throws NacosException; - - /** - * update service to Nacos - * - * @param serviceName name of service - * @param groupName group of service - * @throws NacosException - */ - void updateService(String serviceName, String groupName) throws NacosException; - - /** - * update service to Nacos - * - * @param serviceName name of service - * @param groupName group of service - * @param protectThreshold protectThreshold of service - * @throws NacosException - */ - void updateService(String serviceName, String groupName, Float protectThreshold) throws NacosException; - - /** - * update service to Nacos with with selector's expression - * - * @param serviceName name of service - * @param groupName group of service - * @param protectThreshold protectThreshold of service - * @param expression expression of selector - * @throws NacosException - */ - void updateService(String serviceName, String groupName, Float protectThreshold, String expression) throws NacosException; - - /** - * update service to Nacos - * - * @param serviceName name of service - * @param groupName group of service - * @param protectThreshold protectThreshold of service - * @param metadata metadata of service - * @throws NacosException - */ - void updateService(String serviceName, String groupName, Float protectThreshold, Map metadata) throws NacosException; - - /** - * update service to Nacos - * - * @param service {@link Service} pojo of service - * @throws NacosException - */ - void updateService(Service service) throws NacosException; - - /** - * update service to Nacos with selector - * - * @param service {@link Service} pojo of service - * @param selector {@link AbstractSelector} pojo of selector - * @throws NacosException - */ - void updateService(Service service, AbstractSelector selector) throws NacosException; - /** * Subscribe service to receive events of instances alteration * diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java new file mode 100644 index 000000000..3fccf6e94 --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java @@ -0,0 +1,163 @@ +/* + * 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.naming; + +import com.alibaba.nacos.api.PropertyKeyConst; +import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.MaintainService; +import com.alibaba.nacos.api.naming.pojo.Service; +import com.alibaba.nacos.api.selector.AbstractSelector; +import com.alibaba.nacos.api.selector.ExpressionSelector; +import com.alibaba.nacos.api.selector.NoneSelector; +import com.alibaba.nacos.client.naming.net.NamingProxy; +import com.alibaba.nacos.client.naming.utils.NamingUtils; +import com.alibaba.nacos.client.utils.StringUtils; + +import java.util.Map; +import java.util.Properties; + +/** + * @author liaochuntao + * @since 1.0.0 + */ +@SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") +public class NacosMaintainService implements MaintainService { + + private String namespace; + + private String endpoint; + + private String serverList; + + private NamingProxy serverProxy; + + public NacosMaintainService(String serverList) { + Properties properties = new Properties(); + properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList); + + init(properties); + } + + public NacosMaintainService(Properties properties) { + + init(properties); + } + + private void init(Properties properties) { + namespace = NamingUtils.initNamespace(properties); + initServerAddr(properties); + NamingUtils.initWebRootContext(); + + serverProxy = new NamingProxy(namespace, endpoint, serverList); + serverProxy.setProperties(properties); + } + + private void initServerAddr(Properties properties) { + serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR); + endpoint = NamingUtils.initEndpoint(properties); + if (StringUtils.isNotEmpty(endpoint)) { + serverList = ""; + } + } + + @Override + public Service selectOneService(String serviceName) throws NacosException { + return selectOneService(serviceName, Constants.DEFAULT_GROUP); + } + + @Override + public Service selectOneService(String serviceName, String groupName) throws NacosException { + return serverProxy.queryService(serviceName, groupName); + } + + @Override + public void createService(String serviceName) throws NacosException { + createService(serviceName, Constants.DEFAULT_GROUP); + } + + @Override + public void createService(String serviceName, String groupName) throws NacosException { + createService(serviceName, groupName, Constants.PROTECT_THRESHOLD); + } + + @Override + public void createService(String serviceName, String groupName, Float protectThreshold) throws NacosException { + NoneSelector selector = new NoneSelector(); + Service service = new Service(); + service.setName(serviceName); + service.setGroupName(groupName); + service.setProtectThreshold(protectThreshold); + + createService(service, selector); + } + + @Override + public void createService(String serviceName, String groupName, Float protectThreshold, String expression) throws NacosException { + Service service = new Service(); + service.setName(serviceName); + service.setGroupName(groupName); + service.setProtectThreshold(protectThreshold); + + ExpressionSelector selector = new ExpressionSelector(); + selector.setExpression(expression); + + createService(service, selector); + } + + @Override + public void createService(Service service, AbstractSelector selector) throws NacosException { + serverProxy.createService(service, selector); + } + + @Override + public boolean deleteService(String serviceName) throws NacosException { + return deleteService(serviceName, Constants.DEFAULT_GROUP); + } + + @Override + public boolean deleteService(String serviceName, String groupName) throws NacosException { + return serverProxy.deleteService(serviceName, groupName); + } + + @Override + public void updateService(String serviceName, String groupName, Float protectThreshold) throws NacosException { + Service service = new Service(); + service.setName(serviceName); + service.setGroupName(groupName); + service.setProtectThreshold(protectThreshold); + + updateService(service, new NoneSelector()); + } + + @Override + public void updateService(String serviceName, String groupName, Float protectThreshold, Map metadata) throws NacosException { + Service service = new Service(); + service.setName(serviceName); + service.setGroupName(groupName); + service.setProtectThreshold(protectThreshold); + service.setMetadata(metadata); + + updateService(service, new NoneSelector()); + } + + @Override + public void updateService(Service service, AbstractSelector selector) throws NacosException { + serverProxy.updateService(service, selector); + } + +} diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index a84da05fc..78e32fde4 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -16,20 +16,14 @@ package com.alibaba.nacos.client.naming; import com.alibaba.nacos.api.PropertyKeyConst; -import com.alibaba.nacos.api.SystemPropertyKeyConst; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.listener.EventListener; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; -import com.alibaba.nacos.api.naming.pojo.Service; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; -import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.api.selector.AbstractSelector; -import com.alibaba.nacos.api.selector.ExpressionSelector; -import com.alibaba.nacos.api.selector.NoneSelector; -import com.alibaba.nacos.client.identify.CredentialService; import com.alibaba.nacos.client.naming.beat.BeatInfo; import com.alibaba.nacos.client.naming.beat.BeatReactor; import com.alibaba.nacos.client.naming.core.Balancer; @@ -37,16 +31,13 @@ import com.alibaba.nacos.client.naming.core.EventDispatcher; import com.alibaba.nacos.client.naming.core.HostReactor; import com.alibaba.nacos.client.naming.net.NamingProxy; import com.alibaba.nacos.client.naming.utils.CollectionUtils; +import com.alibaba.nacos.client.naming.utils.NamingUtils; import com.alibaba.nacos.client.naming.utils.UtilAndComs; -import com.alibaba.nacos.client.utils.LogUtils; -import com.alibaba.nacos.client.utils.ParamUtil; -import com.alibaba.nacos.client.utils.StringUtils; -import com.alibaba.nacos.client.utils.TemplateUtils; +import com.alibaba.nacos.client.utils.*; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.math.NumberUtils; import java.util.*; -import java.util.concurrent.Callable; /** * @author nkorange @@ -87,10 +78,9 @@ public class NacosNamingService implements NamingService { } private void init(Properties properties) { - serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR); - initNamespace(properties); - initEndpoint(properties); - initWebRootContext(); + namespace = NamingUtils.initNamespace(properties); + initServerAddr(properties); + NamingUtils.initWebRootContext(); initCacheDir(); initLogName(properties); @@ -130,6 +120,14 @@ public class NacosNamingService implements NamingService { return loadCacheAtStart; } + private void initServerAddr(Properties properties) { + serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR); + endpoint = NamingUtils.initEndpoint(properties); + if (StringUtils.isNotEmpty(endpoint)) { + serverList = ""; + } + } + private void initLogName(Properties properties) { logName = System.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME); if (StringUtils.isEmpty(logName)) { @@ -149,104 +147,6 @@ public class NacosNamingService implements NamingService { } } - private void initEndpoint(final Properties properties) { - if (properties == null) { - - return; - } - //这里通过 dubbo/sca 侧来初始化默认传入的是 true - boolean isUseEndpointParsingRule = Boolean.valueOf(properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE, ParamUtil.USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE)); - String endpointUrl; - if (isUseEndpointParsingRule) { - endpointUrl = ParamUtil.parsingEndpointRule(properties.getProperty(PropertyKeyConst.ENDPOINT)); - if (com.alibaba.nacos.client.utils.StringUtils.isNotBlank(endpointUrl)) { - serverList = ""; - } - } else { - endpointUrl = properties.getProperty(PropertyKeyConst.ENDPOINT); - } - - if (StringUtils.isBlank(endpointUrl)) { - return; - } - - String endpointPort = TemplateUtils.stringEmptyAndThenExecute(System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT), new Callable() { - @Override - public String call() { - - return properties.getProperty(PropertyKeyConst.ENDPOINT_PORT); - } - }); - - endpointPort = TemplateUtils.stringEmptyAndThenExecute(endpointPort, new Callable() { - @Override - public String call() { - return "8080"; - } - }); - - endpoint = endpointUrl + ":" + endpointPort; - } - - private void initNamespace(Properties properties) { - String tmpNamespace = null; - - tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable() { - @Override - public String call() { - String namespace = System.getProperty(PropertyKeyConst.NAMESPACE); - LogUtils.NAMING_LOGGER.info("initializer namespace from System Property :" + namespace); - return namespace; - } - }); - - - tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable() { - @Override - public String call() { - String namespace = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE); - LogUtils.NAMING_LOGGER.info("initializer namespace from System Environment :" + namespace); - return namespace; - } - }); - - tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable() { - @Override - public String call() { - String namespace = CredentialService.getInstance().getCredential().getTenantId(); - LogUtils.NAMING_LOGGER.info("initializer namespace from Credential Module " + namespace); - return namespace; - } - }); - - if (StringUtils.isEmpty(tmpNamespace) && properties != null) { - tmpNamespace = properties.getProperty(PropertyKeyConst.NAMESPACE); - } - - tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable() { - @Override - public String call() { - return UtilAndComs.DEFAULT_NAMESPACE_ID; - } - }); - namespace = tmpNamespace; - } - - private void initWebRootContext() { - // support the web context with ali-yun if the app deploy by EDAS - final String webContext = System.getProperty(SystemPropertyKeyConst.NAMING_WEB_CONTEXT); - TemplateUtils.stringNotEmptyAndThenExecute(webContext, new Runnable() { - @Override - public void run() { - UtilAndComs.WEB_CONTEXT = webContext.indexOf("/") > -1 ? webContext - : "/" + webContext; - - UtilAndComs.NACOS_URL_BASE = UtilAndComs.WEB_CONTEXT + "/v1/ns"; - UtilAndComs.NACOS_URL_INSTANCE = UtilAndComs.NACOS_URL_BASE + "/instance"; - } - }); - } - @Override public void registerInstance(String serviceName, String ip, int port) throws NacosException { registerInstance(serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME); @@ -284,7 +184,7 @@ public class NacosNamingService implements NamingService { if (instance.isEphemeral()) { BeatInfo beatInfo = new BeatInfo(); - beatInfo.setServiceName(NamingUtils.getGroupedName(serviceName, groupName)); + beatInfo.setServiceName(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName)); beatInfo.setIp(instance.getIp()); beatInfo.setPort(instance.getPort()); beatInfo.setCluster(instance.getClusterName()); @@ -292,10 +192,10 @@ public class NacosNamingService implements NamingService { beatInfo.setMetadata(instance.getMetadata()); beatInfo.setScheduled(false); - beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo); + beatReactor.addBeatInfo(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), beatInfo); } - serverProxy.registerService(NamingUtils.getGroupedName(serviceName, groupName), groupName, instance); + serverProxy.registerService(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), groupName, instance); } @Override @@ -325,8 +225,8 @@ public class NacosNamingService implements NamingService { @Override public void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException { - beatReactor.removeBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), instance.getIp(), instance.getPort()); - serverProxy.deregisterService(NamingUtils.getGroupedName(serviceName, groupName), instance); + beatReactor.removeBeatInfo(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), instance.getIp(), instance.getPort()); + serverProxy.deregisterService(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), instance); } @Override @@ -370,9 +270,9 @@ public class NacosNamingService implements NamingService { ServiceInfo serviceInfo; if (subscribe) { - serviceInfo = hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); + serviceInfo = hostReactor.getServiceInfo(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); } else { - serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); + serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); } List list; if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { @@ -424,9 +324,9 @@ public class NacosNamingService implements NamingService { ServiceInfo serviceInfo; if (subscribe) { - serviceInfo = hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); + serviceInfo = hostReactor.getServiceInfo(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); } else { - serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); + serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); } return selectInstances(serviceInfo, healthy); } @@ -472,111 +372,13 @@ public class NacosNamingService implements NamingService { if (subscribe) { return Balancer.RandomByWeight.selectHost( - hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","))); + hostReactor.getServiceInfo(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","))); } else { return Balancer.RandomByWeight.selectHost( - hostReactor.getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","))); + hostReactor.getServiceInfoDirectlyFromServer(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","))); } } - @Override - public void createService(String serviceName) throws NacosException { - createService(serviceName, Constants.DEFAULT_GROUP); - } - - @Override - public void createService(String serviceName, String groupName) throws NacosException { - createService(serviceName, groupName, Constants.PROTECT_THRESHOLD); - } - - @Override - public void createService(String serviceName, String groupName, Float protectThreshold) throws NacosException { - NoneSelector selector = new NoneSelector(); - Service service = new Service(); - service.setName(serviceName); - service.setGroupName(groupName); - service.setProtectThreshold(protectThreshold); - - createService(service, selector); - } - - @Override - public void createService(String serviceName, String groupName, Float protectThreshold, String expression) throws NacosException { - Service service = new Service(); - service.setName(serviceName); - service.setGroupName(groupName); - service.setProtectThreshold(protectThreshold); - - ExpressionSelector selector = new ExpressionSelector(); - selector.setExpression(expression); - - createService(service, selector); - } - - @Override - public void createService(Service service, AbstractSelector selector) throws NacosException { - serverProxy.createService(service, selector); - } - - @Override - public boolean deleteService(String serviceName) throws NacosException { - return deleteService(serviceName, Constants.DEFAULT_GROUP); - } - - @Override - public boolean deleteService(String serviceName, String groupName) throws NacosException { - return serverProxy.deleteService(serviceName, groupName); - } - - @Override - public void updateService(String serviceName, String groupName) throws NacosException { - updateService(serviceName, groupName, Constants.PROTECT_THRESHOLD); - } - - @Override - public void updateService(String serviceName, String groupName, Float protectThreshold) throws NacosException { - Service service = new Service(); - service.setName(serviceName); - service.setGroupName(groupName); - service.setProtectThreshold(protectThreshold); - - updateService(service, new NoneSelector()); - } - - @Override - public void updateService(String serviceName, String groupName, Float protectThreshold, String expression) throws NacosException { - Service service = new Service(); - service.setName(serviceName); - service.setGroupName(groupName); - service.setProtectThreshold(protectThreshold); - - ExpressionSelector selector = new ExpressionSelector(); - selector.setExpression(expression); - - updateService(service, selector); - } - - @Override - public void updateService(String serviceName, String groupName, Float protectThreshold, Map metadata) throws NacosException { - Service service = new Service(); - service.setName(serviceName); - service.setGroupName(groupName); - service.setProtectThreshold(protectThreshold); - service.setMetadata(metadata); - - updateService(service, new NoneSelector()); - } - - @Override - public void updateService(Service service) throws NacosException { - updateService(service, new NoneSelector()); - } - - @Override - public void updateService(Service service, AbstractSelector selector) throws NacosException { - serverProxy.updateService(service, selector); - } - @Override public void subscribe(String serviceName, EventListener listener) throws NacosException { subscribe(serviceName, new ArrayList(), listener); @@ -594,7 +396,7 @@ public class NacosNamingService implements NamingService { @Override public void subscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException { - eventDispatcher.addListener(hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), + eventDispatcher.addListener(hostReactor.getServiceInfo(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")), StringUtils.join(clusters, ","), listener); } @@ -615,7 +417,7 @@ public class NacosNamingService implements NamingService { @Override public void unsubscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException { - eventDispatcher.removeListener(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","), listener); + eventDispatcher.removeListener(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","), listener); } @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index f60732936..0fc425ea2 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -105,6 +105,7 @@ public class NamingProxy { } }, 0, vipSrvRefInterMillis, TimeUnit.MILLISECONDS); + // 这个方法的用处 refreshSrvIfNeed(); } @@ -204,6 +205,20 @@ public class NamingProxy { reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.DELETE); } + public Service queryService(String serviceName, String groupName) throws NacosException { + NAMING_LOGGER.info("[QUERY-SERVICE] {} query service : {}, {}", + namespaceId, serviceName, groupName); + + final Map params = new HashMap(3); + params.put(CommonParams.NAMESPACE_ID, namespaceId); + params.put(CommonParams.SERVICE_NAME, serviceName); + params.put(CommonParams.GROUP_NAME, groupName); + + String result = reqAPI(UtilAndComs.NACOS_URL_SERVICE, params, HttpMethod.GET); + JSONObject jsonObject = JSON.parseObject(result); + return jsonObject.toJavaObject(Service.class); + } + public void createService(Service service, AbstractSelector selector) throws NacosException { NAMING_LOGGER.info("[CREATE-SERVICE] {} creating service : {}", diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/NamingUtils.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/NamingUtils.java new file mode 100644 index 000000000..d6f522bb3 --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/NamingUtils.java @@ -0,0 +1,135 @@ +/* + * 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.naming.utils; + +import com.alibaba.nacos.api.PropertyKeyConst; +import com.alibaba.nacos.api.SystemPropertyKeyConst; +import com.alibaba.nacos.client.identify.CredentialService; +import com.alibaba.nacos.client.naming.utils.UtilAndComs; +import com.alibaba.nacos.client.utils.LogUtils; +import com.alibaba.nacos.client.utils.ParamUtil; +import com.alibaba.nacos.client.utils.StringUtils; +import com.alibaba.nacos.client.utils.TemplateUtils; + +import java.util.Properties; +import java.util.concurrent.Callable; + +/** + * @author liaochuntao + */ +public class NamingUtils { + + public static final String initNamespace(Properties properties) { + String tmpNamespace = null; + + tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable() { + @Override + public String call() { + String namespace = System.getProperty(PropertyKeyConst.NAMESPACE); + LogUtils.NAMING_LOGGER.info("initializer namespace from System Property :" + namespace); + return namespace; + } + }); + + + tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable() { + @Override + public String call() { + String namespace = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE); + LogUtils.NAMING_LOGGER.info("initializer namespace from System Environment :" + namespace); + return namespace; + } + }); + + tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable() { + @Override + public String call() { + String namespace = CredentialService.getInstance().getCredential().getTenantId(); + LogUtils.NAMING_LOGGER.info("initializer namespace from Credential Module " + namespace); + return namespace; + } + }); + + if (com.alibaba.nacos.client.utils.StringUtils.isEmpty(tmpNamespace) && properties != null) { + tmpNamespace = properties.getProperty(PropertyKeyConst.NAMESPACE); + } + + tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, new Callable() { + @Override + public String call() { + return UtilAndComs.DEFAULT_NAMESPACE_ID; + } + }); + return tmpNamespace; + } + + public static final void initWebRootContext() { + // support the web context with ali-yun if the app deploy by EDAS + final String webContext = System.getProperty(SystemPropertyKeyConst.NAMING_WEB_CONTEXT); + TemplateUtils.stringNotEmptyAndThenExecute(webContext, new Runnable() { + @Override + public void run() { + UtilAndComs.WEB_CONTEXT = webContext.indexOf("/") > -1 ? webContext + : "/" + webContext; + + UtilAndComs.NACOS_URL_BASE = UtilAndComs.WEB_CONTEXT + "/v1/ns"; + UtilAndComs.NACOS_URL_INSTANCE = UtilAndComs.NACOS_URL_BASE + "/instance"; + } + }); + } + + public static final String initEndpoint(final Properties properties) { + if (properties == null) { + + return ""; + } + // 是否开启域名解析规则 + boolean isUseEndpointParsingRule = Boolean.valueOf(properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE, ParamUtil.USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE)); + String endpointUrl; + if (isUseEndpointParsingRule) { + // 获取设置的域名信息 + endpointUrl = ParamUtil.parsingEndpointRule(properties.getProperty(PropertyKeyConst.ENDPOINT)); + if (com.alibaba.nacos.client.utils.StringUtils.isNotBlank(endpointUrl)) { + return ""; + } + } else { + endpointUrl = properties.getProperty(PropertyKeyConst.ENDPOINT); + } + + if (StringUtils.isBlank(endpointUrl)) { + return ""; + } + + String endpointPort = TemplateUtils.stringEmptyAndThenExecute(System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT), new Callable() { + @Override + public String call() { + + return properties.getProperty(PropertyKeyConst.ENDPOINT_PORT); + } + }); + + endpointPort = TemplateUtils.stringEmptyAndThenExecute(endpointPort, new Callable() { + @Override + public String call() { + return "8080"; + } + }); + + return endpointUrl + ":" + endpointPort; + } + +} diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java new file mode 100644 index 000000000..8579c4847 --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java @@ -0,0 +1,90 @@ +package com.alibaba.nacos.client.naming; + +import com.alibaba.nacos.api.NacosFactory; +import com.alibaba.nacos.api.PropertyKeyConst; +import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.MaintainService; +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.pojo.Service; +import com.alibaba.nacos.api.selector.ExpressionSelector; +import com.alibaba.nacos.api.selector.NoneSelector; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; +import static org.junit.Assert.*; + +public class NacosMaintainServiceTest { + + private MaintainService maintainService; + + @Before + public void before() throws NacosException { + Properties properties = new Properties(); + properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848"); + + maintainService = NacosFactory.createMaintainService(properties); + } + + @Test + public void selectOneService() { + try { + Service service = maintainService.selectOneService("nacos-api"); + NAMING_LOGGER.info("service : {}", service.toString()); + } catch (NacosException e) { + NAMING_LOGGER.error(e.getErrMsg()); + } + } + + @Test + public void createService() { + Service service = new Service(); + service.setName("nacos-api"); + service.setGroupName(Constants.DEFAULT_GROUP); + service.setProtectThreshold(1.0f); + Map metadata = new HashMap(); + metadata.put("nacos-1", "this is a test metadata"); + service.setMetadata(metadata); + + ExpressionSelector selector = new ExpressionSelector(); + selector.setExpression("CONSUMER.label.A=PROVIDER.label.A &CONSUMER.label.B=PROVIDER.label.B"); + + try { + maintainService.createService(service, new NoneSelector()); + } catch (NacosException e) { + NAMING_LOGGER.error(e.getErrMsg()); + } + } + + @Test + public void deleteService() { + try { + Assert.assertTrue(maintainService.deleteService("nacos-api")); + } catch (NacosException e) { + NAMING_LOGGER.error(e.getErrMsg()); + } + } + + @Test + public void updateService() { + Service service = new Service(); + service.setName("nacos-api"); + service.setGroupName(Constants.DEFAULT_GROUP); + service.setProtectThreshold(1.0f); + Map metadata = new HashMap(); + metadata.put("nacos-1", "nacos-3-update"); + service.setMetadata(metadata); + + try { + maintainService.updateService(service, new NoneSelector()); + } catch (NacosException e) { + NAMING_LOGGER.error(e.getErrMsg()); + } + } +} diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java index e384c1089..7eb555168 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java @@ -31,50 +31,14 @@ public class NacosNamingServiceTest { nameService = NacosFactory.createNamingService(properties); } - @Test - public void createService() { - Service service = new Service(); - service.setName("nacos-api"); - service.setGroupName(Constants.DEFAULT_GROUP); - service.setProtectThreshold(1.0f); - Map metadata = new HashMap(); - metadata.put("nacos-1", "this is a test metadata"); - service.setMetadata(metadata); - - ExpressionSelector selector = new ExpressionSelector(); - selector.setExpression("CONSUMER.label.A=PROVIDER.label.A &CONSUMER.label.B=PROVIDER.label.B"); - - try { - nameService.createService(service, new NoneSelector()); - } catch (NacosException e) { - NAMING_LOGGER.error(e.getErrMsg()); - } - } - @Test public void deleteService() { - try { - Assert.assertTrue(nameService.deleteService("nacos-api")); - } catch (NacosException e) { - NAMING_LOGGER.error(e.getErrMsg()); - } + } @Test public void updateService() { - Service service = new Service(); - service.setName("nacos-api"); - service.setGroupName(Constants.DEFAULT_GROUP); - service.setProtectThreshold(1.0f); - Map metadata = new HashMap(); - metadata.put("nacos-1", "nacos-3-update"); - service.setMetadata(metadata); - try { - nameService.updateService(service); - } catch (NacosException e) { - NAMING_LOGGER.error(e.getErrMsg()); - } } @Test diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java new file mode 100644 index 000000000..43b35a1fd --- /dev/null +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java @@ -0,0 +1,24 @@ +package com.alibaba.nacos.config.server.service.dump; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +import static org.junit.Assert.*; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest +@WebAppConfiguration +public class DumpServiceTest { + + @Autowired + DumpService service; + + @Test + public void init() { + service.init(); + } +} diff --git a/naming/src/test/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/TaskDispatcherTest.java b/naming/src/test/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/TaskDispatcherTest.java index 6a67f9430..1bd2a52ae 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/TaskDispatcherTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/TaskDispatcherTest.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.naming.consistency.ephemeral.distro; import com.alibaba.nacos.naming.misc.GlobalConfig; +import com.alibaba.nacos.naming.misc.Loggers; import org.junit.Before; import org.junit.Test; import org.springframework.test.util.ReflectionTestUtils; From 1b76780cd10faab101e7307548a23f10674fa8c9 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Tue, 30 Apr 2019 19:08:16 +0800 Subject: [PATCH 04/12] refactor(naming): Modify the inheritance of NoneSelector class --- .../com/alibaba/nacos/naming/selector/NoneSelector.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/selector/NoneSelector.java b/naming/src/main/java/com/alibaba/nacos/naming/selector/NoneSelector.java index b2b6b42d2..38bab06a1 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/selector/NoneSelector.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/selector/NoneSelector.java @@ -15,8 +15,6 @@ */ package com.alibaba.nacos.naming.selector; -import com.alibaba.nacos.api.selector.AbstractSelector; -import com.alibaba.nacos.api.selector.SelectorType; import com.alibaba.nacos.naming.core.Instance; import java.util.List; @@ -27,11 +25,7 @@ import java.util.List; * @author nkorange * @since 0.7.0 */ -public class NoneSelector extends AbstractSelector implements Selector { - - public NoneSelector() { - this.setType(SelectorType.none.name()); - } +public class NoneSelector extends com.alibaba.nacos.api.selector.NoneSelector implements Selector { @Override public List select(String consumer, List providers) { From 320aca4d90b6a937b11986c7aaff814541934b38 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Tue, 30 Apr 2019 19:27:06 +0800 Subject: [PATCH 05/12] style(api): The revised code is in compliance with the specification --- .../nacos/api/naming/MaintainService.java | 2 + .../naming/NacosMaintainServiceTest.java | 46 ++++++++++--------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java b/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java index afa60986e..56bcebcc4 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java @@ -31,6 +31,7 @@ import java.util.Map; public interface MaintainService { /** + * query service * * @param serviceName * @return @@ -39,6 +40,7 @@ public interface MaintainService { Service selectOneService(String serviceName) throws NacosException; /** + * query service * * @param serviceName * @param groupName diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java index 8579c4847..6125c5c53 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java @@ -11,7 +11,9 @@ import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.NoneSelector; import org.junit.Assert; import org.junit.Before; +import org.junit.FixMethodOrder; import org.junit.Test; +import org.junit.runners.MethodSorters; import java.util.HashMap; import java.util.Map; @@ -20,6 +22,7 @@ import java.util.Properties; import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; import static org.junit.Assert.*; +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class NacosMaintainServiceTest { private MaintainService maintainService; @@ -33,17 +36,7 @@ public class NacosMaintainServiceTest { } @Test - public void selectOneService() { - try { - Service service = maintainService.selectOneService("nacos-api"); - NAMING_LOGGER.info("service : {}", service.toString()); - } catch (NacosException e) { - NAMING_LOGGER.error(e.getErrMsg()); - } - } - - @Test - public void createService() { + public void test1createService() { Service service = new Service(); service.setName("nacos-api"); service.setGroupName(Constants.DEFAULT_GROUP); @@ -63,16 +56,7 @@ public class NacosMaintainServiceTest { } @Test - public void deleteService() { - try { - Assert.assertTrue(maintainService.deleteService("nacos-api")); - } catch (NacosException e) { - NAMING_LOGGER.error(e.getErrMsg()); - } - } - - @Test - public void updateService() { + public void test2updateService() { Service service = new Service(); service.setName("nacos-api"); service.setGroupName(Constants.DEFAULT_GROUP); @@ -87,4 +71,24 @@ public class NacosMaintainServiceTest { NAMING_LOGGER.error(e.getErrMsg()); } } + + @Test + public void test3selectOneService() { + try { + Service service = maintainService.selectOneService("nacos-api"); + System.out.println("service : " + service.toString()); + } catch (NacosException e) { + NAMING_LOGGER.error(e.getErrMsg()); + } + } + + @Test + public void test4deleteService() { + try { + Assert.assertTrue(maintainService.deleteService("nacos-api")); + } catch (NacosException e) { + NAMING_LOGGER.error(e.getErrMsg()); + } + } + } From f8ec7820807e4a570f6acb11bd6c9e9c47831e00 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sun, 5 May 2019 10:12:30 +0800 Subject: [PATCH 06/12] refactor(client): Delete Chinese comments --- .../java/com/alibaba/nacos/client/naming/net/NamingProxy.java | 1 - 1 file changed, 1 deletion(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index 0fc425ea2..4d8ae4b91 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -105,7 +105,6 @@ public class NamingProxy { } }, 0, vipSrvRefInterMillis, TimeUnit.MILLISECONDS); - // 这个方法的用处 refreshSrvIfNeed(); } From 4817b26bc903a3ca4c77c91b4e07c0903263c34f Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Tue, 7 May 2019 11:11:24 +0800 Subject: [PATCH 07/12] refactor(client): Fix the issues raised in the comments --- .../alibaba/nacos/api/common/Constants.java | 2 +- .../nacos/api/naming/MaintainFactory.java | 2 +- .../nacos/api/naming/MaintainService.java | 30 +++- .../nacos/api/naming/pojo/Service.java | 11 ++ .../client/naming/NacosMaintainService.java | 33 ++-- .../client/naming/NacosNamingService.java | 35 +++-- .../nacos/client/naming/net/NamingProxy.java | 20 ++- .../{NamingUtils.java => InitUtils.java} | 2 +- .../naming/NacosMaintainServiceTest.java | 3 +- .../config/server/constant/Constants.java | 2 +- .../test/naming/MaintainService_ITCase.java | 147 ++++++++++++++++++ 11 files changed, 248 insertions(+), 39 deletions(-) rename client/src/main/java/com/alibaba/nacos/client/naming/utils/{NamingUtils.java => InitUtils.java} (99%) create mode 100644 test/src/test/java/com/alibaba/nacos/test/naming/MaintainService_ITCase.java diff --git a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java index 842416a5b..3ec47eda8 100644 --- a/api/src/main/java/com/alibaba/nacos/api/common/Constants.java +++ b/api/src/main/java/com/alibaba/nacos/api/common/Constants.java @@ -103,7 +103,7 @@ public class Constants { public static final int FLOW_CONTROL_INTERVAL = 1000; - public static final float PROTECT_THRESHOLD = 0.0F; + public static final float DEFAULT_PROTECT_THRESHOLD = 0.0F; public static final String LINE_SEPARATOR = Character.toString((char)1); diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainFactory.java b/api/src/main/java/com/alibaba/nacos/api/naming/MaintainFactory.java index fdf634424..423814646 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/MaintainFactory.java @@ -23,7 +23,7 @@ import java.util.Properties; /** * @author liaochuntao - * @since 1.0.0 + * @since 1.0.1 */ public class MaintainFactory { diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java b/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java index 56bcebcc4..8e9ae428a 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.api.naming; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.Service; import com.alibaba.nacos.api.selector.AbstractSelector; @@ -26,10 +27,29 @@ import java.util.Map; * Operations related to Nacos * * @author liaochuntao - * @since 1.0.0 + * @since 1.0.1 */ public interface MaintainService { + /** + * update instance info + * + * @param serviceName + * @param instance + * @throws NacosException + */ + void updateInstance(String serviceName, Instance instance) throws NacosException; + + /** + * update instance info + * + * @param serviceName + * @param groupName + * @param instance + * @throws NacosException + */ + void updateInstance(String serviceName, String groupName, Instance instance) throws NacosException; + /** * query service * @@ -37,7 +57,7 @@ public interface MaintainService { * @return * @throws NacosException */ - Service selectOneService(String serviceName) throws NacosException; + Service queryService(String serviceName) throws NacosException; /** * query service @@ -47,7 +67,7 @@ public interface MaintainService { * @return * @throws NacosException */ - Service selectOneService(String serviceName, String groupName) throws NacosException; + Service queryService(String serviceName, String groupName) throws NacosException; /** * create service to Nacos @@ -123,7 +143,7 @@ public interface MaintainService { * @param protectThreshold protectThreshold of service * @throws NacosException */ - void updateService(String serviceName, String groupName, Float protectThreshold) throws NacosException; + void updateService(String serviceName, String groupName, float protectThreshold) throws NacosException; /** * update service to Nacos @@ -134,7 +154,7 @@ public interface MaintainService { * @param metadata metadata of service * @throws NacosException */ - void updateService(String serviceName, String groupName, Float protectThreshold, Map metadata) throws NacosException; + void updateService(String serviceName, String groupName, float protectThreshold, Map metadata) throws NacosException; /** * update service to Nacos with selector diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java index e4b88fd21..7e8fe2d34 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Service.java @@ -101,4 +101,15 @@ public class Service { public void addMetadata(String key, String value) { this.metadata.put(key, value); } + + @Override + public String toString() { + return "Service{" + + "name='" + name + '\'' + + ", protectThreshold=" + protectThreshold + + ", appName='" + appName + '\'' + + ", groupName='" + groupName + '\'' + + ", metadata=" + metadata + + '}'; + } } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java index 3fccf6e94..a902537f4 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java @@ -20,12 +20,13 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.MaintainService; +import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.Service; import com.alibaba.nacos.api.selector.AbstractSelector; import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.NoneSelector; import com.alibaba.nacos.client.naming.net.NamingProxy; -import com.alibaba.nacos.client.naming.utils.NamingUtils; +import com.alibaba.nacos.client.naming.utils.InitUtils; import com.alibaba.nacos.client.utils.StringUtils; import java.util.Map; @@ -33,7 +34,7 @@ import java.util.Properties; /** * @author liaochuntao - * @since 1.0.0 + * @since 1.0.1 */ @SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") public class NacosMaintainService implements MaintainService { @@ -59,9 +60,9 @@ public class NacosMaintainService implements MaintainService { } private void init(Properties properties) { - namespace = NamingUtils.initNamespace(properties); + namespace = InitUtils.initNamespace(properties); initServerAddr(properties); - NamingUtils.initWebRootContext(); + InitUtils.initWebRootContext(); serverProxy = new NamingProxy(namespace, endpoint, serverList); serverProxy.setProperties(properties); @@ -69,19 +70,29 @@ public class NacosMaintainService implements MaintainService { private void initServerAddr(Properties properties) { serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR); - endpoint = NamingUtils.initEndpoint(properties); + endpoint = InitUtils.initEndpoint(properties); if (StringUtils.isNotEmpty(endpoint)) { serverList = ""; } } @Override - public Service selectOneService(String serviceName) throws NacosException { - return selectOneService(serviceName, Constants.DEFAULT_GROUP); + public void updateInstance(String serviceName, Instance instance) throws NacosException { + updateInstance(serviceName, Constants.DEFAULT_GROUP, instance); } @Override - public Service selectOneService(String serviceName, String groupName) throws NacosException { + public void updateInstance(String serviceName, String groupName, Instance instance) throws NacosException { + serverProxy.updateInstance(serviceName, groupName, instance); + } + + @Override + public Service queryService(String serviceName) throws NacosException { + return queryService(serviceName, Constants.DEFAULT_GROUP); + } + + @Override + public Service queryService(String serviceName, String groupName) throws NacosException { return serverProxy.queryService(serviceName, groupName); } @@ -92,7 +103,7 @@ public class NacosMaintainService implements MaintainService { @Override public void createService(String serviceName, String groupName) throws NacosException { - createService(serviceName, groupName, Constants.PROTECT_THRESHOLD); + createService(serviceName, groupName, Constants.DEFAULT_PROTECT_THRESHOLD); } @Override @@ -135,7 +146,7 @@ public class NacosMaintainService implements MaintainService { } @Override - public void updateService(String serviceName, String groupName, Float protectThreshold) throws NacosException { + public void updateService(String serviceName, String groupName, float protectThreshold) throws NacosException { Service service = new Service(); service.setName(serviceName); service.setGroupName(groupName); @@ -145,7 +156,7 @@ public class NacosMaintainService implements MaintainService { } @Override - public void updateService(String serviceName, String groupName, Float protectThreshold, Map metadata) throws NacosException { + public void updateService(String serviceName, String groupName, float protectThreshold, Map metadata) throws NacosException { Service service = new Service(); service.setName(serviceName); service.setGroupName(groupName); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index 78e32fde4..b7d5207f8 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -23,6 +23,7 @@ import com.alibaba.nacos.api.naming.listener.EventListener; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; +import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.api.selector.AbstractSelector; import com.alibaba.nacos.client.naming.beat.BeatInfo; import com.alibaba.nacos.client.naming.beat.BeatReactor; @@ -31,7 +32,7 @@ import com.alibaba.nacos.client.naming.core.EventDispatcher; import com.alibaba.nacos.client.naming.core.HostReactor; import com.alibaba.nacos.client.naming.net.NamingProxy; import com.alibaba.nacos.client.naming.utils.CollectionUtils; -import com.alibaba.nacos.client.naming.utils.NamingUtils; +import com.alibaba.nacos.client.naming.utils.InitUtils; import com.alibaba.nacos.client.naming.utils.UtilAndComs; import com.alibaba.nacos.client.utils.*; import org.apache.commons.lang3.BooleanUtils; @@ -78,9 +79,9 @@ public class NacosNamingService implements NamingService { } private void init(Properties properties) { - namespace = NamingUtils.initNamespace(properties); + namespace = InitUtils.initNamespace(properties); initServerAddr(properties); - NamingUtils.initWebRootContext(); + InitUtils.initWebRootContext(); initCacheDir(); initLogName(properties); @@ -122,7 +123,7 @@ public class NacosNamingService implements NamingService { private void initServerAddr(Properties properties) { serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR); - endpoint = NamingUtils.initEndpoint(properties); + endpoint = InitUtils.initEndpoint(properties); if (StringUtils.isNotEmpty(endpoint)) { serverList = ""; } @@ -184,7 +185,7 @@ public class NacosNamingService implements NamingService { if (instance.isEphemeral()) { BeatInfo beatInfo = new BeatInfo(); - beatInfo.setServiceName(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName)); + beatInfo.setServiceName(NamingUtils.getGroupedName(serviceName, groupName)); beatInfo.setIp(instance.getIp()); beatInfo.setPort(instance.getPort()); beatInfo.setCluster(instance.getClusterName()); @@ -192,10 +193,10 @@ public class NacosNamingService implements NamingService { beatInfo.setMetadata(instance.getMetadata()); beatInfo.setScheduled(false); - beatReactor.addBeatInfo(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), beatInfo); + beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo); } - serverProxy.registerService(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), groupName, instance); + serverProxy.registerService(NamingUtils.getGroupedName(serviceName, groupName), groupName, instance); } @Override @@ -225,8 +226,8 @@ public class NacosNamingService implements NamingService { @Override public void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException { - beatReactor.removeBeatInfo(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), instance.getIp(), instance.getPort()); - serverProxy.deregisterService(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), instance); + beatReactor.removeBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), instance.getIp(), instance.getPort()); + serverProxy.deregisterService(NamingUtils.getGroupedName(serviceName, groupName), instance); } @Override @@ -270,9 +271,9 @@ public class NacosNamingService implements NamingService { ServiceInfo serviceInfo; if (subscribe) { - serviceInfo = hostReactor.getServiceInfo(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); + serviceInfo = hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); } else { - serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); + serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); } List list; if (serviceInfo == null || CollectionUtils.isEmpty(list = serviceInfo.getHosts())) { @@ -324,9 +325,9 @@ public class NacosNamingService implements NamingService { ServiceInfo serviceInfo; if (subscribe) { - serviceInfo = hostReactor.getServiceInfo(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); + serviceInfo = hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); } else { - serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); + serviceInfo = hostReactor.getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")); } return selectInstances(serviceInfo, healthy); } @@ -372,10 +373,10 @@ public class NacosNamingService implements NamingService { if (subscribe) { return Balancer.RandomByWeight.selectHost( - hostReactor.getServiceInfo(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","))); + hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","))); } else { return Balancer.RandomByWeight.selectHost( - hostReactor.getServiceInfoDirectlyFromServer(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","))); + hostReactor.getServiceInfoDirectlyFromServer(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","))); } } @@ -396,7 +397,7 @@ public class NacosNamingService implements NamingService { @Override public void subscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException { - eventDispatcher.addListener(hostReactor.getServiceInfo(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), + eventDispatcher.addListener(hostReactor.getServiceInfo(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ",")), StringUtils.join(clusters, ","), listener); } @@ -417,7 +418,7 @@ public class NacosNamingService implements NamingService { @Override public void unsubscribe(String serviceName, String groupName, List clusters, EventListener listener) throws NacosException { - eventDispatcher.removeListener(com.alibaba.nacos.api.naming.utils.NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","), listener); + eventDispatcher.removeListener(NamingUtils.getGroupedName(serviceName, groupName), StringUtils.join(clusters, ","), listener); } @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index 4d8ae4b91..eb9bf95e0 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -171,7 +171,7 @@ public class NamingProxy { NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance: {}", namespaceId, serviceName, instance); - final Map params = new HashMap(8); + final Map params = new HashMap(9); params.put(CommonParams.NAMESPACE_ID, namespaceId); params.put(CommonParams.SERVICE_NAME, serviceName); params.put(CommonParams.GROUP_NAME, groupName); @@ -204,6 +204,24 @@ public class NamingProxy { reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.DELETE); } + public void updateInstance(String serviceName, String groupName, Instance instance) throws NacosException { + NAMING_LOGGER.info("[REGISTER-SERVICE] {} update service {} with instance: {}", + namespaceId, serviceName, instance); + + final Map params = new HashMap(8); + params.put(CommonParams.NAMESPACE_ID, namespaceId); + params.put(CommonParams.SERVICE_NAME, serviceName); + params.put(CommonParams.GROUP_NAME, groupName); + params.put(CommonParams.CLUSTER_NAME, instance.getClusterName()); + params.put("ip", instance.getIp()); + params.put("port", String.valueOf(instance.getPort())); + params.put("weight", String.valueOf(instance.getWeight())); + params.put("ephemeral", String.valueOf(instance.isEphemeral())); + params.put("metadata", JSON.toJSONString(instance.getMetadata())); + + reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.PUT); + } + public Service queryService(String serviceName, String groupName) throws NacosException { NAMING_LOGGER.info("[QUERY-SERVICE] {} query service : {}, {}", namespaceId, serviceName, groupName); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/NamingUtils.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/InitUtils.java similarity index 99% rename from client/src/main/java/com/alibaba/nacos/client/naming/utils/NamingUtils.java rename to client/src/main/java/com/alibaba/nacos/client/naming/utils/InitUtils.java index d6f522bb3..d8435b6bd 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/NamingUtils.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/InitUtils.java @@ -31,7 +31,7 @@ import java.util.concurrent.Callable; /** * @author liaochuntao */ -public class NamingUtils { +public class InitUtils { public static final String initNamespace(Properties properties) { String tmpNamespace = null; diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java index 6125c5c53..bb3dd71fa 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java @@ -26,6 +26,7 @@ import static org.junit.Assert.*; public class NacosMaintainServiceTest { private MaintainService maintainService; + private NamingService namingService; @Before public void before() throws NacosException { @@ -75,7 +76,7 @@ public class NacosMaintainServiceTest { @Test public void test3selectOneService() { try { - Service service = maintainService.selectOneService("nacos-api"); + Service service = maintainService.queryService("nacos-api"); System.out.println("service : " + service.toString()); } catch (NacosException e) { NAMING_LOGGER.error(e.getErrMsg()); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/constant/Constants.java b/config/src/main/java/com/alibaba/nacos/config/server/constant/Constants.java index b36163990..9e393ced5 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/constant/Constants.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/constant/Constants.java @@ -111,7 +111,7 @@ public class Constants { public static final String CONFIG_CONTROLLER_PATH = BASE_PATH + "/configs"; - public static final String HEALTH_CONTROLLER_PATH = BASE_PATH + "/health"; + public static final String HEALTH_CONTROLLER_PATH = BASE_PATH + "//health"; public static final String HISTORY_CONTROLLER_PATH = BASE_PATH + "/history"; diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/MaintainService_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/MaintainService_ITCase.java new file mode 100644 index 000000000..d4a144dee --- /dev/null +++ b/test/src/test/java/com/alibaba/nacos/test/naming/MaintainService_ITCase.java @@ -0,0 +1,147 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.test.naming; + +import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.MaintainFactory; +import com.alibaba.nacos.api.naming.MaintainService; +import com.alibaba.nacos.api.naming.NamingFactory; +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.pojo.Instance; +import com.alibaba.nacos.api.naming.pojo.Service; +import com.alibaba.nacos.api.selector.ExpressionSelector; +import com.alibaba.nacos.api.selector.NoneSelector; +import com.alibaba.nacos.naming.NamingApp; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +/** + * @author liaochuntao + * @date 2019-05-07 10:13 + **/ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"}, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class MaintainService_ITCase { + + private MaintainService maintainService; + private NamingService namingService; + private Instance instance; + private Service service; + + @LocalServerPort + private int port; + + @Before + public void init() throws Exception { + + NamingBase.prepareServer(port); + + if (maintainService == null) { + TimeUnit.SECONDS.sleep(10); + maintainService = MaintainFactory.createMaintainService("127.0.0.1" + ":" + port); + } + + if (namingService == null) { + TimeUnit.SECONDS.sleep(10); + namingService = NamingFactory.createNamingService("127.0.0.1" + ":" + port); + } + + instance = new Instance(); + instance.setIp("127.0.0.1"); + instance.setPort(8081); + instance.setWeight(2); + instance.setClusterName(Constants.DEFAULT_CLUSTER_NAME); + Map map = new HashMap(); + map.put("netType", "external"); + map.put("version", "1.0"); + instance.setMetadata(map); + + service = new Service(); + service.setName("nacos-api"); + service.setGroupName(Constants.DEFAULT_GROUP); + service.setProtectThreshold(1.0f); + Map metadata = new HashMap(); + metadata.put("nacos-1", "this is a test metadata"); + service.setMetadata(metadata); + } + + @Test + public void updateInstance() throws NacosException { + Map map = new HashMap(); + map.put("netType", "external-update"); + map.put("version", "2.0"); + instance.setMetadata(map); + namingService.registerInstance("nacos-api", instance); + maintainService.updateInstance("nacos-api", instance); + List instances = namingService.getAllInstances("nacos-api", true); + + Assert.assertEquals(instances.size(), 1); + System.out.println(instances.get(0)); + } + + @Test + public void createService() throws NacosException { + + ExpressionSelector selector = new ExpressionSelector(); + selector.setExpression("CONSUMER.label.A=PROVIDER.label.A &CONSUMER.label.B=PROVIDER.label.B"); + + System.out.println("service info : " + service); + maintainService.createService(service, selector); + Service remoteService = maintainService.queryService("nacos-api"); + System.out.println("remote service info : " + remoteService); + Assert.assertEquals(service.toString(), remoteService.toString()); + } + + @Test + public void updateService() throws NacosException { + Service service = new Service(); + service.setName("nacos-api"); + service.setGroupName(Constants.DEFAULT_GROUP); + service.setProtectThreshold(1.0f); + Map metadata = new HashMap(); + metadata.put("nacos-1", "nacos-3-update"); + service.setMetadata(metadata); + + maintainService.updateService(service, new NoneSelector()); + Service remoteService = maintainService.queryService("nacos-api"); + System.out.println("remote service info : " + remoteService); + Assert.assertEquals(service.toString(), remoteService.toString()); + } + + @Test + public void deleteService() throws NacosException { + Assert.assertTrue(maintainService.deleteService("nacos-api")); + } + + @Test + public void dregInstance() throws NacosException { + namingService.deregisterInstance("nacos-api", "127.0.0.1", 8081); + } + +} From 7ae757ac02fd11676ceb7090485ce06b4e7470c2 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Tue, 7 May 2019 11:24:11 +0800 Subject: [PATCH 08/12] refactor(config): correct the mistake to Constants.HEALTH_CONTROLLER_PATH --- .../com/alibaba/nacos/config/server/constant/Constants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/constant/Constants.java b/config/src/main/java/com/alibaba/nacos/config/server/constant/Constants.java index 9e393ced5..b36163990 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/constant/Constants.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/constant/Constants.java @@ -111,7 +111,7 @@ public class Constants { public static final String CONFIG_CONTROLLER_PATH = BASE_PATH + "/configs"; - public static final String HEALTH_CONTROLLER_PATH = BASE_PATH + "//health"; + public static final String HEALTH_CONTROLLER_PATH = BASE_PATH + "/health"; public static final String HISTORY_CONTROLLER_PATH = BASE_PATH + "/history"; From 8c82f85a743541eb42d17c8883ef832eff391073 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Tue, 7 May 2019 12:03:34 +0800 Subject: [PATCH 09/12] refactor: --- .../main/java/com/alibaba/nacos/api/selector/NoneSelector.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/selector/NoneSelector.java b/api/src/main/java/com/alibaba/nacos/api/selector/NoneSelector.java index 8da7c48c6..1873007cc 100644 --- a/api/src/main/java/com/alibaba/nacos/api/selector/NoneSelector.java +++ b/api/src/main/java/com/alibaba/nacos/api/selector/NoneSelector.java @@ -18,7 +18,7 @@ package com.alibaba.nacos.api.selector; /** * @author liaochuntao - * @since 1.0.0 + * @since 1.0.1 */ public class NoneSelector extends AbstractSelector { From 141f8a19ea0b075f533a49fcae42fc9aed9e4d98 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Wed, 8 May 2019 15:29:01 +0800 Subject: [PATCH 10/12] refactor(client): --- .../java/com/alibaba/nacos/api/naming/MaintainService.java | 4 ++-- .../com/alibaba/nacos/client/naming/NacosMaintainService.java | 4 ++-- .../java/com/alibaba/nacos/client/naming/net/NamingProxy.java | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java b/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java index 8e9ae428a..bfddc8a38 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java @@ -94,7 +94,7 @@ public interface MaintainService { * @param protectThreshold protectThreshold of service * @throws NacosException */ - void createService(String serviceName, String groupName, Float protectThreshold) throws NacosException; + void createService(String serviceName, String groupName, float protectThreshold) throws NacosException; /** * create service to Nacos @@ -105,7 +105,7 @@ public interface MaintainService { * @param expression expression of selector * @throws NacosException */ - void createService(String serviceName, String groupName, Float protectThreshold, String expression) throws NacosException; + void createService(String serviceName, String groupName, float protectThreshold, String expression) throws NacosException; /** * create service to Nacos diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java index a902537f4..93eda8dfa 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java @@ -107,7 +107,7 @@ public class NacosMaintainService implements MaintainService { } @Override - public void createService(String serviceName, String groupName, Float protectThreshold) throws NacosException { + public void createService(String serviceName, String groupName, float protectThreshold) throws NacosException { NoneSelector selector = new NoneSelector(); Service service = new Service(); service.setName(serviceName); @@ -118,7 +118,7 @@ public class NacosMaintainService implements MaintainService { } @Override - public void createService(String serviceName, String groupName, Float protectThreshold, String expression) throws NacosException { + public void createService(String serviceName, String groupName, float protectThreshold, String expression) throws NacosException { Service service = new Service(); service.setName(serviceName); service.setGroupName(groupName); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java index eb9bf95e0..6b27abdeb 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java @@ -205,7 +205,7 @@ public class NamingProxy { } public void updateInstance(String serviceName, String groupName, Instance instance) throws NacosException { - NAMING_LOGGER.info("[REGISTER-SERVICE] {} update service {} with instance: {}", + NAMING_LOGGER.info("[UPDATE-SERVICE] {} update service {} with instance: {}", namespaceId, serviceName, instance); final Map params = new HashMap(8); From ac36a41d8683ae2f51de85a2450afc499e00e718 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Thu, 9 May 2019 10:07:09 +0800 Subject: [PATCH 11/12] =?UTF-8?q?refactor(api=E3=80=81client):=20modify=20?= =?UTF-8?q?MaintainService=20to=20NamingMaintainService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/alibaba/nacos/api/NacosFactory.java | 16 ++++++------- ...actory.java => NamingMaintainFactory.java} | 10 ++++---- ...ervice.java => NamingMaintainService.java} | 2 +- ...e.java => NacosNamingMaintainService.java} | 8 +++---- ...va => NacosNamingMaintainServiceTest.java} | 17 +++++++------ ...java => NamingMaintainService_ITCase.java} | 24 +++++++++---------- 6 files changed, 38 insertions(+), 39 deletions(-) rename api/src/main/java/com/alibaba/nacos/api/naming/{MaintainFactory.java => NamingMaintainFactory.java} (76%) rename api/src/main/java/com/alibaba/nacos/api/naming/{MaintainService.java => NamingMaintainService.java} (99%) rename client/src/main/java/com/alibaba/nacos/client/naming/{NacosMaintainService.java => NacosNamingMaintainService.java} (95%) rename client/src/test/java/com/alibaba/nacos/client/naming/{NacosMaintainServiceTest.java => NacosNamingMaintainServiceTest.java} (82%) rename test/src/test/java/com/alibaba/nacos/test/naming/{MaintainService_ITCase.java => NamingMaintainService_ITCase.java} (85%) diff --git a/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java b/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java index 165965e49..82228c027 100644 --- a/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/NacosFactory.java @@ -20,8 +20,8 @@ import java.util.Properties; import com.alibaba.nacos.api.config.ConfigFactory; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.api.naming.MaintainFactory; -import com.alibaba.nacos.api.naming.MaintainService; +import com.alibaba.nacos.api.naming.NamingMaintainFactory; +import com.alibaba.nacos.api.naming.NamingMaintainService; import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.api.naming.NamingService; @@ -80,22 +80,22 @@ public class NacosFactory { * Create maintain service * * @param serverAddr - * @return MaintainService + * @return NamingMaintainService * @throws NacosException Exception */ - public static MaintainService createMaintainService(String serverAddr) throws NacosException { - return MaintainFactory.createMaintainService(serverAddr); + public static NamingMaintainService createMaintainService(String serverAddr) throws NacosException { + return NamingMaintainFactory.createMaintainService(serverAddr); } /** * Create maintain service * * @param properties - * @return MaintainService + * @return NamingMaintainService * @throws NacosException Exception */ - public static MaintainService createMaintainService(Properties properties) throws NacosException { - return MaintainFactory.createMaintainService(properties); + public static NamingMaintainService createMaintainService(Properties properties) throws NacosException { + return NamingMaintainFactory.createMaintainService(properties); } } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainFactory.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainFactory.java similarity index 76% rename from api/src/main/java/com/alibaba/nacos/api/naming/MaintainFactory.java rename to api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainFactory.java index 423814646..5cf93a382 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainFactory.java @@ -25,24 +25,24 @@ import java.util.Properties; * @author liaochuntao * @since 1.0.1 */ -public class MaintainFactory { +public class NamingMaintainFactory { - public static MaintainService createMaintainService(String serverList) throws NacosException { + public static NamingMaintainService createMaintainService(String serverList) throws NacosException { try { Class driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosMaintainService"); Constructor constructor = driverImplClass.getConstructor(String.class); - MaintainService vendorImpl = (MaintainService)constructor.newInstance(serverList); + NamingMaintainService vendorImpl = (NamingMaintainService)constructor.newInstance(serverList); return vendorImpl; } catch (Throwable e) { throw new NacosException(-400, e.getMessage()); } } - public static MaintainService createMaintainService(Properties properties) throws NacosException { + public static NamingMaintainService createMaintainService(Properties properties) throws NacosException { try { Class driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosMaintainService"); Constructor constructor = driverImplClass.getConstructor(Properties.class); - MaintainService vendorImpl = (MaintainService)constructor.newInstance(properties); + NamingMaintainService vendorImpl = (NamingMaintainService)constructor.newInstance(properties); return vendorImpl; } catch (Throwable e) { throw new NacosException(-400, e.getMessage()); diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainService.java similarity index 99% rename from api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java rename to api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainService.java index bfddc8a38..069e39582 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/MaintainService.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainService.java @@ -29,7 +29,7 @@ import java.util.Map; * @author liaochuntao * @since 1.0.1 */ -public interface MaintainService { +public interface NamingMaintainService { /** * update instance info diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java similarity index 95% rename from client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java rename to client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java index 93eda8dfa..a13638d2e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosMaintainService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java @@ -19,7 +19,7 @@ package com.alibaba.nacos.client.naming; import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.api.naming.MaintainService; +import com.alibaba.nacos.api.naming.NamingMaintainService; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.Service; import com.alibaba.nacos.api.selector.AbstractSelector; @@ -37,7 +37,7 @@ import java.util.Properties; * @since 1.0.1 */ @SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") -public class NacosMaintainService implements MaintainService { +public class NacosNamingMaintainService implements NamingMaintainService { private String namespace; @@ -47,14 +47,14 @@ public class NacosMaintainService implements MaintainService { private NamingProxy serverProxy; - public NacosMaintainService(String serverList) { + public NacosNamingMaintainService(String serverList) { Properties properties = new Properties(); properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList); init(properties); } - public NacosMaintainService(Properties properties) { + public NacosNamingMaintainService(Properties properties) { init(properties); } diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingMaintainServiceTest.java similarity index 82% rename from client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java rename to client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingMaintainServiceTest.java index bb3dd71fa..0157e22a3 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/NacosMaintainServiceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingMaintainServiceTest.java @@ -4,7 +4,7 @@ import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.api.naming.MaintainService; +import com.alibaba.nacos.api.naming.NamingMaintainService; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Service; import com.alibaba.nacos.api.selector.ExpressionSelector; @@ -20,12 +20,11 @@ import java.util.Map; import java.util.Properties; import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; -import static org.junit.Assert.*; @FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class NacosMaintainServiceTest { +public class NacosNamingMaintainServiceTest { - private MaintainService maintainService; + private NamingMaintainService namingMaintainService; private NamingService namingService; @Before @@ -33,7 +32,7 @@ public class NacosMaintainServiceTest { Properties properties = new Properties(); properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848"); - maintainService = NacosFactory.createMaintainService(properties); + namingMaintainService = NacosFactory.createMaintainService(properties); } @Test @@ -50,7 +49,7 @@ public class NacosMaintainServiceTest { selector.setExpression("CONSUMER.label.A=PROVIDER.label.A &CONSUMER.label.B=PROVIDER.label.B"); try { - maintainService.createService(service, new NoneSelector()); + namingMaintainService.createService(service, new NoneSelector()); } catch (NacosException e) { NAMING_LOGGER.error(e.getErrMsg()); } @@ -67,7 +66,7 @@ public class NacosMaintainServiceTest { service.setMetadata(metadata); try { - maintainService.updateService(service, new NoneSelector()); + namingMaintainService.updateService(service, new NoneSelector()); } catch (NacosException e) { NAMING_LOGGER.error(e.getErrMsg()); } @@ -76,7 +75,7 @@ public class NacosMaintainServiceTest { @Test public void test3selectOneService() { try { - Service service = maintainService.queryService("nacos-api"); + Service service = namingMaintainService.queryService("nacos-api"); System.out.println("service : " + service.toString()); } catch (NacosException e) { NAMING_LOGGER.error(e.getErrMsg()); @@ -86,7 +85,7 @@ public class NacosMaintainServiceTest { @Test public void test4deleteService() { try { - Assert.assertTrue(maintainService.deleteService("nacos-api")); + Assert.assertTrue(namingMaintainService.deleteService("nacos-api")); } catch (NacosException e) { NAMING_LOGGER.error(e.getErrMsg()); } diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/MaintainService_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/NamingMaintainService_ITCase.java similarity index 85% rename from test/src/test/java/com/alibaba/nacos/test/naming/MaintainService_ITCase.java rename to test/src/test/java/com/alibaba/nacos/test/naming/NamingMaintainService_ITCase.java index d4a144dee..773848761 100644 --- a/test/src/test/java/com/alibaba/nacos/test/naming/MaintainService_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/naming/NamingMaintainService_ITCase.java @@ -18,8 +18,8 @@ package com.alibaba.nacos.test.naming; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.api.naming.MaintainFactory; -import com.alibaba.nacos.api.naming.MaintainService; +import com.alibaba.nacos.api.naming.NamingMaintainFactory; +import com.alibaba.nacos.api.naming.NamingMaintainService; import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; @@ -47,9 +47,9 @@ import java.util.concurrent.TimeUnit; @RunWith(SpringRunner.class) @SpringBootTest(classes = NamingApp.class, properties = {"server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class MaintainService_ITCase { +public class NamingMaintainService_ITCase { - private MaintainService maintainService; + private NamingMaintainService namingMaintainService; private NamingService namingService; private Instance instance; private Service service; @@ -62,9 +62,9 @@ public class MaintainService_ITCase { NamingBase.prepareServer(port); - if (maintainService == null) { + if (namingMaintainService == null) { TimeUnit.SECONDS.sleep(10); - maintainService = MaintainFactory.createMaintainService("127.0.0.1" + ":" + port); + namingMaintainService = NamingMaintainFactory.createMaintainService("127.0.0.1" + ":" + port); } if (namingService == null) { @@ -98,7 +98,7 @@ public class MaintainService_ITCase { map.put("version", "2.0"); instance.setMetadata(map); namingService.registerInstance("nacos-api", instance); - maintainService.updateInstance("nacos-api", instance); + namingMaintainService.updateInstance("nacos-api", instance); List instances = namingService.getAllInstances("nacos-api", true); Assert.assertEquals(instances.size(), 1); @@ -112,8 +112,8 @@ public class MaintainService_ITCase { selector.setExpression("CONSUMER.label.A=PROVIDER.label.A &CONSUMER.label.B=PROVIDER.label.B"); System.out.println("service info : " + service); - maintainService.createService(service, selector); - Service remoteService = maintainService.queryService("nacos-api"); + namingMaintainService.createService(service, selector); + Service remoteService = namingMaintainService.queryService("nacos-api"); System.out.println("remote service info : " + remoteService); Assert.assertEquals(service.toString(), remoteService.toString()); } @@ -128,15 +128,15 @@ public class MaintainService_ITCase { metadata.put("nacos-1", "nacos-3-update"); service.setMetadata(metadata); - maintainService.updateService(service, new NoneSelector()); - Service remoteService = maintainService.queryService("nacos-api"); + namingMaintainService.updateService(service, new NoneSelector()); + Service remoteService = namingMaintainService.queryService("nacos-api"); System.out.println("remote service info : " + remoteService); Assert.assertEquals(service.toString(), remoteService.toString()); } @Test public void deleteService() throws NacosException { - Assert.assertTrue(maintainService.deleteService("nacos-api")); + Assert.assertTrue(namingMaintainService.deleteService("nacos-api")); } @Test From 221ce1237a818b0782936e2af82df8916e4d9c9f Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Thu, 9 May 2019 10:22:16 +0800 Subject: [PATCH 12/12] fix(api): fix NamingMaintainFactory create NamingMaintainService cannot find Class --- .../com/alibaba/nacos/api/naming/NamingMaintainFactory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainFactory.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainFactory.java index 5cf93a382..d2f3b8e61 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainFactory.java @@ -29,7 +29,7 @@ public class NamingMaintainFactory { public static NamingMaintainService createMaintainService(String serverList) throws NacosException { try { - Class driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosMaintainService"); + Class driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosNamingMaintainService"); Constructor constructor = driverImplClass.getConstructor(String.class); NamingMaintainService vendorImpl = (NamingMaintainService)constructor.newInstance(serverList); return vendorImpl; @@ -40,7 +40,7 @@ public class NamingMaintainFactory { public static NamingMaintainService createMaintainService(Properties properties) throws NacosException { try { - Class driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosMaintainService"); + Class driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosNamingMaintainService"); Constructor constructor = driverImplClass.getConstructor(Properties.class); NamingMaintainService vendorImpl = (NamingMaintainService)constructor.newInstance(properties); return vendorImpl;