Article / 文章中心

SpringCloud注册中心Eureka

发布时间:2021-11-23 点击数:314

CAP理论#
Zookeeper保证CP-
ZooKeeper的leader节点挂掉之后会重新进行leader选举,选举期间整个ZooKeeper集群都是不可用的;先选举leader保证强一致性之后,不能保证可用性。
Eureka保证AP-
Eureka优先保证可用性,Eureka各个节点是平等的,某几个节点挂掉不会影响正常节点的工作,剩余的节点依然可以提供注册和查询服务。而Eureka的客户端在向某个Eureka注册或时如果发现连接失败,则会自动切换至其它节点,只要有一台Eureka还在,就能保证注册服务可用(保证可用性),只不过查到的信息可能不是最新的(不保证强一致性)。
注册中心搭建#
Eureka是一个服务治理组件,它主要包括服务注册和服务发现,主要用来搭建服务注册中心。
Eureka是一个基于REST的服务,用来定位服务,进行中间层服务器的负载均衡和故障转移;
(1)创建一个SpringBoot项目
(2)pom引入注册中心依赖包

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>2.2.0.RELEASE</version> </dependency>

(3)启动类加上注解@EnableEurekaServer

@EnableEurekaServer public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class)
            .bannerMode(Banner.Mode.LOG)
            .build()
            .run(args);
    }
}

(4)配置文件application.properties

server.port=8094 #当前IP eureka.instance.hostname=ip1 #清理服务频次 eureka.instance.lease-renewal-interval-in-seconds=2 eureka.instance.lease-expiration-duration-in-seconds=10 #注册策略 eureka.server.enable-self-preservation=false eureka.client.register-with-eureka=false eureka.client.fetch-registry=false #注册地址:集群配置不配当前ip eureka.client.service-url.defaultZone=http://ip2:${server.port}/eureka/