All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.smoketurner.dropwizard.consul.ribbon.ConsulServerList Maven / Gradle / Ivy

/*
 * Copyright © 2018 Smoke Turner, LLC ([email protected])
 *
 * 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
 *
 *     http://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.smoketurner.dropwizard.consul.ribbon;

import com.google.common.base.Strings;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import com.orbitz.consul.Consul;
import com.orbitz.consul.model.health.ServiceHealth;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class ConsulServerList implements ServerList {

  private final Consul consul;
  private final ConsulServiceDiscoverer serviceDiscoverer;

  /**
   * Constructor
   *
   * @param consul Consul client
   * @param serviceDiscoverer Discoverer
   */
  public ConsulServerList(final Consul consul, final ConsulServiceDiscoverer serviceDiscoverer) {
    this.consul = Objects.requireNonNull(consul);
    this.serviceDiscoverer = Objects.requireNonNull(serviceDiscoverer);
  }

  @Override
  public List getInitialListOfServers() {
    return buildServerList(serviceDiscoverer.discover(consul));
  }

  @Override
  public List getUpdatedListOfServers() {
    return buildServerList(serviceDiscoverer.discover(consul));
  }

  /**
   * Converts a list of {@link ServiceHealth} objects into {@link Server} objects
   *
   * @param services list of healthy service instances
   * @return list of server instances
   */
  private List buildServerList(final Collection services) {
    return services.stream().map(this::buildServer).collect(Collectors.toList());
  }

  /**
   * Build a {@link Server} instance from a Consul {@link ServiceHealth} instance. If the service
   * has an address defined, use that as the server host, otherwise default to using the node
   * address.
   *
   * @param service Consul service health record
   * @return Ribbon Server instance
   */
  private Server buildServer(final ServiceHealth service) {
    final Server server;
    if (!Strings.isNullOrEmpty(service.getService().getAddress())) {
      server = new Server(service.getService().getAddress(), service.getService().getPort());
    } else {
      server = new Server(service.getNode().getAddress(), service.getService().getPort());
    }
    server.setZone(service.getNode().getDatacenter().orElse(Server.UNKNOWN_ZONE));
    return server;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy