2、 Registry
1. Introduction to Eureka
Eureka is the core service of microservice system
Role: registration and discovery
To start all services, you need to register your address in the registry; to call other services, you need to get the registry from the registry to find other services.
Service ID
Host address
item-service—localhost:8001
user-service—localhost:8101
order-service—localhost:8201
2. The working mechanism of Eureka:
-
register
- When the service provider starts, it will repeatedly try to register with Eureka until the registration is successful
-
Pull
- Consumers pull the registry every 30 seconds to refresh it
-
heartbeat
- The service provider sends heartbeat data to Eureka every 30 seconds
- If the Eureka server fails to receive the heartbeat of a service three times in a row, it will delete the registration information of the service
-
Self protection model
- If the network is unstable, 85% of servers have abnormal heartbeat within 15 minutes, they will automatically enter the self-protection mode
- All registration information will not be deleted
- After the network is restored, it can automatically exit the protection mode and return to normal
- During development and debugging, protection mode can be disabled to avoid affecting the test
3. Build Eureka server
3.1 create eurekaserver project
3.2 editing pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tedu</groupId>
<artifactId>sp05-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp05-eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.3 modification application.yml
First of all application.propertie Change to application.yml
spring:
application:
name: eureka-server
server:
port: 2001
eureka:
server:
enable-self-preservation: false
instance:
hostname: eureka1
client:
register-with-eureka: false
fetch-registry: false
- Between Eureka cluster servers, through
hostname
To distinguish eureka.server.enable-self-preservation
Eureka’s self-protection status: whether the rate of heartbeat failure exceeds 85% within 15 minutes. If it exceeds 85%, Eureka server will protect the current instance registration information and prompt a warning. Once it enters the protection mode, Eureka server will try to protect the information in its service registry and will not delete the data in the service registry. That is, no micro services will be logged offeureka.client.register-with-eureka=false
Do not register with yourselfeureka.client.fetch-registry=false
Do not pull registration information from itselfeureka.instance.lease-expiration-duration-in-seconds
After the last heartbeat, how long does it take to determine that the microservice is not available? The default value is 90
3.4 modify main program
- add to
@EnableEurekaServer
package cn.tedu.sp05;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class Sp05EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(Sp05EurekaApplication.class, args);
}
}
3.4 modify hosts file and add Eureka domain name mapping
C:WindowsSystem32driversetchosts
Add content:
127.0.0.1 eureka1
127.0.0.1 eureka2`
3.5 start and access the test
4. Service provider
- Modify the item service, user service and order service to register the microservice with Eureka server
four point one pom.xml Add Eureka dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
four point two application.yml Add Eureka registration configuration
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka
eureka.instance.lease-renewal-interval-in-seconds
Heartbeat interval, default 30 seconds- The default zone, the default location, can be modified to a specific geographic location, such as Beijing, Shanghai, Shenzhen, etc., which indicates the deployment location of Eureka server and needs to be provided by the cloud server
eureka.client.registry-fetch-interval-seconds
The time interval for pulling registration information is 30 seconds by default
4.3 start the service and view the registration information in Eureka
5. High availability of Eureka and “service provider”
5.1 high availability of item service
startup parameter--server.port
You can override the port configuration in YML
5.1.2 configure startup parameters
- item-service-8001
- item-service-8002
5.1.3 start up test
- Visit Eureka to view item service registration information
- Access two port test
http://localhost:8001/35
http://localhost:8002/35
5.2 Eureka high availability
5.2.1. Add profile configuration files of two servers
application-eureka1.yml
`eureka:
instance:
hostname: eureka1
client:
Register with Eureka: the configuration of true # profile will override the public configuration
Fetch registry: the configuration of true # profile will override the public configuration
service-url:
defaultZone: http://eureka2 : 2002 / Eureka # register with eureka2 when eureka1 starts`
application-eureka2.yml
eureka:
instance:
hostname: eureka2
client:
Register with Eureka: the configuration of true # profile will override the public configuration
Fetch registry: the configuration of true # profile will override the public configuration
service-url:
defaultZone: http://eureka1 : 2001 / Eureka # register with eureka1 when eureka2 starts
5.2.2 configure startup parameters
--spring.profiles.active
and--server.port
- Eureka1 startup parameters:
--spring.profiles.active=eureka1 --server.port=2001
- Eureka2 startup parameters:
--spring.profiles.active=eureka2 --server.port=2002
5.2.3 visit Eureka server to view the registration information
5.2.4 when registering Eureka client, register with two servers
Modify the following micro services
- sp02-itemservice
- sp03-userservice
- sp04-orderservice
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
When one Eureka service goes down, it can still connect to another Eureka service