当前位置: 首页 > news >正文

Eureka实战

1.创建父工程SpringCloudTest

SpringCloudTest为父工程,用于引入通用依赖,如spring-boot-starter-web、lombok,这样子工程就可以直接继承,无需重复引入。在dependencyManagement标签中引入和springboot版本对应的springcloud(访问官网查询),本文用的springboot3.5.3,对应springcloud2025.0.0。

SpringCloudTest的pom.xml文件的关键内容如下:

    <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.5.3</version><relativePath/> <!-- lookup parent from repository --></parent>    <modules><module>spring-cloud-eureka-server</module><module>spring-cloud-eureka-client</module></modules><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><dependencyManagement><dependencies><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2025.0.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

2.创建子工程spring-cloud-eureka-server

主要分四步:在parent标签中引入父工程的依赖;引入spring-cloud-starter-netflix-eureka-server依赖;启动类上添加@EnableEurekaServer自动化配置注解;在application.yaml文件中配置eureka注册中心的信息。

application.yaml文件内容:

eureka:instance:instance-id: ${spring.application.name}:${server.port}prefer-ip-address: trueclient:# 注册中心自己不用注册register-with-eureka: falsefetch-registry: false# 注册中心地址,必须以eureka为后缀,否则服务注册不上service-url:defaultZone: http://127.0.0.1:8080/eureka/
spring:application:name: eureka_server
server:port: 8080

3.创建子工程spring-cloud-eureka-cilent

主要分三步:在parent标签中引入父工程的依赖;引入spring-cloud-starter-netflix-eureka-client依赖;在application.yaml中配置服务注册地址。

application.yaml文件内容如下:

server:port: 9000
spring:application:name: spring-cloud-eureka-client
eureka:instance:instance-id: ${spring.application.name}:${server.port}client:service-url:defaultZone: http://127.0.0.1:8080/eureka

4.分别启动spring-cloud-eureka-server和spring-cloud-eureka-cilent

5.访问localhost:8080,结果如下图所示。

6.踩的坑:

1)defaultZone的后缀不是eureka,导致服务找不到注册中心,注册失败。

VICTORY!!!

 

http://www.lryc.cn/news/586268.html

相关文章:

  • Linux - 安全排查 3
  • 带货视频评论洞察 Baseline 学习笔记 (Datawhale Al夏令营)
  • 【读书笔记】《C++ Software Design》第一章《The Art of Software Design》
  • 【大模型面试】50道大型语言模型(LLM)面试问题汇总,看完少走99%弯路!
  • 不止于监控:深入剖析OpenTelemetry的可观察性生态体系
  • LeetCode 3169.无需开会的工作日:排序+一次遍历——不需要正难则反,因为正着根本不难
  • 暑期前端训练day6
  • 历史数据分析——云南白药
  • 连接池的核心接口和常用属性
  • 基于大模型的鼻咽癌全周期预测及诊疗优化研究报告
  • SQL新手入门详细教程和应用实例
  • 零基础 “入坑” Java--- 九、类和对象(二)
  • 芯片验证之验证策略
  • 【MogDB】一种基于ctid分片并发查询以提升大表查询性能的方式
  • 68 指针的减法操作
  • 【Datawhale AI夏令营】Task2 笔记:MCP Server开发的重难点
  • 使用包管理工具CocoaPods、SPM、Carthage的利弊与趋势
  • tiktok 弹幕 逆向分析
  • 系统性能评估方法深度解析:从经典到现代
  • 数据湖和数据库对比
  • 多层感知机的简洁实现
  • Spring Cloud Gateway中常见的过滤器
  • 【时间之外】尘封的智能套件复活记
  • 【QGC】深入解析 QGC 配置管理
  • Gas and Gas Price
  • 闲庭信步使用图像验证平台加速FPGA的开发:第十课——图像gamma矫正的FPGA实现
  • Git企业级开发(最终篇)
  • 闲庭信步使用图像验证平台加速FPGA的开发:第十一课——图像均值滤波的FPGA实现
  • TCP的socket编程
  • OneCode 3.0架构深度剖析:工程化模块管理与自治UI系统的设计与实现