Spring Cloud Series: Service Registration and Discovery Components - Eureka (on)

Author's other platforms:

| CSDN:blog.csdn.net/qq_41153943

| Nuggets: juejin.cn/user/651387…

| Zhihu: www.zhihu.com/people/1024…

| GitHub: github.com/JiangXia-10…

This article has a total of 4950 words and is expected to read for 13 minutes

foreword

In a distributed system, the service registry plays an important role and is an indispensable member of service discovery and client load balancing. In addition to the basic functions of the registration center, its stability, availability and robustness have a great impact on the smooth operation of the entire distributed system.

What is a Service Registry

In previous small monolithic applications, calls between components only needed to be made through interfaces with specification constraints. However, in the microservice architecture, the original large application is usually divided into relatively independent services that provide specific functions according to the business. Each microservice can be dynamically expanded through clustering or other methods. Each The network addresses of microservice instances may change dynamically. In the microservice architecture, the service span is large and the number is large, so it is necessary to establish a decentralized component to register and manage the information of each microservice instance, and at the same time provide the ability to enable each microservice instance to discover each other, so as to achieve The result of calling each other.

Therefore, it can be considered that service registration and discovery include two parts, one is the Server side and the other is the Client. The server side is a common component, which provides the function of service registration and discovery for the client side, maintains the relevant information of the client side registered to itself, and provides an interface for the client side to obtain information about other services in the registry, making dynamic changes The client can make calls between services when the service address is stable. The client registers its own service information on the server in a certain way, and maintains the consistency of its own information within the normal range, so that other services can find itself conveniently. At the same time, it can obtain other service information that it depends on through the server. service call.

So in summary, the service registry refers to proposing a single service in the entire microservice architecture. This service does not complete any business functions of the system, but is only used to complete the service registration and service discovery of the entire microservice system, as well as the health status of the service. Monitoring and management functions.

The functions of the service registry can be summarized as:

1. Store all microservice information, such as microservice name, ip, port, etc.;

2. When making service calls, use service discovery to query the list of available microservices and network addresses for service calls;

3. Perform heartbeat detection on all microservices. If some instances are found to be inaccessible for a long time, the instances will be removed from the service registry.

Common service registration and discovery components include: netflix's eureka, zookeeper (for zk, please refer to the previous article: zookeeper tutorial: getting started ), consul, and Ali's nacos (as nacos as the registration center, please refer to the previous article: SpringCloud: Build Nacos service and service discovery )

What is Eureka

Eureka comes from ancient Greek, meaning "I found it! I found it!" According to legend, Archimedes discovered the principle of buoyancy while taking a bath. He was so happy that he didn't have time to put on his pants and ran to the street and shouted: "Eureka (I found it)!" . In Netflix, Eureka is a basic service component for REST-style service registration and discovery, mainly for load balancing and failover of mid-tier services in AWS. Eureka consists of two parts, one is Eureka Server, which provides service registration and discovery functions; the other is a Java client, called Eureka Client, which is to make it easier to interact with the server, and Eureka Client will regularly register its own information Go to Eureka Server and discover other services from Server. There is a built-in load balancer in the client for basic round robin load balancing.

So Eureka mainly consists of two components: Eureka Server and Eureka Client.

Today's article mainly learns how to develop eureka server as a service registry.

Practical development

My project structure is as follows:

picture

First you need to create a springcloud project. I have created a maven aggregation project here as a parent project for subsequent learning. The parent project is mainly used for dependency management and version control. So you need to add some project-related dependencies to the parent project. The complete pom file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jiangxia</groupId>
    <artifactId>springcloud_parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>springcloud01_eureka_server</module>
    </modules>

    <!--继承springboot的父项目-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>
    <!--父项目不写代码,只维护依赖和版本-->
    <!--自定义properties属性-->
    <properties>
        <!--具体的springcloud版本-->
        <spring.cloud-version>Hoxton.SR6</spring.cloud-version>
    </properties>

    <!--维护版本-->
    <dependencyManagement>
        <dependencies>
            <!--维护springcloud版本依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud-version}</version>
                <!--父项目类型都是pom类型-->
                <type>pom</type>
                <!--当前项目也要导入一个父项目-->
                <scope>import</scope>
            </dependency>
        </dependencies>

    </dependencyManagement>
</project>

Then create a sub-project springcloud01_eureka_server, which is the project that uses eureka-server for the service registration center here. Since the subproject inherits the dependencies of the parent project, you only need to add the following dependencies to the pom file of the subproject:

 <dependencies>
        <!--引入springbootweb依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--引入eurekaserver依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

Then you need to add an entry startup class. In addition to using the @springbootapplication annotation, you also need to add the @enableEurekaServer annotation to indicate that the current project is a service registry.

/**
 * @author jiangxia
 * @date 2022年05月18日 21:02
 */
@SpringBootApplication
//开启eurekaserver注解:表示当前应用是一个服务注册中心
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

The last step is to do some configuration in the configuration file. In the application.properties file, you can configure the service port, service name, registration center address and other configurations, as follows:

#eureka默认端口就是8761,这里和默认保持一致
server.port=8761
#指定服务名称,唯一标识,服务名不能出现下划线,如果不指定名称,显示的就是unknown
spring.application.name=eurekaserver
#指定服务注册中心的地址,暴露服务地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
# 关闭eureka立即注册,默认事true
eureka.client.fetch-registry=false
#让当前应用仅仅是服务注册中心,即开启不自己注册自己,默认是true
eureka.client.register-with-eureka=false

So far, the eureka server development service registration center has been developed. Startup project:

picture

Type in the address bar:

http://localhost:8761/

You can see the management interface of eureka server:

picture

It can be found that there is no application here, because no application has been registered yet. The above has a configuration:

# 关闭eureka立即注册,默认是true
eureka.client.fetch-registry=false
#让当前应用仅仅是服务注册中心,即开启不自己注册自己,默认是true
eureka.client.register-with-eureka=false

If the above configuration is changed to true, or not set (the default value is true). Then the eureka server will register itself in the registration center when it starts, as follows:

picture

picture

And during the above startup process, there is an error message output on the console:

picture

This is because eureka includes two components, server and client. When the eureka server component is introduced into the project, the eureka client will be introduced into the project at the same time. Therefore, it will start itself as a service center when starting, and at the same time Register yourself as a client in the registration center, and we commented the two configurations in the configuration file, so the default is to register immediately at startup (the same is true if the previous configuration is set to true), but the service is not yet ready for registration Done, so report an error! So set the above two configurations to false.

Summarize

The above is the use of eureka server for the development of the service registration center. The main steps are to create the corresponding springcloud project, then import springcloud and eureka server related dependencies, and finally configure the eureka server in the configuration file, and use the @EnableEurekaServer annotation on the entry class to start the service Just register the center.

Subsequent articles will continue the development of eureka client.

related suggestion

Guess you like

Origin blog.csdn.net/qq_41153943/article/details/125646062