This is the 14th day of my participation in Gwen Challenge
Take a look at the use of Eureka today as SpringCloud integrates it into its spring-Cloud-Netflix subproject to implement SpringCloud’s service discovery capabilities. Eureka consists of two components: Eureka Server and Eureka Client. In order to facilitate the test, three modules are established under studyCloud project, namely netflix-Eureka-consumer, Netflix-Eureka-Provider and Netflix-Eureka-Server
Establish the Netflix-Eureka-Server module
Example Modify POM data
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>study</artifactId>
<groupId>brief.talk.spring.cloud</groupId>
<version>1.0.0 - the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>netflix-eureka-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
Copy the code
Add main program
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {
public static void main (String[] args){ SpringApplication.run(EurekaServer.class, args); }}Copy the code
The configuration file application.yml is added
spring:
application:
name: eureka-server
server:
port: 8080
eureka:
client:
# indicates that you will not register yourself in EurekaServer. Default is true.
register-with-eureka: false
# does not fetch the existing registration information from EurekaServer. The default value is true.
fetch-registry: false
Copy the code
Then start EurekaServer. If this screen is displayed, the EurekaServer is successfully started
The two modules of Netflix-Eureka-consumer and Netflix-Eureka-provider have the same operation steps as Netflix-Eureka-server, but the difference is that the POM file introduces the dependency of Eureka-client. However, Netflix-Eureka-Server introduces the dependency of Eureka-Server
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>study</artifactId>
<groupId>brief.talk.spring.cloud</groupId>
<version>1.0.0 - the SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>netflix-eureka-provider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
Copy the code
Also, the YML configuration files are different
netflix-eureka-provider
spring:
application:
name: eureka-provider
server:
port: 8100
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8080/eureka
Copy the code
netflix-eureka-consumer
spring:
application:
name: eureka-consumer
server:
port: 8101
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8080/eureka
Copy the code
Pay attention to the point
In eureka Client, it is recommended to use the @enableEurekaclient annotation @enableEurekaclient of @EnableDiscoveryClient only when the registry is Eureka.
@enableDiscoveryClient: applies to a wide range of applications and supports many functions.
Eureka Server: registry Server
The registry server provides three main external functions:
The service registry
When a service provider starts, it registers information with the Eureka Server through the Eureka Client. The Eureka Server stores the information about the service. The Eureka Server has an internal layer caching mechanism to maintain the entire registry
Provide a registry
If the Eureka Client does not cache the registry when invoking the service, the service consumer will get the latest registry from the Eureka Server
sync
The Eureka Client synchronizes the current Client status through registration, heartbeat mechanism, and Eureka Server.
Eureka Client: registry Client
Eureka Client is a Java Client designed to simplify the interaction with Eureka Server. The Eureka Client pulls, updates, and caches information from the Eureka Server. So when all Eureka Server nodes go down, service consumers can still use the information in the cache to find the service provider, but there will be inconsistencies when the service changes.
Register: registers the service
The service provider, which registers itself in the registry, is also a Eureka Client. When the Eureka Client registers with the Eureka Server, it provides its own metadata, such as IP address, port, health indicator URL, home page, etc.
Renew: Service renewal
The Eureka Client sends a heartbeat every 30 seconds to renew the contract. Renew the contract to inform the Eureka Server that the Eureka Client is running properly. By default, if the Eureka Server does not receive a renewal from the Eureka Client within 90 seconds, the Server deletes the instance from its registry. This time is configurable and is not recommended.
Today is the Dragon Boat Festival, eat and drink, play lele for a day, or write today’s task, ha ha, although the technical point is a little simple.