Spring cloud implements the registration center Eureka

Eureka is an open source product of Netflix that provides service registration and discovery. It provides a complete implementation of service registry and service discovery. It is also one of the most important and core components in the spring cloud system.

Background introduction

Service Centre

The service center, also known as the registration center, manages various service functions, including service registration, discovery, fusing, load, degradation, etc., such as various functions in the background of Dubbo admin.

With the service center, what will happen to the calling relationship? Draw some diagrams to help understand

Item a calls item b

Normally call item a and request item b

After having a service center, any service can't be removed directly. It needs to be called through the service center

Item a calls item B, and item B calls item C

At this time, the calling steps will be two steps: first, project a first requests project B server from the service center, and then project B requests project C service from the service center.

The above projects are only two or three simple calls to each other, but if there are more than 20 and 30 projects, our distributed projects will reach more than 20 by the end of 2015. Draw a diagram to describe the mutual call relationship between dozens of projects, all of which are lines. Any change to one of the projects will involve several projects and restart, It's troublesome and error prone. To obtain services through the service center, you do not need to pay attention to the IP address of the project you call. It is composed of several servers. Each time, you can directly go to the service center to obtain the available services to call.

Since all kinds of services are registered in the service center, there are conditions to do many advanced functions. For example, several services provide the same service to balance the load; Monitor the server call success rate to fuse and remove the fault point in the service list; Monitor the service call time to set different weights for different servers, and so on.

Let me gossip about Netflix before talking about Eureka

Netflix

The following introduction comes from Baidu Encyclopedia:

When I first saw this word, it was at the beginning of various American dramas or movies. The representative American dramas made by Netflix include house of cards, drug lords and strange tales. Later, when studying spring cloud, I found Netflix company and wondered whether they were the same company. After checking the email suffix on GitHub, it was determined that they were indeed the same company. In fact, the microservices of spring cloud were based on the open source products of Netflix company.

Netflix's open source framework components have been verified in the large-scale distributed microservice environment of Netflix for many years, and are gradually accepted by the community as the standard components for constructing microservice framework. Spring cloud open source products are mainly based on the further encapsulation of Netflix open source components to facilitate spring developers to build the basic framework of microservices. For some companies planning to build a microservice framework, making full use of or referring to Netflix's open source microservice components (or spring cloud) and making necessary enterprise customization on this basis is undoubtedly a shortcut to microservice architecture.

Eureka

According to the official introduction:

Spring cloud encapsulates the Eureka module developed by Netflix to realize service registration and discovery. Eureka adopts the C-S design architecture. Eureka server is the server of service registration function. It 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 following:

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

1、Eureka Server

2、Service Provider

3、Service Consumer

Case practice

Eureka Server

Spring cloud has helped me implement the service registry. We only need a few simple steps to complete it.

1. Add dependency in POM

2. Add @ enableeurekaserver annotation in startup code

3. Configuration file

By default, the service registry will also try to register itself as a client, so we need to disable its client registration behavior in application Properties add the following configuration:

After starting the project, visit: http://localhost:8000/ , you can see the following page, in which no service has been found

colony

The registry is such a key service. If it is a single point, the failure is devastating. In a distributed system, the service registry is the most important basic part, and should be in the state of providing services at any time. In order to maintain its availability, using cluster is a good solution. Eureka implements high availability deployment by registering with each other, so we only need to configure Eureka server with other available serviceurls to realize high availability deployment.

Dual node registry

For the first time, we try to build a two node registry.

1. Create application-peer1 Properties as the configuration of the peer1 service center, and point the serviceurl to peer2

2. Create application-peer2 Properties as the configuration of the peer2 service center, and point the serviceurl to peer1

3. Host conversion

Add the following configuration to the hosts file

4. Package start

Execute the following commands in sequence

After starting in sequence, the browser enters: http://localhost:8000/ The renderings are as follows:

It can be seen from the figure that DS replicas, the registry of peer1, already has the relevant configuration information of peer2 and appears in available replicas. We manually stop peer2 to observe. It is found that peer2 will move to the unavailable replicas column, indicating that peer2 is unavailable.

The configuration of this dual node has been completed.

Eureka cluster usage

In production, we may need three or more registration centers to ensure the stability of the service. In fact, the configuration principle is the same. Point the registration centers to other registration centers respectively. Here we only introduce the configuration of the three clusters. In fact, it is similar to the two node registry. Each registry can point to the other two nodes. Use application YML to configure.

application. YML configuration details are as follows:

Start the Eureka registry with the configuration parameters of peer1, peer2 and peer3 respectively.

After starting in sequence, the browser enters: http://localhost:8000/ The renderings are as follows:

You can see the relevant information of peer2 and peer3 in peer1. So far, the Eureka cluster has been completed

Sample code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>