Spring cloud: Eureka service registration and discovery

I. Introduction to Eureka

According to the official introduction:

Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers.

Eureka Is based on REST Services, mainly in AWS Use in the cloud, Location services are used for load balancing and failover of middle tier servers.

Spring Cloud encapsulates the Eureka module developed by Netflix to realize service registration and discovery. Eureka adopts the C-S design architecture. Eureka Server, as a server with service registration function, is the service registration center. Other microservices in the system use Eureka's client to connect to Eureka Server and maintain heartbeat connection. In this way, the maintenance personnel of the system can monitor whether each micro service in the system is running normally through Eureka Server. Some other modules of Spring Cloud (such as Zuul) can discover other micro services in the system through Eureka Server and execute relevant logic.

Eureka consists of two components: Eureka server and Eureka client. Eureka server is used as a service registration server. Eureka client is a java client, which is used to simplify the interaction with the server, act as a polling load balancer, and provide service failover support. Netflix uses another client in its production environment, which provides weighted load balancing based on traffic, resource utilization, and error status

Use a picture to understand:

The figure above briefly describes Eureka's basic architecture, which consists of three roles:

1,Eureka Server
Provide service registration and discovery

2,Service Provider
service provider
Register your service with Eureka so that service consumers can find it

3,Service Consumer
Service consumer
Get the list of registered services from Eureka so that you can consume services

II. Creation of Maven project
Create Maven project as our parent project

III. Eureka service

Create a model directly under the current parent project

Select Spring Cloud Discovery, check Eureka Server, and click Next,

Note here that the directory must be a separate directory under the parent project

After building, modify the configuration file and add @ EnableEurekaServer annotation to the startup class;

#Port number
server.port=8761
#service name
spring.application.name=eureka-server


#Sets the host name of the current instance
eureka.instance.hostname=localhost
#Whether to register itself with EureakServer. The default value is true; Since the current project itself is EurekaServer, it is set to false;
eureka.client.register-with-eureka=false
#Identify whether to obtain registration information from Eureka Server. The default value is true; Since the current project itself is EurekaServer, it is set to false;
eureka.client.fetch-registry=false
#Set Eureka's address
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

After configuration, start the project, open the browser and enter localhost:8761. The page is as follows. So far, EurekaServer service has been built successfully.

IV. setting up EurekaClient service
Similarly, create a Model under the parent project. The steps are similar to building a Server service. They will not be described one by one here. The steps are shown in the following figure:

After building, add web dependency in Eureka client

<!--add to web rely on-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Modify the configuration file, add @ EnableEurekaClient annotation on the startup class, and finally start the project.

#Port number
server.port=8080
#service name
spring.application.name=eureka-client
eureka.client.healthcheck.enable=true
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }

}

Start the project and enter Eureka registration center to find that Eureka client service has been registered

Note: start the Eureka server service first and then start other services, otherwise an error will be reported and the registry cannot be found

In the process, if a small partner encounters problems or has good opinions, he can contact in time

github address

Keywords: Java Spring Distribution Spring Cloud eureka

Added by vahidi on Tue, 25 Jan 2022 07:27:19 +0200