3. Spring Cloud Commons: Common Abstractions

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).

3.1 @EnableDiscoveryClient

Commons provides the@EnableDiscoveryClientannotation. This looks for implementations of theDiscoveryClientinterface viaMETA-INF/spring.factories. Implementations of Discovery Client will add a configuration class tospring.factoriesunder theorg.springframework.cloud.client.discovery.EnableDiscoveryClientkey. Examples ofDiscoveryClientimplementations: areSpring Cloud Netflix Eureka,Spring Cloud Consul DiscoveryandSpring Cloud Zookeeper Discovery.

By default, implementations ofDiscoveryClientwill auto-register the local Spring Boot server with the remote discovery server. This can be disabled by settingautoRegister=falsein@EnableDiscoveryClient.

3.2 ServiceRegistry

Commons now provides aServiceRegistryinterface which provides methods likeregister(Registration)andderegister(Registration)which allow you to provide custom registered services.Registrationis a marker interface.

@Configuration
@EnableDiscoveryClient(autoRegister=false)
public class MyConfiguration {
    private ServiceRegistry registry;

    public MyConfiguration(ServiceRegistry registry) {
        this.registry = registry;
    }

    // called via some external process, such as an event or a custom actuator endpoint
    public void register() {
        Registration registration = constructRegistration();
        this.registry.register(registration);
    }
}

3.2.1 ServiceRegistry Auto-Registration

By default, theServiceRegistryimplementation 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 setspring.cloud.service-registry.auto-registration.enabled=falseto disable the behavior via configuration.

3.2.2 Service Registry Actuator Endpoint

A/service-registryactuator endpoint is provided by Commons. This endpoint relys on aRegistrationbean in the Spring Application Context. Calling/service-registry/instance-statusvia a GET will return the status of theRegistration. A POST to the same endpoint with aStringbody will change the status of the currentRegistrationto the new value. Please see the documentation of theServiceRegistryimplementation you are using for the allowed values for updating the status and the values retured for the status.

3.3 Spring RestTemplate as a Load Balancer Client

RestTemplatecan be automatically configured to use ribbon. To create a load balancedRestTemplatecreate aRestTemplate@Beanand use the@LoadBalancedqualifier.

ARestTemplatebean is no longer created via auto configuration. It must be created by individual applications.

@Configuration
public class MyConfiguration {

    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

public class MyClass {
    @Autowired
    private RestTemplate restTemplate;

    public String doOtherStuff() {
        String results = restTemplate.getForObject("http://stores/stores", String.class);
        return results;
    }
}

The URI needs to use a virtual host name (ie. service name, not a host name). The Ribbon client is used to create a full physical address. SeeRibbonAutoConfigurationfor details of how theRestTemplateis set up.

3.3.1 Retrying Failed Requests

A load balancedRestTemplatecan be configured to retry failed requests. By default this logic is disabled, you can enable it by addingSpring Retryto your application’s classpath. The load balancedRestTemplatewill honor some of the Ribbon configuration values related to retrying failed requests. If you would like to disable the retry logic with Spring Retry on the classpath you can setspring.cloud.loadbalancer.retry.enabled=false. The properties you can use areclient.ribbon.MaxAutoRetries,client.ribbon.MaxAutoRetriesNextServer, andclient.ribbon.OkToRetryOnAllOperations. See theRibbon documentationfor a description of what there properties do.

clientin the above examples should be replaced with your Ribbon client’s name.

3.4 Multiple RestTemplate objects

If you want aRestTemplatethat is not load balanced, create aRestTemplatebean and inject it as normal. To access the load balancedRestTemplateuse the@LoadBalancedqualifier when you create your@Bean.

Important

Notice the@Primaryannotation on the plainRestTemplatedeclaration in the example below, to disambiguate the unqualified@Autowiredinjection.

@Configuration
public class MyConfiguration {

    @LoadBalanced
    @Bean
    RestTemplate loadBalanced() {
        return new RestTemplate();
    }

    @Primary
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

public class MyClass {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    @LoadBalanced
    private RestTemplate loadBalanced;

    public String doOtherStuff() {
        return loadBalanced.getForObject("http://stores/stores", String.class);
    }

    public String doStuff() {
        return restTemplate.getForObject("http://example.com", String.class);
    }
}

If you see errors likejava.lang.IllegalArgumentException: Can not set org.springframework.web.client.RestTemplate field com.my.app.Foo.restTemplate to com.sun.proxy.$Proxy89try injectingRestOperationsinstead or settingspring.aop.proxyTargetClass=true.

3.5 Ignore Network Interfaces

Sometimes it is useful to ignore certain named network interfaces so they can be excluded from Service Discovery registration (eg. running in a Docker container). A list of regular expressions can be set that will cause the desired network interfaces to be ignored. The following configuration will ignore the "docker0" interface and all interfaces that start with "veth".

application.yml.

spring:
  cloud:
    inetutils:
      ignoredInterfaces:
        - docker0
        - veth.*

You can also force to use only specified network addresses using list of regular expressions:

application.yml.

spring:
  cloud:
    inetutils:
      preferredNetworks:
        - 192.168
        - 10.0

You can also force to use only site local addresses. SeeInet4Address.html.isSiteLocalAddress()for more details what is site local address.

application.yml.

spring:
  cloud:
    inetutils:
      useOnlySiteLocalInterfaces: true

results matching ""

    No results matching ""