style fix

pull/3686/head
karsonto 10 months ago
parent bbff4b1afd
commit 0a1726f0c9

@ -22,8 +22,6 @@ import java.util.Map;
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.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
*/
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<Instance> instances) {
return getHostByRandomWeight(instances);
}
/**
* Choose instance by weight.
*
* @param instances Instance List
* @return the chosen instance
*/
public static Instance getHostByRandomWeight2(List<Instance> instances) {
return getHostByRandomWeight(instances);
}
/**
* Spring Cloud LoadBalancer Choose instance by weight.
* @param serviceInstances Instance List
* @return the chosen instance
*/
public static ServiceInstance getHostByRandomWeight3(
List<ServiceInstance> serviceInstances) {
Map<Instance, ServiceInstance> instanceMap = new HashMap<>();
List<Instance> 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<ServiceInstance> serviceInstances) {
Map<Instance, ServiceInstance> instanceMap = new HashMap<>();
List<Instance> 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);
}
}
}
}
}

@ -19,8 +19,6 @@ package com.alibaba.cloud.nacos.discovery;
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.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import org.springframework.cloud.client.ServiceInstance;
/**
* @author <a href="mailto:echooy.mxq@gmail.com">echooymxq</a>
* @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<ServiceInstance> getInstances(String serviceId) throws NacosException {
String group = discoveryProperties.getGroup();
List<Instance> 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<ServiceInstance> getInstances(String serviceId) throws NacosException {
String group = discoveryProperties.getGroup();
List<Instance> 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<String> getServices() throws NacosException {
String group = discoveryProperties.getGroup();
ListView<String> 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<String> getServices() throws NacosException {
String group = discoveryProperties.getGroup();
ListView<String> services = namingService().getServicesOfServer(1, Integer.MAX_VALUE, group);
return services.getData();
}
public static List<ServiceInstance> hostToServiceInstanceList(
List<Instance> instances, String serviceId) {
List<ServiceInstance> 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<ServiceInstance> hostToServiceInstanceList(List<Instance> instances, String serviceId) {
List<ServiceInstance> 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();
}
}

@ -19,116 +19,118 @@ 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;
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> {
T build();
}
static class InstanceServiceBuilder implements Builder<Instance> {
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<String, String> 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<ServiceInstance> {
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<String, String> 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> {
T build();
}
final static class InstanceServiceBuilder implements Builder<Instance> {
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<String, String> 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<ServiceInstance> {
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<String, String> 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;
}
}
}

Loading…
Cancel
Save