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

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

相关推荐
ReedFoley7 小时前
【笔记】动手学Ollama 第五章 Ollama 在 LangChain 中的使用 - Python 集成
笔记·langchain
月盈缺7 小时前
学习嵌入式的第二十二天——数据结构——双向链表
数据结构·学习·链表
朱皮皮呀8 小时前
Spring Cloud——服务注册与服务发现原理与实现
运维·spring cloud·eureka·服务发现·php
★YUI★9 小时前
学习游戏制作记录(制作系统与物品掉落系统)8.16
学习·游戏·ui·unity·c#
努力还债的学术吗喽9 小时前
【速通】深度学习模型调试系统化方法论:从问题定位到性能优化
人工智能·深度学习·学习·调试·模型·方法论
Mr Sorry13 小时前
Non-stationary Diffusion For Probabilistic Time Series Forecasting论文阅读笔记
论文阅读·笔记
南猿北者14 小时前
Cmake学习笔记
笔记·学习·策略模式
码小文15 小时前
Altium Designer 22使用笔记(8)---PCB电气约束设置
笔记·嵌入式硬件·硬件工程·ad22
diablobaal15 小时前
云计算学习100天-第26天
学习·云计算
测试老哥16 小时前
pytest+requests+allure自动化测试接入Jenkins学习
自动化测试·软件测试·学习·测试工具·职场和发展·jenkins·pytest