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

io.smallrye.stork.servicediscovery.composite.CompositeServiceDiscoveryProvider Maven / Gradle / Ivy

Go to download

A service discovery provider that combines multiple service discoveries. The mechanism returns services from _all_ the discovery providers that it combines, That is, it obtains service lists from all services and just then returns a combined list.

The newest version!
package io.smallrye.stork.servicediscovery.composite;

import java.util.ArrayList;
import java.util.List;

import jakarta.enterprise.context.ApplicationScoped;

import io.smallrye.stork.api.ServiceDiscovery;
import io.smallrye.stork.api.config.ServiceConfig;
import io.smallrye.stork.api.config.ServiceDiscoveryAttribute;
import io.smallrye.stork.api.config.ServiceDiscoveryType;
import io.smallrye.stork.spi.ServiceDiscoveryProvider;
import io.smallrye.stork.spi.StorkInfrastructure;

/**
 * A service discovery provider creating {@link CompositeServiceDiscovery} instances.
 */
@ServiceDiscoveryType("composite")
@ServiceDiscoveryAttribute(name = "services", description = "A comma-separated list of services that this services consists of.", required = true)
@ApplicationScoped
public class CompositeServiceDiscoveryProvider
        implements ServiceDiscoveryProvider {

    @Override
    public ServiceDiscovery createServiceDiscovery(CompositeConfiguration config, String serviceName,
            ServiceConfig serviceConfig, StorkInfrastructure storkInfrastructure) {
        // Service names for composite config should be listed as a comma separated list:
        // stork..discovery.services=serviceA,serviceB,serviceC
        String serviceList = config.getServices();
        if (serviceList == null) {
            throw new IllegalArgumentException("'services' property missing for service '" + serviceName + "'. " +
                    "Please provide a comma separated list of service names.");
        }

        String[] services = serviceList.split(",");

        List serviceNames = new ArrayList<>();
        for (String service : services) {
            service = service.trim();
            if (service.isBlank()) {
                throw new IllegalArgumentException("Blank constituent service name for service '" + serviceName + "'");
            }
            serviceNames.add(service);
        }
        return new CompositeServiceDiscovery(serviceName, serviceNames);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy