From bbff4b1afd65173fd2c28342db16110b0d1cb290 Mon Sep 17 00:00:00 2001 From: karsonto Date: Fri, 12 Apr 2024 11:38:30 +0800 Subject: [PATCH 1/2] enhancement --- .../cloud/nacos/balancer/NacosBalancer.java | 14 +- .../discovery/NacosServiceDiscovery.java | 139 +++++++----------- .../util/NacosServiceInstanceConverter.java | 134 +++++++++++++++++ 3 files changed, 192 insertions(+), 95 deletions(-) create mode 100644 spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/util/NacosServiceInstanceConverter.java diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/balancer/NacosBalancer.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/balancer/NacosBalancer.java index 836cffe498..e96dc2deb5 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/balancer/NacosBalancer.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/balancer/NacosBalancer.java @@ -22,15 +22,15 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; +import org.springframework.cloud.client.ServiceInstance; import com.alibaba.cloud.commons.lang.StringUtils; import com.alibaba.cloud.nacos.NacosServiceInstance; import com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer; +import com.alibaba.cloud.nacos.util.NacosServiceInstanceConverter; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.client.naming.core.Balancer; -import org.springframework.cloud.client.ServiceInstance; - /** * @author itmuch.com XuDaojie * @since 2021.1 @@ -59,15 +59,7 @@ public static ServiceInstance getHostByRandomWeight3( List serviceInstances) { Map instanceMap = new HashMap<>(); List nacosInstance = serviceInstances.stream().map(serviceInstance -> { - Map metadata = serviceInstance.getMetadata(); - - // see - // com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery.hostToServiceInstance() - Instance instance = new Instance(); - instance.setIp(serviceInstance.getHost()); - instance.setPort(serviceInstance.getPort()); - instance.setWeight(Double.parseDouble(metadata.get("nacos.weight"))); - instance.setHealthy(Boolean.parseBoolean(metadata.get("nacos.healthy"))); + Instance instance = NacosServiceInstanceConverter.fromServiceInstance(serviceInstance); instanceMap.put(instance, serviceInstance); return instance; }).collect(Collectors.toList()); diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/discovery/NacosServiceDiscovery.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/discovery/NacosServiceDiscovery.java index a786be8e8e..8092d08684 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/discovery/NacosServiceDiscovery.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/discovery/NacosServiceDiscovery.java @@ -17,104 +17,75 @@ package com.alibaba.cloud.nacos.discovery; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; + +import org.springframework.cloud.client.ServiceInstance; import com.alibaba.cloud.nacos.NacosDiscoveryProperties; -import com.alibaba.cloud.nacos.NacosServiceInstance; import com.alibaba.cloud.nacos.NacosServiceManager; +import com.alibaba.cloud.nacos.util.NacosServiceInstanceConverter; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; -import org.springframework.cloud.client.ServiceInstance; - /** * @author echooymxq * @author changjin wei(魏昌进) **/ public class NacosServiceDiscovery { - private NacosDiscoveryProperties discoveryProperties; - - private NacosServiceManager nacosServiceManager; - - public NacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties, - NacosServiceManager nacosServiceManager) { - this.discoveryProperties = discoveryProperties; - this.nacosServiceManager = nacosServiceManager; - } - - /** - * Return all instances for the given service. - * @param serviceId id of service - * @return list of instances - * @throws NacosException nacosException - */ - public List getInstances(String serviceId) throws NacosException { - String group = discoveryProperties.getGroup(); - List instances = namingService().selectInstances(serviceId, group, - true); - return hostToServiceInstanceList(instances, serviceId); - } - - /** - * Return the names of all services. - * @return list of service names - * @throws NacosException nacosException - */ - public List getServices() throws NacosException { - String group = discoveryProperties.getGroup(); - ListView services = namingService().getServicesOfServer(1, - Integer.MAX_VALUE, group); - return services.getData(); - } - - public static List hostToServiceInstanceList( - List instances, String serviceId) { - List result = new ArrayList<>(instances.size()); - for (Instance instance : instances) { - ServiceInstance serviceInstance = hostToServiceInstance(instance, serviceId); - if (serviceInstance != null) { - result.add(serviceInstance); - } - } - return result; - } - - public static ServiceInstance hostToServiceInstance(Instance instance, - String serviceId) { - if (instance == null || !instance.isEnabled() || !instance.isHealthy()) { - return null; - } - NacosServiceInstance nacosServiceInstance = new NacosServiceInstance(); - nacosServiceInstance.setHost(instance.getIp()); - nacosServiceInstance.setPort(instance.getPort()); - nacosServiceInstance.setServiceId(serviceId); - nacosServiceInstance.setInstanceId(instance.getInstanceId()); - - Map metadata = new HashMap<>(); - metadata.put("nacos.instanceId", instance.getInstanceId()); - metadata.put("nacos.weight", instance.getWeight() + ""); - metadata.put("nacos.healthy", instance.isHealthy() + ""); - metadata.put("nacos.cluster", instance.getClusterName() + ""); - if (instance.getMetadata() != null) { - metadata.putAll(instance.getMetadata()); - } - metadata.put("nacos.ephemeral", String.valueOf(instance.isEphemeral())); - nacosServiceInstance.setMetadata(metadata); - - if (metadata.containsKey("secure")) { - boolean secure = Boolean.parseBoolean(metadata.get("secure")); - nacosServiceInstance.setSecure(secure); - } - return nacosServiceInstance; - } - - private NamingService namingService() { - return nacosServiceManager.getNamingService(); - } + private NacosDiscoveryProperties discoveryProperties; + + private NacosServiceManager nacosServiceManager; + + public NacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties, + NacosServiceManager nacosServiceManager) { + this.discoveryProperties = discoveryProperties; + this.nacosServiceManager = nacosServiceManager; + } + + /** + * Return all instances for the given service. + * + * @param serviceId id of service + * @return list of instances + * @throws NacosException nacosException + */ + public List getInstances(String serviceId) throws NacosException { + String group = discoveryProperties.getGroup(); + List instances = namingService().selectInstances(serviceId, group, + true); + return hostToServiceInstanceList(instances, serviceId); + } + + /** + * Return the names of all services. + * + * @return list of service names + * @throws NacosException nacosException + */ + public List getServices() throws NacosException { + String group = discoveryProperties.getGroup(); + ListView services = namingService().getServicesOfServer(1, + Integer.MAX_VALUE, group); + return services.getData(); + } + + public static List hostToServiceInstanceList( + List instances, String serviceId) { + List result = new ArrayList<>(instances.size()); + for (Instance instance : instances) { + ServiceInstance serviceInstance = NacosServiceInstanceConverter.fromInstanceAndServiceId(instance, serviceId); + if (serviceInstance != null) { + result.add(serviceInstance); + } + } + return result; + } + + private NamingService namingService() { + return nacosServiceManager.getNamingService(); + } } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/util/NacosServiceInstanceConverter.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/util/NacosServiceInstanceConverter.java new file mode 100644 index 0000000000..4ce8bac729 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/util/NacosServiceInstanceConverter.java @@ -0,0 +1,134 @@ +/* + * Copyright 2013-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.cloud.nacos.util; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.cloud.client.ServiceInstance; + +import com.alibaba.cloud.nacos.NacosServiceInstance; +import com.alibaba.nacos.api.naming.pojo.Instance; + +/** + * @author Karson + */ +public class NacosServiceInstanceConverter { + + public static final String WEIGHT = "nacos.weight"; + + public static final String HEALTHY = "nacos.healthy"; + + public static ServiceInstance fromInstanceAndServiceId(Instance instance, String serviceId) { + return ServiceInstanceBuilder.fromInstanceAndServiceId(instance, serviceId).build(); + } + + public static Instance fromServiceInstance(ServiceInstance instance) { + return InstanceServiceBuilder.fromServiceInstance(instance).build(); + } + + interface Builder { + + T build(); + } + + static class InstanceServiceBuilder implements Builder { + + private ServiceInstance serviceInstance; + + private InstanceServiceBuilder() { + + } + + private void setServiceInstance(ServiceInstance serviceInstance) { + this.serviceInstance = serviceInstance; + } + + private static InstanceServiceBuilder fromServiceInstance(ServiceInstance instance) { + InstanceServiceBuilder instanceServiceBuilder = new InstanceServiceBuilder(); + instanceServiceBuilder.setServiceInstance(instance); + return instanceServiceBuilder; + } + + @Override + public Instance build() { + Instance instance = new Instance(); + Map metadata = serviceInstance.getMetadata(); + instance.setIp(serviceInstance.getHost()); + instance.setPort(serviceInstance.getPort()); + instance.setWeight(Double.parseDouble(metadata.get(WEIGHT))); + instance.setHealthy(Boolean.parseBoolean(metadata.get(HEALTHY))); + return instance; + } + } + + + static class ServiceInstanceBuilder implements Builder { + + private Instance instance; + private String serviceId; + + private ServiceInstanceBuilder() { + } + + private static ServiceInstanceBuilder fromInstanceAndServiceId(Instance instance, String serviceId) { + ServiceInstanceBuilder nacosServiceInstanceBuilder = new ServiceInstanceBuilder(); + nacosServiceInstanceBuilder.setInstance(instance); + nacosServiceInstanceBuilder.setServiceId(serviceId); + return nacosServiceInstanceBuilder; + } + + + private void setInstance(Instance instance) { + this.instance = instance; + } + + private void setServiceId(String serviceId) { + this.serviceId = serviceId; + } + + @Override + public ServiceInstance build() { + if (this.instance == null || !this.instance.isEnabled() || !this.instance.isHealthy()) { + return null; + } + NacosServiceInstance nacosServiceInstance = new NacosServiceInstance(); + nacosServiceInstance.setHost(instance.getIp()); + nacosServiceInstance.setPort(instance.getPort()); + nacosServiceInstance.setServiceId(serviceId); + nacosServiceInstance.setInstanceId(instance.getInstanceId()); + + Map metadata = new HashMap<>(); + metadata.put("nacos.instanceId", instance.getInstanceId()); + metadata.put(WEIGHT, instance.getWeight() + ""); + metadata.put(HEALTHY, instance.isHealthy() + ""); + metadata.put("nacos.cluster", instance.getClusterName() + ""); + if (instance.getMetadata() != null) { + metadata.putAll(instance.getMetadata()); + } + metadata.put("nacos.ephemeral", String.valueOf(instance.isEphemeral())); + nacosServiceInstance.setMetadata(metadata); + + if (metadata.containsKey("secure")) { + boolean secure = Boolean.parseBoolean(metadata.get("secure")); + nacosServiceInstance.setSecure(secure); + } + return nacosServiceInstance; + } + } + +} From 0a1726f0c999ae90c6f178a8e893634fb1352f32 Mon Sep 17 00:00:00 2001 From: karsonto Date: Fri, 12 Apr 2024 13:58:55 +0800 Subject: [PATCH 2/2] style fix --- .../cloud/nacos/balancer/NacosBalancer.java | 87 +++---- .../discovery/NacosServiceDiscovery.java | 91 ++++---- .../util/NacosServiceInstanceConverter.java | 212 +++++++++--------- 3 files changed, 194 insertions(+), 196 deletions(-) diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/balancer/NacosBalancer.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/balancer/NacosBalancer.java index e96dc2deb5..f1fc916224 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/balancer/NacosBalancer.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/balancer/NacosBalancer.java @@ -22,8 +22,6 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; -import org.springframework.cloud.client.ServiceInstance; - import com.alibaba.cloud.commons.lang.StringUtils; import com.alibaba.cloud.nacos.NacosServiceInstance; import com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer; @@ -31,59 +29,62 @@ import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.client.naming.core.Balancer; +import org.springframework.cloud.client.ServiceInstance; + + /** * @author itmuch.com XuDaojie * @since 2021.1 */ public class NacosBalancer extends Balancer { - private static final String IPV4_REGEX = "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}"; + private static final String IPV4_REGEX = "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}"; - private static final String IPV6_KEY = "IPv6"; + private static final String IPV6_KEY = "IPv6"; - /** - * Choose instance by weight. - * @param instances Instance List - * @return the chosen instance - */ - public static Instance getHostByRandomWeight2(List instances) { - return getHostByRandomWeight(instances); - } + /** + * Choose instance by weight. + * + * @param instances Instance List + * @return the chosen instance + */ + public static Instance getHostByRandomWeight2(List instances) { + return getHostByRandomWeight(instances); + } - /** - * Spring Cloud LoadBalancer Choose instance by weight. - * @param serviceInstances Instance List - * @return the chosen instance - */ - public static ServiceInstance getHostByRandomWeight3( - List serviceInstances) { - Map instanceMap = new HashMap<>(); - List nacosInstance = serviceInstances.stream().map(serviceInstance -> { - Instance instance = NacosServiceInstanceConverter.fromServiceInstance(serviceInstance); - instanceMap.put(instance, serviceInstance); - return instance; - }).collect(Collectors.toList()); + /** + * Spring Cloud LoadBalancer Choose instance by weight. + * + * @param serviceInstances Instance List + * @return the chosen instance + */ + public static ServiceInstance getHostByRandomWeight3(List serviceInstances) { + Map instanceMap = new HashMap<>(); + List nacosInstance = serviceInstances.stream().map(serviceInstance -> { + Instance instance = NacosServiceInstanceConverter.fromServiceInstance(serviceInstance); + instanceMap.put(instance, serviceInstance); + return instance; + }).collect(Collectors.toList()); - Instance instance = getHostByRandomWeight2(nacosInstance); - NacosServiceInstance nacosServiceInstance = (NacosServiceInstance) instanceMap.get(instance); - // When local support IPv6 address stack, referred to use IPv6 address. - if (StringUtils.isNotEmpty(NacosLoadBalancer.ipv6)) { - convertIPv4ToIPv6(nacosServiceInstance); + Instance instance = getHostByRandomWeight2(nacosInstance); + NacosServiceInstance nacosServiceInstance = (NacosServiceInstance) instanceMap.get(instance); + // When local support IPv6 address stack, referred to use IPv6 address. + if (StringUtils.isNotEmpty(NacosLoadBalancer.ipv6)) { + convertIPv4ToIPv6(nacosServiceInstance); + } + return nacosServiceInstance; } - return nacosServiceInstance; - } - /** - * There is two type Ip,using IPv6 should use IPv6 in metadata to replace IPv4 in IP - * field. - */ - private static void convertIPv4ToIPv6(NacosServiceInstance instance) { - if (Pattern.matches(IPV4_REGEX, instance.getHost())) { - String ip = instance.getMetadata().get(IPV6_KEY); - if (StringUtils.isNotEmpty(ip)) { - instance.setHost(ip); - } + /** + * There is two type Ip,using IPv6 should use IPv6 in metadata to replace IPv4 in IP field. + */ + private static void convertIPv4ToIPv6(NacosServiceInstance instance) { + if (Pattern.matches(IPV4_REGEX, instance.getHost())) { + String ip = instance.getMetadata().get(IPV6_KEY); + if (StringUtils.isNotEmpty(ip)) { + instance.setHost(ip); + } + } } - } } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/discovery/NacosServiceDiscovery.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/discovery/NacosServiceDiscovery.java index 8092d08684..3b7aebb2d8 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/discovery/NacosServiceDiscovery.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/discovery/NacosServiceDiscovery.java @@ -19,8 +19,6 @@ import java.util.ArrayList; import java.util.List; -import org.springframework.cloud.client.ServiceInstance; - import com.alibaba.cloud.nacos.NacosDiscoveryProperties; import com.alibaba.cloud.nacos.NacosServiceManager; import com.alibaba.cloud.nacos.util.NacosServiceInstanceConverter; @@ -29,63 +27,60 @@ import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; +import org.springframework.cloud.client.ServiceInstance; /** * @author echooymxq * @author changjin wei(魏昌进) **/ public class NacosServiceDiscovery { - private NacosDiscoveryProperties discoveryProperties; + private NacosDiscoveryProperties discoveryProperties; - private NacosServiceManager nacosServiceManager; + private NacosServiceManager nacosServiceManager; - public NacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties, - NacosServiceManager nacosServiceManager) { - this.discoveryProperties = discoveryProperties; - this.nacosServiceManager = nacosServiceManager; - } + public NacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties, NacosServiceManager nacosServiceManager) { + this.discoveryProperties = discoveryProperties; + this.nacosServiceManager = nacosServiceManager; + } - /** - * Return all instances for the given service. - * - * @param serviceId id of service - * @return list of instances - * @throws NacosException nacosException - */ - public List getInstances(String serviceId) throws NacosException { - String group = discoveryProperties.getGroup(); - List instances = namingService().selectInstances(serviceId, group, - true); - return hostToServiceInstanceList(instances, serviceId); - } + /** + * Return all instances for the given service. + * + * @param serviceId id of service + * @return list of instances + * @throws NacosException nacosException + */ + public List getInstances(String serviceId) throws NacosException { + String group = discoveryProperties.getGroup(); + List instances = namingService().selectInstances(serviceId, group, true); + return hostToServiceInstanceList(instances, serviceId); + } - /** - * Return the names of all services. - * - * @return list of service names - * @throws NacosException nacosException - */ - public List getServices() throws NacosException { - String group = discoveryProperties.getGroup(); - ListView services = namingService().getServicesOfServer(1, - Integer.MAX_VALUE, group); - return services.getData(); - } + /** + * Return the names of all services. + * + * @return list of service names + * @throws NacosException nacosException + */ + public List getServices() throws NacosException { + String group = discoveryProperties.getGroup(); + ListView services = namingService().getServicesOfServer(1, Integer.MAX_VALUE, group); + return services.getData(); + } - public static List hostToServiceInstanceList( - List instances, String serviceId) { - List result = new ArrayList<>(instances.size()); - for (Instance instance : instances) { - ServiceInstance serviceInstance = NacosServiceInstanceConverter.fromInstanceAndServiceId(instance, serviceId); - if (serviceInstance != null) { - result.add(serviceInstance); - } - } - return result; - } + public static List hostToServiceInstanceList(List instances, String serviceId) { + List result = new ArrayList<>(instances.size()); + for (Instance instance : instances) { + ServiceInstance serviceInstance = NacosServiceInstanceConverter.fromInstanceAndServiceId(instance, serviceId); + if (serviceInstance != null) { + result.add(serviceInstance); + } + } + return result; + } - private NamingService namingService() { - return nacosServiceManager.getNamingService(); - } + private NamingService namingService() { + return nacosServiceManager.getNamingService(); + } } diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/util/NacosServiceInstanceConverter.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/util/NacosServiceInstanceConverter.java index 4ce8bac729..0544e2f27f 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/util/NacosServiceInstanceConverter.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/util/NacosServiceInstanceConverter.java @@ -19,116 +19,118 @@ import java.util.HashMap; import java.util.Map; -import org.springframework.cloud.client.ServiceInstance; - import com.alibaba.cloud.nacos.NacosServiceInstance; import com.alibaba.nacos.api.naming.pojo.Instance; +import org.springframework.cloud.client.ServiceInstance; + + + /** * @author Karson */ -public class NacosServiceInstanceConverter { - - public static final String WEIGHT = "nacos.weight"; - - public static final String HEALTHY = "nacos.healthy"; - - public static ServiceInstance fromInstanceAndServiceId(Instance instance, String serviceId) { - return ServiceInstanceBuilder.fromInstanceAndServiceId(instance, serviceId).build(); - } - - public static Instance fromServiceInstance(ServiceInstance instance) { - return InstanceServiceBuilder.fromServiceInstance(instance).build(); - } - - interface Builder { - - T build(); - } - - static class InstanceServiceBuilder implements Builder { - - private ServiceInstance serviceInstance; - - private InstanceServiceBuilder() { - - } - - private void setServiceInstance(ServiceInstance serviceInstance) { - this.serviceInstance = serviceInstance; - } - - private static InstanceServiceBuilder fromServiceInstance(ServiceInstance instance) { - InstanceServiceBuilder instanceServiceBuilder = new InstanceServiceBuilder(); - instanceServiceBuilder.setServiceInstance(instance); - return instanceServiceBuilder; - } - - @Override - public Instance build() { - Instance instance = new Instance(); - Map metadata = serviceInstance.getMetadata(); - instance.setIp(serviceInstance.getHost()); - instance.setPort(serviceInstance.getPort()); - instance.setWeight(Double.parseDouble(metadata.get(WEIGHT))); - instance.setHealthy(Boolean.parseBoolean(metadata.get(HEALTHY))); - return instance; - } - } - - - static class ServiceInstanceBuilder implements Builder { - - private Instance instance; - private String serviceId; - - private ServiceInstanceBuilder() { - } - - private static ServiceInstanceBuilder fromInstanceAndServiceId(Instance instance, String serviceId) { - ServiceInstanceBuilder nacosServiceInstanceBuilder = new ServiceInstanceBuilder(); - nacosServiceInstanceBuilder.setInstance(instance); - nacosServiceInstanceBuilder.setServiceId(serviceId); - return nacosServiceInstanceBuilder; - } - - - private void setInstance(Instance instance) { - this.instance = instance; - } - - private void setServiceId(String serviceId) { - this.serviceId = serviceId; - } - - @Override - public ServiceInstance build() { - if (this.instance == null || !this.instance.isEnabled() || !this.instance.isHealthy()) { - return null; - } - NacosServiceInstance nacosServiceInstance = new NacosServiceInstance(); - nacosServiceInstance.setHost(instance.getIp()); - nacosServiceInstance.setPort(instance.getPort()); - nacosServiceInstance.setServiceId(serviceId); - nacosServiceInstance.setInstanceId(instance.getInstanceId()); - - Map metadata = new HashMap<>(); - metadata.put("nacos.instanceId", instance.getInstanceId()); - metadata.put(WEIGHT, instance.getWeight() + ""); - metadata.put(HEALTHY, instance.isHealthy() + ""); - metadata.put("nacos.cluster", instance.getClusterName() + ""); - if (instance.getMetadata() != null) { - metadata.putAll(instance.getMetadata()); - } - metadata.put("nacos.ephemeral", String.valueOf(instance.isEphemeral())); - nacosServiceInstance.setMetadata(metadata); - - if (metadata.containsKey("secure")) { - boolean secure = Boolean.parseBoolean(metadata.get("secure")); - nacosServiceInstance.setSecure(secure); - } - return nacosServiceInstance; - } - } +final public class NacosServiceInstanceConverter { + + private static final String WEIGHT = "nacos.weight"; + + private static final String HEALTHY = "nacos.healthy"; + + private NacosServiceInstanceConverter() { } + + public static ServiceInstance fromInstanceAndServiceId(Instance instance, String serviceId) { + return ServiceInstanceBuilder.fromInstanceAndServiceId(instance, serviceId).build(); + } + + public static Instance fromServiceInstance(ServiceInstance instance) { + return InstanceServiceBuilder.fromServiceInstance(instance).build(); + } + + interface Builder { + + T build(); + } + + final static class InstanceServiceBuilder implements Builder { + + private ServiceInstance serviceInstance; + + private InstanceServiceBuilder() { + + } + + private void setServiceInstance(ServiceInstance serviceInstance) { + this.serviceInstance = serviceInstance; + } + + private static InstanceServiceBuilder fromServiceInstance(ServiceInstance instance) { + InstanceServiceBuilder instanceServiceBuilder = new InstanceServiceBuilder(); + instanceServiceBuilder.setServiceInstance(instance); + return instanceServiceBuilder; + } + + @Override + public Instance build() { + Instance instance = new Instance(); + Map metadata = serviceInstance.getMetadata(); + instance.setIp(serviceInstance.getHost()); + instance.setPort(serviceInstance.getPort()); + instance.setWeight(Double.parseDouble(metadata.get(WEIGHT))); + instance.setHealthy(Boolean.parseBoolean(metadata.get(HEALTHY))); + return instance; + } + } + + final static class ServiceInstanceBuilder implements Builder { + + private Instance instance; + private String serviceId; + + private ServiceInstanceBuilder() { + } + + private static ServiceInstanceBuilder fromInstanceAndServiceId(Instance instance, String serviceId) { + ServiceInstanceBuilder nacosServiceInstanceBuilder = new ServiceInstanceBuilder(); + nacosServiceInstanceBuilder.setInstance(instance); + nacosServiceInstanceBuilder.setServiceId(serviceId); + return nacosServiceInstanceBuilder; + } + + private void setInstance(Instance instance) { + this.instance = instance; + } + + private void setServiceId(String serviceId) { + this.serviceId = serviceId; + } + + @Override + public ServiceInstance build() { + if (this.instance == null || !this.instance.isEnabled() || !this.instance.isHealthy()) { + return null; + } + NacosServiceInstance nacosServiceInstance = new NacosServiceInstance(); + nacosServiceInstance.setHost(instance.getIp()); + nacosServiceInstance.setPort(instance.getPort()); + nacosServiceInstance.setServiceId(serviceId); + nacosServiceInstance.setInstanceId(instance.getInstanceId()); + + Map metadata = new HashMap<>(); + metadata.put("nacos.instanceId", instance.getInstanceId()); + metadata.put(WEIGHT, instance.getWeight() + ""); + metadata.put(HEALTHY, instance.isHealthy() + ""); + metadata.put("nacos.cluster", instance.getClusterName() + ""); + if (instance.getMetadata() != null) { + metadata.putAll(instance.getMetadata()); + } + metadata.put("nacos.ephemeral", String.valueOf(instance.isEphemeral())); + nacosServiceInstance.setMetadata(metadata); + + if (metadata.containsKey("secure")) { + boolean secure = Boolean.parseBoolean(metadata.get("secure")); + nacosServiceInstance.setSecure(secure); + } + return nacosServiceInstance; + } + } }