[Springcloud] Detailed Eureka (service management)

Here Insert Picture Description
Today we talk about the first component --Eureka Springcloud in.

Eureka What is that?

Eureka is a micro-services framework component is responsible for completion of the service governance. In short, since the micro-services among the services are independent of each other, and that if they want to call interface with each other, you need to use Eureka.
Eureka is equivalent dubbo + zk.
Here Insert Picture Description
Among them, the registry is a server Eureka, the service providers and consumers are Eureka client. Here we take a look at the service of governance mechanisms Eureka.

service providers

Service Registration

B service (service provider) at startup, will send via REST requests will register itself to Eureka Server (service registry) on. After receiving this registry REST request, the relevant information is stored in a Map-layer structure, this is the instance name service name Map + particular service.

Service Name position
Services B 192.168.0.100:8000、192.168.0.101:8000
Services D 192.168.0.100:9000、192.168.0.101:9000、192.168.0.102:9000、

You can see, B and D services are service providers, are clustered deployment. B 100, 101 service deployed on two servers, the port number is 8000. 100, 101, D services deployed on three servers, the port number is 9000.
It is worth mentioning is that we can modify the following parameters in the configuration file to start and close the registration function.

eureka.client.register-with-eureka=true

Synchronization Service

We can also see in the figure, not only for service providers and consumers to do a cluster deployment, the registration center has done a cluster deployment. When the service provider sends a registration request to a registration center which, this registry forwards the request to the other cluster of registry, thereby achieving synchronization service.
Premise service is synchronized between each registration center is registered as a service.

Service contract

Service providers will continue to maintain a heartbeat to tell the registry: "I am alive", which is called service contract. The following two parameters need our attention:

eureka.instance.lease-renewal-interval-in-seconds=30
eureka.instance.lease-expiration-duration-in-seconds=90

The former representative of the service provider every 30s to the registry to send a heartbeat, the latter said that if did not receive a heartbeat registration center within 90s, you think it's dead, it will be in the registration list of "excluded." Both time is the system default, it can also be modified according to the actual situation.

Consumer Services

Access to services

When consumer service starts, it sends a REST request to the registration center, registration center will service list is sent. This service list for services consumers, is read-only, and is updated every 30s.
Open access to services function:

eureka.client.fetch-registry=true

Modify the update cache list:

eureka.client.registry-fetch

Service call

After the service consumer access to the list of services, it will be to find the address of the service provider's server through the service name. Because the service provider is in the form of clusters, so there are multiple server addresses. The default is to use a polling way to call.

Service offline

When a consumer or service provider to shut down, it sends a request to the REST service off the assembly line registration center, registration center after receiving the request, the service status to offline, the offline event and to spread out .

Service registry

Excluding fail

We speak service contract when I said before, if more than 90s did not send heartbeat, the registry will be removed. Registry at startup, it creates a scheduled task, from time to time (default 60s) when the list of the current Super League (default 90s) did not renew removed out of service.

Self-protection

In order to avoid "suicide excluded", the registry self-protection mechanism. Shajiao suicide excluded? Imagine, if a network is unstable, in 90 seconds services on the registration list did not come on time sends a heartbeat, that they removed all registration centers should do? NO, the registry was not stupid.
If the proportion of heart failure than 85% within 15 minutes, the registry will instance the current registration information protected, no culling.
Further thought, even if not excluded, that consumers still call is unsuccessful ah, how do? This involves a fault-tolerant mechanisms such as circuit breakers, talk about the future.

And the difference between zk

(1) ZK is more biased in favor of consistency, Eureka prefer to availability.
zk has master and follower of the points, but when entering election mode, can not provide services outside. Eureka is in the form of clusters, the same as the status of each server is unavailable will not appear.
(2) Eureka has a self-protection mechanism, ZK no.

summary

(1) About service governance mechanisms to everyone drew a handsome ugly ugly handsome chart for reference:
Here Insert Picture Description
(2) deep thinking about a problem, we know, Eureka is the interface for calls between each individual service, and that if is a service interface calls within and between each file it, how should I call?
A: There are two ways: directly or with Spring's IOC new technology. Calls between internal service equivalent to service layer transfer dao layer, so you can write in the service layer:
① direct new way

UserDao userdao=new UserDao();

② with Spring's IOC technical

@Autowired
UserDao userdao;
Published 258 original articles · won praise 769 · views 340 000 +

Guess you like

Origin blog.csdn.net/qsbbl/article/details/97624037