Eureka Server集群的搭建

x33g5p2x  于2021-09-24 转载在 其他  
字(2.0k)|赞(0)|评价(0)|浏览(253)

什么是Eureka

Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。
SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。

Eureka Server

Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。

Eureka Server之间通过复制的方式完成数据的同步,Eureka还提供了客户端缓存机制,即使所有的Eureka Server都挂掉,客户端依然可以利用缓存中的信息消费其他服务的API。综上,Eureka通过心跳检查、客户端缓存等机制,确保了系统的高可用性、灵活性和可伸缩性。

搭建步骤

本次预计搭建三个server服务,互相注册,作为一个注册中心集群。

准备工作

配置host,后续用本机host模拟不同的域名或服务器

# Windows 系统下host文件位置:C:\Windows\System32\drivers\etc
127.0.0.1 host80
127.0.0.1 host81
127.0.0.1 host82

创建工程

这里我创建一个父工程,其他的 Demo 都放在该工程下

父POM声明依赖

<dependency>
  <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-dependencies</artifactId>
   <version>${spring-cloud-dependencies.version}</version>
   <type>pom</type>
   <scope>runtime</scope>
</dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
   <version>${netflix-eureka-server.version}</version>
</dependency>

创建子模块

POM依赖,这里不需要版本号,因为在父POM中已经定义

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

启动类上增加注解

@EnableEurekaServer
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServer8080 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer8080.class, args);
    }
}

配置文件

spring.application.name=eureka-server-8080

server.port=8080
eureka.instance.hostname=host80
eureka.client.serviceUrl.defaultZone=http://host80:8080/eureka/,http://host81:8081/eureka/,http://host82:8082/eureka/

# 是否向自己注册自己
eureka.client.register-with-eureka=false
# 是否检索服务
eureka.client.fetch-registry=true

同理创建另外两个Server服务

启动

分别启动三个Server服务,然后分别访问:
http://host80:8080/
http://host80:8081/
http://host80:8082/

代码仓库:
https://gitee.com/pengld-demo/my-spring-cloud-demo

相关文章

微信公众号

最新文章

更多