Introduction to Eureka
Service registration and service discovery: Eureka adopts the design architecture of CS. Eureka Sever is the server of service registration function, which is the service registration center. Other microservices in the system use Eureka's client to connect to Eureka Seve and maintain a 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.
In service registration and discovery, there is a registry. When the server starts, it will register its current server information, such as service address and communication address, in the registration center by alias. The other party (consumer service provider) obtains the actual service communication address from the registry in the way of this alias, and then realizes the local RPC call. The core design idea of RPC remote call framework is to use the registry to manage a dependency between each service (service governance concept). In any RPC remote framework, there will be a registry (storing information about the service address (interface address)).
Eureka consists of two components: Eureka Server and Eureka Client
Eureka Server provides service registration service
After each micro service node is started through configuration, it will be registered in EurekaServer, so that the service registry in EurekaServer will store the information of all available service nodes, and the information of service nodes can be seen visually in the interface.
EurekaClient accesses through the registry
It is an ava client, which is used to simplify the interaction of Eureka Server. The client also has a built-in load balancer using round robin load algorithm. After the application starts, a heartbeat will be sent to Eureka Server (the default cycle is 30 seconds). If Eureka Server does not receive the heartbeat of a node in multiple heartbeat cycles, Eureka Server will remove the service node from the service registry (90 seconds by default)
Spring Cloud introduces Eureka
1. Create cloud Eureka server7001 subproject
Five steps: introduce in detail in the construction of Spring Cloud project,
Dependency needs to be introduced (indicates eureka server):
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
server: port: 7001 eureka: instance: hostname: eureka7001.com #Instance name of eureka server client: register-with-eureka: false #false means you don't register yourself fetch-registry: false #false indicates that you are a registry to maintain service instances service-url: defaultZone: http://eureka7002.com:7002/eureka/
For main startup, add the annotation @EnableEurekaServer
The cloud-consumer-order80 of Eureka client will register into Eureka server and become a consumer of services.
1. Change POM and introduce the dependency Eureka client:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2. Change yml
server: port: 8001 eureka: client: register-with-eureka: true fetch-registry: true service-url: # Cluster version defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
3. Main startup, add @EnableEurekaClient dependency:
The cloud provider payment8001 of Eureka client will register into Eureka server and become a service provider.
It is consistent with the above steps.
Refer to cloud Eureka server7001 and create a new cloud Eureka server7002
Modify the hostss file:
Fill in the hosts file, which can be opened with VSC, modified and saved.
Modify the yml of 7001 to correspond to the address of 7002:
server: port: 7001 spring: application: name: cloud-eureka-service eureka: instance: # Instance name of eureka server hostname: eureka7001.com client: # false means that you do not register yourself with the registry register-with-eureka: false # false means that my own end is the registration center. My responsibility is to maintain service instances without retrieving services fetch-registry: false service-url: # Setting the address query service and registration service that interact with Eureka Server need to rely on this address defaultZone: http://eureka7002.com:7002/eureka/
Modify the yml of 7002 to correspond to the address of 7001:
server: port: 7002 spring: application: name: cloud-eureka-service2 eureka: instance: hostname: eureka7002.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka7001.com:7001/eureka/
Publish the payment service 8001 micro service to the above two Eureka cluster configurations, yml
eureka: client: register-with-eureka: true fetch-registry: true service-url: # Cluster version defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
Publish the order service 80 microservice to the above two Eureka cluster configurations.
eureka: client: register-with-eureka: true #Whether to put your own registration money eureka fetch-registry: true #Whether to retrieve the registered information from eureka service-url: # Cluster version defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
Payment service provider 8001 cluster environment construction:
Previously, we built a cluster of registration centers. Similarly, we built a cluster of payment services.
Create a new 8002 payment service.
Five steps, create according to 8001.
server: port: 8002 spring: application: name: cloud-payment-service8002 datasource: # Current data source operation type type: com.alibaba.druid.pool.DruidDataSource # mysql driver class driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8 username: root password: root eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://eureka7001.com/eureka,http://eureka7002.com/eureka
Add the @LoadBalanced annotation in the config of the order service,