Eurekamysql
Netflix在设计Eureka时遵照的就是AP原则 CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),三者不可兼得
Eureka是Netflix的一个子模块,是一个基于Rest的服务,用于服务定位,已实现云端中间层服务发现和故障转移。 服务注册与发现对于微服务架构来讲是很是重要的,有了服务发现与注册,只须要使用服务的标识符,就能够访问到服务,而不须要修改服务调用的配置文件了。 功能相似于dubbo的注册中心,好比zookeeper
Eureka基本架构redis
Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现(请对比Zookeeper)。 Eureka 采用了 C-S 的设计架构。Eureka Server 做为服务注册功能的服务器,它是服务注册中心。 而系统中的其余微服务,使用 Eureka 的客户端链接到 Eureka Server并维持心跳链接。这样系统的维护人员就能够经过 Eureka Server 来监控系统中各个微服务是否正常运行。SpringCloud 的一些其余模块(好比Zuul)就能够经过 Eureka Server 来发现系统中的其余微服务,并执行相关的逻辑。 Eureka包含两个组件:Eureka Server和Eureka Client Eureka Server提供服务注册服务 各个节点启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储全部可用服务节点的信息,服务节点的信息能够在界面中直观的看到 EurekaClient是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具有一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。若是Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)
三大结构算法
一、Eureka server提供服务注册与发现 二、service provide将自身服务注册到Eureka,从而使服务消费方可以找到 三、service consumer从Eureka获取注册服务列表,从而可以消费服务
项目结构spring
总父工程 通用模块API 服务提供者Provider 服务消费者Consumer eureka服务注册中心module
POMsql
主要包含一个spring-cloud-starter-eureka-servermongodb
<?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"> <parent> <artifactId>cloud</artifactId> <groupId>com.lee</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-eureka-7001</artifactId> <dependencies> <!--eureka-server服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- 修改后当即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
注意:apache
基本上咱们要引入cloud的一个新的技术组件,会有两步 一、新增一个相关的maven坐标 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> 二、在主启动类上标注启动该新组件的相关注解标签 @EnableEurekaServer @EnableZuulProxy @EnableXXXXX ...
APPLICATION.YMLwindows
主要暴露eureka端口和对外服务地址浏览器
server: port: 7001 eureka: instance: hostname: localhost #eureka服务端的实例名称 client: register-with-eureka: false #false表示不向注册中心注册本身。 fetch-registry: false #false表示本身端就是注册中心,个人职责就是维护服务实例,并不须要去检索服务 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都须要依赖这个地址。
主启动类安全
package com.lee.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; //EurekaServer服务器端启动类,接受其它微服务注册进来 @EnableEurekaServer @SpringBootApplication public class EurekaServer7001_App { public static void main(String[] args) { SpringApplication.run(EurekaServer7001_App.class,args); } }
测试
http://localhost:7001
修改cloud-provider-dept-8001
POM - 新增 eureka客户端 config配置中心
<!-- 将微服务provider侧注册进eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--注意:--> <!-- 若是eureka下载不下来的话 改为这个 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.6.RELEASE</version> </dependency> -->
YML - 新增 eureka服务地址
eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7001/eureka
主启动类 - 新增 将本服务启动后自动注册进eureka服务中
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
测试:
一、启动 cloud-eureka-7001 二、启动 cloud-provider-dept-8001 三、http://localhost:7001 注意:暴露在eureka中的微服务名称,即provider的yml中spring:application:name的名称 若是找不到服务的话,大几率是应为spring-cloud-starter-eureka和 <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> 版本不一致致使的
Eureka服务中的显示
Application | AMIs | Availability Zones | Status |
---|---|---|---|
CLOUD-DEPT | n/a (1) | (1) | UP (1) - windows10.microdone.cn:cloud-dept:8001 |
目标:修改 windows10.microdone.cn:cloud-dept:8001的显示
cloud-provider-dept-8001修改:
YML
eureka: instance: instance-id: cloud-dept8001
结果:
Application | AMIs | Availability Zones | Status |
---|---|---|---|
CLOUD-DEPT | n/a (1) | (1) | UP (1) - cloud-dept8001 |
鼠标移动到上侧超连接时,浏览器左下角的连接显示
cloud-provider-dept-8001修改:
YML
eureka: instance: prefer-ip-address: true #访问路径能够显示IP地址
cloud-dept8001超了解点击后内容的构建
cloud-provider-dept-8001修改:
POM
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
YML
info: #内容以info开头下边随便写 app.name: cloud author.name: lee app.function: 部门服务提供者 build.artifactId: $project.artifactId$ build.version: $project.version$
CLOUD父工程POM
<build><!--构建--> <finalName>microservicecloud</finalName> <resources> <!--能够访问src/main/resources下的文件--> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <delimiters> <delimit>$</delimit> </delimiters> </configuration> </plugin> </plugins> </build>
结果:
访问:http://192.168.101.39:8001/info 显示: {"app": { "name":"cloud", "function":"部门服务提供者" }, "author":{"name":"lee"}, "build":{ "artifactId":"$project.artifactId$", "version":"$project.version$" } }
(1)、仿照cloud-eureka-7001建立cloud-eureka-7002和cloud-eureka-7003
(2)、修改 映射
c:\Windows\System32\drivers\etc下的host文件 添加以下: 127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com
(3)、三台eureka服务的yml配置
eureka: instance: hostname: eureka7001.com ##eureka服务端的实例名称,知识localhost换了个名而已 eureka: client: defaultZone: http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka ##也能够是http://localhost:7002/eureka,http://localhost:7003/eureka
(4)、cloud-provider-dept-8001 YML服务地址修改
eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
测试:
http://eureka7001.com:7001 http://eureka7002.com:7002 http://eureka7003.com:7003
某时刻某一个微服务不可用了,或者长时间没有被调用,eureka不会马上清理,依旧会对该微服务的信息进行保存。(网络拥堵或者调用超时) 什么是自我保护模式? 默认状况下,若是EurekaServer在必定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。可是当网络分区故障发生时,微服务与EurekaServer之间没法正常通讯,以上行为可能变得很是危险了——由于微服务自己实际上是健康的,此时本不该该注销这个微服务。Eureka经过“自我保护模式”来解决这个问题——当EurekaServer节点在短期内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进入该模式,EurekaServer就会保护服务注册表中的信息,再也不删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式。 在自我保护模式中,Eureka Server会保护服务注册表中的信息,再也不注销任何服务实例。当它收到的心跳数从新恢复到阈值以上时,该Eureka Server节点就会自动退出自我保护模式。它的设计哲学就是宁肯保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。一句话讲解:好死不如赖活着 综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁肯同时保留全部微服务(健康的微服务和不健康的微服务都会保留),也不盲目注销任何健康的微服务。使用自我保护模式,可让Eureka集群更加的健壮、稳定。
RDBS(mysql/oracle/sqlserver)---->ACID NOSQL(redis/mongodb)----->CAP --------------------------------------------------------- --------------------------------------------------------- ACID: atomicity原子性、 consistency一致性、 isolation独立性、 durability持久性 CAP: consistency强一致性、 availability高可用、 partition tolerance 分区容错性 --------------------------------------------------------- --------------------------------------------------------- Eureka遵照AP Zookeeper遵照CP 做为服务注册中心,Eureka比Zookeeper好在哪里 著名的CAP理论指出,一个分布式系统不可能同时知足C(一致性)、A(可用性)和P(分区容错性)。因为分区容错性P在是分布式系统中必需要保证的,所以咱们只能在A和C之间进行权衡。 所以 Zookeeper保证的是CP, Eureka则是AP。 1. Zookeeper保证CP 当向注册中心查询服务列表时,咱们能够容忍注册中心返回的是几分钟之前的注册信息,但不能接受服务直接down掉不可用。也就是说,服务注册功能对可用性的要求要高于一致性。可是zk会出现这样一种状况,当master节点由于网络故障与其余节点失去联系时,剩余节点会从新进行leader选举。问题在于,选举leader的时间太长,30 ~ 120s, 且选举期间整个zk集群都是不可用的,这就致使在选举期间注册服务瘫痪。在云部署的环境下,因网络问题使得zk集群失去master节点是较大几率会发生的事,虽然服务可以最终恢复,可是漫长的选举时间致使的注册长期不可用是不能容忍的。 2. Eureka保证AP Eureka看明白了这一点,所以在设计时就优先保证可用性。Eureka各个节点都是平等的,几个节点挂掉不会影响正常节点的工做,剩余的节点依然能够提供注册和查询服务。而Eureka的客户端在向某个Eureka注册或时若是发现链接失败,则会自动切换至其它节点,只要有一台Eureka还在,就能保证注册服务可用(保证可用性),只不过查到的信息可能不是最新的(不保证强一致性)。除此以外,Eureka还有一种自我保护机制,若是在15分钟内超过85%的节点都没有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障,此时会出现如下几种状况: 2.一、Eureka再也不从注册列表中移除由于长时间没收到心跳而应该过时的服务 2.二、Eureka仍然可以接受新服务的注册和查询请求,可是不会被同步到其它节点上(即保证当前节点依然可用) 2.三、当网络稳定时,当前实例新的注册信息会被同步到其它节点中 所以, Eureka能够很好的应对因网络故障致使部分节点失去联系的状况,而不会像zookeeper那样使整个注册服务瘫痪。
拓展知识
#服务发现 @AutoWire private DiscoveryClient client; List<String> serviceList = client.getServices();//列出Eureka中全部的服务 List<ServiceInstance> instanceList = client.getInstances("CLOUD-DEPT");//查找服务实例 instance.getServiceId();//实例ID instance.getHost();//实例主机 instance.getPort();//实例端口 instance.getUri();//实例Uri @EnableDiscoveryClient //主启动类上+服务发现注解