Table of contents
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.)
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
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:
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:
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:
Configuration:
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.
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:
The service is registered to all eureka:
4. Service registration
The registration center is ready. Next, we need to prepare a service and register it with the configuration center.
rely:
Configuration:
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:
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.
Configuration:
@LoadBalanced is used to host the RestTemplate to the ribbon for management.
Service call:
result:
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:
Service layer:
Service call:
Open Feign:
result:
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:
start up:
Service layer:
Service call: