“This is the 24th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

1. What is Eureka

As the registry center of Spring Cloud’s Netflix sub-project, Eureka provides service registration and discovery functions. In the microservice project, where each service is distributed on a different machine, Eureka is used to provide a platform for all services to register and to retrieve addresses from the Eureka registry when requesting other services.

2. Principle of Eureka

Eureka framework uses CS architecture, through a Server, multiple clients to communicate and provide functional services.

As the service registration Server, Eureka Server is the service registration and discovery center and manages the registration information of Eureka Client. The Eureka Server itself is registered as a Client, and all Eureka clients can discover other services through the registry in the Server.

Eureka Client is a Java application Client, that is, a standalone service. To facilitate Client management in the Server, Eureka periodically checks the heartbeat of each Eureka Client to ensure the availability of registered services in the list. In order to deal with the request interaction between services, the Eureka Client uses a load balancer based on polling.

3. Characteristics of Eureka

As the central node, Eureka Server not only needs to regularly check the monitoring status of other Eureka clients, but also needs to judge the feasibility of its own service. If the heartbeat of more than 85% of the registered nodes fails within 15 minutes, the connection between Eureka Server and other Eurka clients is considered to have failed. In the event of a failure, Eureka will respond as follows:

  • Eureka believes that a network fault may cause the Client to be removed from the Server
  • Eureka can still receive registration and query requests for new services, but does not synchronize with other nodes to ensure that the current node is available
  • After the network is restored, the information during this period of time will be synchronized and the service will not be affected

4. Use of Eureka

As a service registration and discovery center, Eureka is divided into the server that provides registration and discovery functions and the client that registers and is discovered.

4.1 Creating aServer EurekaServer

When creating the project, SpringBoot has provided us with relevant initiator information. To create a EurekaServer, we need to introduce the Dependency of EurekaServer under Spring Cloud Discovery into the project. And Spring Web dependencies that provide services to run properly.

In this case, the dependency information imported by the server based on the initiator is:

<! - eureka dependency information server - > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <! Versioning with Spring Cloud No eureka rely on failed download - > < dependencyManagement > < dependencies > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>Copy the code

After the project is created, you need to configure the project to ensure the normal running of the service. As a EurekaServer service, the current project is marked as the Server using the @enableEurekaserver annotation on the startup class. In addition, the configuration information of the Server is defined in the configuration file.

# Service information
spring.application.name=eureka-server
server.port=8081
The server also exists as a client. Disable the client registration function here
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Define the server protection mechanism
eureka.server.enable-self-preservation=true
eureka.server.renewal-percent-threshold=0.5
Define the registry path
eureka.client.service-url.default-zone=http://${eureka.instance.hostname}:${server.port}/eureka
Copy the code

After the configuration is complete, the Eureka Server project can be started normally. After the successful startup, the console outputs the Eureka startup information.

In order to better manage the Eureka Client registration information, Eureka Server also provides a visual page to observe the current registered service information. After the Eureka Server is started, you can access httP:// hostname:{hostname}:hostname:{port}, where the list of clients registered in the Eureka Server is displayed.

4.2 Creating a Eureka Client

The process of creating a Client is essentially the same as that of a server, but the Spring Web and Spring Cloud Discovery Client dependencies are used to select the initiator.

The maven dependency information obtained after the project creation is roughly the same as that of Eureka Server service, which is also changed from Server to client.

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Copy the code

EurekaClient When the EurekaClient is started, the @enableEurekaclient annotation must be annotated on the startup class, and the service information and the address of the remote access registry must be configured in the configuration file.

# Service information
server.port=8091
spring.application.name=producer
Registry path
eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka
Configure heartbeat interval detection
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=10
Copy the code

After the configuration is complete and the project is successfully started, you can see that the Client finds the registry through the configured path and registers itself in it.

If you open the Eureka Servel management page again, you can see that the client has been registered.