This article is based on the Spring Cloud Finchley.release.

Service discovery: Eureka Clients

Service discovery is a key principle of microservice architecture. Manually configuring each client is complex and error prone. Netflix offers Erueka as both a server and a client for service discovery. The Srever can be configured, and each server can register with each other for ha deployment.

How to use Eureka Client

Will the groupId is org. Springframework. Cloud, artifactId for spring – the cloud – starter – netflix – eureka – client dependence can be added to the project introduction of eureka client, Refer to the Spring Cloud Project for a specific version number

Registered Eureka Client

When the client registers with Eureka, it will provide a series of metadata information, such as host, port, HEALTH check URL, home page, etc. Eureka accepts heartbeat messages sent by each service, and if a heartbeat message is not received within a configured timeout, the instance is removed from the register. For example, the following services:

@SpringBootApplication @RestController public class Application { @RequestMapping("/") public String home() { return "Hello world"; } public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); }}Copy the code

When spring-cloud-starter-Netflix-Eureka-client is added to the classpath, the application is automatically registered with the Eureka Server. You only need to configure the Eureka Server location.

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/Copy the code

You can configure the registered service name and service port with ${spring.application.name},${server.port}. Adding spring-cloud-starter-Netflix-Eureka-client to the classpath registers the service as an instance of Eureka, meaning that the service itself registers with itself and is also a client that obtains other services from the registry. If you want to disable self-registered services, set Eureka.client. enabled=false. For more configuration information, see EurekaInstanceConfigBean and EurekaClientConfigBean.

Authorized by Eureka Server

If had the authorization of the HTTP Server configuration, basic information, can add authorization information to the Eureka. Client. ServiceUrl. DefaultZone set in the authorization information, Eureka. Client. ServiceUrl defaultZone = http://user:password@localhost:8761/eureka.

Status page and health check

The default status page and health check interfaces of Eureka instances are /info and/Health, which are the default endpoints of Spring Boot Actuators. If you do not want to use the default Settings, you can modify the Servlet Path (server.servletPath=/custom) or the management. ContextPath =/admin) in YAML

eureka:
  instance:
    statusPageUrlPath: ${management.server.servlet.context-path}/info
    healthCheckUrlPath: ${management.server.servlet.context-path}/healthCopy the code
Register a secure application

If the application requires HTTPS communication, you can set the following parameters:

  • eureka.instance.nonSecurePortEnabled=false
  • eureka.instance.securePortEnabled=true

The two configurations enable Eureka to publish secure communications. Each time, DiscoveryClient returns a URI starting with HTTPS. Similarly, if the service is set up this way, the health check will use HTTPS. Since Eureka works on an Intranet, it still publishes non-HTTPS health checks and status endpoints, so you need to manually replace them, for example:

eureka:
  instance:
    statusPageUrl: https://${eureka.hostname}/info
    healthCheckUrl: https://${eureka.hostname}/health
    homePageUrl: https://${eureka.hostname}/Copy the code
Health check

By default, Eureka uses heartbeat to determine whether a service is available. Unless otherwise specified, the service finds that clients do not issue health checks based on Spring Boot Actuators. When a service is successfully registered, Eureka always marks the application as UP. You can modify the configuration as follows:

eureka:
  client:
    healthcheck:
      enabled: trueCopy the code

Note that this configuration can only be configured in application.yml. If configured in bootstrap.yml, side effects may occur, such as the UNKNOWN state of the registration service.

Eureka metadata

Standard metadata information includes host, port, IP, status page, health check. If you need a custom metadata information can be configured to eureka. The instance. The metadataMap, the metadata information can be obtain their services

Example Change the ID of the Eureka instance

Spring Cloud Eureka Will be below ${spring. Cloud. Client. The hostname} : ${spring. Application. The name} : ${spring. Application. Instance_id: ${server port}}} Info The ID of the registered microeureka instance. For example myhost: myappname: 8080. You can modify the instance ID as follows

eureka:
  instance:
    instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}Copy the code
Use of Eureka Client

EurekaClient allows you to obtain information about service discovery, such as:

@Autowired
private EurekaClient discoveryClient;

public String serviceUrl() {
    InstanceInfo instance = discoveryClient.getNextServerFromEureka("STORES", false);
    return instance.getHomePageUrl();
}Copy the code

Be careful not to apply EurekaClient to @PostConstruct or @scheduled methods or any other method whose ApplicationContext has not yet started.

Remove Jersey dependency

By default, EurekaClient uses Jersey as its HTTP communication tool. If you don’t want to rely on Jersey, you can remove the Jersey dependency. Spring Cloud uses RestTempalte as a communication tool by default.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-apache-client4</artifactId>
        </exclusion>
    </exclusions>
</dependency>Copy the code
Why is service registration slow

By default, the heartbeat duration is 30 seconds. An instance is judged to be available only if the client, server, and client caches are available simultaneously. Can modify the eureka. Instance. LeaseRenewalIntervalInSeconds parameters modified heartbeat. Try not to change this parameter.