Spring Cloud (05) - Eureka Client Introduction

Eureka Client Introduction

Eureka Client Eureka Server corresponding to the client for communicating with the Eureka Server. For applications, Eureka Client can be divided into client applications and server applications, the relationship between them as shown in FIG. Application server register itself with Eureka Server, application client application service information acquired from Eureka Server, thereby enabling the application server to initiate calls directly.

eureka_client_architecture.png

Use Eureka Client need to add the following dependent.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Added spring-cloud-starter-netflix-eureka-clientEureka Client default will automatically register with the Eureka Server after dependent, the default access Eureka Server Shi http://localhost:8761/eureka/. If your Eureka Server is not deployed or listening on localhost port 8761 is not, by the application.properties file eureka.client.serviceUrl.defaultZonespecified. Eureka Client registered with the Eureka Server service id will take spring.application.namethe value, if not defined in application.properties file spring.application.name, the default will be UNKOWN. So as an application server Eureka Client, usually need to be defined in the application.properties spring.application.name. For service providers, the most important as Eureka Client is instanceId, each instanceId must be unique, the default is instanceId the form.

${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}}

That is, if your application is a service provider, corresponding application name app1, hostname is host1, issued by the port number is 8080, the corresponding instanceId is host1:app1:8080, if the same service at a 8081 port also released, then instanceId its default host1:app1:8081.

server.portApplication.properties need to specify a file, it is not the default automatic acquisition, and spring.cloud.client.hostnamewill automatically obtain the hostname of the machine.

Can eureka.instance.instanceIdcustomize instanceId values, such as the following is defined as the instanceId hostname:portform.

eureka.instance.instanceId=${spring.cloud.client.hostname}:${server.port}

For Eureka Client can also eureka.instance.preferIpAddress=truepriority to return the current IP address of the host when the need to obtain a specified hostname. At this time, by ${spring.cloud.client.ip-address}a current host IP placeholder, can be used to define the instanceId ${spring.cloud.client.ip-address}placeholder obtain the current IP address of the host, such as the following is defined as the instanceId ip:portform.

eureka.instance.preferIpAddress=true
eureka.instance.instanceId=${spring.cloud.client.ip-address}:${server.port}

If your application is purely as an application client, then your corresponding Eureka Client may not need to be registered with the Eureka Server, at this time can eureka.client.registerWithEureka=falsenot be registered to Eureka Server specified.

The Eureka Client can be configured as a complete angle from Client configuration information may refer to org.springframework.cloud.netflix.eureka.EurekaClientConfigBeanAPI documentation or source code, can be configured from the angle Instance configuration information may refer to the complete org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBeanAPI documentation or source code. Eureka Client sent every 30 seconds by default heartbeat to Eureka Server, Eureka Server default angle from 90 seconds do not receive a heartbeat i.e., Eureka Client Eureka Client considered unusable. In Eureka Client can eureka.instance.leaseRenewalIntervalInSeconds=20send a heartbeat every 20 seconds to Eureka Server specified Eureka Client. By Eureka Server may eureka.instance.leaseExpirationDurationInSeconds=60specify more than 60 seconds when a Eureka Client not receive a heartbeat, Eureka Client is considered unusable. Eureka Client default every 30 seconds a registered service information from Eureka Server, you can eureka.client.registryFetchIntervalSeconds=10specify the service once every 10 seconds to obtain registration information. Eureka Server do by default every 60 seconds cleanup operation is not available in the Eureka Client, you can eureka.server.evictionIntervalTimerInMs=120000do a clean-up operation to specify every 120 seconds.

As an application of the client, we can inject com.netflix.discovery.EurekaClient, then get an instance of a service address provided by the application server instance EurekaClient example, thereby enabling the corresponding service calls. In the following instanceUrl()by means of examples injected EurekaClient getNextServerFromEureka()acquired service spring-cloud-service-provider(which corresponds to the application server ${spring.application.name}) information in one example, then obtain the home address to the instance.

@Autowired
private EurekaClient eurekaClient;

@GetMapping("instance")
public String instanceUrl() {
    String virtualHostname = "spring-cloud-service-provider";
    InstanceInfo instanceInfo = this.eurekaClient.getNextServerFromEureka(virtualHostname, false);
    return instanceInfo.getHomePageUrl();
}

In addition to using EurekaClient native, you can also choose to use Spring packaging org.springframework.cloud.client.discovery.DiscoveryClient, its usage easier.

@Autowired
private DiscoveryClient discoveryClient;

@GetMapping("instance2")
public String instanceUrl2() {
    String serviceId = "spring-cloud-service-provider";
    List<ServiceInstance> instances = this.discoveryClient.getInstances(serviceId);
    if (CollectionUtils.isNotEmpty(instances)) {
        return instances.get(0).getUri().toString();
    }
    return null;
}

DiscoveryClient package Spring Cloud is an abstract of service discovery, which can have many different implementations, in addition to outside EurekaDiscoveryClient Eureka-based implementation, there will be Consul-based implementation of ConsulDiscoveryClient and so on. If necessary the user can implement your own DiscoveryClient achieved. When using a custom DiscoveryClient achieve, generally you will realize ServiceRegistry custom. ServiceRegistry look at the name should know is used for service registration.

Take to the service address of the application service provider after, you can call the corresponding service, and such service provider spring-cloud-service-providerin /hellopublishing this path a service, you can access to this service through the following manner and obtain the corresponding return.

@Autowired
private RestTemplateBuilder restTemplateBuilder;

@GetMapping("world")
public String helloWorld() throws Exception {
    String instanceUrl = this.instanceUrl();
    String serviceUrl = instanceUrl + "hello";
    String result = this.restTemplateBuilder.build().getForObject(new URI(serviceUrl), String.class);
    return result;
}

Applications using Spring Cloud Usually we do not directly use DiscoveryClient or EurekaClient get to the service address, go down to the specified service address initiates a request, but the use of simpler Feign.

(Note: This article is written based on Spring cloud Finchley.SR1)

Guess you like

Origin elim.iteye.com/blog/2443136