Spring Cloud Eureka Server
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.zws.cloud</groupId> <artifactId>Eurka-Server1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Eurka-Server1</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <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> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
2. 配置application.yml
server: port: 8761eureka: instance: hostname: localhost client: # 下面两行表明自己是一个EurekaServer服务 registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/spring: application: name: Eureka-Server
3. 启动类
package com.zws.cloud.server;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}
4. 访问:
http://localhost:8761/
Eureka Server集群配置
集群配置只在配置文件上有区别
1.Server1
server: port: 8761eureka: instance: hostname: peer1 client: serviceUrl: defaultZone: http://peer2:8762/eureka/,http://peer3:8763/eureka/spring: application: name: Eureka-Server
2.Server2
server: port: 8762eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: http://peer1:8761/eureka/,http://peer3:8763/eureka/spring: application: name: Eureka-Server
3.Server3
server: port: 8763eureka: instance: hostname: peer3 client: serviceUrl: defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/spring: application: name: Eureka-Server
4./etc/hosts配置
127.0.0.1 peer1127.0.0.1 peer2127.0.0.1 peer3
Eureka工作原理
1. 服务注册
当Eureka Client向Eureka Server注册时,Eureka Client提供自身的元数据,比如 IP地址、 端口、运行状况指标的Url、主页地址等信息。
2. 服务续约
Eureka Client在默认的情况下会每隔30秒发送一次心跳来进行服务续约。通过服务续约来告知Eureka Serve :该 Eureka Client 仍然可用,没有出现故障。正常情况下,如果Eureka Server在90秒内没有收到Eureka Client的心跳,Eureka Server会将Eureka Client 实例从注册列表中删除。注意:官网建议不要更改服务续约的间隔时间。
3. 获取服务注册列表信息
Eureka Client从Eureka Server获取服务注册表信息,并将其缓存在本地。Eureka Client 会使用服务注册列表信息查找其他服务的信息,从而进行远程调用。该注册列表信息定时(每30秒)更新一次,每次返回注册列表信息可能与Eureka Client 的缓存信息不同,Eureka Client会自己处理这些信息。如果由于某种原因导致注册列表信.息不能及时匹配,Eureka Client 会重新获取整个注册表信息。 Eureka server 缓存了所有的服务注册列表信息,并将整个注册列表以及每个应用程序的信息进行了压缩,压缩内容和没有压缩的内容完全相同。Eureka Client和Eureka server可以使用Json和XML数据格式进行通信。在默认的情况下,Eureka client使用Json格式的方式来获取服务注册列表的信息。
4. 服务下线
Eureka client在程序关闭时可以向Eureka Server发送下线请求。发送请求后,实例信息将从 Eureka server的服务注册列表中删除。该下线请求不会自动完成,关闭时调用以下代码:该客户端的需要在程序 DiscoveryManager.getInstance().shutdowncomponent();
5. 服务剔除
在默认情况下,当 Eureka client 连续 90 秒没有向 Eureka server 发送服务续约(即心跳),EurekaServer会将该服务实例从服务注册列表删除,即服务剔除。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。