diff --git a/api/revapi.json b/api/revapi.json index e444dede..a68a9644 100644 --- a/api/revapi.json +++ b/api/revapi.json @@ -33,8 +33,101 @@ "code": "java.method.addedToInterface", "new": "method java.util.Optional io.smallrye.stork.api.ServiceInstance::getPath()", "justification": "New method to handle API gateways" + }, + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method void io.smallrye.stork.api.Service::(java.lang.String, io.smallrye.stork.api.LoadBalancer, io.smallrye.stork.api.ServiceDiscovery, boolean)", + "new": "method void io.smallrye.stork.api.Service::(java.lang.String, io.smallrye.stork.api.LoadBalancer, io.smallrye.stork.api.ServiceDiscovery, io.smallrye.stork.api.ServiceRegistrar, boolean)", + "justification": "Service Registrar is now part of the Service" + }, + { + "ignore": true, + "code": "java.method.addedToInterface", + "new": "method io.smallrye.stork.api.config.ConfigWithType io.smallrye.stork.api.config.ServiceConfig::serviceRegistrar()", + "justification": "Registrar included in the service configuration" + }, + { + "ignore": true, + "code": "java.method.removed", + "old": "method java.util.List io.smallrye.stork.spi.config.ConfigProvider::getRegistrarConfigs()", + "justification": "Registrar configuration is now part of configs" + }, + { + "ignore": true, + "code": "java.method.parameterTypeChanged", + "old": "parameter io.smallrye.stork.api.ServiceRegistrar io.smallrye.stork.spi.internal.ServiceRegistrarLoader & io.smallrye.stork.api.MetadataKey>::createServiceRegistrar(===io.smallrye.stork.api.config.ServiceRegistrarConfig===, io.smallrye.stork.spi.StorkInfrastructure)", + "new": "parameter io.smallrye.stork.api.ServiceRegistrar io.smallrye.stork.spi.internal.ServiceRegistrarLoader & io.smallrye.stork.api.MetadataKey>::createServiceRegistrar(===io.smallrye.stork.api.config.ConfigWithType===, io.smallrye.stork.spi.StorkInfrastructure)", + "parameterIndex": "0", + "justification": "Use the ConfigWithType more generic type for registrars." + }, + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method io.smallrye.stork.api.ServiceRegistrar io.smallrye.stork.spi.internal.ServiceRegistrarLoader & io.smallrye.stork.api.MetadataKey>::createServiceRegistrar(io.smallrye.stork.api.config.ServiceRegistrarConfig, io.smallrye.stork.spi.StorkInfrastructure)", + "new": "method io.smallrye.stork.api.ServiceRegistrar io.smallrye.stork.spi.internal.ServiceRegistrarLoader & io.smallrye.stork.api.MetadataKey>::createServiceRegistrar(io.smallrye.stork.api.config.ConfigWithType, java.lang.String, io.smallrye.stork.spi.StorkInfrastructure)", + "justification": "Service name is not needed anymore. It is present at Service config level." + }, + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method void io.smallrye.stork.test.TestConfigProvider::addServiceConfig(java.lang.String, java.lang.String, java.lang.String, java.util.Map, java.util.Map)", + "new": "method void io.smallrye.stork.test.TestConfigProvider::addServiceConfig(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.Map, java.util.Map, boolean)", + "justification": "Registrars are now part of configs" + }, + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method void io.smallrye.stork.test.TestConfigProvider::addServiceConfig(java.lang.String, java.lang.String, java.lang.String, java.util.Map, java.util.Map, boolean)", + "new": "method void io.smallrye.stork.test.TestConfigProvider::addServiceConfig(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.Map, java.util.Map, java.util.Map)", + "justification": "Registrars are now part of configs" + }, + { + "ignore": true, + "code": "java.method.removed", + "old": "method void io.smallrye.stork.test.TestConfigProvider::addServiceRegistrarConfig(java.lang.String, java.lang.String, java.util.Map)", + "justification": "Registrars are now part of configs" + }, + { + "ignore": true, + "code": "java.method.removed", + "old": "method java.util.List io.smallrye.stork.test.TestConfigProvider::getRegistrarConfigs()", + "justification": "Registrars are now part of configs" + }, + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method void io.smallrye.stork.test.TestConfigProviderBean::addServiceConfig(java.lang.String, java.lang.String, java.lang.String, java.util.Map, java.util.Map)", + "new": "method void io.smallrye.stork.test.TestConfigProviderBean::addServiceConfig(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.Map, java.util.Map, java.util.Map)", + "justification": "Registrars are now part of configs" + }, + { + "ignore": true, + "code": "java.method.removed", + "old": "method void io.smallrye.stork.test.TestConfigProviderBean::addServiceRegistrarConfig(java.lang.String, java.lang.String, java.util.Map)", + "justification": "Registrars are now part of configs" + }, + { + "ignore": true, + "code": "java.method.removed", + "old": "method java.util.List io.smallrye.stork.test.TestConfigProviderBean::getRegistrarConfigs()", + "justification": "Registrars are now part of configs" + }, + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method io.smallrye.stork.api.ServiceRegistrar io.smallrye.stork.test.TestServiceRegistrarProviderLoader::createServiceRegistrar(io.smallrye.stork.api.config.ServiceRegistrarConfig, io.smallrye.stork.spi.StorkInfrastructure)", + "new": "method io.smallrye.stork.api.ServiceRegistrar io.smallrye.stork.test.TestServiceRegistrarProviderLoader::createServiceRegistrar(io.smallrye.stork.api.config.ConfigWithType, java.lang.String, io.smallrye.stork.spi.StorkInfrastructure)", + "justification": "Use the ConfigWithType more generic type for registrars and service name." + }, + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method io.smallrye.stork.api.ServiceDefinition io.smallrye.stork.api.ServiceDefinition::of(io.smallrye.stork.api.config.ConfigWithType, io.smallrye.stork.api.config.ConfigWithType)", + "new": "method io.smallrye.stork.api.ServiceDefinition io.smallrye.stork.api.ServiceDefinition::of(io.smallrye.stork.api.config.ConfigWithType, io.smallrye.stork.api.config.ConfigWithType, io.smallrye.stork.api.config.ConfigWithType)", + "justification": "Registrar included." } - ] + ] } }, { "extension" : "revapi.reporter.json", diff --git a/api/src/main/java/io/smallrye/stork/api/Service.java b/api/src/main/java/io/smallrye/stork/api/Service.java index 68b5496d..79d3d634 100644 --- a/api/src/main/java/io/smallrye/stork/api/Service.java +++ b/api/src/main/java/io/smallrye/stork/api/Service.java @@ -16,6 +16,7 @@ public class Service { private final Semaphore instanceSelectionLock; private final LoadBalancer loadBalancer; private final ServiceDiscovery serviceDiscovery; + private final ServiceRegistrar serviceRegistrar; private final String serviceName; /** @@ -24,12 +25,14 @@ public class Service { * @param serviceName the name, must not be {@code null}, must not be blank * @param loadBalancer the load balancer, can be {@code null} * @param serviceDiscovery the service discovery, must not be {@code null} + * @param serviceRegistrar the service registrar, can be {@code null} * @param requiresStrictRecording whether strict recording must be enabled */ public Service(String serviceName, LoadBalancer loadBalancer, ServiceDiscovery serviceDiscovery, - boolean requiresStrictRecording) { + ServiceRegistrar serviceRegistrar, boolean requiresStrictRecording) { this.loadBalancer = loadBalancer; this.serviceDiscovery = serviceDiscovery; + this.serviceRegistrar = serviceRegistrar; this.serviceName = serviceName; this.instanceSelectionLock = requiresStrictRecording ? new Semaphore(1) : null; } @@ -146,6 +149,15 @@ public ServiceDiscovery getServiceDiscovery() { return serviceDiscovery; } + /** + * Get the underlying service registrar + * + * @return service registrar + */ + public ServiceRegistrar getServiceRegistrar() { + return serviceRegistrar; + } + /** * @return the service name. */ diff --git a/api/src/main/java/io/smallrye/stork/api/ServiceDefinition.java b/api/src/main/java/io/smallrye/stork/api/ServiceDefinition.java index a33913ee..722d9a0b 100644 --- a/api/src/main/java/io/smallrye/stork/api/ServiceDefinition.java +++ b/api/src/main/java/io/smallrye/stork/api/ServiceDefinition.java @@ -9,10 +9,12 @@ public class ServiceDefinition { private final ConfigWithType loadBalancer; private final ConfigWithType serviceDiscovery; + private final ConfigWithType serviceRegistrar; - private ServiceDefinition(ConfigWithType sd, ConfigWithType lb) { + private ServiceDefinition(ConfigWithType sd, ConfigWithType lb, ConfigWithType sr) { serviceDiscovery = ParameterValidation.nonNull(sd, "service discovery config"); loadBalancer = lb; + serviceRegistrar = sr; } /** @@ -30,10 +32,22 @@ public static ServiceDefinition of(ConfigWithType sd) { * * @param sd the service discovery config, must not be {@code null} * @param lb the load balancer config, if {@code null}, round-robin is used. + * @param sr the service registrar config, must not be {@code null} + * @return the created service definition + */ + public static ServiceDefinition of(ConfigWithType sd, ConfigWithType lb, ConfigWithType sr) { + return new ServiceDefinition(sd, lb, sr); + } + + /** + * Creates a new {@link ServiceDefinition} using the given {@link ConfigWithType} . + * + * @param sd the service discovery config, must not be {@code null} + * @param lb the load balancer config, if {@code null}, round-robin is used. * @return the created service definition */ public static ServiceDefinition of(ConfigWithType sd, ConfigWithType lb) { - return new ServiceDefinition(sd, lb); + return new ServiceDefinition(sd, lb, null); } /** @@ -49,4 +63,11 @@ public ConfigWithType getLoadBalancer() { public ConfigWithType getServiceDiscovery() { return serviceDiscovery; } + + /** + * @return the configured service discovery config. + */ + public ConfigWithType getServiceRegistrar() { + return serviceRegistrar; + } } diff --git a/api/src/main/java/io/smallrye/stork/api/ServiceRegistrar.java b/api/src/main/java/io/smallrye/stork/api/ServiceRegistrar.java index 325f0d0f..80b451ff 100644 --- a/api/src/main/java/io/smallrye/stork/api/ServiceRegistrar.java +++ b/api/src/main/java/io/smallrye/stork/api/ServiceRegistrar.java @@ -9,4 +9,5 @@ default Uni registerServiceInstance(String serviceName, String ipAddress, } Uni registerServiceInstance(String serviceName, Metadata metadata, String ipAddress, int port); + } diff --git a/api/src/main/java/io/smallrye/stork/api/config/ServiceConfig.java b/api/src/main/java/io/smallrye/stork/api/config/ServiceConfig.java index a4b53c4e..e56af3eb 100644 --- a/api/src/main/java/io/smallrye/stork/api/config/ServiceConfig.java +++ b/api/src/main/java/io/smallrye/stork/api/config/ServiceConfig.java @@ -1,7 +1,8 @@ package io.smallrye.stork.api.config; /** - * Service configuration, wraps both (optional) LoadBalancer configuration and (required) ServiceDiscovery configuration + * Service configuration, wraps (optional) LoadBalancer configuration, (required) ServiceDiscovery configuration and (optional) + * ServiceRegistrar configuration * for a single service */ public interface ServiceConfig { @@ -25,6 +26,13 @@ public interface ServiceConfig { */ ConfigWithType serviceDiscovery(); + /** + * ServiceRegistrar configuration for the service + * + * @return (required) service registrar configuration + */ + ConfigWithType serviceRegistrar(); + /** * Whether the communication should use a secure connection (e.g. HTTPS) * diff --git a/api/src/main/java/io/smallrye/stork/api/config/ServiceRegistrarType.java b/api/src/main/java/io/smallrye/stork/api/config/ServiceRegistrarType.java index 4bc4c046..81ef0175 100644 --- a/api/src/main/java/io/smallrye/stork/api/config/ServiceRegistrarType.java +++ b/api/src/main/java/io/smallrye/stork/api/config/ServiceRegistrarType.java @@ -14,7 +14,7 @@ *

* The type is used to determine service registrar. *

