Explore the technical principles of the 'Eureka Client' framework in the Java class library
EUREKA Client's technical analysis
Overview:
In distributed systems, the registration and discovery of service are very important components.Netflix is open to Eureka to solve this problem.Eureka uses the client-server architecture, where the service registration center is the server, and the application of the service serves as the client.Eureka Client is a Java class library for implementing the Eureka client function in the application.This article will explore the technical principles of the Eureka Client framework.
EUREKA Client's workflow:
1. When the application starts, Eureka Client sends a registration request to the service registration center.It will provide metadata information of the application, such as names, versions, hosts, and ports.
2. After receiving the registration request for the service registration center, the application of the application will be stored in the registry.
3. Eureka Client maintains a heartbeat connection with the registration center.If there is no heartbeat request within the scheduled time, the registration center will delete the application from the registry to prevent other applications from obtaining an instance that has been offline.
4. When other applications need to access the services provided by the application, they will send query requests to the service registration center.
5. Service registration center will return a list of available examples, including host, port and other metadata information.
6. The application selects an instance to call according to the returned instance list.
Eureka Client's configuration:
The following is an example of a basic Eureka Client configuration:
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.discovery.DefaultEurekaClientConfig;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.discovery.EurekaClientConfigBuilder;
import com.netflix.discovery.shared.transport.EurekaTransportConfig;
import com.netflix.discovery.shared.transport.SimpleEurekaTransportConfig;
import com.netflix.discovery.shared.transport.jersey.EurekaJerseyClientImpl;
import com.netflix.discovery.shared.transport.jersey.TransportClientFactories;
import com.netflix.discovery.shared.transport.jersey.TransportClientFactories.PollingTransportClientFactory;
import com.netflix.discovery.shared.transport.jersey.TransportClientFactories.StaticEurekaTransportConfig;
import com.netflix.discovery.shared.transport.jersey.TransportClientFactories.TransportConfigURL;
import com.netflix.discovery.util.EurekaEntityComparators;
import com.netflix.discovery.util.InstanceInfoGenerator;
public class EurekaClientExample {
public static void main(String[] args) {
// Create Eureka Client configuration
EurekaClientConfig config = new DefaultEurekaClientConfig();
// Create Eureka Transport configuration
EurekaTransportConfig transportConfig = new SimpleEurekaTransportConfig();
// Create Eureka Jersey Client
EurekaJerseyClientImpl jerseyClient = TransportClientFactories.createTransportClient(
new StaticEurekaTransportConfig(new TransportConfigURL("http://localhost:8761/eureka/")),
new PollingTransportClientFactory(transportConfig),
new InstanceInfoGenerator(new EurekaEntityComparators(config), ""),
config
);
// Eureka Client
ApplicationInfoManager applicationInfoManager = new ApplicationInfoManager(
config,
() -> "EurekaClientexample" // Provide an application name for Eureka Client
);
// Create Eureka Client
EurekaClient eurekaClient = new EurekaClient(
applicationInfoManager,
config,
jerseyClient
);
// Register for Eureka Server
eurekaClient.register();
// ... other application logic ...
// Turn off Eureka Client
eurekaClient.shutdown();
}
}
In this example, we created an Eureka Client instance, registered an instance from Eureka Server, then executed some other application logic, and finally closed the Eureka Client.
Summarize:
Eureka Client is a Java class library for implementing the Eureka client.It interacts with the service registration center to realize the application of application registration, discovery and calling functions.This article introduces the work flow and basic configuration example of Eureka Client.Using Eureka Client, we can easily implement the service registration and discovery in the distributed system to improve the scalability and availability of the system.