@@ -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 ) {
@@ -259,7 +253,6 @@ public CommonProviderResponse registerInstance(CommonProviderRequest req,
259253 }
260254 return response ;
261255 }
262-
263256 @ Override
264257 public void deregisterInstance (CommonProviderRequest req ) throws PolarisException {
265258
@@ -270,24 +263,20 @@ public void deregisterInstance(CommonProviderRequest req) throws PolarisExceptio
270263 return ;
271264 }
272265 // 优先设置成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 ());
266+
267+ Instance instance = buildDeregisterNacosInstance (req );
278268 // deregister with nacos naming service
279- service .deregisterInstance (serviceName , nacosContext .getGroupName (),
269+ service .deregisterInstance (instance . getServiceName () , nacosContext .getGroupName (),
280270 instance );
281271 LOG .info ("nacos client deregister service {} success, groupName: {}, clusterName: {}, instance: {}" ,
282- serviceName , nacosContext .getGroupName (), nacosContext .getClusterName (), instance );
272+ instance . getServiceName () , nacosContext .getGroupName (), nacosContext .getClusterName (), instance );
283273 } catch (NacosException e ) {
284274 throw new RetriableException (ErrorCode .NETWORK_ERROR ,
285275 String .format ("nacos fail to deregister host %s:%d service %s" , req .getHost (),
286276 req .getPort (),
287277 req .getService ()), e );
288278 }
289279 }
290-
291280 @ Override
292281 public void heartbeat (CommonProviderRequest req ) throws PolarisException {
293282 // do nothing
@@ -361,8 +350,9 @@ protected void doDestroy() {
361350 }
362351 }
363352
364- private Instance buildRegisterNacosInstance (CommonProviderRequest req , String serviceName ) {
353+ private Instance buildRegisterNacosInstance (CommonProviderRequest req ) {
365354
355+ String serviceName = getServiceName (req );
366356 String nameSpace = req .getNamespace ();
367357
368358 String instanceId = String .format (INSTANCE_NAME , nameSpace , nacosContext .getGroupName (),
@@ -402,10 +392,21 @@ private Instance buildRegisterNacosInstance(CommonProviderRequest req, String se
402392 return instance ;
403393 }
404394
405- private Instance buildDeregisterNacosInstance (CommonProviderRequest req , String serviceName ) {
395+ private String getServiceName (CommonProviderRequest req ) {
396+ // nacos上注册和polaris不同的服务名时优先用nacosContext中的serviceName
397+ String serviceName = req .getService ();
398+ if (StringUtils .isNotEmpty (nacosContext .getServiceName ())) {
399+ serviceName = nacosContext .getServiceName ();
400+ }
401+ return serviceName ;
402+ }
403+
404+ private Instance buildDeregisterNacosInstance (CommonProviderRequest req ) {
405+ String serviceName = getServiceName (req );
406406 String instanceId = String .format (INSTANCE_NAME , req .getNamespace (), nacosContext .getGroupName (),
407407 serviceName , req .getHost (), req .getPort ());
408408 Instance instance = new Instance ();
409+ instance .setServiceName (serviceName );
409410 instance .setInstanceId (instanceId );
410411 instance .setEnabled (true );
411412 instance .setEphemeral (nacosContext .isEphemeral ());
0 commit comments