Nacos service registration support username and password

This commit is contained in:
hongqiaowei
2022-03-29 18:28:12 +08:00
parent f16e231837
commit 5e7e26e46e
2 changed files with 31 additions and 7 deletions

View File

@@ -20,11 +20,13 @@ package we.service_registry.nacos;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
import com.alibaba.cloud.nacos.registry.NacosServiceRegistry;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.naming.NamingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import we.util.Consts;
import we.util.JacksonUtils;
import we.util.PropertiesUtils;
import we.util.ReflectionUtils;
@@ -45,14 +47,21 @@ public abstract class FizzNacosHelper {
Properties ps = new Properties();
for (String propertyName : nacosProperties.stringPropertyNames()) {
String propertyValue = nacosProperties.getProperty(propertyName);
if (propertyName.endsWith(PropertyKeyConst.USERNAME)) {
ps.setProperty(PropertyKeyConst.USERNAME, propertyValue);
} else if (propertyName.endsWith(PropertyKeyConst.PASSWORD)) {
ps.setProperty(PropertyKeyConst.PASSWORD, propertyValue);
} else {
String pn = propertyName.substring(ndl);
if (pn.indexOf(Consts.S.DASH) > -1) {
pn = PropertiesUtils.normalize(pn);
}
ps.setProperty(pn, nacosProperties.getProperty(propertyName));
ps.setProperty(pn, propertyValue);
}
}
FizzNacosProperties fizzNacosProperties = new FizzNacosProperties();
FizzNacosProperties fizzNacosProperties = new FizzNacosProperties(ps);
PropertiesUtils.setBeanPropertyValue(fizzNacosProperties, ps);
fizzNacosProperties.setApplicationContext(applicationContext);
@@ -84,7 +93,8 @@ public abstract class FizzNacosHelper {
NacosServiceManager nacosServiceManager = new NacosServiceManager();
ReflectionUtils.set(nacosServiceRegistry, "nacosServiceManager", nacosServiceManager);
NacosRegistration nacosRegistration = new NacosRegistration(null, fizzNacosProperties, applicationContext);
NamingService namingService = nacosServiceManager.getNamingService(fizzNacosProperties.getNacosProperties());
Properties nps = fizzNacosProperties.getNacosProperties();
NamingService namingService = nacosServiceManager.getNamingService(nps);
return new FizzNacosServiceRegistration(fizzNacosProperties.getId(), nacosRegistration, nacosServiceRegistry, namingService);
}
}

View File

@@ -99,6 +99,12 @@ public class FizzNacosProperties extends NacosDiscoveryProperties {
private boolean init = false;
private Properties config;
public FizzNacosProperties(Properties config) {
this.config = config;
}
public void init() {
if (init) {
return;
@@ -452,9 +458,17 @@ public class FizzNacosProperties extends NacosDiscoveryProperties {
properties.put(NAMING_LOAD_CACHE_AT_START, namingLoadCacheAtStart);
properties.put("enabled", true);
properties.put("server-addr", this.getServerAddr());
// properties.put("server-addr", serverAddr);
properties.put("com.alibaba.nacos.naming.log.filename", "");
config.forEach(
(c, v) -> {
if (!properties.containsKey(c)) {
properties.put(c, v);
}
}
);
return properties;
}
}