4444import java .util .concurrent .ConcurrentHashMap ;
4545import java .util .concurrent .atomic .AtomicBoolean ;
4646
47- import static com .alibaba .nacos .api .common .Constants .DEFAULT_GROUP ;
4847import static com .tencent .polaris .api .config .plugin .DefaultPlugins .SERVER_CONNECTOR_NACOS ;
4948import static com .tencent .polaris .plugins .connector .common .constant .NacosConstant .MetadataMapKey .*;
5049
@@ -70,7 +69,6 @@ public class NacosConnector extends DestroyableServerConnector {
7069 */
7170 private final AtomicBoolean initialized = new AtomicBoolean ();
7271
73-
7472 /**
7573 * Connector id .
7674 */
@@ -97,12 +95,7 @@ public class NacosConnector extends DestroyableServerConnector {
9795 private NacosContext nacosContext ;
9896
9997 /**
100- * Nacos namespace & NamingService mappings .
101- */
102- private final Map <String , NamingService > namingServices = new ConcurrentHashMap <>();
103-
104- /**
105- * Nacos namespace & NacosServiceMerger mappings .
98+ * Nacos namespace & NacosService mappings .
10699 */
107100 private final Map <String , NacosService > nacosServices = new ConcurrentHashMap <>();
108101
@@ -158,15 +151,14 @@ private void initActually(InitContext ctx, ServerConnectorConfig connectorConfig
158151 if (metadata .containsKey (NACOS_EPHEMERAL_KEY )) {
159152 nacosContext .setEphemeral (Boolean .parseBoolean (metadata .get (NACOS_EPHEMERAL_KEY )));
160153 }
161- if (metadata .containsKey (NACOS_WEIGHT_KEY )){
154+ if (metadata .containsKey (NACOS_WEIGHT_KEY )) {
162155 nacosContext .setNacosWeight (Double .parseDouble (metadata .get (NACOS_WEIGHT_KEY )));
163156 }
164157 if (metadata .containsKey (PropertyKeyConst .NAMESPACE ) && StringUtils .isNotEmpty (
165158 metadata .get (PropertyKeyConst .NAMESPACE ))) {
166159 nacosContext .setNamespace (metadata .get (PropertyKeyConst .NAMESPACE ));
167160 }
168- getOrCreateNamingService (nacosContext .getNamespace ());
169-
161+ getOrCreateNacosService (nacosContext .getNamespace ());
170162 }
171163
172164 private Properties decodeNacosConfigProperties (ServerConnectorConfig config ) {
@@ -188,49 +180,36 @@ private Properties decodeNacosConfigProperties(ServerConnectorConfig config) {
188180 return properties ;
189181 }
190182
191- public NamingService getOrCreateNamingService ( String namespace ) {
192- // nacos中一个命名空间仅对应一个sdk实例
193- NamingService namingService = namingServices .get (namespace );
194- if (namingService != null ) {
195- return namingService ;
183+
184+ public NacosService getOrCreateNacosService ( String namespace ) {
185+ NacosService nacosService = nacosServices .get (namespace );
186+ if (nacosService != null ) {
187+ return nacosService ;
196188 }
189+ // nacos sdk封装的服务,用于给polaris-java调用
197190 synchronized (lock ) {
191+ NamingService namingService ;
198192 Properties properties = new Properties (nacosProperties );
199193 // polaris 默认namespace 为default,nacos中映射为public
200194 if (StringUtils .isEmpty (nacosProperties .getProperty (PropertyKeyConst .NAMESPACE ))
201195 && !StringUtils .equals (namespace , "default" )) {
202196 properties .setProperty (PropertyKeyConst .NAMESPACE , namespace );
203197 }
204-
205198 try {
206199 namingService = NacosFactory .createNamingService (properties );
200+ Thread .sleep (1000 );
201+ nacosService = new NacosService (namingService , nacosContext );
202+ nacosServices .put (namespace , nacosService );
203+ return nacosService ;
207204 } catch (NacosException e ) {
208205 LOG .error ("[Connector][Nacos] fail to create naming service to {}, namespace {}" ,
209206 properties .get (PropertyKeyConst .SERVER_ADDR ), namespace , e );
210207 return null ;
211- }
212- try {
213- Thread .sleep (1000 );
214208 } catch (InterruptedException e ) {
215209 e .printStackTrace ();
216210 }
217- namingServices .put (namespace , namingService );
218- return namingService ;
219211 }
220- }
221-
222- public NacosService getNacosService (String namespace ) {
223- NacosService nacosService = nacosServices .get (namespace );
224- if (nacosService != null ) {
225- return nacosService ;
226- }
227- // nacos sdk封装的服务,用于给polaris-java调用
228- NamingService namingService = getOrCreateNamingService (namespace );
229- synchronized (lock ) {
230- nacosService = new NacosService (namingService , nacosContext );
231- nacosServices .put (namespace , nacosService );
232- }
233- return nacosService ;
212+ throw new PolarisException (ErrorCode .INTERNAL_ERROR , "Failed to initialize Nacos service for namespace: " + namespace );
234213 }
235214
236215 public NacosContext getNacosContext () {
@@ -258,13 +237,11 @@ public CommonProviderResponse registerInstance(CommonProviderRequest req,
258237 CommonProviderResponse response = new CommonProviderResponse ();
259238
260239 if (isRegisterEnable ()) {
261- NamingService namingService = getOrCreateNamingService (req .getNamespace ());
262-
240+ NamingService namingService = getOrCreateNacosService (req .getNamespace ()).getNamingService ();
263241 if (namingService == null ) {
264242 LOG .error ("[Nacos] fail to lookup namingService for service {}" , req .getService ());
265243 return null ;
266244 }
267-
268245 try {
269246 Instance instance = buildRegisterNacosInstance (req );
270247 namingService .registerInstance (instance .getServiceName (),
@@ -284,8 +261,7 @@ public CommonProviderResponse registerInstance(CommonProviderRequest req,
284261 public void deregisterInstance (CommonProviderRequest req ) throws PolarisException {
285262
286263 try {
287- NamingService service = getOrCreateNamingService (req .getNamespace ());
288-
264+ NamingService service = getOrCreateNacosService (req .getNamespace ()).getNamingService ();
289265 if (service == null ) {
290266 LOG .error ("[Nacos] fail to lookup namingService for service {}" , req .getService ());
291267 return ;
@@ -296,7 +272,6 @@ public void deregisterInstance(CommonProviderRequest req) throws PolarisExceptio
296272 serviceName = nacosContext .getServiceName ();
297273 }
298274 Instance instance = buildDeregisterNacosInstance (req , nacosContext .getGroupName ());
299-
300275 // deregister with nacos naming service
301276 service .deregisterInstance (serviceName , nacosContext .getGroupName (),
302277 instance );
@@ -380,15 +355,6 @@ protected void doDestroy() {
380355 nacosService .destroy ();
381356 });
382357 }
383- if (CollectionUtils .isNotEmpty (namingServices )) {
384- namingServices .forEach ((s , namingService ) -> {
385- try {
386- namingService .shutDown ();
387- } catch (NacosException ignore ) {
388- }
389- });
390- }
391-
392358 }
393359 }
394360
@@ -414,7 +380,6 @@ private Instance buildRegisterNacosInstance(CommonProviderRequest req) {
414380 instance .setWeight (nacosContext .getNacosWeight ());
415381 Map <String , String > metadata = new HashMap <>(Optional .ofNullable (req .getMetadata ())
416382 .orElse (Collections .emptyMap ()));
417-
418383 // 填充默认 protocol 以及 version 属性信息
419384 if (StringUtils .isNotEmpty (req .getProtocol ())) {
420385 metadata .put ("protocol" , req .getProtocol ());
@@ -438,12 +403,8 @@ private Instance buildRegisterNacosInstance(CommonProviderRequest req) {
438403 return instance ;
439404 }
440405
441- private Instance buildDeregisterNacosInstance (CommonProviderRequest req , String group ) {
442- String serviceName = req .getService ();
443- if (StringUtils .isNotEmpty (nacosContext .getServiceName ())) {
444- serviceName = nacosContext .getServiceName ();
445- }
446- String instanceId = String .format (INSTANCE_NAME , req .getNamespace (), group ,
406+ private Instance buildDeregisterNacosInstance (CommonProviderRequest req , String serviceName ) {
407+ String instanceId = String .format (INSTANCE_NAME , req .getNamespace (), nacosContext .getGroupName (),
447408 serviceName , req .getHost (), req .getPort ());
448409 Instance instance = new Instance ();
449410 instance .setInstanceId (instanceId );
0 commit comments