feat(client): add feature of service curd
This commit is contained in:
parent
fceff72272
commit
ea214e1d50
@ -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);
|
||||
|
@ -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<String> 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<String, String> 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
|
||||
*
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
@ -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<String, String> 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<String>(), listener);
|
||||
|
@ -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<String, String> params = new HashMap<String, String>(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<String, String> params = new HashMap<String, String>(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<String, String> params = new HashMap<String, String>(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 {
|
||||
|
||||
|
@ -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";
|
||||
|
@ -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<String, String> metadata = new HashMap<String, String>();
|
||||
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<String, String> metadata = new HashMap<String, String>();
|
||||
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);
|
||||
}
|
||||
}
|
19
update_git_info.sh
Executable file
19
update_git_info.sh
Executable file
@ -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
|
Loading…
Reference in New Issue
Block a user