From 5e7e26e46e0e1af5b68103021c7b11c160a6e739 Mon Sep 17 00:00:00 2001 From: hongqiaowei Date: Tue, 29 Mar 2022 18:28:12 +0800 Subject: [PATCH] Nacos service registration support username and password --- .../nacos/FizzNacosHelper.java | 22 ++++++++++++++----- .../nacos/FizzNacosProperties.java | 16 +++++++++++++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/fizz-core/src/main/java/we/service_registry/nacos/FizzNacosHelper.java b/fizz-core/src/main/java/we/service_registry/nacos/FizzNacosHelper.java index 92d5807..f0fac50 100644 --- a/fizz-core/src/main/java/we/service_registry/nacos/FizzNacosHelper.java +++ b/fizz-core/src/main/java/we/service_registry/nacos/FizzNacosHelper.java @@ -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 pn = propertyName.substring(ndl); - if (pn.indexOf(Consts.S.DASH) > -1) { - pn = PropertiesUtils.normalize(pn); + 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, propertyValue); } - ps.setProperty(pn, nacosProperties.getProperty(propertyName)); } - 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); } } diff --git a/fizz-core/src/main/java/we/service_registry/nacos/FizzNacosProperties.java b/fizz-core/src/main/java/we/service_registry/nacos/FizzNacosProperties.java index 63e734e..ede338e 100644 --- a/fizz-core/src/main/java/we/service_registry/nacos/FizzNacosProperties.java +++ b/fizz-core/src/main/java/we/service_registry/nacos/FizzNacosProperties.java @@ -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; } }