This Spring Cloud series of directories
- If you plan to learn about Spring Cloud, too
- Spring Cloud Eureka implements service registration and discovery
- Spring Cloud Eureka implements security control
Don’t stop learning every day, and be sure to be better than others, but don’t let them throw you off
Students, your Spring Cloud is here again!
The first two articles implemented a single point registry, with one service provider and one consumer, and then added security controls on top of that.
This article implements a highly available version of the service registry. In an online environment, high availability is a basic requirement so that the entire service does not become unresponsive in the event of a single point of failure.
The high availability here includes the high availability of the registry. Service providers register with the service center cluster, so that service consumers can discover services through the cluster, and the registry will not be unavailable, causing the whole system crash.
It also includes high availability of service providers, where a service provider registers multiple instances in a registry so that even if one of them goes down, the other instances can still provide services.
High availability service registry discovery center
The figure below is an official Eureka high availability architecture diagram.
! [Eureka architecture diagram]
1. Create a registry, focusing on configuration files
The bootstrap.yml configuration is as follows:
spring:
application:
Name: eureka-ha-center ## Set the name of the application
cloud:
inetutils:
ignoredInterfaces:
- docker0
- veth.*
- VM.*
preferredNetworks:
192.168
Copy the code
Application. Yml configuration is as follows:
spring:
profiles: eureka-center1
server:
port: 1989
eureka:
instance:
hostname: ha-eureak-center1
Appname: registry
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
DefaultZone: http://localhost:1988/eureka # # registered to eureka - center2, port 1988
---
spring:
profiles: eureka-center2
server:
port: 1988
eureka:
instance:
hostname: ha-eureak-center2
Appname: registry
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
DefaultZone: http://localhost:1989/eureka # # registered to eureka - center1, port 1989
Copy the code
With the Profiles parameter, the configuration is enabled later on startup based on vm parameters.
Client. RegisterWithEureka and client. RegisterWithEureka set to true, said to be registered to the eureka. Set to false in single point mode. The high availability version will allow registration to Eureka. Note: The Serviceurl. defaultZone of Eureka-center1 and Eureka-Center2 are registered with each other.
2. Start the application.
– Start registry 1 on port 1989 by setting the VM parameter -dspring.profiles. active=eureka-center1
– Start registry 2 on port 1988 by setting the VM parameter -dspring.profiles. active=eureka-center2.
If you visit http://localhost:1988 or http://localhost:1989, you can enter the EUreka UI and see the Eureka service registered with you.
2. Create a highly available service provider and register with the two registries above
Application. Yml configuration is as follows:
spring:
profiles: ha-provider1
application:
name: ha-provider
security:
user:
name: root
password: root
server:
port: 1990
eureka:
instance:
preferIpAddress: true
client:
serviceUrl:
defaultZone: http://localhost:1989/eureka,http://localhost:1988/eureka
---
spring:
profiles: ha-provider2
application:
name: ha-provider
security:
user:
name: root
password: root
server:
port: 1991
eureka:
instance:
preferIpAddress: true
client:
serviceUrl:
defaultZone: http://localhost:1989/eureka,http://localhost:1988/eureka
Copy the code
Profiles are also used to distinguish between two service providers.
Notice that defaultZone sets up the two registry services started above, separated by a comma.
Then set the VM parameter -dspring.profiles. active=ha-provider1 to start the first service provider and the VM parameter -dspring.profiles. active= ha-Provider2 to start the second service provider.
Then open the Eureka UI and see that the registry has been registered
3. Create service consumers
Application. Yml configuration:
spring:
application:
name: ha-customer
server:
port: 1992
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1989/eureka,http://localhost:1988/eureka
instance:
preferIpAddress: true
Copy the code
Start service consumers
By accessing the HTTP interface to see if the service is available, the logs show that each invocation may be loaded to a different service provider instance.
4. Stop a service provider.
Access to the HTTP interface does not affect the service and is automatically loaded onto the normal service provider instance.
Stop a registry instance.
Access to the HTTP interface remains unaffected.
Of course, there is the source code, click me to view the source code, and give me a star
Want a praise is really too difficult, read to a praise, WISH you change strong, ✌️