From 08ca6a2a59d81ae93504e259bf3a99c9e419bcbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E6=9D=A5=E5=AE=BE?= Date: Mon, 9 Dec 2019 13:44:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0nacos=E7=9A=84namespace?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/RegistryServiceNacosImpl.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/impl/RegistryServiceNacosImpl.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/impl/RegistryServiceNacosImpl.java index a7bfced5..fc47b42f 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/impl/RegistryServiceNacosImpl.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/impl/RegistryServiceNacosImpl.java @@ -2,6 +2,7 @@ package com.gitee.sop.adminserver.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.api.naming.NamingService; @@ -18,11 +19,7 @@ import org.springframework.util.CollectionUtils; import javax.annotation.PostConstruct; import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * nacos接口实现, https://nacos.io/zh-cn/docs/open-api.html @@ -36,6 +33,9 @@ public class RegistryServiceNacosImpl implements RegistryService { @Value("${nacos.discovery.server-addr:${registry.nacos-server-addr:}}") private String nacosAddr; + @Value("${nacos.discovery.namespace:${spring.cloud.nacos.discovery.namespace:}}") + private String nacosNamespace; + private NamingService namingService; @PostConstruct @@ -43,7 +43,12 @@ public class RegistryServiceNacosImpl implements RegistryService { if (StringUtils.isBlank(nacosAddr)) { throw new IllegalArgumentException("请在配置文件中指定nacos.discovery.server-addr参数"); } - namingService = NamingFactory.createNamingService(nacosAddr); + Properties nacosProperties = new Properties(); + nacosProperties.put(PropertyKeyConst.SERVER_ADDR, nacosAddr); + if (StringUtils.isNotBlank(nacosNamespace)) { + nacosProperties.put(PropertyKeyConst.NAMESPACE, nacosNamespace); + } + namingService = NamingFactory.createNamingService(nacosProperties); } @Override