This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

0 environment

System environment: Win10 editor: IDEACopy the code

1 Registry

Eureka is the registry in springcloud. Reason: When monolithic applications are similar to a straight line as the project gets bigger and bigger, systems are unbundled like that coupling (modules that collaborate with each other to call each other) in order to solve the coupling between services a registry is necessaryCopy the code

2 Eureka constitute

Eureka is divided into two parts: the server, which is the registry that receives other service registrations, and the client, which is the Java client used for registrations. Eureka has three roles: Eureka Server: Registration center Eureka Provider: service Provider Eureka Consumer: service ConsumerCopy the code

3 Foundation Construction

Eureka is developed by Java, and SpringCloud uses Springboot to encapsulate Eureka, making it easy to use.Copy the code

1 Specifies the version

 

 

2 Create a SpringBoot project to add Eureka dependencies

2 YML Enable the Eureka function

Spring: application: name: eureka 1234 # By default, Eureka Server is also a normal microservice, i.e., when it is a registry, it has two types: 1: registry 2: Common service (registering yourself on your own) He is a registrar who registers other people's marriages. He is also a member of the human race. He may also be married, so it is not impossible to register himself as old. # register-with-Eureka is false to indicate that the current project is not registered in the service center Obtaining registration information from the Server Eureka: client: register-with-eureka: false fetch-registry: falseCopy the code

3 Start the Project

Browser type http://localhost:1234/

4 Eureka Background Management page

4 Eureka cluster

After the registry is used, all services are exchanged through the service registry. The stability of the service registry is very important. Once the registry collapses, the whole system will be affected Multiple Eureka instances register with each other and synchronize data to form a Eureka cluster.Copy the code

1 a panoramic

 

2 yml configuration

Spring: application: name: eureka 1234 # By default, Eureka Server is also a normal microservice, i.e., when it is a registry, it has two types: 1: registry 2: Common service (registering yourself on your own) He is a registrar who registers other people's marriages. He is also a member of the human race. He may also be married, so it is not impossible to register himself as old. # register-with-Eureka is false to indicate that the current project is not registered in the service center Eureka: client: register-with-eureka: true fetch-registry: true http://eurekaB:1235/eureka instance: hostname: eurekaACopy the code
Spring: application: name: eureka 1235 # By default, Eureka Server is also a normal microservice, i.e., when it is a registry, it has two types: 1: registry 2: Common service (registering yourself on your own) He is a registrar who registers other people's marriages. He is also a member of the human race. He may also be married, so it is not impossible to register himself as old. # register-with-Eureka is false to indicate that the current project is not registered in the service center Eureka: client: register-with-eureka: true fetch-registry: true http://eurekaA:1234/eureka instance: hostname: eurekaBCopy the code

Three dozen jars

 

 

 

 

 

java jar xxxx.jar --spring.profiles.active=a
java jar xxxx.jar --spring.profiles.active=b
Copy the code
Some errors may be reported when launching 2 instances. This does not affect the login of the pageCopy the code

 

 

After the success of the startup is indeed mutual registration to build a clusterCopy the code

5 Eureka details

Eureka consists of Eureka Server and Eureka ClientCopy the code

1 Eureka Server

The external functions are as follows: (Receiving and provisioning on the server and synchronization on the server) 1. Register (receive) All services are registered on the server 2. Provide the registry (issue) The registry organizes all registered services into a list. Eureka gets this registry when calling the service. Synchronization Status The Eureka Client synchronizes the current Client status with the Eureka Server through registration and heartbeatCopy the code

2 Eureka Client

Eureka Client does a lot of things for you (automatically pull updates and cache server information even when all server nodes are cold and the Client can still get the desired address (but the address may not be accurate)) to simplify the interaction between each service and serverCopy the code

2.1 Service Registration

Service provider registers itself with service registry (server) Tip -> Service provider (partition on service partition) is essentially Eureka Client. When Eureka Client registers with Eureka Server, it needs to provide necessary meta information such as IP address name Running status Port etc...Copy the code

2.2 Service Renewal

After the Eureka Client registers with the Eureka Server, the Eureka Client sends a heartbeat message to the Eureka Server every 30 seconds (to tell the Client that I am still alive). If the message has been received by the Eureka Client for 90 consecutive days (the message cannot be sent for three consecutive times), the Eureka Client sends a heartbeat message to the Eureka Server It thinks the Client has hung up and immediately removes the Client from the service listCopy the code
Eureka: instance: # The default service renewal time is 30s lease-renewal-interval-in-seconds. The default validity time of the 30 # service is 90s lease-expiration-duration-in-seconds: 90Copy the code

2.3 Services Offline

Sends a message to the server when the client is offlineCopy the code

2.4 Obtaining Registry Information

The Eureka Client obtains service registration information from the Eureka Server and caches it locally. The local Client invokes the service to search for information such as the IP address of the remote service. The Cache service on the Eureka Client is updated periodically for 30 seconds If the local cache registry information changes from the information sent by the server, the Client automatically processes itCopy the code
Eureka: client # Whether to run fetch-registry: true # Update registry-fetch-interval-seconds: 30Copy the code

6 Eureka Cluster Principle

 

In this diagram, Eureka uses Replicate data synchronization. Nodes between different Eureka servers are equally positioned. Clusters between nodes are created by registering high-availability nodes with each other with a topmost serviceUrl The client automatically switches to other servers (each server node synchronizes with each other) Eureka Connection Mode Single-line connection is not recommended (1->2->3 In this case, 1 and 3 also synchronize.) You can specify multiple registered addresses (in this case, configure serviceUrl 1->2 1->3). Eureka zone: 1. Region > Different geographical regions 2. Zone > Specific equipment roomCopy the code

summary

2.Eureka has three roles: Server (registry) Client (provider) Consumer (service consumer) 3.Eureka builds dependencies Enable the server service on application. Configure the service name in YML. 4. Eureka cluster (similar to the Internet left you to transfer) For example, 2 eureka YML configuration note register-with-Eureka: true fetch-registry: True configuration a hostname serviceUrl - > other yml name (http://hostname:port/application) packaging operation for convenience Can see each other registered 5. Eureka server details - > Provides three external functions: Client -> Features: Automatic pull update cache information Service registration: the service provider registers itself (the business is divided into two roles) The essence is a person) need to provide some meta information WHEN registering IP what (similar id card mobile phone number what proof you are you joke good know you and you make friends) service renewal: registration up nightmare just started client to server30s renewal (fee) once tell the server don't kill me I am still alive When the server failed to receive the client message for 3 consecutive 90s, it fell offline (similar to the black bar, when your time is up, I will give you 3 chances and you haven't paid yet. You poor ghost, don't stand in the manger and hurry down), so it was removed from the service list by the server Get registry information (consumer): get information from the server cache local update operation -> Whether to update according to the information comparison provider: to register with the server to provide their own information and heartbeat time with the server (survival) agreement and service offline function Consumer: obtain the service registration information from the server and cache the service information for regular update processing 6. Cluster Server each node is equal. Replicate data synchronization server URL is the way dial-up nodes connect to each other (what if one node crashes)Copy the code