作者:爷 叼烟闯天下
来源:CSDN 爷 叼烟闯天下
作为服务注册中心Eureka比Zookeeper好在哪里?(面试)
1、Eureka 遵守AP原则,Zookeeper 遵守CP原则。根据CAP理论,一个分布式系统不可能同时满足一致性、可用性和分区容错性,由于分区容错性是分布式 ,系统中必须保证的,因此我们只能在一致性和可用性之间权衡。
2、Zookeeper采用CP,节点采用主从。一旦主机down机,会在多个从中进行 决策选举一个从作为主,但是选举的时间30~120s,时间太长,在选举期间会导致集群不可用,这样就会导致整个注册中心瘫痪。
3、Eureka采用AP,所有节点平等,没有主从。如果有一个节点挂掉,会自动切换到一个可用的节点,只要 有一个Eureka节点正常运行,就能保证注册中心可用。只不过查到的信息可能不是最新的(不保证强一致性)。 除此之外Eureka还有自我保护机制。因此Eureka可以很好的应对因网络故障导致部分节点失去联系的情况, 而不会像Zookeeper那样使整个注册服务瘫痪。
CAP原则
关系型数据库事务:ACID
NoSQL:CAP
1.传统的ACID
A(Atomicity)原子性
一个事务的所有系列操作步骤被看成是一个动作,所有的步骤要么全部完成要么一个也不会完成,如果事务过程中任何一点失败,将要被改变的数据库记录就不会被真正被改变。
C(Consistency)一致性
数据库的约束、级联和触发机制Trigger都必须满足事务的一致性。也就是说,通过各种途径包括外键约束等任何写入数据库的数据都是有效的,不能发生表与表之间存在外键约束,但是有数据却违背这种约束性。所有改变数据库数据的动作事务必须完成,没有事务会创建一个无效数据状态,这是不同于CAP理论的一致性"consistency"。
I(Isolation)隔离性
主要用于实现并发控制, 隔离能够确保并发执行的事务能够顺序一个接一个执行,通过隔离,一个未完成事务不会影响另外一个未完成事务。
D(Durability)持久性
一旦一个事务被提交,它应该持久保存,不会因为和其他操作冲突而取消这个事务。很多人认为这意味着事务是持久在磁盘上,但是规范没有特别定义这点。
2.CAP
C(Consistency)强一致性
同样数据在分布式系统中所有地方都是被复制成相同。
A(Availability)可用性
所有在分布式系统活跃的节点都能够处理操作且能响应查询。
P(Partition tolerance)分区容错性
在两个复制系统之间,如果发生了计划之外的网络连接问题,对于这种情况,有一套容错性设计来保证。
CAP是分布式系统中进行平衡的理论,它是由 Eric Brewer发布在2000年。
CAP原则是NoSQL数据库的基石。
一般情况下CAP理论认为你不能同时拥有上述三种,只能同时选择两种,这是一个实践总结,当有网络分区情况下,也就是分布式系统中,你不能又要有完美一致性和100%的可用性,只能这在两者选择一个。在单机系统中,你则需要在一致性和延迟性latency之间权衡。
CAP理论的核心是:一个分布式系统不可能同时很好的满足一致性、可用性和分区容错性这三个需求。因此根据CAP原理,将NoSQL数据库分成了满足CA原则、满足CP原则、满足AP原则三类。
CA原则:单点集群,满足一致性、可用性的系统,通常在可扩展性上不太强大。
CP原则:满足一致性、分区容错性的系统,通常性能不是特别高。
AP原则:满足可用性、分区容错性的系统,通常可能对一致性要求低些。
业务流程:
- 服务提供方
- 服务消费方
- 服务注册中心(eureka)
集群
- 注册中心
- 服务提供方
能够正常显示
http://192.168.151.102:8080/actuator/info
info:
问题:
1台注册中心服务器扛不住怎么办?
注册中心服务器挂掉了怎么办?
单点故障
考虑集群
3台进行集群:
microservice-eureka-7001
microservice-eureka-7002
microservice-eureka-7003
域名的模拟:
eureka7001.com
eureka7002.com
eureka7003.com
修改hosts文件:
添加如下内容:
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
http://eureka7001.com:7001
服务提供方分配式部署
microservice-provider-emp-8080
microservice-provider-emp-8081
microservice-provider-emp-8082
负载均衡
Ribbon、Feign (消费方)
了解这些,现在可以开始我们的工程搭建了。
父工程里面是空的,直接创建即可。
域名的模拟:
eureka7001.com
eureka7002.com
eureka7003.com
需要修改hosts文件:
WIR+R打开:
找到etc下的host
向里面添加
127.0.0.1 eureka7001.com127.0.0.1 eureka7002.com127.0.0.1 eureka7003.com
如果权限限制,复制一份host到外面目录,改好后再覆盖回去,之后可以在 http://eureka 7001.com:7001 等地址进行访问。
microservice-eureka-7001
MicroserviceEureka7001Applicationpackage com.newer.demo.microservice.eureka; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication//标记注册中心服务端@EnableEurekaServerpublic class MicroserviceEureka7001Application { public static void main(String[] args) { SpringApplication.run(MicroserviceEureka7001Application.class, args); } }
pom.xml(依赖)
application.yml(配置信息)
server: port: 7001 eureka: client: # 自己就是注册中心,不需要检索服务,默认为true fetch-registry: false # 不把自己注册进去,默认为true register-with-eureka: false #集群环境 service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: # 注册中心的IP hostname: localhost
microservice-erueka-7002
MicroserviceErueka7002Application
package com.newer.demo.microservice.erueka; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication//标记注册中心服务端@EnableEurekaServerpublic class MicroserviceErueka7002Application { public static void main(String[] args) { SpringApplication.run(MicroserviceErueka7002Application.class, args); } }
pom.xml
<?xml version="1.0" encoding="UTF-8"?>4.0.0org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASEcom.newer.demo microservice-eureka-7001 0.0.1-SNAPSHOTmicroservice-eureka-7001Demo project for Spring Boot1.8Hoxton.SR4org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-devtools runtimetrueorg.springframework.boot spring-boot-starter-test testorg.junit.vintage junit-vintage-engine org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version}pomimportorg.springframework.boot spring-boot-maven-plugin
application.yml
server: port: 7002 eureka: client: # 自己就是注册中心,不需要检索服务,默认为true fetch-registry: false # 不把自己注册进去,默认为true register-with-eureka: false #集群环境 service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: # 注册中心的IP hostname: localhost
microservice-eureka-7003
pom.xml
<?xml version="1.0" encoding="UTF-8"?>4.0.0org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASEcom.newer.demo microservice-eureka-7001 0.0.1-SNAPSHOTmicroservice-eureka-7001Demo project for Spring Boot1.8Hoxton.SR4org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-devtools runtimetrueorg.springframework.boot spring-boot-starter-test testorg.junit.vintage junit-vintage-engine org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version}pomimportorg.springframework.boot spring-boot-maven-plugin
MicroserviceEureka7003Application
package com.newer.demo.microservice.eureka; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication//标记注册中心服务端@EnableEurekaServerpublic class MicroserviceEureka7003Application { public static void main(String[] args) { SpringApplication.run(MicroserviceEureka7003Application.class, args); } }
application.yml
server: port: 7003 eureka: client: # 自己就是注册中心,不需要检索服务,默认为true fetch-registry: false # 不把自己注册进去,默认为true register-with-eureka: false #集群环境 service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: # 注册中心的IP hostname: localhost
以上是3个注册中心的集群
明天老苏将把后部分内容分享出来,或者有兴趣的可以去原文作者那里观摩!
作者:爷 叼烟闯天下
来源:CSDN 爷 叼烟闯天下