- * Use {@code stork.service-registration.my-registration=my-service-registrar} to use the + * Use {@code stork.my-registration.service-registrar.type=my-service-registrar} to use the * {@link ServiceRegistrarProvider} annotated with {@code @ServiceRegistrar("my-service-registrar")} */ @Retention(RetentionPolicy.RUNTIME) diff --git a/api/src/main/java/io/smallrye/stork/spi/ServiceRegistrarProvider.java b/api/src/main/java/io/smallrye/stork/spi/ServiceRegistrarProvider.java index b5248971..793bff68 100644 --- a/api/src/main/java/io/smallrye/stork/spi/ServiceRegistrarProvider.java +++ b/api/src/main/java/io/smallrye/stork/spi/ServiceRegistrarProvider.java @@ -3,7 +3,7 @@ import io.smallrye.stork.api.MetadataKey; import io.smallrye.stork.api.ServiceRegistrar; -public interface ServiceRegistrarProvider & MetadataKey> { - ServiceRegistrar createServiceRegistrar(ConfigType config, String serviceRegistrarName, +public interface ServiceRegistrarProvider & MetadataKey> { + ServiceRegistrar createServiceRegistrar(T config, String serviceRegistrarName, StorkInfrastructure infrastructure); } diff --git a/api/src/main/java/io/smallrye/stork/spi/config/ConfigProvider.java b/api/src/main/java/io/smallrye/stork/spi/config/ConfigProvider.java index 8ce8cba7..dbcc788f 100644 --- a/api/src/main/java/io/smallrye/stork/spi/config/ConfigProvider.java +++ b/api/src/main/java/io/smallrye/stork/spi/config/ConfigProvider.java @@ -1,10 +1,8 @@ package io.smallrye.stork.spi.config; -import java.util.Collections; import java.util.List; import io.smallrye.stork.api.config.ServiceConfig; -import io.smallrye.stork.api.config.ServiceRegistrarConfig; /** * Configuration provider for Service Discovery and Load Balancer @@ -18,15 +16,6 @@ public interface ConfigProvider { */ List getConfigs(); - /** - * Get a list of service registrar configurations - * - * @return a list of configurations - */ - default List getRegistrarConfigs() { - return Collections.emptyList(); - } - /** * Priority of the configuration provider. * A single ConfigProvider is used in an application, the one with the highest priority diff --git a/api/src/main/java/io/smallrye/stork/spi/config/SimpleServiceConfig.java b/api/src/main/java/io/smallrye/stork/spi/config/SimpleServiceConfig.java index ceb7a813..a23c61fb 100644 --- a/api/src/main/java/io/smallrye/stork/spi/config/SimpleServiceConfig.java +++ b/api/src/main/java/io/smallrye/stork/spi/config/SimpleServiceConfig.java @@ -15,14 +15,17 @@ public class SimpleServiceConfig implements ServiceConfig { private final String serviceName; private final ConfigWithType loadBalancerConfig; - private final ConfigWithType ConfigWithType; + private final ConfigWithType serviceDiscoveryConfig; + + private final ConfigWithType serviceRegistrarConfig; private SimpleServiceConfig(String serviceName, ConfigWithType loadBalancerConfig, - ConfigWithType ConfigWithType) { + ConfigWithType serviceDiscoveryConfig, ConfigWithType serviceRegistrarConfig) { this.serviceName = serviceName; this.loadBalancerConfig = loadBalancerConfig; - this.ConfigWithType = ConfigWithType; + this.serviceDiscoveryConfig = serviceDiscoveryConfig; + this.serviceRegistrarConfig = serviceRegistrarConfig; } @Override @@ -37,7 +40,12 @@ public ConfigWithType loadBalancer() { @Override public ConfigWithType serviceDiscovery() { - return ConfigWithType; + return serviceDiscoveryConfig; + } + + @Override + public ConfigWithType serviceRegistrar() { + return serviceRegistrarConfig; } /** @@ -46,7 +54,9 @@ public ConfigWithType serviceDiscovery() { public static class Builder { String serviceName; ConfigWithType loadBalancerConfig; - ConfigWithType configWithType; + ConfigWithType serviceDiscoveryConfig; + + ConfigWithType serviceRegistrarConfig; boolean secure; /** @@ -63,11 +73,22 @@ public Builder setLoadBalancer(ConfigWithType loadBalancer) { /** * Sets the service discovery config. * - * @param serviceDiscovery the service discovery config + * @param serviceDiscoveryConfig the service discovery config * @return the current builder */ - public Builder setServiceDiscovery(ConfigWithType serviceDiscovery) { - configWithType = serviceDiscovery; + public Builder setServiceDiscovery(ConfigWithType serviceDiscoveryConfig) { + this.serviceDiscoveryConfig = serviceDiscoveryConfig; + return this; + } + + /** + * Sets the service registrar config. + * + * @param serviceRegistrarConfig the service registrar config + * @return the current builder + */ + public Builder setServiceRegistrar(ConfigWithType serviceRegistrarConfig) { + this.serviceRegistrarConfig = serviceRegistrarConfig; return this; } @@ -99,7 +120,7 @@ public Builder setSecure(boolean secure) { * @return the built config */ public SimpleServiceConfig build() { - return new SimpleServiceConfig(serviceName, loadBalancerConfig, configWithType); + return new SimpleServiceConfig(serviceName, loadBalancerConfig, serviceDiscoveryConfig, serviceRegistrarConfig); } } @@ -164,4 +185,34 @@ public Map parameters() { } } + /** + * An implementation of {@link ConfigWithType} using an unmodifiable {@link Map} as backend to store + * the configuration. + */ + public static class SimpleServiceRegistrarConfig implements ConfigWithType { + private final String type; + private final Map parameters; + + /** + * Creates a new SimpleServiceRegistrarConfig. + * + * @param type the type + * @param parameters the configuration map + */ + public SimpleServiceRegistrarConfig(String type, Map parameters) { + this.type = type; + this.parameters = parameters; + } + + @Override + public String type() { + return type; + } + + @Override + public Map parameters() { + return parameters; + } + } + } diff --git a/api/src/main/java/io/smallrye/stork/spi/internal/ServiceRegistrarLoader.java b/api/src/main/java/io/smallrye/stork/spi/internal/ServiceRegistrarLoader.java index ed22d1c5..dbe8d6de 100644 --- a/api/src/main/java/io/smallrye/stork/spi/internal/ServiceRegistrarLoader.java +++ b/api/src/main/java/io/smallrye/stork/spi/internal/ServiceRegistrarLoader.java @@ -2,7 +2,7 @@ import io.smallrye.stork.api.MetadataKey; import io.smallrye.stork.api.ServiceRegistrar; -import io.smallrye.stork.api.config.ServiceRegistrarConfig; +import io.smallrye.stork.api.config.ConfigWithType; import io.smallrye.stork.spi.ElementWithType; import io.smallrye.stork.spi.StorkInfrastructure; @@ -15,9 +15,10 @@ public interface ServiceRegistrarLoader createServiceRegistrar(ServiceRegistrarConfig config, - StorkInfrastructure storkInfrastructure); + ServiceRegistrar createServiceRegistrar(ConfigWithType config, + String serviceName, StorkInfrastructure storkInfrastructure); } diff --git a/config-generator/src/main/java/io/smallrye/stork/config/generator/ConfigClassWriter.java b/config-generator/src/main/java/io/smallrye/stork/config/generator/ConfigClassWriter.java index 56176163..3679b2f2 100644 --- a/config-generator/src/main/java/io/smallrye/stork/config/generator/ConfigClassWriter.java +++ b/config-generator/src/main/java/io/smallrye/stork/config/generator/ConfigClassWriter.java @@ -191,12 +191,12 @@ public String createServiceRegistrarLoader(Element element, String metadataKey, out.println(" @Override"); out.println(format( - " public ServiceRegistrar<%s> createServiceRegistrar(ServiceRegistrarConfig config, ", + " public ServiceRegistrar<%s> createServiceRegistrar(ConfigWithType config, String serviceName, ", metadataKeyName)); out.println(" StorkInfrastructure storkInfrastructure) {"); out.println(format(" %s typedConfig = new %s(config.parameters());", configClassName, configClassName)); out.println( - " return provider.createServiceRegistrar(typedConfig, config.name(), storkInfrastructure);"); + " return provider.createServiceRegistrar(typedConfig, serviceName, storkInfrastructure);"); out.println(" }"); writeTypeMethod(type, out); diff --git a/core/src/main/java/io/smallrye/stork/Stork.java b/core/src/main/java/io/smallrye/stork/Stork.java index 524418d5..9ed39cc2 100644 --- a/core/src/main/java/io/smallrye/stork/Stork.java +++ b/core/src/main/java/io/smallrye/stork/Stork.java @@ -28,7 +28,6 @@ import io.smallrye.stork.api.ServiceRegistrar; import io.smallrye.stork.api.StorkServiceRegistry; import io.smallrye.stork.api.config.ServiceConfig; -import io.smallrye.stork.api.config.ServiceRegistrarConfig; import io.smallrye.stork.impl.RoundRobinLoadBalancer; import io.smallrye.stork.impl.RoundRobinLoadBalancerProvider; import io.smallrye.stork.integration.DefaultStorkInfrastructure; @@ -125,17 +124,13 @@ public Stork(StorkInfrastructure storkInfrastructure) { ConfigProvider configProvider = lookForConfigProvider(); if (configProvider != null) { for (ServiceConfig serviceConfig : configProvider.getConfigs()) { - Service service = createService(loadBalancerLoaders, serviceDiscoveryLoaders, serviceConfig); + Service service = createService(loadBalancerLoaders, serviceDiscoveryLoaders, registrarLoaders, serviceConfig); services.put(serviceConfig.serviceName(), service); } for (Service service : services.values()) { service.getServiceDiscovery().initialize(this); } - - for (ServiceRegistrarConfig registrarConfig : configProvider.getRegistrarConfigs()) { - serviceRegistrars.put(registrarConfig.name(), createServiceRegistrar(registrarConfig, registrarLoaders)); - } } } @@ -173,30 +168,21 @@ private static ConfigProvider lookForConfigProvider() { return highestPrioConfigProvider.orElse(null); } - private ServiceRegistrar createServiceRegistrar(ServiceRegistrarConfig registrarConfig, - Map registrarLoaders) { - ServiceRegistrarLoader registrarLoader = registrarLoaders.get(registrarConfig.type()); - if (registrarLoader == null) { - throw new IllegalArgumentException("No service registrar found for type " + registrarConfig.type()); - } - return registrarLoader.createServiceRegistrar(registrarConfig, infrastructure); - } - private Service createService(ServiceConfig serviceConfig) { return createService(loadFromServiceLoader(LoadBalancerLoader.class), loadFromServiceLoader(ServiceDiscoveryLoader.class), - serviceConfig); + loadFromServiceLoader(ServiceRegistrarLoader.class), serviceConfig); } private Service createService(Map loadBalancerLoaders, Map serviceDiscoveryProviders, - ServiceConfig serviceConfig) { - var configWithType = serviceConfig.serviceDiscovery(); - if (configWithType == null) { + Map serviceRegistrarLoaders, ServiceConfig serviceConfig) { + var serviceDiscoveryConfig = serviceConfig.serviceDiscovery(); + if (serviceDiscoveryConfig == null) { throw new IllegalArgumentException( "No service discovery defined for service " + serviceConfig.serviceName()); } - String serviceDiscoveryType = configWithType.type(); + String serviceDiscoveryType = serviceDiscoveryConfig.type(); if (serviceDiscoveryType == null) { throw new IllegalArgumentException( "Service discovery type not defined for service " + serviceConfig.serviceName()); @@ -211,12 +197,12 @@ private Service createService(Map loadBalancerLoader // Backward compatibility LOGGER.warn("The 'secure' attribute is deprecated, use the 'secure' service discovery attribute instead"); // We do not know if we can add to the parameters, such create a new SimpleConfigWithType - Map newConfig = new HashMap<>(configWithType.parameters()); + Map newConfig = new HashMap<>(serviceDiscoveryConfig.parameters()); newConfig.put("secure", "true"); - configWithType = new SimpleServiceConfig.SimpleServiceDiscoveryConfig(serviceDiscoveryType, newConfig); + serviceDiscoveryConfig = new SimpleServiceConfig.SimpleServiceDiscoveryConfig(serviceDiscoveryType, newConfig); } - final var serviceDiscovery = serviceDiscoveryProvider.createServiceDiscovery(configWithType, + final var serviceDiscovery = serviceDiscoveryProvider.createServiceDiscovery(serviceDiscoveryConfig, serviceConfig.serviceName(), serviceConfig, infrastructure); final var loadBalancerConfig = serviceConfig.loadBalancer(); @@ -236,7 +222,22 @@ private Service createService(Map loadBalancerLoader loadBalancer = loadBalancerProvider.createLoadBalancer(loadBalancerConfig, serviceDiscovery); } - return new Service(serviceConfig.serviceName(), loadBalancer, serviceDiscovery, + final var serviceRegistrarConfig = serviceConfig.serviceRegistrar(); + ServiceRegistrar serviceRegistrar = null; + if (serviceRegistrarConfig == null) { + LOGGER.debug("No service registrar configured for service {}", serviceConfig.serviceName()); + } else { + String serviceRegistrarType = serviceRegistrarConfig.type(); + final var serviceRegistrarLoader = serviceRegistrarLoaders.get(serviceRegistrarType); + if (serviceRegistrarLoader == null) { + throw new IllegalArgumentException("No ServiceRegistrarLoader for type " + serviceRegistrarType); + } + + serviceRegistrar = serviceRegistrarLoader.createServiceRegistrar(serviceRegistrarConfig, + serviceConfig.serviceName(), infrastructure); + } + + return new Service(serviceConfig.serviceName(), loadBalancer, serviceDiscovery, serviceRegistrar, loadBalancer.requiresStrictRecording()); } @@ -278,8 +279,14 @@ public static void initialize() { initialize(new DefaultStorkInfrastructure()); } - //mstodo: configuration should rather be separate, metadata key might not be the best thing to re use @SuppressWarnings("unchecked") + /** + * Whether the communication should use a secure connection (e.g. HTTPS) + * + * @return true if SSL, TLS, etc. should be used for the communication + * @deprecated Use stork.getService("my-service").getServiceRegistrar() method instead + */ + @Deprecated public & MetadataKey> ServiceRegistrar getServiceRegistrar( String registrarName) { ServiceRegistrar registrar = serviceRegistrars.get(registrarName); diff --git a/core/src/main/java/io/smallrye/stork/impl/DefaultServiceInstance.java b/core/src/main/java/io/smallrye/stork/impl/DefaultServiceInstance.java index 06548704..467f8d54 100644 --- a/core/src/main/java/io/smallrye/stork/impl/DefaultServiceInstance.java +++ b/core/src/main/java/io/smallrye/stork/impl/DefaultServiceInstance.java @@ -2,6 +2,7 @@ import java.util.Collections; import java.util.Map; +import java.util.Objects; import java.util.Optional; import io.smallrye.stork.api.Metadata; @@ -96,4 +97,19 @@ public Metadata getMetadata() { public Map getLabels() { return labels; } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof DefaultServiceInstance)) + return false; + DefaultServiceInstance that = (DefaultServiceInstance) o; + return getPort() == that.getPort() && getHost().equals(that.getHost()); + } + + @Override + public int hashCode() { + return Objects.hash(getHost(), getPort()); + } } diff --git a/core/src/main/java/io/smallrye/stork/utils/StorkAddressUtils.java b/core/src/main/java/io/smallrye/stork/utils/StorkAddressUtils.java index 67802f98..d53d0993 100644 --- a/core/src/main/java/io/smallrye/stork/utils/StorkAddressUtils.java +++ b/core/src/main/java/io/smallrye/stork/utils/StorkAddressUtils.java @@ -1,7 +1,5 @@ package io.smallrye.stork.utils; -import java.net.InetAddress; - import static java.lang.String.format; /** @@ -39,6 +37,31 @@ public static HostAndPort parseToHostAndPort(String address, } } + /** + * Creates a new {@link String} instance containing either {@code host:port} or just {@code host}. + * + * @param hostAndPort {@link HostAndPort} instance from an address. + * @return the String containing either {@code host:port} or just {@code host} + */ + public static String parseToString(HostAndPort hostAndPort) { + if (hostAndPort == null) { + return ""; + } + String hostAndPortString = hostAndPort.host; + if (hostAndPort.port != 0) { + hostAndPortString += ":" + hostAndPort.port; + } + if (hostAndPort.path.isPresent()) { + if (hostAndPort.path.get().startsWith("/")) { + hostAndPortString += hostAndPort.path.get(); + } else { + hostAndPortString += "/" + hostAndPort.path.get(); + } + } + + return hostAndPortString; + } + private static HostAndPort parseNonIpv6Address(String serviceAddress, int defaultPort, String serviceName) { String[] hostAndMaybePort = serviceAddress.split(":"); diff --git a/core/src/test/java/io/smallrye/stork/FakeServiceConfig.java b/core/src/test/java/io/smallrye/stork/FakeServiceConfig.java index dfa3f4a8..6521fbe8 100644 --- a/core/src/test/java/io/smallrye/stork/FakeServiceConfig.java +++ b/core/src/test/java/io/smallrye/stork/FakeServiceConfig.java @@ -8,11 +8,13 @@ public class FakeServiceConfig implements ServiceConfig { private final String name; private final ConfigWithType lb; private final ConfigWithType sd; + private final ConfigWithType sr; - public FakeServiceConfig(String name, ConfigWithType sd, ConfigWithType lb) { + public FakeServiceConfig(String name, ConfigWithType sd, ConfigWithType lb, ConfigWithType sr) { this.name = name; this.lb = lb; this.sd = sd; + this.sr = sr; } @Override @@ -30,6 +32,11 @@ public ConfigWithType serviceDiscovery() { return sd; } + @Override + public ConfigWithType serviceRegistrar() { + return sr; + } + @Override public boolean secure() { return false; diff --git a/core/src/test/java/io/smallrye/stork/StorkTest.java b/core/src/test/java/io/smallrye/stork/StorkTest.java index 987acd16..856e571e 100644 --- a/core/src/test/java/io/smallrye/stork/StorkTest.java +++ b/core/src/test/java/io/smallrye/stork/StorkTest.java @@ -153,7 +153,7 @@ public String type() { public Map parameters() { return null; } - }, null)); + }, null, null)); TestEnv.install(ConfigProvider.class, TestEnv.AnchoredConfigProvider.class); Assertions.assertThrows(IllegalArgumentException.class, Stork::initialize); @@ -161,7 +161,7 @@ public Map parameters() { @Test public void testServiceWithServiceDiscoveryButNoMatchingProvider() { - TestEnv.configurations.add(new FakeServiceConfig("a", SERVICE_DISCOVERY_CONFIG_WITH_INVALID_PROVIDER, null)); + TestEnv.configurations.add(new FakeServiceConfig("a", SERVICE_DISCOVERY_CONFIG_WITH_INVALID_PROVIDER, null, null)); TestEnv.install(ConfigProvider.class, TestEnv.AnchoredConfigProvider.class); Assertions.assertThrows(IllegalArgumentException.class, Stork::initialize); } @@ -169,7 +169,7 @@ public void testServiceWithServiceDiscoveryButNoMatchingProvider() { @Test public void testWithLoadBalancerButNoMatchingProvider() { TestEnv.configurations - .add(new FakeServiceConfig("a", FAKE_SERVICE_DISCOVERY_CONFIG, LOAD_BALANCER_WITH_INVALID_PROVIDER)); + .add(new FakeServiceConfig("a", FAKE_SERVICE_DISCOVERY_CONFIG, LOAD_BALANCER_WITH_INVALID_PROVIDER, null)); TestEnv.install(ConfigProvider.class, TestEnv.AnchoredConfigProvider.class); Assertions.assertThrows(IllegalArgumentException.class, Stork::initialize); } @@ -177,7 +177,7 @@ public void testWithLoadBalancerButNoMatchingProvider() { @Test public void testWithServiceDiscoveryAndASingleServiceInstance() { TestEnv.configurations.add(new FakeServiceConfig("a", - FAKE_SERVICE_DISCOVERY_CONFIG, null)); + FAKE_SERVICE_DISCOVERY_CONFIG, null, null)); ServiceInstance instance = mock(ServiceInstance.class); AnchoredServiceDiscoveryProvider.services.add(instance); TestEnv.install(ConfigProvider.class, TestEnv.AnchoredConfigProvider.class); @@ -193,7 +193,7 @@ public void testWithServiceDiscoveryAndASingleServiceInstance() { @Test public void testWithLegacySecureServiceDiscovery() { TestEnv.configurations.add(new FakeSecureServiceConfig("s", - FAKE_SERVICE_DISCOVERY_CONFIG, null)); + FAKE_SERVICE_DISCOVERY_CONFIG, null, null)); ServiceInstance instance = mock(ServiceInstance.class); AnchoredServiceDiscoveryProvider.services.add(instance); TestEnv.install(ConfigProvider.class, TestEnv.AnchoredConfigProvider.class); @@ -209,7 +209,7 @@ public void testWithLegacySecureServiceDiscovery() { @Test public void testWithSecureServiceDiscovery() { TestEnv.configurations.add(new FakeServiceConfig("s", - FAKE_SECURE_SERVICE_DISCOVERY_CONFIG, null)); + FAKE_SECURE_SERVICE_DISCOVERY_CONFIG, null, null)); ServiceInstance instance = mock(ServiceInstance.class); AnchoredServiceDiscoveryProvider.services.add(instance); TestEnv.install(ConfigProvider.class, TestEnv.AnchoredConfigProvider.class); @@ -225,7 +225,7 @@ public void testWithSecureServiceDiscovery() { @Test public void testWithServiceDiscoveryAndATwoServiceInstances() { TestEnv.configurations.add(new FakeServiceConfig("a", - FAKE_SERVICE_DISCOVERY_CONFIG, null)); + FAKE_SERVICE_DISCOVERY_CONFIG, null, null)); ServiceInstance instance1 = mock(ServiceInstance.class); ServiceInstance instance2 = mock(ServiceInstance.class); AnchoredServiceDiscoveryProvider.services.add(instance1); @@ -245,7 +245,7 @@ public void testWithServiceDiscoveryAndATwoServiceInstances() { @Test public void testWithLoadBalancer() { TestEnv.configurations.add(new FakeServiceConfig("a", - FAKE_SERVICE_DISCOVERY_CONFIG, FAKE_LOAD_BALANCER_CONFIG)); + FAKE_SERVICE_DISCOVERY_CONFIG, FAKE_LOAD_BALANCER_CONFIG, null)); TestEnv.install(ConfigProvider.class, TestEnv.AnchoredConfigProvider.class); Stork.initialize(); Stork stork = Stork.getInstance(); @@ -263,7 +263,7 @@ public void testWithDefaultLoadBalancer() { AnchoredServiceDiscoveryProvider.services.add(instance3); TestEnv.configurations.add(new FakeServiceConfig("a", - FAKE_SERVICE_DISCOVERY_CONFIG, null)); + FAKE_SERVICE_DISCOVERY_CONFIG, null, null)); TestEnv.install(ConfigProvider.class, TestEnv.AnchoredConfigProvider.class); @@ -298,7 +298,7 @@ public String type() { public Map parameters() { return Collections.emptyMap(); } - })); + }, null)); TestEnv.install(ConfigProvider.class, TestEnv.AnchoredConfigProvider.class); @@ -317,7 +317,7 @@ public static class ServiceAConfigProvider implements ConfigProvider { @Override public List getConfigs() { - ServiceConfig service = new FakeServiceConfig("a", FAKE_SERVICE_DISCOVERY_CONFIG, null); + ServiceConfig service = new FakeServiceConfig("a", FAKE_SERVICE_DISCOVERY_CONFIG, null, null); return List.of(service); } @@ -331,7 +331,7 @@ public static class ServiceBConfigProvider implements ConfigProvider { @Override public List getConfigs() { - ServiceConfig service = new FakeServiceConfig("b", FAKE_SERVICE_DISCOVERY_CONFIG, null); + ServiceConfig service = new FakeServiceConfig("b", FAKE_SERVICE_DISCOVERY_CONFIG, null, null); return List.of(service); } @@ -343,8 +343,8 @@ public int priority() { private static class FakeSecureServiceConfig extends FakeServiceConfig { - private FakeSecureServiceConfig(String name, ConfigWithType sd, ConfigWithType lb) { - super(name, sd, lb); + private FakeSecureServiceConfig(String name, ConfigWithType sd, ConfigWithType lb, ConfigWithType sr) { + super(name, sd, lb, sr); } @Override diff --git a/core/src/test/java/io/smallrye/stork/StorkWithCDITest.java b/core/src/test/java/io/smallrye/stork/StorkWithCDITest.java index 63c0aae7..0f5665dc 100644 --- a/core/src/test/java/io/smallrye/stork/StorkWithCDITest.java +++ b/core/src/test/java/io/smallrye/stork/StorkWithCDITest.java @@ -163,7 +163,7 @@ public static class ServiceAConfigProvider implements ConfigProvider { @Override public List getConfigs() { - ServiceConfig service = new FakeServiceConfig("a", FAKE_SERVICE_DISCOVERY_CONFIG, null); + ServiceConfig service = new FakeServiceConfig("a", FAKE_SERVICE_DISCOVERY_CONFIG, null, null); return List.of(service); } @@ -178,7 +178,7 @@ public static class ServiceBConfigProvider implements ConfigProvider { @Override public List getConfigs() { - ServiceConfig service = new FakeServiceConfig("b", FAKE_SERVICE_DISCOVERY_CONFIG, null); + ServiceConfig service = new FakeServiceConfig("b", FAKE_SERVICE_DISCOVERY_CONFIG, null, null); return List.of(service); } @@ -208,7 +208,7 @@ public String type() { public Map parameters() { return null; } - }, null)); + }, null, null)); weld.addBeanClass(MyConfigProvider.class); run(); @@ -217,7 +217,7 @@ public Map parameters() { @Test public void testServiceWithServiceDiscoveryButNoMatchingProvider() { - configurations.add(new FakeServiceConfig("a", SERVICE_DISCOVERY_CONFIG_WITH_INVALID_PROVIDER, null)); + configurations.add(new FakeServiceConfig("a", SERVICE_DISCOVERY_CONFIG_WITH_INVALID_PROVIDER, null, null)); weld.addBeanClass(MyConfigProvider.class); run(); Assertions.assertThrows(IllegalArgumentException.class, Stork::initialize); @@ -226,7 +226,7 @@ public void testServiceWithServiceDiscoveryButNoMatchingProvider() { @Test public void testWithLoadBalancerButNoMatchingProvider() { configurations - .add(new FakeServiceConfig("a", FAKE_SERVICE_DISCOVERY_CONFIG, LOAD_BALANCER_WITH_INVALID_PROVIDER)); + .add(new FakeServiceConfig("a", FAKE_SERVICE_DISCOVERY_CONFIG, LOAD_BALANCER_WITH_INVALID_PROVIDER, null)); weld.addBeanClass(MyConfigProvider.class); run(); Assertions.assertThrows(IllegalArgumentException.class, Stork::initialize); @@ -235,7 +235,7 @@ public void testWithLoadBalancerButNoMatchingProvider() { @Test public void testWithServiceDiscoveryAndASingleServiceInstance() { configurations.add(new FakeServiceConfig("a", - FAKE_SERVICE_DISCOVERY_CONFIG, null)); + FAKE_SERVICE_DISCOVERY_CONFIG, null, null)); ServiceInstance instance = mock(ServiceInstance.class); AnchoredServiceDiscoveryProvider.services.add(instance); weld.addBeanClass(AnchoredServiceDiscoveryProvider.class); @@ -258,7 +258,7 @@ public void testWithServiceDiscoveryAndASingleServiceInstance() { @Test public void testWithLegacySecureServiceDiscovery() { configurations.add(new FakeSecureServiceConfig("s", - FAKE_SERVICE_DISCOVERY_CONFIG, null)); + FAKE_SERVICE_DISCOVERY_CONFIG, null, null)); ServiceInstance instance = mock(ServiceInstance.class); AnchoredServiceDiscoveryProvider.services.add(instance); weld.addBeanClass(AnchoredServiceDiscoveryProvider.class); @@ -283,7 +283,7 @@ public void testWithLegacySecureServiceDiscovery() { @Test public void testWithSecureServiceDiscovery() { configurations.add(new FakeServiceConfig("s", - FAKE_SECURE_SERVICE_DISCOVERY_CONFIG, null)); + FAKE_SECURE_SERVICE_DISCOVERY_CONFIG, null, null)); ServiceInstance instance = mock(ServiceInstance.class); AnchoredServiceDiscoveryProvider.services.add(instance); weld.addBeanClass(AnchoredServiceDiscoveryProvider.class); @@ -307,7 +307,7 @@ public void testWithSecureServiceDiscovery() { @Test public void testWithServiceDiscoveryAndATwoServiceInstances() { configurations.add(new FakeServiceConfig("a", - FAKE_SERVICE_DISCOVERY_CONFIG, null)); + FAKE_SERVICE_DISCOVERY_CONFIG, null, null)); ServiceInstance instance1 = mock(ServiceInstance.class); ServiceInstance instance2 = mock(ServiceInstance.class); AnchoredServiceDiscoveryProvider.services.add(instance1); @@ -335,7 +335,7 @@ public void testWithServiceDiscoveryAndATwoServiceInstances() { @Test public void testWithLoadBalancer() { configurations.add(new FakeServiceConfig("a", - FAKE_SERVICE_DISCOVERY_CONFIG, FAKE_LOAD_BALANCER_CONFIG)); + FAKE_SERVICE_DISCOVERY_CONFIG, FAKE_LOAD_BALANCER_CONFIG, null)); weld.addBeanClass(AnchoredServiceDiscoveryProvider.class); weld.addBeanClass(MockLoadBalancerProvider.class); weld.addBeanClass(AnchoredServiceDiscoveryProviderLoader.class); @@ -362,7 +362,7 @@ public void testWithDefaultLoadBalancer() { AnchoredServiceDiscoveryProvider.services.add(instance3); configurations.add(new FakeServiceConfig("a", - FAKE_SERVICE_DISCOVERY_CONFIG, null)); + FAKE_SERVICE_DISCOVERY_CONFIG, null, null)); weld.addBeanClass(AnchoredServiceDiscoveryProvider.class); @@ -406,7 +406,7 @@ public String type() { public Map parameters() { return Collections.emptyMap(); } - })); + }, null)); weld.addBeanClass(AnchoredServiceDiscoveryProvider.class); weld.addBeanClass(MyConfigProvider.class); @@ -428,8 +428,8 @@ public Map parameters() { private static class FakeSecureServiceConfig extends FakeServiceConfig { - private FakeSecureServiceConfig(String name, ConfigWithType sd, ConfigWithType lb) { - super(name, sd, lb); + private FakeSecureServiceConfig(String name, ConfigWithType sd, ConfigWithType lb, ConfigWithType sr) { + super(name, sd, lb, sr); } @Override diff --git a/core/src/test/java/io/smallrye/stork/impl/RoundRobinLoadBalancerProviderCDITest.java b/core/src/test/java/io/smallrye/stork/impl/RoundRobinLoadBalancerProviderCDITest.java index 0535f0a0..42e1c829 100644 --- a/core/src/test/java/io/smallrye/stork/impl/RoundRobinLoadBalancerProviderCDITest.java +++ b/core/src/test/java/io/smallrye/stork/impl/RoundRobinLoadBalancerProviderCDITest.java @@ -61,7 +61,7 @@ public Map parameters() { @BeforeEach public void init() { - configurations.add(new FakeServiceConfig("a", FAKE_SERVICE_DISCOVERY_CONFIG, LB_CONFIG)); + configurations.add(new FakeServiceConfig("a", FAKE_SERVICE_DISCOVERY_CONFIG, LB_CONFIG, null)); weld.addBeanClass(MyLbConfigProvider.class); weld.addBeanClass(RoundRobinLoadBalancerProvider.class); run(); diff --git a/core/src/test/java/io/smallrye/stork/utils/StorkAddressUtilsTest.java b/core/src/test/java/io/smallrye/stork/utils/StorkAddressUtilsTest.java index 4d3361e5..f7900693 100644 --- a/core/src/test/java/io/smallrye/stork/utils/StorkAddressUtilsTest.java +++ b/core/src/test/java/io/smallrye/stork/utils/StorkAddressUtilsTest.java @@ -50,6 +50,22 @@ void shouldParseIpV4WithoutPort() { assertThat(result.path).isEmpty(); } + @Test + void shouldParseHostAndPort() { + HostAndPort hostAndPort = new HostAndPort("localhost", 8080); + String hostAndPortString = StorkAddressUtils.parseToString(hostAndPort); + assertThat(hostAndPortString).isEqualTo("localhost:8080"); + + } + + @Test + void shouldParseHostPortAndPath() { + HostAndPort hostAndPort = new HostAndPort("localhost", 8080, "hello"); + String hostAndPortString = StorkAddressUtils.parseToString(hostAndPort); + assertThat(hostAndPortString).isEqualTo("localhost:8080/hello"); + + } + @Test void shouldParseIpV4WithoutPortButWithScheme() { String ipV4WithoutPort = "127.0.0.1"; diff --git a/docs/docs/service-discovery/consul.md b/docs/docs/service-discovery/consul.md index bc2dd8f5..88654e0c 100644 --- a/docs/docs/service-discovery/consul.md +++ b/docs/docs/service-discovery/consul.md @@ -43,7 +43,7 @@ Stork also provides the ability to register services using Consul as backend. ### Service registration configuration -For each service that should register the service instances in Consul, configure the service discovery `type`: +For each service that should register the service instances in Consul, configure the service registrar `type`: === "stork standalone" ```properties diff --git a/docs/docs/service-discovery/eureka.md b/docs/docs/service-discovery/eureka.md index 2e9dbe90..6f34cb35 100644 --- a/docs/docs/service-discovery/eureka.md +++ b/docs/docs/service-discovery/eureka.md @@ -56,7 +56,7 @@ Stork also provides the ability to register services using Eureka as backend. ### Service registration configuration -For each service that should register the service instances in Eureka, configure the service discovery `type`: +For each service that should register the service instances in Eureka, configure the service registrar `type`: === "stork standalone" ```properties diff --git a/docs/docs/service-discovery/static-list.md b/docs/docs/service-discovery/static-list.md index 75d7ab75..fece2f2c 100644 --- a/docs/docs/service-discovery/static-list.md +++ b/docs/docs/service-discovery/static-list.md @@ -17,7 +17,7 @@ To use the `static` service discovery, first add the appropriate Service Discove ## Configuration -For each service that should use the static list of service instances configure the service discovery `type`: +For each service that should use the static list of service instances configure the service registrar `type`: === "stork standalone" ```properties @@ -33,4 +33,26 @@ For each service that should use the static list of service instances configure These are all the static service discovery parameters: ---8<-- "../service-discovery/static-list/target/classes/META-INF/stork-docs/static-sd-attributes.txt" \ No newline at end of file +--8<-- "../service-discovery/static-list/target/classes/META-INF/stork-docs/static-sd-attributes.txt" + +## Service registration + +Stork also provides the ability to register services using Static list as backend. + +### Service registration configuration + +For each service that should register the service instances in a static list, configure the service registrar `type`: + +=== "stork standalone" +```properties +stork.my-service.service-registrar.type=static +``` + +=== "stork in quarkus" +```properties +quarkus.stork.my-service.service-registrar.type=static +``` + +Static service registrar is configured with the following parameters: + +--8<-- "../service-discovery/consul/target/classes/META-INF/stork-docs/static-sr-attributes.txt" \ No newline at end of file diff --git a/docs/snippets/examples/AcmeSelectorApiUsage.java b/docs/snippets/examples/AcmeSelectorApiUsage.java index aa996d68..31de7002 100644 --- a/docs/snippets/examples/AcmeSelectorApiUsage.java +++ b/docs/snippets/examples/AcmeSelectorApiUsage.java @@ -5,6 +5,7 @@ import io.smallrye.stork.api.ServiceInstance; import io.smallrye.stork.api.StorkServiceRegistry; import io.smallrye.stork.servicediscovery.staticlist.StaticConfiguration; +import io.smallrye.stork.servicediscovery.staticlist.StaticRegistrarConfiguration; public class AcmeSelectorApiUsage { @@ -12,7 +13,7 @@ public void example(StorkServiceRegistry stork) { String list = "localhost:8080, localhost:8081"; stork.defineIfAbsent("my-service", ServiceDefinition.of( new StaticConfiguration().withAddressList(list), - new AcmeLoadBalancerConfiguration().withMyAttribute("my-value")) + new AcmeLoadBalancerConfiguration().withMyAttribute("my-value"),new StaticRegistrarConfiguration()) ); Uni uni = stork.getService("my-service").selectInstance(); diff --git a/docs/snippets/examples/DefinitionExample.java b/docs/snippets/examples/DefinitionExample.java index f3bb7b73..aa4a4512 100644 --- a/docs/snippets/examples/DefinitionExample.java +++ b/docs/snippets/examples/DefinitionExample.java @@ -3,7 +3,9 @@ import io.smallrye.stork.Stork; import io.smallrye.stork.api.ServiceDefinition; import io.smallrye.stork.loadbalancer.random.RandomConfiguration; +import io.smallrye.stork.servicediscovery.consul.ConsulRegistrarConfiguration; import io.smallrye.stork.servicediscovery.staticlist.StaticConfiguration; +import io.smallrye.stork.servicediscovery.staticlist.StaticRegistrarConfiguration; public class DefinitionExample { @@ -19,5 +21,10 @@ public static void example(Stork stork) { stork.defineIfAbsent("my-second-service", ServiceDefinition.of(new StaticConfiguration().withAddressList(example), new RandomConfiguration())); + + // Another service using the random selection strategy, instead of round-robin and a static service registrar + stork.defineIfAbsent("my-second-service", + ServiceDefinition.of(new StaticConfiguration().withAddressList(example), + new RandomConfiguration(), new StaticRegistrarConfiguration())); } } diff --git a/docs/snippets/examples/StorkApiExample.java b/docs/snippets/examples/StorkApiExample.java index 9936cd13..c87045d5 100644 --- a/docs/snippets/examples/StorkApiExample.java +++ b/docs/snippets/examples/StorkApiExample.java @@ -5,6 +5,7 @@ import io.smallrye.stork.api.ServiceInstance; import io.smallrye.stork.loadbalancer.random.RandomConfiguration; import io.smallrye.stork.servicediscovery.staticlist.StaticConfiguration; +import io.smallrye.stork.servicediscovery.staticlist.StaticRegistrarConfiguration; import java.time.Duration; @@ -29,5 +30,10 @@ public static void main(String[] args) { ServiceInstance instance = stork.getService("my-second-service").selectInstance() .await().atMost(Duration.ofSeconds(1)); System.out.println(instance.getHost() + ":" + instance.getPort()); + + // Another service using the random selection strategy, instead of round-robin and a static service registrar + stork.defineIfAbsent("my-third-service", + ServiceDefinition.of(new StaticConfiguration().withAddressList(example), + new RandomConfiguration(), new StaticRegistrarConfiguration())); } } diff --git a/load-balancer/least-requests/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/LeastRequestsLoadBalancerTest.java b/load-balancer/least-requests/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/LeastRequestsLoadBalancerTest.java index 406fd84d..bc3c6dee 100644 --- a/load-balancer/least-requests/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/LeastRequestsLoadBalancerTest.java +++ b/load-balancer/least-requests/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/LeastRequestsLoadBalancerTest.java @@ -34,11 +34,10 @@ public class LeastRequestsLoadBalancerTest { @BeforeEach void setUp() { TestConfigProvider.clear(); - TestConfigProvider.addServiceConfig("first-service", "least-requests", "static", - null, - Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2))); + TestConfigProvider.addServiceConfig("first-service", "least-requests", "static", null, + null, Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2)), null); TestConfigProvider.addServiceConfig("without-instances", "least-requests", - "empty-services", null, Collections.emptyMap()); + "empty-services", null, null, Collections.emptyMap(), null); stork = StorkTestUtils.getNewStorkInstance(); } diff --git a/load-balancer/least-response-time/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/LeastResponseTimeLoadBalancerCDITest.java b/load-balancer/least-response-time/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/LeastResponseTimeLoadBalancerCDITest.java index 1e21a67b..d129cea5 100644 --- a/load-balancer/least-response-time/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/LeastResponseTimeLoadBalancerCDITest.java +++ b/load-balancer/least-response-time/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/LeastResponseTimeLoadBalancerCDITest.java @@ -58,8 +58,7 @@ public class LeastResponseTimeLoadBalancerCDITest { void setUp() { config.clear(); config.addServiceConfig("first-service", "least-response-time", "static", - null, - Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2))); + null, Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2))); config.addServiceConfig("first-service-secure-random", "least-response-time", "static", Map.of("use-secure-random", "true"), Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2))); diff --git a/load-balancer/least-response-time/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/LeastResponseTimeLoadBalancerTest.java b/load-balancer/least-response-time/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/LeastResponseTimeLoadBalancerTest.java index 31628eb1..d83fac2b 100644 --- a/load-balancer/least-response-time/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/LeastResponseTimeLoadBalancerTest.java +++ b/load-balancer/least-response-time/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/LeastResponseTimeLoadBalancerTest.java @@ -43,14 +43,13 @@ public class LeastResponseTimeLoadBalancerTest { @BeforeEach void setUp() { TestConfigProvider.clear(); - TestConfigProvider.addServiceConfig("first-service", "least-response-time", "static", - null, - Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2))); - TestConfigProvider.addServiceConfig("first-service-secure-random", "least-response-time", "static", - Map.of("use-secure-random", "true"), - Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2))); + TestConfigProvider.addServiceConfig("first-service", "least-response-time", "static", null, + null, Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2)), null); + TestConfigProvider.addServiceConfig("first-service-secure-random", "least-response-time", "static", null, + Map.of("use-secure-random", "true"), Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2)), + null); TestConfigProvider.addServiceConfig("without-instances", "least-response-time", - "empty-services", null, Collections.emptyMap()); + "empty-services", null, null, Collections.emptyMap(), null); stork = StorkTestUtils.getNewStorkInstance(); } diff --git a/load-balancer/power-of-two-choices/src/test/java/io/smallrye/stork/loadbalancer/poweroftwochoices/PowerOfTwoChoicesLoadBalancerTest.java b/load-balancer/power-of-two-choices/src/test/java/io/smallrye/stork/loadbalancer/poweroftwochoices/PowerOfTwoChoicesLoadBalancerTest.java index f2f83985..a629d2fe 100644 --- a/load-balancer/power-of-two-choices/src/test/java/io/smallrye/stork/loadbalancer/poweroftwochoices/PowerOfTwoChoicesLoadBalancerTest.java +++ b/load-balancer/power-of-two-choices/src/test/java/io/smallrye/stork/loadbalancer/poweroftwochoices/PowerOfTwoChoicesLoadBalancerTest.java @@ -39,17 +39,17 @@ public class PowerOfTwoChoicesLoadBalancerTest { @BeforeEach void setUp() { TestConfigProvider.clear(); - TestConfigProvider.addServiceConfig("first-service", "power-of-two-choices", "static", - null, - Map.of("address-list", String.format("%s,%s,%s,%s", FST_SRVC_1, FST_SRVC_2, FST_SRVC_3, FST_SRVC_4))); - TestConfigProvider.addServiceConfig("first-service-secure-random", "power-of-two-choices", "static", + TestConfigProvider.addServiceConfig("first-service", "power-of-two-choices", "static", null, + null, Map.of("address-list", String.format("%s,%s,%s,%s", FST_SRVC_1, FST_SRVC_2, FST_SRVC_3, FST_SRVC_4)), + null); + TestConfigProvider.addServiceConfig("first-service-secure-random", "power-of-two-choices", "static", null, Map.of("use-secure-random", "true"), - Map.of("address-list", String.format("%s,%s,%s,%s", FST_SRVC_1, FST_SRVC_2, FST_SRVC_3, FST_SRVC_4))); + Map.of("address-list", String.format("%s,%s,%s,%s", FST_SRVC_1, FST_SRVC_2, FST_SRVC_3, FST_SRVC_4)), null); TestConfigProvider.addServiceConfig("singleton-service", "power-of-two-choices", "static", null, - Map.of("address-list", FST_SRVC_1)); + null, Map.of("address-list", FST_SRVC_1), null); TestConfigProvider.addServiceConfig("without-instances", "power-of-two-choices", - "empty-services", null, Collections.emptyMap()); + "empty-services", null, null, Collections.emptyMap(), null); stork = StorkTestUtils.getNewStorkInstance(); } diff --git a/load-balancer/random/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/RandomLoadBalancerCDITest.java b/load-balancer/random/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/RandomLoadBalancerCDITest.java index 1407c1e1..62cde8a1 100644 --- a/load-balancer/random/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/RandomLoadBalancerCDITest.java +++ b/load-balancer/random/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/RandomLoadBalancerCDITest.java @@ -49,8 +49,7 @@ public class RandomLoadBalancerCDITest { void setUp() { config.clear(); config.addServiceConfig("first-service", "random", "static", - null, - Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2))); + null, Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2))); config.addServiceConfig("first-service-secure-random", "random", "static", Map.of("use-secure-random", "true"), Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2))); diff --git a/load-balancer/random/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/RandomLoadBalancerTest.java b/load-balancer/random/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/RandomLoadBalancerTest.java index 73e34b65..810168ee 100644 --- a/load-balancer/random/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/RandomLoadBalancerTest.java +++ b/load-balancer/random/src/test/java/io/smallrye/stork/loadbalancer/leastresponsetime/RandomLoadBalancerTest.java @@ -33,16 +33,15 @@ public class RandomLoadBalancerTest { @BeforeEach void setUp() { TestConfigProvider.clear(); - TestConfigProvider.addServiceConfig("first-service", "random", "static", - null, - Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2))); - TestConfigProvider.addServiceConfig("first-service-secure-random", "random", "static", - Map.of("use-secure-random", "true"), - Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2))); - TestConfigProvider.addServiceConfig("singleton-service", "random", "static", - null, Map.of("address-list", FST_SRVC_1)); + TestConfigProvider.addServiceConfig("first-service", "random", "static", null, + null, Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2)), null); + TestConfigProvider.addServiceConfig("first-service-secure-random", "random", "static", null, + Map.of("use-secure-random", "true"), Map.of("address-list", String.format("%s,%s", FST_SRVC_1, FST_SRVC_2)), + null); + TestConfigProvider.addServiceConfig("singleton-service", "random", "static", null, + null, Map.of("address-list", FST_SRVC_1), null); TestConfigProvider.addServiceConfig("without-instances", "random", - "empty-services", null, Collections.emptyMap()); + "empty-services", null, null, Collections.emptyMap(), null); stork = StorkTestUtils.getNewStorkInstance(); } diff --git a/microprofile/revapi.json b/microprofile/revapi.json index 0f405aec..77d91243 100644 --- a/microprofile/revapi.json +++ b/microprofile/revapi.json @@ -24,7 +24,14 @@ "criticality" : "highlight", "minSeverity" : "POTENTIALLY_BREAKING", "minCriticality" : "documented", - "differences" : [ ] + "differences" : [ + { + "ignore": true, + "code": "java.method.removed", + "old": "method java.util.List io.smallrye.stork.microprofile.MicroProfileConfigProvider::getRegistrarConfigs()", + "justification": "Registrar config is now part of the service config." + } + ] } }, { "extension" : "revapi.reporter.json", diff --git a/microprofile/src/main/java/io/smallrye/stork/microprofile/MicroProfileConfigProvider.java b/microprofile/src/main/java/io/smallrye/stork/microprofile/MicroProfileConfigProvider.java index b39fcdc8..924e3301 100644 --- a/microprofile/src/main/java/io/smallrye/stork/microprofile/MicroProfileConfigProvider.java +++ b/microprofile/src/main/java/io/smallrye/stork/microprofile/MicroProfileConfigProvider.java @@ -1,7 +1,6 @@ package io.smallrye.stork.microprofile; import static io.smallrye.stork.Stork.STORK; -import static io.smallrye.stork.Stork.STORK_REGISTRAR; import java.util.ArrayList; import java.util.HashMap; @@ -14,9 +13,7 @@ import org.jboss.logging.Logger; import io.smallrye.stork.api.config.ServiceConfig; -import io.smallrye.stork.api.config.ServiceRegistrarConfig; import io.smallrye.stork.spi.config.ConfigProvider; -import io.smallrye.stork.spi.config.SimpleRegistrarConfig; import io.smallrye.stork.spi.config.SimpleServiceConfig; /** @@ -47,8 +44,16 @@ public class MicroProfileConfigProvider implements ConfigProvider { */ public static final String SERVICE_DISCOVERY_EMBEDDED = "service-discovery.type"; + /** + * The service-registrar segment used in the configuration key. + */ + public static final String SERVICE_REGISTRAR = "service-registrar"; + /** + * The service discovery type configuration key. + */ + public static final String SERVICE_REGISTRAR_EMBEDDED = "service-registrar.type"; + private final List serviceConfigs = new ArrayList<>(); - private final List registrarConfigs = new ArrayList<>(); /** * Creates a new instance of MicroProfileConfigProvider. @@ -89,19 +94,6 @@ public MicroProfileConfigProvider() { String serviceProperty = propertyKey(propertyName.substring(serviceNameEndIdx)); serviceProperties.put(serviceProperty, config.getValue(propertyName, String.class)); - } else if (STORK_REGISTRAR.equals(matcher.group())) { - // all registration properties are of form - // stork-registrar..... - // or stork-registrar."".... - if (!matcher.find()) { - log.warn("Potentially invalid property for SmallRye Stork: " + propertyName); - } - String registrarName = unwrapFromQuotes(matcher.group()); - int registrarNameEndIdx = matcher.end(); - Map properties = propertiesByRegistrarName.computeIfAbsent(registrarName, - ignored -> new HashMap<>()); - String property = propertyKey(propertyName.substring(registrarNameEndIdx)); - properties.put(property, config.getValue(propertyName, String.class)); } } @@ -109,20 +101,6 @@ public MicroProfileConfigProvider() { SimpleServiceConfig serviceConfig = buildServiceConfig(serviceEntry); serviceConfigs.add(serviceConfig); } - for (Map.Entry> serviceEntry : propertiesByRegistrarName.entrySet()) { - SimpleRegistrarConfig serviceConfig = buildRegistrarConfig(serviceEntry); - registrarConfigs.add(serviceConfig); - } - } - - private SimpleRegistrarConfig buildRegistrarConfig(Map.Entry> serviceEntry) { - String registrarName = serviceEntry.getKey(); - Map parameters = serviceEntry.getValue(); - String registrarType = parameters.get("type"); - if (registrarType == null) { - throw new IllegalArgumentException("no type defined for service registrar " + registrarName); - } - return new SimpleRegistrarConfig(registrarType, registrarName, parameters); } private SimpleServiceConfig buildServiceConfig(Map.Entry> serviceEntry) { @@ -150,12 +128,22 @@ private SimpleServiceConfig buildServiceConfig(Map.Entry getConfigs() { return serviceConfigs; } - @Override - public List getRegistrarConfigs() { - return registrarConfigs; - } - @Override public int priority() { return 100; diff --git a/microprofile/src/test/java/io/smallrye/stork/microprofile/MicroProfileConfigProviderTest.java b/microprofile/src/test/java/io/smallrye/stork/microprofile/MicroProfileConfigProviderTest.java index d3d4b6ef..579531d1 100644 --- a/microprofile/src/test/java/io/smallrye/stork/microprofile/MicroProfileConfigProviderTest.java +++ b/microprofile/src/test/java/io/smallrye/stork/microprofile/MicroProfileConfigProviderTest.java @@ -59,13 +59,14 @@ void cleanUp() { @Test void shouldConfigureServiceRegistrar() { Map properties = new HashMap<>(); - properties.put("stork-registrar." + MY_REGISTRAR + ".type", TestServiceRegistrarProvider.TYPE); - properties.put("stork-registrar." + MY_REGISTRAR + ".param1", "http://localhost:8080"); - properties.put("stork-registrar." + MY_REGISTRAR + ".param2", "param2-value"); + properties.put("stork." + MY_REGISTRAR + ".service-discovery.type", "test-sd-1"); + properties.put("stork." + MY_REGISTRAR + ".service-registrar.type", TestServiceRegistrarProvider.TYPE); + properties.put("stork." + MY_REGISTRAR + ".service-registrar.param1", "http://localhost:8080"); + properties.put("stork." + MY_REGISTRAR + ".service-registrar.param2", "param2-value"); Stork stork = storkForConfig(properties); - ServiceRegistrar serviceRegistrar = stork.getServiceRegistrar(MY_REGISTRAR); + ServiceRegistrar serviceRegistrar = stork.getService(MY_REGISTRAR).getServiceRegistrar(); assertThat(serviceRegistrar).isInstanceOf(TestServiceRegistrarProvider.TestServiceRegistrar.class); serviceRegistrar.registerServiceInstance("foo", Metadata.of(TestMetadata.class) diff --git a/service-discovery/composite/src/test/java/io/smallrye/stork/servicediscovery/composite/CompositeServiceDiscoveryTest.java b/service-discovery/composite/src/test/java/io/smallrye/stork/servicediscovery/composite/CompositeServiceDiscoveryTest.java index 3e469dfa..1f17e134 100644 --- a/service-discovery/composite/src/test/java/io/smallrye/stork/servicediscovery/composite/CompositeServiceDiscoveryTest.java +++ b/service-discovery/composite/src/test/java/io/smallrye/stork/servicediscovery/composite/CompositeServiceDiscoveryTest.java @@ -50,8 +50,8 @@ void shouldGetAllServiceInstances() { @Test void shouldFailForLackOfServicesList() { TestConfigProvider.clear(); - TestConfigProvider.addServiceConfig("composite-service", null, "composite", - null, Collections.emptyMap()); + TestConfigProvider.addServiceConfig("composite-service", null, "composite", null, + null, Collections.emptyMap(), null); assertThatThrownBy(StorkTestUtils::getNewStorkInstance).isInstanceOf(IllegalArgumentException.class); } @@ -59,14 +59,14 @@ void shouldFailForLackOfServicesList() { @Test void shouldFailOnEmptyServiceName() { TestConfigProvider.clear(); - TestConfigProvider.addServiceConfig("first-service", null, "static", - null, Map.of("1", "localhost:8080", "2", "localhost:8081")); + TestConfigProvider.addServiceConfig("first-service", null, "static", null, + null, Map.of("1", "localhost:8080", "2", "localhost:8081"), null); - TestConfigProvider.addServiceConfig("second-service", null, "static", - null, Map.of("3", "localhost:8082")); + TestConfigProvider.addServiceConfig("second-service", null, "static", null, + null, Map.of("3", "localhost:8082"), null); - TestConfigProvider.addServiceConfig("third-service", null, "composite", - null, Map.of("services", "first-service,,second-service")); + TestConfigProvider.addServiceConfig("third-service", null, "composite", null, + null, Map.of("services", "first-service,,second-service"), null); assertThatThrownBy(StorkTestUtils::getNewStorkInstance).isInstanceOf(IllegalArgumentException.class); } diff --git a/service-discovery/consul/revapi.json b/service-discovery/consul/revapi.json index c5fcaafb..e2545105 100644 --- a/service-discovery/consul/revapi.json +++ b/service-discovery/consul/revapi.json @@ -25,7 +25,13 @@ "minSeverity" : "POTENTIALLY_BREAKING", "minCriticality" : "documented", "differences" : [ - + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method io.smallrye.stork.api.ServiceRegistrar io.smallrye.stork.servicediscovery.consul.ConsulServiceRegistrarProviderLoader::createServiceRegistrar(io.smallrye.stork.api.config.ServiceRegistrarConfig, io.smallrye.stork.spi.StorkInfrastructure)", + "new": "method io.smallrye.stork.api.ServiceRegistrar io.smallrye.stork.servicediscovery.consul.ConsulServiceRegistrarProviderLoader::createServiceRegistrar(io.smallrye.stork.api.config.ConfigWithType, java.lang.String, io.smallrye.stork.spi.StorkInfrastructure)", + "justification": "Use the ConfigWithType more generic type for registrars." + } ] } }, { diff --git a/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceDiscoveryCDITest.java b/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceDiscoveryCDITest.java index f5231626..0a845fd1 100644 --- a/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceDiscoveryCDITest.java +++ b/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceDiscoveryCDITest.java @@ -78,7 +78,8 @@ void shouldNotFetchWhenRefreshPeriodNotReached() throws InterruptedException { //Given a service `my-service` registered in consul and a refresh-period of 5 minutes String serviceName = "my-service"; config.addServiceConfig("my-service", null, "consul", - null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5M")); + null, + Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5M")); stork = StorkTestUtils.getNewStorkInstance(); List tags = List.of("primary"); registerService(serviceName, 8406, tags, "example.com"); @@ -124,7 +125,8 @@ void shouldRefetchWhenRefreshPeriodReached() throws InterruptedException { //Given a service `my-service` registered in consul and a refresh-period of 5 seconds String serviceName = "my-service"; config.addServiceConfig("my-service", null, "consul", - null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5")); + null, + Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5")); stork = StorkTestUtils.getNewStorkInstance(); //Given a service `my-service` registered in consul List tags = List.of("primary"); @@ -179,8 +181,8 @@ void shouldRefetchWhenRefreshPeriodReached() throws InterruptedException { void shouldDiscoverServiceWithSpecificName() throws InterruptedException { //Given a service `my-service` registered in consul and a refresh-period of 5 seconds String serviceName = "my-consul-service"; - config.addServiceConfig("my-consul-service", null, "consul", - null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5", + config.addServiceConfig("my-consul-service", null, "consul", null, + Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5", "application", "my-consul-service")); stork = StorkTestUtils.getNewStorkInstance(); //Given a service `my-service` registered in consul @@ -238,7 +240,8 @@ void shouldPreserveIdsOnRefetch() throws InterruptedException { //Given a service `my-service` registered in consul and a refresh-period of 5 seconds String serviceName = "my-service"; config.addServiceConfig("my-service", null, "consul", - null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5")); + null, + Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5")); stork = StorkTestUtils.getNewStorkInstance(); //Given a service `my-service` registered in consul List tags = List.of("primary"); diff --git a/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceDiscoveryTest.java b/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceDiscoveryTest.java index 07d8f15b..6cd66588 100644 --- a/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceDiscoveryTest.java +++ b/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceDiscoveryTest.java @@ -62,8 +62,9 @@ void setUp() { void shouldNotFetchWhenRefreshPeriodNotReached() throws InterruptedException { //Given a service `my-service` registered in consul and a refresh-period of 5 minutes String serviceName = "my-service"; - TestConfigProvider.addServiceConfig("my-service", null, "consul", - null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5M")); + TestConfigProvider.addServiceConfig("my-service", null, "consul", null, + null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5M"), + null); stork = StorkTestUtils.getNewStorkInstance(); List tags = List.of("primary"); registerService(serviceName, 8406, tags, "example.com"); @@ -108,8 +109,9 @@ void shouldNotFetchWhenRefreshPeriodNotReached() throws InterruptedException { void shouldRefetchWhenRefreshPeriodReached() throws InterruptedException { //Given a service `my-service` registered in consul and a refresh-period of 5 seconds String serviceName = "my-service"; - TestConfigProvider.addServiceConfig("my-service", null, "consul", - null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5")); + TestConfigProvider.addServiceConfig("my-service", null, "consul", null, + null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5"), + null); stork = StorkTestUtils.getNewStorkInstance(); //Given a service `my-service` registered in consul List tags = List.of("primary"); @@ -164,9 +166,10 @@ void shouldRefetchWhenRefreshPeriodReached() throws InterruptedException { void shouldDiscoverServiceWithSpecificName() throws InterruptedException { //Given a service `my-service` registered in consul and a refresh-period of 5 seconds String serviceName = "my-consul-service"; - TestConfigProvider.addServiceConfig("my-consul-service", null, "consul", + TestConfigProvider.addServiceConfig("my-consul-service", null, "consul", null, null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5", - "application", "my-consul-service")); + "application", "my-consul-service"), + null); stork = StorkTestUtils.getNewStorkInstance(); //Given a service `my-service` registered in consul registerService("my-consul-service", 8406, null, "consul.com"); @@ -193,9 +196,10 @@ void shouldDiscoverServiceWithSpecificName() throws InterruptedException { void shouldHandleTheSecureAttribute() throws InterruptedException { //Given a service `my-service` registered in consul and a refresh-period of 5 seconds String serviceName = "my-consul-service"; - TestConfigProvider.addServiceConfig("my-consul-service", null, "consul", + TestConfigProvider.addServiceConfig("my-consul-service", null, "consul", null, null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5", - "application", "my-consul-service", "secure", "true")); + "application", "my-consul-service", "secure", "true"), + null); stork = StorkTestUtils.getNewStorkInstance(); //Given a service `my-service` registered in consul registerService("my-consul-service", 8406, null, "consul.com"); @@ -222,8 +226,9 @@ void shouldHandleTheSecureAttribute() throws InterruptedException { void shouldPreserveIdsOnRefetch() throws InterruptedException { //Given a service `my-service` registered in consul and a refresh-period of 5 seconds String serviceName = "my-service"; - TestConfigProvider.addServiceConfig("my-service", null, "consul", - null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5")); + TestConfigProvider.addServiceConfig("my-service", null, "consul", null, + null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5"), + null); stork = StorkTestUtils.getNewStorkInstance(); //Given a service `my-service` registered in consul List tags = List.of("primary"); diff --git a/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceRegistrationCDITest.java b/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceRegistrationCDITest.java index 51f62273..cdbee352 100644 --- a/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceRegistrationCDITest.java +++ b/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceRegistrationCDITest.java @@ -68,15 +68,14 @@ void setUp() { @Test void shouldRegisterServiceInstancesInConsul() throws InterruptedException { - config.addServiceConfig("my-service", null, "consul", - null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5")); - config.addServiceRegistrarConfig("my-consul-registrar", "consul", + String serviceName = "my-service"; + config.addServiceConfig(serviceName, null, "consul", + "consul", null, + Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort)), Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort))); Stork stork = StorkTestUtils.getNewStorkInstance(); - String serviceName = "my-service"; - - ServiceRegistrar consulRegistrar = stork.getServiceRegistrar("my-consul-registrar"); + ServiceRegistrar consulRegistrar = stork.getService(serviceName).getServiceRegistrar(); CountDownLatch registrationLatch = new CountDownLatch(1); consulRegistrar.registerServiceInstance(serviceName, Metadata.of(ConsulMetadataKey.class) @@ -91,7 +90,7 @@ void shouldRegisterServiceInstancesInConsul() throws InterruptedException { Service service = stork.getService(serviceName); service.getServiceDiscovery().getServiceInstances() - .onFailure().invoke(th -> fail("Failed to get service instances from Kubernetes", th)) + .onFailure().invoke(th -> fail("Failed to get service instances from Consul", th)) .subscribe().with(instances::set); await().atMost(Duration.ofSeconds(20)) diff --git a/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceRegistrationTest.java b/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceRegistrationTest.java index 5a2612ae..ae9dce76 100644 --- a/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceRegistrationTest.java +++ b/service-discovery/consul/src/test/java/io/smallrye/stork/servicediscovery/consul/ConsulServiceRegistrationTest.java @@ -54,15 +54,13 @@ void setUp() { @Test void shouldRegisterServiceInstancesInConsul() throws InterruptedException { - TestConfigProvider.addServiceConfig("my-service", null, "consul", - null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5")); - TestConfigProvider.addServiceRegistrarConfig("my-consul-registrar", "consul", + String serviceName = "my-service"; + TestConfigProvider.addServiceConfig(serviceName, null, "consul", "consul", + null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5"), Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort))); Stork stork = StorkTestUtils.getNewStorkInstance(); - String serviceName = "my-service"; - - ServiceRegistrar consulRegistrar = stork.getServiceRegistrar("my-consul-registrar"); + ServiceRegistrar consulRegistrar = stork.getService(serviceName).getServiceRegistrar(); CountDownLatch registrationLatch = new CountDownLatch(1); consulRegistrar.registerServiceInstance(serviceName, Metadata.of(ConsulMetadataKey.class) diff --git a/service-discovery/eureka/revapi.json b/service-discovery/eureka/revapi.json index a4186133..9ed07c10 100644 --- a/service-discovery/eureka/revapi.json +++ b/service-discovery/eureka/revapi.json @@ -25,6 +25,13 @@ "minSeverity" : "POTENTIALLY_BREAKING", "minCriticality" : "documented", "differences" : [ + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method io.smallrye.stork.api.ServiceRegistrar io.smallrye.stork.servicediscovery.eureka.EurekaServiceRegistrarProviderLoader::createServiceRegistrar(io.smallrye.stork.api.config.ServiceRegistrarConfig, io.smallrye.stork.spi.StorkInfrastructure)", + "new": "method io.smallrye.stork.api.ServiceRegistrar io.smallrye.stork.servicediscovery.eureka.EurekaServiceRegistrarProviderLoader::createServiceRegistrar(io.smallrye.stork.api.config.ConfigWithType, java.lang.String, io.smallrye.stork.spi.StorkInfrastructure)", + "justification": "Use the ConfigWithType more generic type for registrars and service name." + } ] } }, { diff --git a/service-discovery/eureka/src/test/java/io/smallrye/stork/servicediscovery/eureka/EurekaRegistrationCDITest.java b/service-discovery/eureka/src/test/java/io/smallrye/stork/servicediscovery/eureka/EurekaRegistrationCDITest.java index 74bf0421..ac6a78b1 100644 --- a/service-discovery/eureka/src/test/java/io/smallrye/stork/servicediscovery/eureka/EurekaRegistrationCDITest.java +++ b/service-discovery/eureka/src/test/java/io/smallrye/stork/servicediscovery/eureka/EurekaRegistrationCDITest.java @@ -79,13 +79,14 @@ public void cleanup() { @Test public void testRegistrationServiceInstances(TestInfo info) { - config.addServiceRegistrarConfig("my-eureka-registrar", "eureka", - Map.of("eureka-host", eureka.getHost(), "eureka-port", String.valueOf(port))); String serviceName = "my-service"; + config.addServiceConfig(serviceName, null, "eureka", "eureka", null, + Map.of("eureka-host", eureka.getHost(), "eureka-port", String.valueOf(port)), + Map.of("eureka-host", eureka.getHost(), "eureka-port", String.valueOf(port))); Stork stork = configureAndGetStork(serviceName); - ServiceRegistrar eurekaServiceRegistrar = stork.getServiceRegistrar("my-eureka-registrar"); + ServiceRegistrar eurekaServiceRegistrar = stork.getService(serviceName).getServiceRegistrar(); CountDownLatch registrationLatch = new CountDownLatch(1); eurekaServiceRegistrar.registerServiceInstance(serviceName, Metadata.of(EurekaMetadataKey.class) diff --git a/service-discovery/eureka/src/test/java/io/smallrye/stork/servicediscovery/eureka/EurekaRegistrationTest.java b/service-discovery/eureka/src/test/java/io/smallrye/stork/servicediscovery/eureka/EurekaRegistrationTest.java index c011f3f7..44a0f00d 100644 --- a/service-discovery/eureka/src/test/java/io/smallrye/stork/servicediscovery/eureka/EurekaRegistrationTest.java +++ b/service-discovery/eureka/src/test/java/io/smallrye/stork/servicediscovery/eureka/EurekaRegistrationTest.java @@ -68,13 +68,13 @@ public void cleanup() { @Test public void testRegistrationServiceInstances(TestInfo info) { - TestConfigProvider.addServiceRegistrarConfig("my-eureka-registrar", "eureka", - Map.of("eureka-host", eureka.getHost(), "eureka-port", String.valueOf(port))); String serviceName = "my-service"; + TestConfigProvider.addServiceConfig(serviceName, null, "eureka", "eureka", null, null, + Map.of("eureka-host", eureka.getHost(), "eureka-port", String.valueOf(port))); Stork stork = configureAndGetStork(serviceName); - ServiceRegistrar eurekaServiceRegistrar = stork.getServiceRegistrar("my-eureka-registrar"); + ServiceRegistrar eurekaServiceRegistrar = stork.getService(serviceName).getServiceRegistrar(); CountDownLatch registrationLatch = new CountDownLatch(1); diff --git a/service-discovery/knative/src/test/java/io/smallrye/stork/servicediscovery/knative/KnativeServiceDiscoveryRealClusterTest.java b/service-discovery/knative/src/test/java/io/smallrye/stork/servicediscovery/knative/KnativeServiceDiscoveryRealClusterTest.java index 00a1b4e1..6d62be92 100644 --- a/service-discovery/knative/src/test/java/io/smallrye/stork/servicediscovery/knative/KnativeServiceDiscoveryRealClusterTest.java +++ b/service-discovery/knative/src/test/java/io/smallrye/stork/servicediscovery/knative/KnativeServiceDiscoveryRealClusterTest.java @@ -35,9 +35,10 @@ void setUp() { void shouldDiscoverHeroesKnative() { String svc = "hero-service"; - TestConfigProvider.addServiceConfig(svc, null, "knative", + TestConfigProvider.addServiceConfig(svc, null, "knative", null, null, Map.of("knative-host", "https://api.sandbox-m2.ll9k.p1.openshiftapps.com:6443", "knative-namespace", - "amunozhe-dev", "application", "rest-heroes")); + "amunozhe-dev", "application", "rest-heroes"), + null); Stork stork = StorkTestUtils.getNewStorkInstance(); @@ -67,8 +68,8 @@ void shouldDiscoverHeroesKnative() { void shouldDiscoverNamespacedKnativeServicesWithApp() { String svc = "my-service"; - TestConfigProvider.addServiceConfig("my-service", null, "knative", - null, Map.of("knative-namespace", "default", "application", "helloworld-go")); + TestConfigProvider.addServiceConfig("my-service", null, "knative", null, + null, Map.of("knative-namespace", "default", "application", "helloworld-go"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); diff --git a/service-discovery/knative/src/test/java/io/smallrye/stork/servicediscovery/knative/KnativeServiceDiscoveryTest.java b/service-discovery/knative/src/test/java/io/smallrye/stork/servicediscovery/knative/KnativeServiceDiscoveryTest.java index 31606b48..44c22139 100644 --- a/service-discovery/knative/src/test/java/io/smallrye/stork/servicediscovery/knative/KnativeServiceDiscoveryTest.java +++ b/service-discovery/knative/src/test/java/io/smallrye/stork/servicediscovery/knative/KnativeServiceDiscoveryTest.java @@ -50,8 +50,8 @@ void setUp() { @Test void shouldDiscoverNamespacedKnativeServices() { - TestConfigProvider.addServiceConfig("my-knservice", null, "knative", - null, Map.of("knative-host", k8sMasterUrl, "knative-namespace", "test")); + TestConfigProvider.addServiceConfig("my-knservice", null, "knative", null, + null, Map.of("knative-host", k8sMasterUrl, "knative-namespace", "test"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); @@ -103,8 +103,8 @@ private static Service buildKnService(String knativeService, String url, String @Test void shouldDiscoverKnativeServicesInAllNs() { - TestConfigProvider.addServiceConfig("my-knservice", null, "knative", - null, Map.of("knative-host", k8sMasterUrl, "knative-namespace", "all")); + TestConfigProvider.addServiceConfig("my-knservice", null, "knative", null, + null, Map.of("knative-host", k8sMasterUrl, "knative-namespace", "all"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); String knativeService = "my-knservice"; @@ -158,8 +158,8 @@ void shouldGetServiceFromK8sDefaultNamespaceUsingProgrammaticAPI() { @Test void shouldHandleSecureAttribute() { - TestConfigProvider.addServiceConfig("my-knservice", null, "knative", - null, Map.of("knative-host", k8sMasterUrl, "knative-namespace", "test", "secure", "true")); + TestConfigProvider.addServiceConfig("my-knservice", null, "knative", null, + null, Map.of("knative-host", k8sMasterUrl, "knative-namespace", "test", "secure", "true"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); @@ -197,8 +197,8 @@ void shouldFetchInstancesFromTheClusterWhenCacheIsInvalidated() { // Stork is called to get service instances again // Stork contacts the cluster to get the instances : it gets 0 of them - TestConfigProvider.addServiceConfig("my-knservice", null, "knative", - null, Map.of("knative-host", k8sMasterUrl, "knative-namespace", "test", "secure", "true")); + TestConfigProvider.addServiceConfig("my-knservice", null, "knative", null, + null, Map.of("knative-host", k8sMasterUrl, "knative-namespace", "test", "secure", "true"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); @@ -255,8 +255,8 @@ void shouldFetchInstancesFromTheCache() throws InterruptedException { return buildKnService(knSvcName, "hello.test.127.0.0.1.sslip.io", "test"); }).always(); - TestConfigProvider.addServiceConfig("my-knservice", null, "knative", - null, Map.of("knative-host", k8sMasterUrl, "knative-namespace", "test", "secure", "true")); + TestConfigProvider.addServiceConfig("my-knservice", null, "knative", null, + null, Map.of("knative-host", k8sMasterUrl, "knative-namespace", "test", "secure", "true"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); diff --git a/service-discovery/kubernetes/src/test/java/io/smallrye/stork/servicediscovery/kubernetes/KubernetesServiceDiscoveryCacheTest.java b/service-discovery/kubernetes/src/test/java/io/smallrye/stork/servicediscovery/kubernetes/KubernetesServiceDiscoveryCacheTest.java index d95b24e3..b9784fe6 100644 --- a/service-discovery/kubernetes/src/test/java/io/smallrye/stork/servicediscovery/kubernetes/KubernetesServiceDiscoveryCacheTest.java +++ b/service-discovery/kubernetes/src/test/java/io/smallrye/stork/servicediscovery/kubernetes/KubernetesServiceDiscoveryCacheTest.java @@ -111,8 +111,8 @@ void shouldPreserveIdsOnRefetch() throws InterruptedException { // Stork is called to get service instances again // Stork contacts the cluster to get the instances but it preserves the Stork service instances Id - TestConfigProvider.addServiceConfig("svc", null, "kubernetes", - null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "refresh-period", "3")); + TestConfigProvider.addServiceConfig("svc", null, "kubernetes", null, + null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "refresh-period", "3"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); String serviceName = "svc"; diff --git a/service-discovery/kubernetes/src/test/java/io/smallrye/stork/servicediscovery/kubernetes/KubernetesServiceDiscoveryRealClusterIT.java b/service-discovery/kubernetes/src/test/java/io/smallrye/stork/servicediscovery/kubernetes/KubernetesServiceDiscoveryRealClusterIT.java index e6efd5e3..6fc8bfc2 100644 --- a/service-discovery/kubernetes/src/test/java/io/smallrye/stork/servicediscovery/kubernetes/KubernetesServiceDiscoveryRealClusterIT.java +++ b/service-discovery/kubernetes/src/test/java/io/smallrye/stork/servicediscovery/kubernetes/KubernetesServiceDiscoveryRealClusterIT.java @@ -37,8 +37,8 @@ void setUp() { @Test void shouldGetServiceFromK8sDefaultNamespace() { - TestConfigProvider.addServiceConfig("rest-service", null, "kubernetes", - null, Map.of("k8s-namespace", "stork-demo")); + TestConfigProvider.addServiceConfig("rest-service", null, "kubernetes", null, + null, Map.of("k8s-namespace", "stork-demo"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); String serviceName = "rest-service"; @@ -63,8 +63,8 @@ void shouldGetServiceFromK8sDefaultNamespace() { void shouldGetServicesForDefaultNamespaceOnNonSpecified() { String serviceName = "pod1"; - TestConfigProvider.addServiceConfig(serviceName, null, "kubernetes", - Collections.emptyMap(), Map.of("k8s-host", "https://192.168.49.2:8443/")); + TestConfigProvider.addServiceConfig(serviceName, null, "kubernetes", null, + Collections.emptyMap(), Map.of("k8s-host", "https://192.168.49.2:8443/"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); AtomicReference> instances = new AtomicReference<>(); @@ -82,17 +82,15 @@ void shouldGetServicesForDefaultNamespaceOnNonSpecified() { @Test void shouldRegisterServiceInstancesInDefaultNamespace() throws InterruptedException { - TestConfigProvider.addServiceConfig("svc", null, "kubernetes", - null, Map.of("k8s-host", "https://127.0.0.1:41711/", "k8s-namespace", "stork")); - TestConfigProvider.addServiceRegistrarConfig("my-kube-registrar", "kubernetes", + String serviceName = "svc"; + TestConfigProvider.addServiceConfig(serviceName, null, "kubernetes", "kubernetes", + null, Map.of("k8s-host", "https://127.0.0.1:41711/", "k8s-namespace", "stork"), Map.of("k8s-host", "https://127.0.0.1:41711/")); Stork stork = StorkTestUtils.getNewStorkInstance(); - String serviceName = "svc"; String[] ips = { "10.96.96.231", "10.96.96.232", "10.96.96.233" }; - // String[] ips = { "10.96.96.231" }; - ServiceRegistrar kubeRegistrar = stork.getServiceRegistrar("my-kube-registrar"); + ServiceRegistrar kubeRegistrar = stork.getService(serviceName).getServiceRegistrar(); CountDownLatch registrationLatch = new CountDownLatch(ips.length); for (String ip : ips) { diff --git a/service-discovery/kubernetes/src/test/java/io/smallrye/stork/servicediscovery/kubernetes/KubernetesServiceDiscoveryTest.java b/service-discovery/kubernetes/src/test/java/io/smallrye/stork/servicediscovery/kubernetes/KubernetesServiceDiscoveryTest.java index 559369f0..e5db1f3f 100644 --- a/service-discovery/kubernetes/src/test/java/io/smallrye/stork/servicediscovery/kubernetes/KubernetesServiceDiscoveryTest.java +++ b/service-discovery/kubernetes/src/test/java/io/smallrye/stork/servicediscovery/kubernetes/KubernetesServiceDiscoveryTest.java @@ -63,8 +63,8 @@ void setUp() { @Test void shouldGetServiceFromK8sDefaultNamespace() { - TestConfigProvider.addServiceConfig("svc", null, "kubernetes", - null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace)); + TestConfigProvider.addServiceConfig("svc", null, "kubernetes", null, + null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace), null); Stork stork = StorkTestUtils.getNewStorkInstance(); String serviceName = "svc"; @@ -101,8 +101,8 @@ void shouldGetServiceFromK8sDefaultNamespace() { @Test void shouldGetServiceFromK8sWithApplicationNameConfig() { - TestConfigProvider.addServiceConfig("svc", null, "kubernetes", - null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "application", "greetingApp")); + TestConfigProvider.addServiceConfig("svc", null, "kubernetes", null, + null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "application", "greetingApp"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); String serviceName = "svc"; @@ -178,8 +178,8 @@ void shouldGetServiceFromK8sDefaultNamespaceUsingProgrammaticAPI() { @Test void shouldHandleSecureAttribute() { - TestConfigProvider.addServiceConfig("svc", null, "kubernetes", - null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "secure", "true")); + TestConfigProvider.addServiceConfig("svc", null, "kubernetes", null, + null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "secure", "true"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); String serviceName = "svc"; @@ -217,8 +217,8 @@ void shouldHandleSecureAttribute() { @Test void shouldDiscoverServiceWithSpecificName() { - TestConfigProvider.addServiceConfig("svc", null, "kubernetes", - null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "application", "rest-service")); + TestConfigProvider.addServiceConfig("svc", null, "kubernetes", null, + null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "application", "rest-service"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); String serviceName = "svc"; @@ -252,8 +252,8 @@ void shouldDiscoverServiceWithSpecificName() { @Test void shouldGetServiceFromSpecificNamespace() { - TestConfigProvider.addServiceConfig("svc", null, "kubernetes", - null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", "ns1")); + TestConfigProvider.addServiceConfig("svc", null, "kubernetes", null, + null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", "ns1"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); String serviceName = "svc"; @@ -295,8 +295,8 @@ void shouldGetServiceUsingFirstPortWhenMultiplePortsFromSpecificNamespace() { String serviceName = "svc"; String specificNs = "ns1"; - TestConfigProvider.addServiceConfig(serviceName, null, "kubernetes", - null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", "ns1")); + TestConfigProvider.addServiceConfig(serviceName, null, "kubernetes", null, + null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", "ns1"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); String[] ips = new String[] { "10.96.96.231", "10.96.96.232", "10.96.96.233" }; @@ -340,8 +340,8 @@ void shouldGetServiceUsingSelectedPortNameWhenMultiplePortsFromSpecificNamespace String serviceName = "svc"; String specificNs = "ns1"; - TestConfigProvider.addServiceConfig(serviceName, null, "kubernetes", - null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", "ns1", "port-name", "http1")); + TestConfigProvider.addServiceConfig(serviceName, null, "kubernetes", null, + null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", "ns1", "port-name", "http1"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); String[] ips = new String[] { "10.96.96.231", "10.96.96.232", "10.96.96.233" }; @@ -383,8 +383,8 @@ void shouldGetServiceUsingSelectedPortNameWhenMultiplePortsFromSpecificNamespace @Test void shouldGetServiceFromAllNamespace() { - TestConfigProvider.addServiceConfig("svc", null, "kubernetes", - null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", "all")); + TestConfigProvider.addServiceConfig("svc", null, "kubernetes", null, + null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", "all"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); String serviceName = "svc"; @@ -423,8 +423,8 @@ void shouldGetServiceFromAllNamespace() { @Test void shouldPreserveIdsOnRefetch() throws InterruptedException { - TestConfigProvider.addServiceConfig("svc", null, "kubernetes", - null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "refresh-period", "3")); + TestConfigProvider.addServiceConfig("svc", null, "kubernetes", null, + null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "refresh-period", "3"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); String serviceName = "svc"; @@ -501,8 +501,8 @@ void shouldFetchInstancesFromTheClusterWhenCacheIsInvalidated() throws Interrupt // Stork is called to get service instances again // Stork contacts the cluster to get the instances : it gets 0 of them - TestConfigProvider.addServiceConfig("svc", null, "kubernetes", - null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "refresh-period", "3")); + TestConfigProvider.addServiceConfig("svc", null, "kubernetes", null, + null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "refresh-period", "3"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); String serviceName = "svc"; @@ -560,8 +560,8 @@ void shouldFetchInstancesFromTheCache() throws InterruptedException { return endpoints; }).always(); - TestConfigProvider.addServiceConfig("svc", null, "kubernetes", - null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "refresh-period", "3")); + TestConfigProvider.addServiceConfig("svc", null, "kubernetes", null, + null, Map.of("k8s-host", k8sMasterUrl, "k8s-namespace", defaultNamespace, "refresh-period", "3"), null); Stork stork = StorkTestUtils.getNewStorkInstance(); AtomicReference> instances = new AtomicReference<>(); diff --git a/service-discovery/static-list/revapi.json b/service-discovery/static-list/revapi.json index f548e110..3c974a8e 100644 --- a/service-discovery/static-list/revapi.json +++ b/service-discovery/static-list/revapi.json @@ -25,6 +25,13 @@ "minSeverity" : "POTENTIALLY_BREAKING", "minCriticality" : "documented", "differences" : [ + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method void io.smallrye.stork.servicediscovery.staticlist.StaticListServiceDiscovery::(java.util.List)", + "new": "method void io.smallrye.stork.servicediscovery.staticlist.StaticListServiceDiscovery::(java.lang.String, java.util.List)", + "justification": "Static Service Discovery needs now the service name to locate the corresponding instances" + } ] } diff --git a/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscovery.java b/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscovery.java index 983b5337..52f7de8c 100644 --- a/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscovery.java +++ b/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscovery.java @@ -1,12 +1,16 @@ package io.smallrye.stork.servicediscovery.staticlist; -import java.util.Collections; +import java.util.ArrayList; import java.util.List; import io.smallrye.mutiny.Uni; import io.smallrye.stork.api.ServiceDiscovery; import io.smallrye.stork.api.ServiceInstance; import io.smallrye.stork.impl.DefaultServiceInstance; +import io.smallrye.stork.servicediscovery.staticlist.StaticListServiceRegistrar.StaticAddressesBackend; +import io.smallrye.stork.utils.HostAndPort; +import io.smallrye.stork.utils.ServiceInstanceIds; +import io.smallrye.stork.utils.StorkAddressUtils; /** * An implementation of service discovery returning a static list of service instances. @@ -14,18 +18,38 @@ public final class StaticListServiceDiscovery implements ServiceDiscovery { private final List instances; + private final String serviceName; /** * Creates a new instance of StaticListServiceDiscovery. * * @param instances the list of instance */ - public StaticListServiceDiscovery(List instances) { - this.instances = Collections.unmodifiableList(instances); + public StaticListServiceDiscovery(String serviceName, List instances) { + this.serviceName = serviceName; + this.instances = new ArrayList<>(instances); + } @Override public Uni> getServiceInstances() { + List addresses = StaticAddressesBackend.getAddresses(serviceName); + for (String address : addresses) { + try { + HostAndPort hostAndPort = StorkAddressUtils.parseToHostAndPort(address, 80, "service"); + DefaultServiceInstance serviceInstance = new DefaultServiceInstance(ServiceInstanceIds.next(), hostAndPort.host, + hostAndPort.port, + hostAndPort.path, false); + if (!instances.contains(serviceInstance)) { + instances + .add(serviceInstance); + } + } catch (Exception e) { + throw new IllegalArgumentException( + "Address not parseable to URL: " + address + " for service " + serviceName); + } + } + return Uni.createFrom().item(instances); } } diff --git a/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryProvider.java b/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryProvider.java index 597938a1..b9118a5c 100644 --- a/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryProvider.java +++ b/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryProvider.java @@ -11,6 +11,7 @@ import io.smallrye.stork.api.config.ServiceDiscoveryAttribute; import io.smallrye.stork.api.config.ServiceDiscoveryType; import io.smallrye.stork.impl.DefaultServiceInstance; +import io.smallrye.stork.servicediscovery.staticlist.StaticListServiceRegistrar.StaticAddressesBackend; import io.smallrye.stork.spi.ServiceDiscoveryProvider; import io.smallrye.stork.spi.StorkInfrastructure; import io.smallrye.stork.utils.HostAndPort; @@ -43,6 +44,7 @@ public ServiceDiscovery createServiceDiscovery(StaticConfiguration config, Strin addressList .add(new DefaultServiceInstance(ServiceInstanceIds.next(), hostAndPort.host, hostAndPort.port, hostAndPort.path, isSecure(config.getSecure(), hostAndPort.port))); + StaticAddressesBackend.add(serviceName, address); } catch (Exception e) { throw new IllegalArgumentException( "Address not parseable to URL: " + address + " for service " + serviceName); @@ -53,7 +55,7 @@ public ServiceDiscovery createServiceDiscovery(StaticConfiguration config, Strin Collections.shuffle(addressList); } - return new StaticListServiceDiscovery(addressList); + return new StaticListServiceDiscovery(serviceName, addressList); } private boolean isSecure(String secureAttribute, int port) { diff --git a/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceRegistrar.java b/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceRegistrar.java new file mode 100644 index 00000000..8bdb3fa5 --- /dev/null +++ b/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceRegistrar.java @@ -0,0 +1,72 @@ +package io.smallrye.stork.servicediscovery.staticlist; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.smallrye.mutiny.Uni; +import io.smallrye.stork.api.Metadata; +import io.smallrye.stork.api.ServiceRegistrar; +import io.smallrye.stork.spi.StorkInfrastructure; +import io.smallrye.stork.utils.HostAndPort; +import io.smallrye.stork.utils.StorkAddressUtils; + +public class StaticListServiceRegistrar implements ServiceRegistrar { + private static final Logger log = LoggerFactory.getLogger(StaticListServiceRegistrar.class); + private final StaticRegistrarConfiguration config; + + public StaticListServiceRegistrar(StaticRegistrarConfiguration config, String serviceName, + StorkInfrastructure infrastructure) { + this.config = config; + } + + @Override + public Uni registerServiceInstance(String serviceName, Metadata metadata, + String ipAddress, + int port) { + HostAndPort hostAndPortToAdd = StorkAddressUtils.parseToHostAndPort(ipAddress, port, + "service '" + serviceName + "'"); + String hostAndPortToAddString = StorkAddressUtils.parseToString(hostAndPortToAdd); + StaticAddressesBackend.add(serviceName, hostAndPortToAddString); + return Uni.createFrom().voidItem(); + } + + public static final class StaticAddressesBackend { + + private static Map> backend; + + public static List getAddresses(String serviceName) { + if (backend == null) { + backend = new HashMap<>(); + backend.put(serviceName, Collections.emptyList()); + } + return backend.get(serviceName); + } + + public static void add(String serviceName, String address) { + if (backend == null) { + backend = new HashMap<>(); + backend.put(serviceName, new ArrayList<>()); + } else if (backend.get(serviceName) == null) { + List adresses = new ArrayList<>(); + adresses.add(address); + backend.put(serviceName, adresses); + } else if (!backend.get(serviceName).contains(address)) { + backend.get(serviceName).add(address); + + } + } + + public static void clear(String serviceName) { + if (backend != null) { + backend.remove(serviceName); + } + } + } + +} diff --git a/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceRegistrarProvider.java b/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceRegistrarProvider.java new file mode 100644 index 00000000..b0b68c7c --- /dev/null +++ b/service-discovery/static-list/src/main/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceRegistrarProvider.java @@ -0,0 +1,24 @@ +package io.smallrye.stork.servicediscovery.staticlist; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.smallrye.stork.api.Metadata; +import io.smallrye.stork.api.ServiceRegistrar; +import io.smallrye.stork.api.config.ServiceRegistrarType; +import io.smallrye.stork.spi.ServiceRegistrarProvider; +import io.smallrye.stork.spi.StorkInfrastructure; + +@ServiceRegistrarType(value = "static", metadataKey = Metadata.DefaultMetadataKey.class) +public class StaticListServiceRegistrarProvider + implements ServiceRegistrarProvider { + + private static final Logger log = LoggerFactory.getLogger(StaticListServiceRegistrarProvider.class); + + @Override + public ServiceRegistrar createServiceRegistrar(StaticRegistrarConfiguration config, + String serviceRegistrarName, StorkInfrastructure infrastructure) { + return new StaticListServiceRegistrar(config, serviceRegistrarName, infrastructure); + } + +} diff --git a/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryProgrammaticApiTest.java b/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryProgrammaticApiTest.java index 64b6ace9..b718e063 100644 --- a/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryProgrammaticApiTest.java +++ b/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryProgrammaticApiTest.java @@ -61,6 +61,14 @@ void shouldGetAllServiceInstances() { .getInstances() .await().atMost(Duration.ofSeconds(5)); + for (ServiceInstance si : serviceInstances) { + System.out.println("----- instances: \n"); + System.out.println("----- si host: " + si.getHost()); + System.out.println("----- si port: " + si.getPort()); + System.out.println("----- si id: " + si.getId()); + System.out.println("----- si path: " + si.getPath()); + } + assertThat(serviceInstances).hasSize(2); List list = serviceInstances.stream().map(si -> si.getHost() + ":" + si.getPort()).collect(Collectors.toList()); diff --git a/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryTest.java b/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryTest.java index 62b7a871..34fa0115 100644 --- a/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryTest.java +++ b/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticListServiceDiscoveryTest.java @@ -23,20 +23,20 @@ public class StaticListServiceDiscoveryTest { @BeforeEach void setUp() { TestConfigProvider.clear(); - TestConfigProvider.addServiceConfig("first-service", null, "static", - null, Map.of("address-list", "localhost:8080, localhost:8081")); + TestConfigProvider.addServiceConfig("first-service", null, "static", null, + null, Map.of("address-list", "localhost:8080, localhost:8081"), null); - TestConfigProvider.addServiceConfig("second-service", null, "static", - null, Map.of("address-list", "localhost:8082", "secure", "true")); + TestConfigProvider.addServiceConfig("second-service", null, "static", null, + null, Map.of("address-list", "localhost:8082", "secure", "true"), null); - TestConfigProvider.addServiceConfig("third-service", null, "static", - null, Map.of("address-list", "localhost:8083")); + TestConfigProvider.addServiceConfig("third-service", null, "static", null, + null, Map.of("address-list", "localhost:8083"), null); - TestConfigProvider.addServiceConfig("secured-service", null, "static", - null, Map.of("address-list", "localhost:443, localhost")); + TestConfigProvider.addServiceConfig("secured-service", null, "static", null, + null, Map.of("address-list", "localhost:443, localhost"), null); - TestConfigProvider.addServiceConfig("scheme-service", null, "static", - null, Map.of("address-list", "http://localhost:8080, https://localhost:8081")); + TestConfigProvider.addServiceConfig("scheme-service", null, "static", null, + null, Map.of("address-list", "http://localhost:8080, https://localhost:8081"), null); stork = StorkTestUtils.getNewStorkInstance(); } @@ -91,8 +91,8 @@ void shouldFailOnMissingService() { @Test void shouldFailOnInvalidFormat() { TestConfigProvider.clear(); - TestConfigProvider.addServiceConfig("broken-service", null, "static", - null, Map.of("address-list", "localhost:8080, localhost:8081, , ")); + TestConfigProvider.addServiceConfig("broken-service", null, "static", null, + null, Map.of("address-list", "localhost:8080, localhost:8081, , "), null); assertThatThrownBy(StorkTestUtils::getNewStorkInstance).isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Address not parseable"); } diff --git a/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticServiceRegistrationTest.java b/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticServiceRegistrationTest.java new file mode 100644 index 00000000..26c1390a --- /dev/null +++ b/service-discovery/static-list/src/test/java/io/smallrye/stork/servicediscovery/staticlist/StaticServiceRegistrationTest.java @@ -0,0 +1,68 @@ +package io.smallrye.stork.servicediscovery.staticlist; + +import static io.smallrye.stork.servicediscovery.staticlist.StaticListServiceRegistrar.*; +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Duration; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import io.smallrye.stork.Stork; +import io.smallrye.stork.api.Metadata; +import io.smallrye.stork.api.ServiceInstance; +import io.smallrye.stork.api.ServiceRegistrar; +import io.smallrye.stork.test.StorkTestUtils; +import io.smallrye.stork.test.TestConfigProvider; + +public class StaticServiceRegistrationTest { + + Stork stork; + + @BeforeEach + void setUp() { + TestConfigProvider.clear(); + TestConfigProvider.addServiceConfig("first-service", null, "static", "static", + null, Map.of("address-list", "localhost:8080, localhost:8081"), + Map.of("address-list", "localhost:8080, localhost:8081")); + + stork = StorkTestUtils.getNewStorkInstance(); + } + + @AfterEach + void clear() { + StaticAddressesBackend.clear("first-service"); + } + + @Test + void shouldGetAllServiceInstances() { + String serviceName = "first-service"; + List serviceInstances = stork.getService(serviceName) + .getInstances() + .await().atMost(Duration.ofSeconds(5)); + + assertThat(serviceInstances).hasSize(2); + assertThat(serviceInstances.stream().map(ServiceInstance::getHost)).containsExactlyInAnyOrder("localhost", + "localhost"); + assertThat(serviceInstances.stream().map(ServiceInstance::getPort)).containsExactlyInAnyOrder(8080, + 8081); + assertThat(serviceInstances.stream().map(ServiceInstance::isSecure)).allSatisfy(b -> assertThat(b).isFalse()); + + ServiceRegistrar staticRegistrar = stork.getService(serviceName).getServiceRegistrar(); + + staticRegistrar.registerServiceInstance(serviceName, "remotehost", 9090); + + serviceInstances = stork.getService(serviceName) + .getInstances() + .await().atMost(Duration.ofSeconds(5)); + + assertThat(serviceInstances).hasSize(3); + assertThat(serviceInstances.stream().map(ServiceInstance::getHost)).containsExactlyInAnyOrder("localhost", + "localhost", "remotehost"); + assertThat(serviceInstances.stream().map(ServiceInstance::getPort)).containsExactlyInAnyOrder(8080, + 8081, 9090); + } +} diff --git a/test-utils/revapi.json b/test-utils/revapi.json index 5bcbfd70..0b5e9874 100644 --- a/test-utils/revapi.json +++ b/test-utils/revapi.json @@ -72,6 +72,58 @@ "new": "class io.smallrye.stork.test.TestServiceRegistrarProviderLoader", "annotation": "@jakarta.enterprise.context.ApplicationScoped", "justification": "The loaders are now also exposed as CDI beans" + }, + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method void io.smallrye.stork.test.TestConfigProvider::addServiceConfig(java.lang.String, java.lang.String, java.lang.String, java.util.Map, java.util.Map)", + "new": "method void io.smallrye.stork.test.TestConfigProvider::addServiceConfig(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.Map, java.util.Map, boolean)", + "justification": "Registrars are now part of configs" + }, + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method void io.smallrye.stork.test.TestConfigProvider::addServiceConfig(java.lang.String, java.lang.String, java.lang.String, java.util.Map, java.util.Map, boolean)", + "new": "method void io.smallrye.stork.test.TestConfigProvider::addServiceConfig(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.Map, java.util.Map, java.util.Map)", + "justification": "Registrars are now part of configs" + }, + { + "ignore": true, + "code": "java.method.removed", + "old": "method void io.smallrye.stork.test.TestConfigProvider::addServiceRegistrarConfig(java.lang.String, java.lang.String, java.util.Map)", + "justification": "Registrar config is now part of service config." + }, + { + "ignore": true, + "code": "java.method.removed", + "old": "method java.util.List io.smallrye.stork.test.TestConfigProvider::getRegistrarConfigs()", + "justification": "Registrars are now part of configs" + }, + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method void io.smallrye.stork.test.TestConfigProviderBean::addServiceConfig(java.lang.String, java.lang.String, java.lang.String, java.util.Map, java.util.Map)", + "new": "method void io.smallrye.stork.test.TestConfigProviderBean::addServiceConfig(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.Map, java.util.Map, java.util.Map)", + "justification": "Registrars are now part of configs" + }, + { + "ignore": true, + "code": "java.method.removed", + "old": "method void io.smallrye.stork.test.TestConfigProviderBean::addServiceRegistrarConfig(java.lang.String, java.lang.String, java.util.Map)", + "justification": "Registrar config is now part of service config." + }, + { + "ignore": true, + "code": "java.method.removed", + "old": "method java.util.List io.smallrye.stork.test.TestConfigProviderBean::getRegistrarConfigs()", + "justification": "Registrar config is now part of service config." + }, + { + "ignore": true, + "code": "java.method.numberOfParametersChanged", + "old": "method io.smallrye.stork.api.ServiceRegistrar io.smallrye.stork.test.TestServiceRegistrarProviderLoader::createServiceRegistrar(io.smallrye.stork.api.config.ServiceRegistrarConfig, io.smallrye.stork.spi.StorkInfrastructure)", + "new": "method io.smallrye.stork.api.ServiceRegistrar io.smallrye.stork.test.TestServiceRegistrarProviderLoader::createServiceRegistrar(io.smallrye.stork.api.config.ConfigWithType, java.lang.String, io.smallrye.stork.spi.StorkInfrastructure)", + "justification": "Registrars are now part of service config." } ] } diff --git a/test-utils/src/main/java/io/smallrye/stork/test/TestConfigProvider.java b/test-utils/src/main/java/io/smallrye/stork/test/TestConfigProvider.java index a8e4c19f..76ac4415 100644 --- a/test-utils/src/main/java/io/smallrye/stork/test/TestConfigProvider.java +++ b/test-utils/src/main/java/io/smallrye/stork/test/TestConfigProvider.java @@ -8,7 +8,6 @@ import io.smallrye.stork.api.config.ConfigWithType; import io.smallrye.stork.api.config.ServiceConfig; -import io.smallrye.stork.api.config.ServiceRegistrarConfig; import io.smallrye.stork.spi.config.ConfigProvider; /** @@ -16,7 +15,6 @@ */ public class TestConfigProvider implements ConfigProvider { private static final List configs = new ArrayList<>(); - private static final List registrarConfigs = new ArrayList<>(); private static int priority = Integer.MAX_VALUE - 1; @@ -30,15 +28,17 @@ public static int getPriority() { @Deprecated public static void addServiceConfig(String name, String loadBalancer, String serviceDiscovery, - Map loadBalancerParams, Map serviceDiscoveryParams, boolean secure) { + String serviceRegistrar, Map loadBalancerParams, Map serviceDiscoveryParams, + boolean secure) { if (secure) { serviceDiscoveryParams.put("secure", "true"); } - addServiceConfig(name, loadBalancer, serviceDiscovery, loadBalancerParams, serviceDiscoveryParams); + addServiceConfig(name, loadBalancer, serviceDiscovery, null, loadBalancerParams, serviceDiscoveryParams, null); } public static void addServiceConfig(String name, String loadBalancer, String serviceDiscovery, - Map loadBalancerParams, Map serviceDiscoveryParams) { + String serviceRegistrar, Map loadBalancerParams, Map serviceDiscoveryParams, + Map serviceRegistrarParams) { configs.add(new ServiceConfig() { @Override public String serviceName() { @@ -74,31 +74,26 @@ public Map parameters() { } }; } - }); - } - - public static void addServiceRegistrarConfig(String registrarName, String registrarType, Map parameters) { - registrarConfigs.add(new ServiceRegistrarConfig() { - @Override - public String name() { - return registrarName; - } @Override - public String type() { - return registrarType; - } + public ConfigWithType serviceRegistrar() { + return serviceRegistrar == null ? null : new ConfigWithType() { + @Override + public String type() { + return serviceRegistrar; + } - @Override - public Map parameters() { - return parameters; + @Override + public Map parameters() { + return Objects.requireNonNullElse(serviceRegistrarParams, Collections.emptyMap()); + } + }; } }); } public static void clear() { configs.clear(); - registrarConfigs.clear(); } @Override @@ -106,11 +101,6 @@ public List getConfigs() { return configs; } - @Override - public List getRegistrarConfigs() { - return registrarConfigs; - } - @Override public int priority() { return priority; diff --git a/test-utils/src/main/java/io/smallrye/stork/test/TestConfigProviderBean.java b/test-utils/src/main/java/io/smallrye/stork/test/TestConfigProviderBean.java index 1aca965a..1cd1539d 100644 --- a/test-utils/src/main/java/io/smallrye/stork/test/TestConfigProviderBean.java +++ b/test-utils/src/main/java/io/smallrye/stork/test/TestConfigProviderBean.java @@ -10,7 +10,6 @@ import io.smallrye.stork.api.config.ConfigWithType; import io.smallrye.stork.api.config.ServiceConfig; -import io.smallrye.stork.api.config.ServiceRegistrarConfig; import io.smallrye.stork.spi.config.ConfigProvider; /** @@ -20,8 +19,6 @@ @ApplicationScoped public class TestConfigProviderBean implements ConfigProvider { private final List configs = new ArrayList<>(); - private final List registrarConfigs = new ArrayList<>(); - private int priority = Integer.MAX_VALUE; public void setPriority(int priority) { @@ -65,31 +62,72 @@ public Map parameters() { } }; } + + @Override + public ConfigWithType serviceRegistrar() { + return null; + } }); } - public void addServiceRegistrarConfig(String registrarName, String registrarType, Map parameters) { - registrarConfigs.add(new ServiceRegistrarConfig() { + public void addServiceConfig(String name, String loadBalancer, String serviceDiscovery, String serviceRegistrar, + Map loadBalancerParams, Map serviceDiscoveryParams, + Map serviceRegistrarParams) { + configs.add(new ServiceConfig() { @Override - public String name() { - return registrarName; + public String serviceName() { + return name; } @Override - public String type() { - return registrarType; + public ConfigWithType loadBalancer() { + return loadBalancer == null ? null : new ConfigWithType() { + @Override + public String type() { + return loadBalancer; + } + + @Override + public Map parameters() { + return Objects.requireNonNullElse(loadBalancerParams, Collections.emptyMap()); + } + }; } @Override - public Map parameters() { - return parameters; + public ConfigWithType serviceDiscovery() { + return new ConfigWithType() { + @Override + public String type() { + return serviceDiscovery; + } + + @Override + public Map parameters() { + return Objects.requireNonNullElse(serviceDiscoveryParams, Collections.emptyMap()); + } + }; + } + + @Override + public ConfigWithType serviceRegistrar() { + return serviceRegistrar == null ? null : new ConfigWithType() { + @Override + public String type() { + return serviceRegistrar; + } + + @Override + public Map parameters() { + return Objects.requireNonNullElse(serviceRegistrarParams, Collections.emptyMap()); + } + }; } }); } public void clear() { configs.clear(); - registrarConfigs.clear(); } @Override @@ -97,11 +135,6 @@ public List getConfigs() { return configs; } - @Override - public List getRegistrarConfigs() { - return registrarConfigs; - } - @Override public int priority() { return priority;