https://spring.io/projects/spring-cloud-netflix
https://github.com/Netflix/eureka
https://github.com/spring-cloud-samples/eureka
1.实际上Netflix开源了好多组件,Eureka只是其中的一个,大家可以查看GitHub上面的相关开源项目,
1.在微服务架构系统之中,我们经常提三个角色:
1.1.注册中心 (Register):服务发布者将服务发布到注册中心,服务消费者从注册中心获取可以进行访问的服务列表;
1.2.服务提供者(Provider):需要发布服务的应用
1.3.服务消费者(Consumer):需要访问服务的应用
1.首先,我们可以看到上面注册中心使用了集群部署:us-east-1c,us-east-1d,us-east-1e(美国东部1c,1d,1e注册中心)
2.集群中的机器,数据会进行同步复制更新(replicate),保证注册中心数据最终一致性;
1.服务发布者,即下游相关的平台或者是提供服务的应用,对于自己的应用服务(Application Service),做下面的操作
1.1.服务注册:将自己的服务接口方法以及服务发布的地址等,注册到Eureka Server注册中心中;
1.2.服务更新:对已经发布到注册中心的服务通知进行更新操作;
1.3.服务删除:通过相关操作(如停止应用操作)通知注册中心,将应用服务从注册中心删除(移除);
2.服务发布者通过Eureka这个注册中心,每30秒发送一次心跳更新注册中心的数据;
如果服务消费者一段时间之内不能更新这个服务发布者的服务信息,那么90s之内,这个服务将会被注册中心移除;
1.服务消费者(Application Client),在应用启动的时候,根据自己订阅的服务,会去注册中心(Eureka Server)拉取所有的服务清单列表
会缓存到应用本地缓存,
2.
这里的版本引入主要是下面这个
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
当然pom里还有一些其他的内容,我们不做一一讲解
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gaoxinfu.demo.spring.cloud</groupId>
<artifactId>demo-spring-cloud-netflix-eureka-server</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo-spring-cloud-netflix-eureka-server</name>
<description>Eureka Server demo project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>eurekademo.EurekaApplication</start-class>
<java.version>1.8</java.version>
<docker.image.prefix>springcloud</docker.image.prefix>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.2.3</version>
<configuration>
<baseImage>openjdk:8-jre-alpine</baseImage>
<imageName>${
docker.image.prefix}/${
project.artifactId}</imageName>
<exposes>8761</exposes>
<entryPoint>["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${
project.build.directory}</directory>
<include>${
project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- defined in spring-cloud-starter-parent pom (as documentation hint),
but needs to be repeated here -->
<configuration>
<requiresUnpack>
<dependency>
<groupId>com.netflix.eureka</groupId>
<artifactId>eureka-core</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.eureka</groupId>
<artifactId>eureka-client</artifactId>
</dependency>
</requiresUnpack>
</configuration>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<configuration>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
</configuration>
</plugin>
<plugin>
<!--skip deploy (this is just a test module) -->
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
server:
port: 8761
eureka:
client:
registerWithEureka: true
fetchRegistry: true
server:
waitTimeInMsWhenSyncEmpty: 0
server.port:定义Eureka Server 端口
eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。由于当前这个应用就是Eureka Server,故而设为false。
eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false。
spring:
application:
name: demo-spring-cloud-netflix-eureka-server
cloud:
config:
uri: ${
CONFIG_SERVER_URL:http://localhost:8888}
package com.gaoxinfu.demo.spring.cloud.netflix.eureka.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-09-11 11:18
*/
@EnableAutoConfiguration
@EnableEurekaServer
public class DemoSpringCloudNetflixEurekaServerApp {
public static void main(String[] args) {
SpringApplication.run(DemoSpringCloudNetflixEurekaServerApp.class,args);
}
}
https://gitee.com/gaoxinfu_admin/demo-spring-cloud/tree/master/demo-spring-cloud-netflix-eureka-server