Spring Cloud学习笔记:Eureka集群搭建样例

这是本人学习的总结,主要学习资料如下

  • 马士兵教育

1、项目架构

因为这是单机模拟集群搭建,为了方便管理就都放在了一个项目中。这次准备搭建三个项目server1, server2, server3

2、Dependency

项目依赖放根目录,免得代码重复

xml 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.30</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

3、项目启动类

分别在三个module中写启动类,类名改一下就好。

java 复制代码
@EnableEurekaServer
@SpringBootApplication(exclude = {GsonAutoConfiguration.class})
public class EurekaServer1Application {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer1Application.class);
    }
}

4、application.yml

配置如下,基本和单机配置一致,只不过要多加一个属性表示这三个服务组成一个集群。

yml 复制代码
spring:
  application:
    name: eureka-cluster
server:
  port: 1001

eureka:
  instance:
    hostname: eureka-1
    # 是否将自己ip注册到eureka中,默认false
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka-1:1001/eureka/,http://eureka-2:1002/eureka/,http://eureka-3:1003/eureka/
      

另外,我在自己电脑上对hosts文件做了修改,所以配置中hostname不是localhost

bash 复制代码
192.168.2.3 eureka-1
192.168.2.3 eureka-2
192.168.2.3 eureka-3

5、启动项目

启动三个项目,到配置页中检查。因为已成集群,所以三个地址都会指向同一个配置页。http://eureka-1:1001,http://eureka-2:1002,http://eureka-3:1003。

可以看到有三个服务组成集群。

相关推荐
水云桐程序员1 小时前
从零开发软件的步骤,软件开发从零到完成应该怎么做?
笔记·学习·软件构建·个人开发
David猪大卫1 小时前
【Linux修炼】环境变量、进程地址空间
linux·c语言·经验分享·笔记·学习·ubuntu·centos
叼烟扛炮2 小时前
Linux 学习笔记(五十六):IO 多路复用初探——select 的“三宗罪”
linux·服务器·数据库·笔记·学习·php
叼烟扛炮8 小时前
Linux 学习笔记(五十八):IO 多路复用封神——epoll 底层原理揭秘
linux·运维·服务器·网络·笔记·学习·php
陈布衣10 小时前
MySQL(1)_MySQL 5.7.21 Linux 安装笔记
linux·笔记·mysql
胡写代码10 小时前
把数据库连接搬进配置中心,服务起不来:spring.config.import 的 4 个必填项
spring cloud·微服务
缘来是你远去是我13 小时前
本地个人笔记exe
笔记
FakeOccupational14 小时前
【电路笔记 STM32】RTC配置:晶振选择+闹钟Configuration+中断设置
笔记·stm32·实时音视频
迪康coolmu14 小时前
企业U盘管控落地实战:基于迪康端点安全一体化管理系统的移动存储安全实践
运维·人工智能·经验分享·笔记·安全·电脑
Z21-X17 小时前
云原生运维学习笔记:容器技术的演进、底层原理与 Docker 全生命周期管理
运维·笔记·学习·云原生