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:1001http://eureka-2:1002http://eureka-3:1003

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

相关推荐
Rock_yzh2 小时前
AI学习日记——卷积神经网络(CNN):完整实现与可视化分析
人工智能·python·深度学习·神经网络·学习·cnn
Test.X3 小时前
学习16天:pytest学习
学习·pytest
XISHI_TIANLAN3 小时前
【多模态学习】Q&A6: 什么是MOE架构?Router Z Loss函数是指什么?负载均衡损失(Load Balancing Loss)又是什么?
学习·算法·语言模型
Diligence8154 小时前
计算机网络学习总结(二)应用层
学习·计算机网络
小墨宝4 小时前
web前端学习LangGraph
前端·学习
memorycx5 小时前
听课笔记CSAPP
笔记
尘似鹤5 小时前
微信小程序学习(五)
学习·微信小程序·小程序
计算机毕业设计小帅5 小时前
【2026计算机毕业设计】基于微信小程序的英语在线学习系统
学习·微信小程序·毕业设计·课程设计
千忧散5 小时前
Unity Socket学习笔记 (三)TCP&UDP
笔记·学习·unity·c#
今天只学一颗糖5 小时前
Linux学习笔记--触摸屏驱动
笔记·学习