/** * A {@link LoadBalancer} that does no load balancing and always hits the given URL. * * @param url The URL * @return The {@link LoadBalancer} */ static LoadBalancer fixed(URL url) { return new FixedLoadBalancer(url); }
/** * Creates a {@link LoadBalancer} for the given service ID. * * @param serviceID The service ID * @return The {@link LoadBalancer} */ public LoadBalancer create(String serviceID) { return new DiscoveryClientRoundRobinLoadBalancer(serviceID, discoveryClient); }
/** * Creates a {@link LoadBalancer} from the given {@link ServiceInstanceList}. * * @param serviceInstanceList The {@link ServiceInstanceList} * @return The {@link LoadBalancer} */ public LoadBalancer create(ServiceInstanceList serviceInstanceList) { return new ServiceInstanceListRoundRobinLoadBalancer(serviceInstanceList); } }
/** * @param serviceID The service Id * @return An {@link Optional} with the load balancer */ protected Optional<? extends LoadBalancer> resolveLoadBalancerForServiceID(String serviceID) { if (serviceInstanceLists.containsKey(serviceID)) { ServiceInstanceList serviceInstanceList = serviceInstanceLists.get(serviceID); LoadBalancer loadBalancer = beanContext.getBean(ServiceInstanceListLoadBalancerFactory.class).create(serviceInstanceList); return Optional.ofNullable(loadBalancer); } else { LoadBalancer loadBalancer = beanContext.getBean(DiscoveryClientLoadBalancerFactory.class).create(serviceID); return Optional.of(loadBalancer); } } }
@Override public Publisher<ServiceInstance> select(@Nullable Object discriminator) { return Publishers.just(getNextAvailable(serviceInstanceList.getInstances())); }
/** * @param serviceInstances A list of service instances * @return The next available instance or a {@link NoAvailableServiceException} if none */ protected ServiceInstance getNextAvailable(List<ServiceInstance> serviceInstances) { List<ServiceInstance> availableServices = serviceInstances.stream() .filter(si -> si.getHealthStatus().equals(HealthStatus.UP)) .collect(Collectors.toList()); int len = availableServices.size(); if (len == 0) { throw new NoAvailableServiceException(getServiceID()); } int i = index.getAndAccumulate(len, (cur, n) -> cur >= n - 1 ? 0 : cur + 1); try { return availableServices.get(i); } catch (IndexOutOfBoundsException e) { throw new NoAvailableServiceException(getServiceID()); } } }
} else { if (loadBalancer instanceof FixedLoadBalancer) { contextPath = ((FixedLoadBalancer) loadBalancer).getUrl().getPath();
boolean isHealthCheck = configuration.isHealthCheck(); LoadBalancer loadBalancer = loadBalancerFactory.create(instanceList);
/** * @param serviceID The service Id * @return An {@link Optional} with the load balancer */ protected Optional<? extends LoadBalancer> resolveLoadBalancerForServiceID(String serviceID) { if (serviceInstanceLists.containsKey(serviceID)) { ServiceInstanceList serviceInstanceList = serviceInstanceLists.get(serviceID); LoadBalancer loadBalancer = beanContext.getBean(ServiceInstanceListLoadBalancerFactory.class).create(serviceInstanceList); return Optional.ofNullable(loadBalancer); } else { LoadBalancer loadBalancer = beanContext.getBean(DiscoveryClientLoadBalancerFactory.class).create(serviceID); return Optional.of(loadBalancer); } } }
/** * A {@link LoadBalancer} that does no load balancing and always hits the given URL. * * @param url The URL * @return The {@link LoadBalancer} */ static LoadBalancer fixed(URL url) { return new FixedLoadBalancer(url); }
/** * Creates a {@link LoadBalancer} for the given service ID. * * @param serviceID The service ID * @return The {@link LoadBalancer} */ public LoadBalancer create(String serviceID) { return new DiscoveryClientRoundRobinLoadBalancer(serviceID, discoveryClient); }
/** * Creates a {@link LoadBalancer} from the given {@link ServiceInstanceList}. * * @param serviceInstanceList The {@link ServiceInstanceList} * @return The {@link LoadBalancer} */ public LoadBalancer create(ServiceInstanceList serviceInstanceList) { return new ServiceInstanceListRoundRobinLoadBalancer(serviceInstanceList); } }
@Override public Publisher<ServiceInstance> select(@Nullable Object discriminator) { return Publishers.just(getNextAvailable(serviceInstanceList.getInstances())); }
/** * @param serviceInstances A list of service instances * @return The next available instance or a {@link NoAvailableServiceException} if none */ protected ServiceInstance getNextAvailable(List<ServiceInstance> serviceInstances) { List<ServiceInstance> availableServices = serviceInstances.stream() .filter(si -> si.getHealthStatus().equals(HealthStatus.UP)) .collect(Collectors.toList()); int len = availableServices.size(); if (len == 0) { throw new NoAvailableServiceException(getServiceID()); } int i = index.getAndAccumulate(len, (cur, n) -> cur >= n - 1 ? 0 : cur + 1); return availableServices.get(i); } }
} else { if (loadBalancer instanceof FixedLoadBalancer) { contextPath = ((FixedLoadBalancer) loadBalancer).getUrl().getPath();
/** * @param serviceID The service Id * @return An {@link Optional} with the load balancer */ protected Optional<? extends LoadBalancer> resolveLoadBalancerForServiceID(String serviceID) { if (serviceInstanceLists.containsKey(serviceID)) { ServiceInstanceList serviceInstanceList = serviceInstanceLists.get(serviceID); LoadBalancer loadBalancer = beanContext.getBean(ServiceInstanceListLoadBalancerFactory.class).create(serviceInstanceList); return Optional.ofNullable(loadBalancer); } else { LoadBalancer loadBalancer = beanContext.getBean(DiscoveryClientLoadBalancerFactory.class).create(serviceID); return Optional.of(loadBalancer); } } }
/** * A {@link LoadBalancer} that does no load balancing and always hits the given URL. * * @param url The URL * @return The {@link LoadBalancer} */ static LoadBalancer fixed(URL url) { return new FixedLoadBalancer(url); }
/** * Creates a {@link LoadBalancer} for the given service ID. * * @param serviceID The service ID * @return The {@link LoadBalancer} */ public LoadBalancer create(String serviceID) { return new DiscoveryClientRoundRobinLoadBalancer(serviceID, discoveryClient); }
/** * Creates a {@link LoadBalancer} from the given {@link ServiceInstanceList}. * * @param serviceInstanceList The {@link ServiceInstanceList} * @return The {@link LoadBalancer} */ public LoadBalancer create(ServiceInstanceList serviceInstanceList) { return new ServiceInstanceListRoundRobinLoadBalancer(serviceInstanceList); } }
@Override public Publisher<ServiceInstance> select(@Nullable Object discriminator) { return Publishers.just(getNextAvailable(serviceInstanceList.getInstances())); }