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..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,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.NamingMaintainFactory; +import com.alibaba.nacos.api.naming.NamingMaintainService; 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 NamingMaintainService + * @throws NacosException Exception + */ + public static NamingMaintainService createMaintainService(String serverAddr) throws NacosException { + return NamingMaintainFactory.createMaintainService(serverAddr); + } + + /** + * Create maintain service + * + * @param properties + * @return NamingMaintainService + * @throws NacosException Exception + */ + public static NamingMaintainService createMaintainService(Properties properties) throws NacosException { + return NamingMaintainFactory.createMaintainService(properties); + } + } 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..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,6 +103,8 @@ public class Constants { public static final int FLOW_CONTROL_INTERVAL = 1000; + public static final float DEFAULT_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/NamingMaintainFactory.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainFactory.java new file mode 100644 index 000000000..d2f3b8e61 --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainFactory.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.1 + */ +public class NamingMaintainFactory { + + public static NamingMaintainService createMaintainService(String serverList) throws NacosException { + try { + Class driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosNamingMaintainService"); + Constructor constructor = driverImplClass.getConstructor(String.class); + NamingMaintainService vendorImpl = (NamingMaintainService)constructor.newInstance(serverList); + return vendorImpl; + } catch (Throwable e) { + throw new NacosException(-400, e.getMessage()); + } + } + + public static NamingMaintainService createMaintainService(Properties properties) throws NacosException { + try { + Class driverImplClass = Class.forName("com.alibaba.nacos.client.naming.NacosNamingMaintainService"); + Constructor constructor = driverImplClass.getConstructor(Properties.class); + 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/NamingMaintainService.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainService.java new file mode 100644 index 000000000..069e39582 --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainService.java @@ -0,0 +1,168 @@ +/* + * 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.Instance; +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.1 + */ +public interface NamingMaintainService { + + /** + * 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 + * + * @param serviceName + * @return + * @throws NacosException + */ + Service queryService(String serviceName) throws NacosException; + + /** + * query service + * + * @param serviceName + * @param groupName + * @return + * @throws NacosException + */ + Service queryService(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 06cb467da..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 @@ -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 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/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..1873007cc --- /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.1 + */ +public class NoneSelector extends AbstractSelector { + + public NoneSelector() { + this.setType(SelectorType.none.name()); + } +} diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java new file mode 100644 index 000000000..a13638d2e --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java @@ -0,0 +1,174 @@ +/* + * 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.NamingMaintainService; +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.InitUtils; +import com.alibaba.nacos.client.utils.StringUtils; + +import java.util.Map; +import java.util.Properties; + +/** + * @author liaochuntao + * @since 1.0.1 + */ +@SuppressWarnings("PMD.ServiceOrDaoClassShouldEndWithImplRule") +public class NacosNamingMaintainService implements NamingMaintainService { + + private String namespace; + + private String endpoint; + + private String serverList; + + private NamingProxy serverProxy; + + public NacosNamingMaintainService(String serverList) { + Properties properties = new Properties(); + properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList); + + init(properties); + } + + public NacosNamingMaintainService(Properties properties) { + + init(properties); + } + + private void init(Properties properties) { + namespace = InitUtils.initNamespace(properties); + initServerAddr(properties); + InitUtils.initWebRootContext(); + + serverProxy = new NamingProxy(namespace, endpoint, serverList); + serverProxy.setProperties(properties); + } + + private void initServerAddr(Properties properties) { + serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR); + endpoint = InitUtils.initEndpoint(properties); + if (StringUtils.isNotEmpty(endpoint)) { + serverList = ""; + } + } + + @Override + public void updateInstance(String serviceName, Instance instance) throws NacosException { + updateInstance(serviceName, Constants.DEFAULT_GROUP, instance); + } + + @Override + 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); + } + + @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.DEFAULT_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 09b6da76e..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 @@ -16,7 +16,6 @@ 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; @@ -26,7 +25,6 @@ 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.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; @@ -34,19 +32,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.InitUtils; 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.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Properties; -import java.util.concurrent.Callable; +import java.util.*; /** * @author nkorange @@ -87,10 +79,9 @@ public class NacosNamingService implements NamingService { } private void init(Properties properties) { - serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR); - initNamespace(properties); - initEndpoint(properties); - initWebRootContext(); + namespace = InitUtils.initNamespace(properties); + initServerAddr(properties); + InitUtils.initWebRootContext(); initCacheDir(); initLogName(properties); @@ -130,6 +121,14 @@ public class NacosNamingService implements NamingService { return loadCacheAtStart; } + private void initServerAddr(Properties properties) { + serverList = properties.getProperty(PropertyKeyConst.SERVER_ADDR); + endpoint = InitUtils.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 +148,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); 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..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 @@ -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; @@ -170,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); @@ -203,6 +204,84 @@ 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("[UPDATE-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); + + 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 : {}", + 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/InitUtils.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/InitUtils.java new file mode 100644 index 000000000..d8435b6bd --- /dev/null +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/InitUtils.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 InitUtils { + + 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/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/NacosNamingMaintainServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingMaintainServiceTest.java new file mode 100644 index 000000000..0157e22a3 --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingMaintainServiceTest.java @@ -0,0 +1,94 @@ +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.NamingMaintainService; +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.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class NacosNamingMaintainServiceTest { + + private NamingMaintainService namingMaintainService; + private NamingService namingService; + + @Before + public void before() throws NacosException { + Properties properties = new Properties(); + properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848"); + + namingMaintainService = NacosFactory.createMaintainService(properties); + } + + @Test + public void test1createService() { + 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 { + namingMaintainService.createService(service, new NoneSelector()); + } catch (NacosException e) { + NAMING_LOGGER.error(e.getErrMsg()); + } + } + + @Test + public void test2updateService() { + 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 { + namingMaintainService.updateService(service, new NoneSelector()); + } catch (NacosException e) { + NAMING_LOGGER.error(e.getErrMsg()); + } + } + + @Test + public void test3selectOneService() { + try { + Service service = namingMaintainService.queryService("nacos-api"); + System.out.println("service : " + service.toString()); + } catch (NacosException e) { + NAMING_LOGGER.error(e.getErrMsg()); + } + } + + @Test + public void test4deleteService() { + try { + Assert.assertTrue(namingMaintainService.deleteService("nacos-api")); + } 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 new file mode 100644 index 000000000..7eb555168 --- /dev/null +++ b/client/src/test/java/com/alibaba/nacos/client/naming/NacosNamingServiceTest.java @@ -0,0 +1,48 @@ +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 deleteService() { + + } + + @Test + public void updateService() { + + } + + @Test + public void registerInstance() throws NacosException { + nameService.registerInstance("nacos-api", "127.0.0.1", 8009); + } +} 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/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) { 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; diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/NamingMaintainService_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/NamingMaintainService_ITCase.java new file mode 100644 index 000000000..773848761 --- /dev/null +++ b/test/src/test/java/com/alibaba/nacos/test/naming/NamingMaintainService_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.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; +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 NamingMaintainService_ITCase { + + private NamingMaintainService namingMaintainService; + private NamingService namingService; + private Instance instance; + private Service service; + + @LocalServerPort + private int port; + + @Before + public void init() throws Exception { + + NamingBase.prepareServer(port); + + if (namingMaintainService == null) { + TimeUnit.SECONDS.sleep(10); + namingMaintainService = NamingMaintainFactory.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); + namingMaintainService.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); + namingMaintainService.createService(service, selector); + Service remoteService = namingMaintainService.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); + + 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(namingMaintainService.deleteService("nacos-api")); + } + + @Test + public void dregInstance() throws NacosException { + namingService.deregisterInstance("nacos-api", "127.0.0.1", 8081); + } + +}