From 3186452eceed71bb5845de07f32ef93994fe6c42 Mon Sep 17 00:00:00 2001
From: buildmaster Patterns such as service discovery, load balancing and circuit breakers lend themselves to a common abstraction layer that can be consumed by all Spring Cloud clients, independent of the implementation (e.g. discovery via Eureka or Consul). Commons provides the By default, implementations of The use of Commons creates a Spring Boot Commons now provides a Commons creates a Spring Boot Commons now provides a Each By default, the A A Each By default, the A A @EnableDiscoveryClient
annotation. This looks for implementations of the DiscoveryClient
interface via META-INF/spring.factories
. Implementations of Discovery Client will add a configuration class to spring.factories
under the org.springframework.cloud.client.discovery.EnableDiscoveryClient
key. Examples of DiscoveryClient
implementations: are Spring Cloud Netflix Eureka, Spring Cloud Consul Discovery and Spring Cloud Zookeeper Discovery.DiscoveryClient
will auto-register the local Spring Boot server with the remote discovery server. This can be disabled by setting autoRegister=false
in @EnableDiscoveryClient
.Note @EnableDiscoveryClient
is no longer required. It is enough to just have a DiscoveryClient
implementation
-on the classpath to cause the Spring Boot application to register with the service discovery server.HealthIndicator
that DiscoveryClient
implementations can participate in by implementing DiscoveryHealthIndicator
. To disable the composite HealthIndicator
set spring.cloud.discovery.client.composite-indicator.enabled=false
. A generic HealthIndicator
based on DiscoveryClient
is auto-configured (DiscoveryClientHealthIndicator). To disable it, set `spring.cloud.discovery.client.health-indicator.enabled=false
. To disable the description field of the DiscoveryClientHealthIndicator
set spring.cloud.discovery.client.health-indicator.include-description=false
, otherwise it can bubble up as the description
of the rolled up HealthIndicator
.ServiceRegistry
interface which provides methods like register(Registration)
and deregister(Registration)
which allow you to provide custom registered services. Registration
is a marker interface.@Configuration
+on the classpath to cause the Spring Boot application to register with the service discovery server.
HealthIndicator
that DiscoveryClient
implementations can participate in by implementing DiscoveryHealthIndicator
. To disable the composite HealthIndicator
set spring.cloud.discovery.client.composite-indicator.enabled=false
. A generic HealthIndicator
based on DiscoveryClient
is auto-configured (DiscoveryClientHealthIndicator
). To disable it, set spring.cloud.discovery.client.health-indicator.enabled=false
. To disable the description field of the DiscoveryClientHealthIndicator
set spring.cloud.discovery.client.health-indicator.include-description=false
, otherwise it can bubble up as the description
of the rolled up HealthIndicator
.ServiceRegistry
interface which provides methods like register(Registration)
and deregister(Registration)
which allow you to provide custom registered services. Registration
is a marker interface.@Configuration
@EnableDiscoveryClient(autoRegister=false)
public class MyConfiguration {
private ServiceRegistry registry;
@@ -15,7 +15,7 @@ on the classpath to cause the Spring Boot application to register with the servi
Registration registration = constructRegistration();
this.registry.register(registration);
}
-}
ServiceRegistry
implementation has its own Registry
implementation.ServiceRegistry
implementation will auto-register the running service. To disable that behavior, there are two methods. You can set @EnableDiscoveryClient(autoRegister=false)
to permanently disable auto-registration. You can also set spring.cloud.service-registry.auto-registration.enabled=false
to disable the behavior via configuration./service-registry
actuator endpoint is provided by Commons. This endpoint relys on a Registration
bean in the Spring Application Context. Calling /service-registry/instance-status
via a GET will return the status of the Registration
. A POST to the same endpoint with a String
body will change the status of the current Registration
to the new value. Please see the documentation of the ServiceRegistry
implementation you are using for the allowed values for updating the status and the values retured for the status.RestTemplate
can be automatically configured to use ribbon. To create a load balanced RestTemplate
create a RestTemplate
@Bean
and use the @LoadBalanced
qualifier.Warning RestTemplate
bean is no longer created via auto configuration. It must be created by individual applications.@Configuration
+}
ServiceRegistry
implementation has its own Registry
implementation.ServiceRegistry
implementation will auto-register the running service. To disable that behavior, there are two methods. You can set @EnableDiscoveryClient(autoRegister=false)
to permanently disable auto-registration. You can also set spring.cloud.service-registry.auto-registration.enabled=false
to disable the behavior via configuration./service-registry
actuator endpoint is provided by Commons. This endpoint relies on a Registration
bean in the Spring Application Context. Calling /service-registry
via a GET will return the status of the Registration
. A POST to the same endpoint with a JSON body will change the status of the current Registration
to the new value. The JSON body has to include the status
field with the preferred value. Please see the documentation of the ServiceRegistry
implementation you are using for the allowed values for updating the status and the values returned for the status. For instance, Eureka’s supported statuses are UP
, DOWN
, OUT_OF_SERVICE
and UNKNOWN
.RestTemplate
can be automatically configured to use ribbon. To create a load balanced RestTemplate
create a RestTemplate
@Bean
and use the @LoadBalanced
qualifier.Warning RestTemplate
bean is no longer created via auto configuration. It must be created by individual applications.@Configuration
public class MyConfiguration {
@LoadBalanced
@@ -182,4 +182,23 @@ in downstream projects. In addition, if you provide a bean of type
spring.cloud.httpclientfactories.apache.enabled
or spring.cloud.httpclientfactories.ok.enabled
to
-false
.false
.
A /features
actuator endpoint is provided by Commons. This endpoint returns features available on the classpath and if they are enabled or not. The information returned includes the feature type, name, version and vendor.
There are two types of 'features': abstract and named.
Abstract features are features where an interface or abstract class is defined that an implementation creates, such as DiscoveryClient
, LoadBalancerClient
or LockService
. The abstract class or interface is used to find a bean of that type in the context. The version displayed is bean.getClass().getPackage().getImplementationVersion()
.
Named features are features that don’t have a particular class they implement, such as "Circuit Breaker", "API Gateway", "Spring Cloud Bus", etc… These features require a name and a bean type.
Any module can declare any number of HasFeature
beans. Some examples:
@Bean +public HasFeatures commonsFeatures() { + return HasFeatures.abstractFeatures(DiscoveryClient.class, LoadBalancerClient.class); +} + +@Bean +public HasFeatures consulFeatures() { + return HasFeatures.namedFeatures( + new NamedFeature("Spring Cloud Bus", ConsulBusAutoConfiguration.class), + new NamedFeature("Circuit Breaker", HystrixCommandAspect.class)); +} + +@Bean +HasFeatures localFeatures() { + return HasFeatures.builder() + .abstractFeature(Foo.class) + .namedFeature(new NamedFeature("Bar Feature", Bar.class)) + .abstractFeature(Baz.class) + .build(); +}
Each of these beans should go in an appropriately guarded @Configuration
.