11 Ekim 2021 Pazartesi

SpringCloud LoadBalancer ServiceInstanceListSupplier Arayüzü

Giriş
Şu satırı dahil ederiz
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
get metodu - Request
Açıklaması şöyle
IgniteServiceInstanceListSuppler will return instances based on the request key.
Şöyle yaparız
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.client.loadbalancer.RequestDataContext;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;

public class IgniteServiceInstanceListSuppler implements ServiceInstanceListSupplier {
  private final IgniteEx ignite = ...;
  private volatile List<ServiceInstance> instances = ...;
  @Override
  public String getServiceId() {
    return ignite.localNode().consistentId().toString();
  }
  @Override
  public Flux<List<ServiceInstance>> get() {
    return Flux.just(instances);
  }
  @Override
  public Flux<List<ServiceInstance>> get(Request req) {
    if (req.getContext() instanceof RequestDataContext) {
      HttpHeaders headers = ((RequestDataContext)req.getContext()).
        .getClientRequest().getHeaders();
      String cacheName = headers.getFirst("affinity-cache-name");
      String affinityKey = headers.getFirst("affinity-key");

      Affinity<Object> affinity = affinities.computeIfAbsent(
        cacheName, k -> ((GatewayProtectedCacheProxy)ignite.cache(cacheName)).
          context().cache().affinity()
      );

      ClusterNode node = affinity.mapKeyToNode(affinityKey);
      if (node != null)
        return Flux.just(singletonList(toServiceInstance(node)));
    }
    return get();
  }
}


Hiç yorum yok:

Yorum Gönder