Detailed explanation of Eureka service registration and invocation

Table of contents

1 Overview

2. Environment

2.1. Project structure

2.2. Project structure

2.3. Dependency

3. Registration Center

3.1. Configuration and use

3.2. Management page

3.3. Cluster

4. Service registration

5. Service call

5.2.Ribbon

5.3.Feign

5.4.OpenFeign


1 Overview

We call the traffic entering the microservice cluster from the outside as "north-south traffic", and the traffic flowing between the nodes of the microservice cluster as "east-west traffic". As far as "north-south traffic" is concerned, the microservice architecture is actually still a centralized architecture. This center is a "directory", because a "directory" is needed to record service-related information (such as which server it is deployed on), "north-south traffic" to find the service. This "directory" is called "registration center" in the microservice system. The registry is the most important component in the microservice system. In the Netflix version of spring cloud, the registry component is eureka.

Service providers register data with the registry (Eureka Server), and consumers obtain data from the registry (Eureka Server). The microservices in the system use the Eureka client to link to the Eureka Server, and maintain a heartbeat (that is, send a signal to the server at a fixed time to prove that it is alive.)

3f010a2d57bc44c3929635624023fddf.png

In the entire process above, there are two components involved:

  • registration center
  • Remote Service Invocation Component

In the Netflix version of spring cloud, the registration center has always been eureka, but there are several options for remote service call components:

  • Ribbon
  • Feign
  • OpenFeign。

The relationship between them is gradually optimized and replaced from top to bottom.

This article will first talk about using eureka for service registration, and then sequentially talk about the use of the above remote service call components and their optimization points compared with the previous one.

2. Environment

2.1. Project structure

f77f7640556d4341a8bf7ff4a6d71a52.png

The entire sample project uses a standard maven structure and is divided into three sub-modules:

  • eureka, registry
  • userService, service provider
  • consumer, service caller

The dependency version management of the entire project is performed in the top-level parent pom.xml.

2.2. Project structure

The reason why the entire sample project is divided into three parts is to use the communication component between the service caller and the service to show how to combine the registration center for inter-service communication:

57985a07acbd488c81204921c264f5e2.png

2.3. Dependency

On the spring official website, enter the spring cloud project. The home page of the project records in detail the version correspondence between the Netflix version of spring cloud and spring boot:

327dc33e149b496dac0d19b5749d8fa4.png

For this project, we choose spring boot 2.6.X and spring cloud Netflix aka jubilee.

Introduce dependencies in the top-level parent pom:

 <properties>
        <spring-cloud.version>2021.0.7</spring-cloud.version>
        <spring-boot.version>2.6.10</spring-boot.version>
    </properties>
    <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>

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

3. Registration Center

3.1. Configuration and use

rely:

0f066b4c876c42cc86f33b98bdd9643f.png

Configuration:

a9ae69d42d474828bd096b37daed03dd.png

start up:

@SpringBootApplication
@EnableEurekaServer
public class EurekaMain {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain.class);
    }
}

3.2. Management page

You can access the management page of eureka server through IP+port number:

The most important part of the entire management page is the framed part. This part shows what services are registered on the current eureka server. There is no service registration yet, so nothing is displayed.

0b584b7bd7dc4a628d6ea7730380b845.png

3.3. Cluster

Eureka supports cluster mode for mutual redundancy and disaster recovery. The configuration of the eureka cluster is very simple, there is no need to write an example separately, the following is the eureka cluster configuration file made by the blogger before, it will be clear at a glance after reading it.

Associate other eurekas on each eureka:

362efe9726a545588320a18d472450ba.png

 The service is registered to all eureka:

32370aef2e5f469ebba21f6711dcdbf0.png

4. Service registration

The registration center is ready. Next, we need to prepare a service and register it with the configuration center.
rely:

219da76711e8446185e49a32d368dfd5.png

Configuration:

0ecd5d6e60224e5b95b318561dbf453b.png

start up:

@SpringBootApplication
@EnableEurekaClient
public class UserServiceMain {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceMain.class,args);
    }
}

After starting the service, you can check whether the service has been registered on the eureka server: 

6bb460a189634e888562a16acfda092f.png

5. Service call

After preparing the registration center and registering the service, the next step is how to call the service. That is, how to use the following components to call services:

  • Ribbon
  • Feign
  • OpenFeign

5.2.Ribbon

Ribbon is just a load balancing component, which needs to rely on other HTTP communication components to achieve communication. Generally, the RestTemplate that comes with ribbon+spring boot is used.

rely:

Ribbon has been integrated in eureka, so when you want to use ribbon to adjust services, you can directly introduce eureka's dependencies.

59975dff3e7e414098e545cd5d33a227.png

Configuration:

@LoadBalanced is used to host the RestTemplate to the ribbon for management.

2d3da4cd755f4dbc989e803402a1bb63.png

a3b3510bbc5d45df9acd2c4743fbaa03.png

Service call:

ef71b7117e0249dda00285ed6dd08c52.png result:

f5cd5143680f43ec9216d7ee2f0e9e48.png

5.3.Feign

Ribbon+restTemplate can complete service calls and load balancing, but there is an obvious place that the service name is directly written to death, which is not easy to maintain. The main idea in the srping system is dependency inversion, that is, interface-oriented programming, so the community launched Feign, which encapsulates ribbon and http request tools, so that in addition to completing load balancing and http requests, it can also complete service calls through interface-oriented programming .

rely:

78a7b4f555464513b483707bca7b10d7.png

Service layer:

7752a45e57484571a6178a5e3128924e.png Service call:

a794982e4c4e41669e065a9f6529873b.png Open Feign:

e497bf6d566748a1901bbd155344a089.png

result:

f5326f1139a6460ba07ef9b0ac4fb5cd.png

5.4.OpenFeign

Feign is an interface-oriented, declarative service call component, which encapsulates the ribbon, with the purpose of simplifying the service call code and making the service call have a rest style. Openfeigin is an upgraded version of feigin, which enhances the support for springMVC annotations.

The above is an example of OpenFeign written by a blogger.

rely:

5bf7e8479d7b4da18a9f1220d13e9f56.png

 start up:

7a532e9362704fcd8c02bf8ced57b82d.png

Service layer:

96dc16d710be49c79ee0f9f50edf9829.png

Service call:

84655da40d274dc9a5bffa1f765e1640.png

Guess you like

Origin blog.csdn.net/Joker_ZJN/article/details/130907451