nacos的namespace发布接口之源码解析
接着上面这篇文章,开始动手实现自定义的接口实现namespace的添加。
com.heshi.nacos.service.NacosNameSpaceService
package com.heshi.nacos.service;
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.client.config.filter.impl.ConfigFilterChainManager;
import com.alibaba.nacos.client.config.http.HttpAgent;
import com.alibaba.nacos.client.config.http.MetricsHttpAgent;
import com.alibaba.nacos.client.config.http.ServerHttpAgent;
import com.alibaba.nacos.client.config.impl.ClientWorker;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.client.utils.TenantUtil;
import com.heshi.nacos.common.NacosConstants;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
/**
* @Author: heshi
* @Date: 2020/8/13 19:35
* @since
*/
public class NacosNameSpaceService {
private static final Logger LOGGER = LogUtils.logger(NacosNameSpaceService.class);
private static final long POST_TIMEOUT = 3000L;
/**
* http agent
*/
private HttpAgent agent;
/**
* longpolling
*/
private ClientWorker worker;
private String namespace;
private String encode;
private ConfigFilterChainManager configFilterChainManager = new ConfigFilterChainManager();
public NacosNameSpaceService(Properties properties) throws NacosException {
String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE);
if (StringUtils.isBlank(encodeTmp)) {
encode = Constants.ENCODE;
} else {
encode = encodeTmp.trim();
}
initNamespace(properties);
agent = new MetricsHttpAgent(new ServerHttpAgent(properties));
agent.start();
worker = new ClientWorker(agent, configFilterChainManager, properties);
}
private void initNamespace(Properties properties) {
namespace = parseNamespace(properties);
properties.put(PropertyKeyConst.NAMESPACE, namespace);
}
/**
* 发布namespace
*
* @param namespaceId
* @param namespaceName
* @param namespaceDesc
* @return
*/
public boolean publishNameSpace(String namespaceId, String namespaceName, String namespaceDesc) throws NacosException {
return publishNameSpaceInner(namespaceId, namespaceName, namespaceDesc);
}
private boolean publishNameSpaceInner(String namespaceId, String namespaceName, String namespaceDesc) throws NacosException {
String url = NacosConstants.CONFIG_CONTROLLER_PATH;
List<String> params = new ArrayList<String>();
params.add("customNamespaceId");
params.add(namespaceId);
params.add("namespaceName");
params.add(namespaceName);
params.add("namespaceDesc");
params.add(namespaceDesc);
List<String> headers = new ArrayList<String>();
HttpSimpleClient.HttpResult result = null;
try {
result = agent.httpPost(url, headers, params, encode, POST_TIMEOUT);
} catch (IOException ioe) {
LOGGER.error(ioe.getMessage(), ioe);
return false;
}
return true;
}
private String null2defaultGroup(String group) {
return (null == group) ? Constants.DEFAULT_GROUP : group.trim();
}
public static String parseNamespace(Properties properties) {
String namespaceTmp = null;
String isUseCloudNamespaceParsing =
properties.getProperty(PropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING,
System.getProperty(SystemPropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING,
String.valueOf(Constants.DEFAULT_USE_CLOUD_NAMESPACE_PARSING)));
if (Boolean.parseBoolean(isUseCloudNamespaceParsing)) {
namespaceTmp = TemplateUtils.stringBlankAndThenExecute(namespaceTmp, new Callable<String>() {
@Override
public String call() {
return TenantUtil.getUserTenantForAcm();
}
});
namespaceTmp = TemplateUtils.stringBlankAndThenExecute(namespaceTmp, new Callable<String>() {
@Override
public String call() {
String namespace = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE);
return StringUtils.isNotBlank(namespace) ? namespace : StringUtils.EMPTY;
}
});
}
if (StringUtils.isBlank(namespaceTmp)) {
namespaceTmp = properties.getProperty(PropertyKeyConst.NAMESPACE);
}
return StringUtils.isNotBlank(namespaceTmp) ? namespaceTmp.trim() : StringUtils.EMPTY;
}
}
按照上面所示,首先实现接口发布实现的业务类,主要是调用一个api请求
package com.heshi.nacos.common;
/**
* @Author: heshi
* @Date: 2020/8/13 19:41
* @since
*/
public class NacosConstants {
public static final String CONFIG_CONTROLLER_PATH = "/v1/console/namespaces";
}
com.heshi.nacos.config.NacosNameSpaceConfiguration
实现配置类
package com.heshi.nacos.config;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.exception.NacosException;
import com.heshi.nacos.service.NacosNameSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: heshi
* @Date: 2020/8/13 19:50
* @since
*/
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnProperty(
name = {"spring.cloud.nacos.config.enabled"},
matchIfMissing = true
)
public class NacosNameSpaceConfiguration {
@Autowired
public NacosConfigProperties nacosConfigProperties;
@Bean
@ConditionalOnMissingBean
public NacosNameSpaceService nacosNameSpaceService(NacosConfigProperties nacosConfigProperties) throws NacosException {
return new NacosNameSpaceService(nacosConfigProperties.assembleConfigServiceProperties());
}
}
接下来再构建一个测试controller试试
package com.heshi.nacos.controller;
import com.alibaba.nacos.api.exception.NacosException;
import com.heshi.nacos.service.NacosNameSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: heshi
* @Date: 2020/8/13 19:56
* @since
*/
@RestController
@RequestMapping("/namespace")
@RefreshScope
public class NacosNameSpaceController {
@Autowired
private NacosNameSpaceService nameSpaceService;
@RequestMapping("/add")
public void get(String customNamespaceId, String namespaceName, String namespaceDesc) {
try {
nameSpaceService.publishNameSpace(customNamespaceId, namespaceName, namespaceDesc);
} catch (NacosException e) {
e.printStackTrace();
}
}
}
同理,如果想增加用户、权限的接口,也可以这样干。