refactor(client): Fix the issues raised in the comments

This commit is contained in:
chuntaojun 2019-05-07 11:11:24 +08:00
parent f8ec782080
commit 4817b26bc9
11 changed files with 248 additions and 39 deletions

View File

@ -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);

View File

@ -23,7 +23,7 @@ import java.util.Properties;
/**
* @author liaochuntao
* @since 1.0.0
* @since 1.0.1
*/
public class MaintainFactory {

View File

@ -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<String, String> metadata) throws NacosException;
void updateService(String serviceName, String groupName, float protectThreshold, Map<String, String> metadata) throws NacosException;
/**
* update service to Nacos with selector

View File

@ -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 +
'}';
}
}

View File

@ -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<String, String> metadata) throws NacosException {
public void updateService(String serviceName, String groupName, float protectThreshold, Map<String, String> metadata) throws NacosException {
Service service = new Service();
service.setName(serviceName);
service.setGroupName(groupName);

View File

@ -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<Instance> 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<String> 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<String> 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

View File

@ -171,7 +171,7 @@ public class NamingProxy {
NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance: {}",
namespaceId, serviceName, instance);
final Map<String, String> params = new HashMap<String, String>(8);
final Map<String, String> params = new HashMap<String, String>(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<String, String> params = new HashMap<String, String>(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);

View File

@ -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;

View File

@ -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());

View File

@ -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";

View File

@ -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<String, String> map = new HashMap<String, String>();
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<String, String> metadata = new HashMap<String, String>();
metadata.put("nacos-1", "this is a test metadata");
service.setMetadata(metadata);
}
@Test
public void updateInstance() throws NacosException {
Map<String, String> map = new HashMap<String, String>();
map.put("netType", "external-update");
map.put("version", "2.0");
instance.setMetadata(map);
namingService.registerInstance("nacos-api", instance);
maintainService.updateInstance("nacos-api", instance);
List<Instance> 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<String, String> metadata = new HashMap<String, String>();
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);
}
}