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

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

相关推荐
-SOLO-22 分钟前
开发笔记-摸鱼输入法
笔记
彧azz2 小时前
Java学习语法篇:变量
java·学习
Yanjun2i2 小时前
Agent学习记录五:Pydantic验证
人工智能·python·学习
疯狂打码的少年3 小时前
【计算机网络】TCP/IP协议族(四层体系结构,与OSI对比)
网络·笔记·tcp/ip·计算机网络
xqqxqxxq4 小时前
AI Agent学习:Agent协作工具技术笔记(李博杰《深入理解 AI Agent》4.6观后总结)
笔记·学习
白猫不黑4 小时前
大学网安方向学习路线:零基础与有基础的学习顺序整理
学习·web安全·计算机·网络安全·信息安全·编程·src漏洞
工业HMI实战笔记5 小时前
玩具制造HMI:注塑成型的快速换模与质量监控界面
人工智能·学习·交互·制造
你在我身后5 小时前
发展路线建议
学习
彧azz5 小时前
数据结构:关于图的学习
c语言·数据结构·笔记·学习·算法
苦猿的大模型日记6 小时前
Day57|从0学习 Claude Code(七):我没把说明书全塞给它,用到哪本才翻哪本
学习