前言:
前面一篇文章我们搭建了注册中心,并将服务注册到了注册中心,在实际的项目中,eureka服务中心通常有多个,也就是通常所说的服务集群,这样在某个服务中心挂了的时候,依然能保证服务的可用,下面将记录基本的eureka集群的搭建。
一、修改host 文件(这一步可有可无,只是为了项目地址更容易区分)
在C:\Windows\System32\drivers\etc文件夹下,找到host 文件,修改本机IP127.0.0.1的映射地址为:eureka01.com, eureka02.com, eureka03.com;
二、配置yml文件
1、设置端口号;
#设置端口号:7001
server:
port: 7001
#设置eureka服务主机名
eureka:
instance:
hostname: eureka01.com
#将服务注册到其他两个服务
client:
fetch-registry: false #开启服务检索
register-with-eureka: false #将自己注册到注册中心
service-url:
defaultZone: http://eureka02.com:7002/eureka/,http://eureka03.com:7003/eureka/
#eureka服务端地址
2、编写启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer_App01 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_App01.class, args);
}
}
3、引入jar包
<!-- eureka,服务---->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
三、开启服务
可以里看到,在服务器下面eureka03下面注册了两个eureka01和eureka02服务。
总结;
Eureka和zookeeper的区别:
1)、Eureka保证的是AP,即高可用和分区容错性,Eureka集群没有类似于zookeeper的leader选举机制,所以,在某个节点宕机的情况下,不会出现服务中断,如果zookeeper的leader发生宕机,会有5~30min的选举,因此,Zookeeper保证的是CP,强一致性和分区容错性。