Selection of registration center and construction of Eurake service

Selection of registration center and construction of Eurake service

In the previous article, I introduced the microservice infrastructure ( microservice infrastructure ). It also learned that the registry has Zookeeper and Eurake. Then which one should we choose as the service registry when developing?

Objectively speaking, it is feasible to use which as the registration center, but it still has to be selected according to the specific needs of the project.

The registration center is selected by the CAP theorem .

1. Zookeeper: cp design, it guarantees consistency and partition fault tolerance. When a cluster is built, if a node fails, the leader will be elected, or if more than half of the nodes are unavailable, the service cannot be provided, so the availability is not Satisfaction.
2.Eurake: ap design, no master and slave nodes, one node is down, and other nodes can be automatically switched to use, which guarantees availability, but cannot guarantee consistency.

In a distributed system, p is an element that must be guaranteed, so the design mode can only be selected for specific scenarios,
such as
financial systems, where a large amount of data needs to be backed up, etc., consistency needs to be guaranteed, zookeeper
e-commerce system is optional , and system response needs to be guaranteed Time needs to ensure the normal use of the system. At this time, the availability of the system must be considered, and euroke is optional.
I personally recommend using euroke, after all, system availability is still very important.

Euarke service construction

Step 1: Create a project

Idea create a project, select spring Initializr
Insert picture description here
and then the next step. After completing the coordinate configuration of maven, enter the dependency selection page and
Insert picture description here
select Eureka Server in Cloud Discovery, which will automatically add related Eureka dependencies to you.

The second step is to add the annotation @EnableEurekaServer

Eurake registration service has two components: Eurake Server (server) and Eurake Client (client)
@EnableEurekaServer is used to declare the service as the server and open the dialogue between services.
Startup process: The
Insert picture description here
Eurake server starts to provide a registration service (registry center). After each other client service node is started, it will be registered to the registry, and the registry list will store the information of other nodes.

The third step is to add configuration
# 默认的端口
server.port=8761
# 声明自己是服务端,不需要往自己身上注册
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

Pay attention to registerWithEureka and fetchRegistry, which are used to declare that you are a server and do not need to register yourself.
Insert picture description here
In this way, the Eurake service is completed, and there is no consumer to provide the service. The next article introduces the client service construction.

Eurake registration service notes
1. The self-protection mode cannot be turned off

A string of red fonts appears in the eureka management backstage: It is a warning, indicating that there is a low service online rate
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE
NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT
BEING EXPIRED JUST TO BE SAFE.
Check method for closing: eureka server configuration file plus
server:
enable-self-preservation: false
Note: Self-protection mode is prohibited to be closed, the default is to enable true

2. Why can the client register only by adding the address of the registration center?

This belongs to the latter content, let me talk about it first, there is an obvious introduction in the official website:

By having spring-cloud-starter-netflix-eureka-client on the classpath, your
application automatically registers with the Eureka Server. Configuration is
required to locate the Eureka server, as shown in the following example

In other words, if you mainly add the registry configuration, you can register to the registry by default, so it doesn't matter whether you add @EnableEurakeClient or not.

Guess you like

Origin blog.csdn.net/qq_31142237/article/details/90409919