What had been
Eureka is a service discovery framework developed by Netflix. As a REST-based service, Eureka is mainly used to locate middle-tier Services running in AWS(Amazon Web Services, Amazon Cloud) domain, so as to achieve load balancing and middle-tier service failover. SpringCloud integrates it into its sub-project spring-Cloud-Netflix to realize SpringCloud’s service discovery capabilities. In essence, Eureka is a dedicated server for service discovery, where some services register and other services find the services they want to invoke and execute. There are many components that can serve as a service discovery server, such as Zookeeper and Consul.
Eureka client
Eureka consists of a client and a server. The client and server communicate with each other through HTTP.
The client will send the registration request to the server. The request parameters are mainly the service name, IP +port and other information, and there are three opportunities to send the registration request
- Initiate registration while initiating initialization
- If the server returns 404 while sending heartbeat, initiate registration
- The client cache instance lease information changed and initiated registration. Procedure
After application launch Client to the server on a regular basis heartbeat messages tell the server (that is, oneself still alive) default is 30 s, of course this time through eureka. Instance. Eureka. Lease renewal – interval – in – seconds to adjust, The default value is 30s.
Other scheduled tasks are initialized when the client is started, including refreshing the client configuration and periodically obtaining registry information (including full and incremental) from the Server. You can view the client source code for details
Eureka source code – client initialization Eureka source code – registration & heartbeat Euerka source code – client registry update Euerka source code – client configuration update
Had the service side
Eureka is AP and can be configured with multiple regions, as can be seen from the architecture diagram above. How does Euerka Server handle instances registered by clients? The Euerka Server is responsible for processing client requests (registration, heartbeat, status update, cancellation, deletion, registration query and other related operations), storing registry information, and synchronizing data between Euerka Server clusters. Euerak Server side stores the registry and recently changed (these two are the source of processing the client to obtain the registry data)
// Registration information
ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>> registry= new ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>>()
// Recently changed
ConcurrentLinkedQueue<RecentlyChangedItem> recentlyChangedQueue = new ConcurrentLinkedQueue<RecentlyChangedItem>()
Copy the code
EurekaServer handles the client download registry with incremental and full downloads, and downloads from readOnlyCacheMap by default
private final ConcurrentMap<Key, Value> readOnlyCacheMap = new ConcurrentHashMap<Key, Value>();
private final LoadingCache<Key, Value> readWriteCacheMap;
Copy the code
If a shared variable is a set and there are high concurrent reads on the shared set, write operations on the shared set, and write operations on the shared set, Highly concurrent reads can also yield stable data. That’s the stability of set iteration.)
Eureka adds read/write locks for registration, write locks for writing, and write locks for querying. The read-write lock problem in Eureka is to solve the iterative stability problem of the shared set, which refers to the registry and recentlyChangeQueue queue
-
Why can’t write locks be added to renew operations
If a write lock is added to a contract renewal operation, it means that the server cannot process any read/write operation of another client when the contract renewal is processed. Note: The renewal operation is a very frequent operation
-
Why do renew operations not have read locks
If a read lock is added to a contract renewal operation, all write requests of other clients will be blocked. The contract renewal operation is a frequent operation
-
Why do write operations have read locks
Adding a write lock means that any write operation will block the read/write processing of the registry.
What happens when a write adds a read lock? It is possible for multiple clients to read the same data in the registry and modify it, but this is not a problem because registry is JUC
-
Why do read operations add write locks
Because the write operation adds a read lock, the read operation adds a write lock in order to achieve read and write mutual exclusion
Note: Write locks are added for incremental downloads only, but not for full downloads
-
Why does full download not add write lock
Full download operation is the registry, incremental download operation is recentlyChange value
If a full download adds a write lock, it means that any renewal operations that occur during its read are blocked
Eureka source code – server initialization Eureka source code – processing client registration Eureka source code – processing access registry
Source code reading Details
Consider the design of Eureka and its handling methods, such as read/write separation, read/write lock, full incremental download, and the design of states (status, overidenStatus, etc.).