本篇文章主讲eureka的高可用结构,eureka为何比传统的注册中心性能更佳,eureka是如何进行服务的注册与发现;在本篇文章中不会教出初学者进行如何搭建聚合工程,而是给出具体的client , server代码。java
eureka 是 一个服务注册与发现的组件 ,其可以更好的支持负载均衡和服务中间层,一般有 Eureka server (提供REST服务), Euraka client(消费REST服务),它们之间经过 service 交流更加的简便。git
传统的注册中心工做于暴露的IP地址和主机名,在服务的注册与发现中进行加载负载均衡的过程会更加的繁琐;eureka在中间层提供了加载负载均衡机制,弥补了传统的注册中心的不足之处,使加载负载均衡更加简便,性能方面也显著提升;github
eureka的空间分布中每一个集群的一个节点(例如 us-east-lc)就是一个区域(Region),每一个region中有许多的的分区(Zone),分区中存放的是client;每一个eureka server中至少会有一个zone用于处理zone失败的状况;web
euraka的region会发现本身区域中的实例(instance)spring
当服务端注册到 eureka server 时,会每隔30秒发送一次心跳给server 用于从新租约;app
若是客户端不可以从新租约,那么90秒后会从服务列表中剔除;负载均衡
集群节点1的注册信息会被复制给节点2,以此类推,保证了eueka来自任何zone的客户端(30秒一次)均可以查找region的信息用于发现可用服务进行远程调用;spring-boot
这边使用的是比较新的cloud版本为Finchley.SR2,boot版本 2.0.6;性能
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- SpringCloud版本 -->
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
<start-web.version>2.0.4.RELEASE</start-web.version>
</properties>
<!-- cloud工程版本管理 -->
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${start-web.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencies>
<!-- Eureka服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
application.yml中定义了 application server 的 端口为10086 ,eureka的实例主机名为localhost;application server 相对于 eureka server 其也是 client ; 做为 server 是禁止向本身注册服务;service-url 表示其余的 client 能够经过此路径,将服务注册在server 上,或者从 server上发现有那些服务;字体
server:
port: 10086
eureka:
instance:
hostname: localhost
client:
# 做为server,表示禁止向本身注册
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
@EnableEurekaServer注解表示开启eureka server功能,做为 application server;
/**
* @Author lsc
* <p> eureka 服务端 </p>
*/
@SpringBootApplication
@EnableEurekaServer//表示开启eureka server功能
public class EurekaServerApp {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApp.class,args);
}
}
访问路径也就是是server-url: http://localhost:10086/ ; 以下图所示,因为只启动了server,没有其它服务向其注册,因此服务列表的实例为空;
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
client 的 端口地址为8090,而且将应用名称名为为 eureka-client,在服务列表便于区分是哪一个实例;服务的注册地址也就是server的地址;
server:
port: 8090
spring:
application:
name: eureka-client # 应用名称
eureka:
client:
service-url:
# 服务注册地址
defaultZone: http://localhost:10086/eureka/
@EnableEurekaClient注解表示开启 eureka client 做为 application client;
/**
* @Author lsc
* <p> </p>
*/
@SpringBootApplication
@EnableEurekaClient//表示开启 eureka client
public class EurekaClientApp {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApp.class,args);
}
}
从页面中能够看见咱们向server 注册了一个名为 EUREKA-CLIENT的服务,红色字体目前不用管它,是eureka的健康保护机制,前面提到的若是client 30秒以内未续租,90秒后会被剔除;