@@ -233,21 +233,15 @@ public void deRegisterServiceHandler(ServiceEventKey eventKey) throws PolarisExc
233233 public CommonProviderResponse registerInstance (CommonProviderRequest req ,
234234 Map <String , String > customHeader ) throws PolarisException {
235235 CommonProviderResponse response = new CommonProviderResponse ();
236-
237236 if (isRegisterEnable ()) {
238237 NamingService namingService = getOrCreateNacosService (req .getNamespace ()).getNamingService ();
239238 if (namingService == null ) {
240239 LOG .error ("nacos client fail to lookup namingService for service {}" , req .getService ());
241240 return null ;
242241 }
243242 try {
244- // nacos上注册和polaris不同的服务名时优先用nacosContext中的serviceName
245- String serviceName = req .getService ();
246- if (StringUtils .isNotEmpty (nacosContext .getServiceName ())) {
247- serviceName = nacosContext .getServiceName ();
248- }
249- Instance instance = buildRegisterNacosInstance (req , serviceName );
250- namingService .registerInstance (serviceName ,
243+ Instance instance = buildRegisterNacosInstance (req );
244+ namingService .registerInstance (instance .getServiceName (),
251245 nacosContext .getGroupName (), instance );
252246 response .setInstanceID (instance .getInstanceId ());
253247 } catch (NacosException e ) {
@@ -260,34 +254,6 @@ public CommonProviderResponse registerInstance(CommonProviderRequest req,
260254 return response ;
261255 }
262256
263- @ Override
264- public void deregisterInstance (CommonProviderRequest req ) throws PolarisException {
265-
266- try {
267- NamingService service = getOrCreateNacosService (req .getNamespace ()).getNamingService ();
268- if (service == null ) {
269- LOG .error ("nacos client fail to lookup namingService for service {}" , req .getService ());
270- return ;
271- }
272- // 优先设置成nacos的service name,如没有再设置成req的service name
273- String serviceName = req .getService ();
274- if (StringUtils .isNotEmpty (nacosContext .getServiceName ())) {
275- serviceName = nacosContext .getServiceName ();
276- }
277- Instance instance = buildDeregisterNacosInstance (req , nacosContext .getGroupName ());
278- // deregister with nacos naming service
279- service .deregisterInstance (serviceName , nacosContext .getGroupName (),
280- instance );
281- LOG .info ("nacos client deregister service {} success, groupName: {}, clusterName: {}, instance: {}" ,
282- serviceName , nacosContext .getGroupName (), nacosContext .getClusterName (), instance );
283- } catch (NacosException e ) {
284- throw new RetriableException (ErrorCode .NETWORK_ERROR ,
285- String .format ("nacos fail to deregister host %s:%d service %s" , req .getHost (),
286- req .getPort (),
287- req .getService ()), e );
288- }
289- }
290-
291257 @ Override
292258 public void heartbeat (CommonProviderRequest req ) throws PolarisException {
293259 // do nothing
@@ -361,8 +327,9 @@ protected void doDestroy() {
361327 }
362328 }
363329
364- private Instance buildRegisterNacosInstance (CommonProviderRequest req , String serviceName ) {
330+ private Instance buildRegisterNacosInstance (CommonProviderRequest req ) {
365331
332+ String serviceName = getServiceName (req );
366333 String nameSpace = req .getNamespace ();
367334
368335 String instanceId = String .format (INSTANCE_NAME , nameSpace , nacosContext .getGroupName (),
@@ -402,10 +369,46 @@ private Instance buildRegisterNacosInstance(CommonProviderRequest req, String se
402369 return instance ;
403370 }
404371
405- private Instance buildDeregisterNacosInstance (CommonProviderRequest req , String serviceName ) {
372+ @ Override
373+ public void deregisterInstance (CommonProviderRequest req ) throws PolarisException {
374+
375+ try {
376+ NamingService service = getOrCreateNacosService (req .getNamespace ()).getNamingService ();
377+ if (service == null ) {
378+ LOG .error ("nacos client fail to lookup namingService for service {}" , req .getService ());
379+ return ;
380+ }
381+ // 优先设置成nacos的service name,如没有再设置成req的service name
382+
383+ Instance instance = buildDeregisterNacosInstance (req );
384+ // deregister with nacos naming service
385+ service .deregisterInstance (instance .getServiceName (), nacosContext .getGroupName (),
386+ instance );
387+ LOG .info ("nacos client deregister service {} success, groupName: {}, clusterName: {}, instance: {}" ,
388+ instance .getServiceName (), nacosContext .getGroupName (), nacosContext .getClusterName (), instance );
389+ } catch (NacosException e ) {
390+ throw new RetriableException (ErrorCode .NETWORK_ERROR ,
391+ String .format ("nacos fail to deregister host %s:%d service %s" , req .getHost (),
392+ req .getPort (),
393+ req .getService ()), e );
394+ }
395+ }
396+
397+ private String getServiceName (CommonProviderRequest req ) {
398+ // nacos上注册和polaris不同的服务名时优先用nacosContext中的serviceName
399+ String serviceName = req .getService ();
400+ if (StringUtils .isNotEmpty (nacosContext .getServiceName ())) {
401+ serviceName = nacosContext .getServiceName ();
402+ }
403+ return serviceName ;
404+ }
405+
406+ private Instance buildDeregisterNacosInstance (CommonProviderRequest req ) {
407+ String serviceName = getServiceName (req );
406408 String instanceId = String .format (INSTANCE_NAME , req .getNamespace (), nacosContext .getGroupName (),
407409 serviceName , req .getHost (), req .getPort ());
408410 Instance instance = new Instance ();
411+ instance .setServiceName (serviceName );
409412 instance .setInstanceId (instanceId );
410413 instance .setEnabled (true );
411414 instance .setEphemeral (nacosContext .isEphemeral ());
0 commit comments