news 2026/4/15 23:12:17

从零搭建 Spring Cloud 微服务项目:注册中心 + 网关 + 配置中心全流程

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
从零搭建 Spring Cloud 微服务项目:注册中心 + 网关 + 配置中心全流程

从零搭建 Spring Cloud 微服务项目:注册中心 + 网关 + 配置中心全流程

Spring Cloud 是构建微服务架构的标准框架,2025-2026 年主流版本基于 Spring Boot 3.x(兼容 Java 21+),强调零信任、分布式配置和动态路由。 本教程从零开始,搭建一个包含Eureka 注册中心(服务发现)、Spring Cloud Gateway 网关(路由/负载/限流)和Spring Cloud Config 配置中心(统一配置管理)的微服务项目。

目标:运行后,你能看到服务注册、动态路由和配置拉取的全流程。假设你有 Java/Maven 基础,项目用 Spring Boot 3.3+ 和 Spring Cloud 2023.0.3(最新稳定版)。

第一步:环境准备(10 分钟)
  1. 安装 JDK 21+:从 Oracle 或 Adoptium 下载。
  2. Maven 3.9+:配置好仓库(阿里云镜像推荐)。
  3. IDE:IntelliJ IDEA 或 VS Code(Spring Boot 插件)。
  4. 依赖版本(pom.xml 中的版本管理):
    <properties><java.version>21</java.version><spring-boot.version>3.3.0</spring-boot.version><spring-cloud.version>2023.0.3</spring-cloud.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

项目结构(多模块 Maven 项目):

microservices-demo ├── eureka-server ← 注册中心 ├── config-server ← 配置中心 ├── api-gateway ← 网关 ├── user-service ← 示例微服务1 ├── order-service ← 示例微服务2 └── pom.xml ← 父 POM
第二步:搭建 Eureka 注册中心(服务发现)

Eureka 是 Netflix 的服务注册组件,支持高可用。

  1. 创建 eureka-server 子模块(Spring Initializr:添加 spring-cloud-starter-netflix-eureka-server)。

  2. pom.xml

    <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies>
  3. application.yml

    server:port:8761spring:application:name:eureka-servereureka:client:register-with-eureka:false# 不注册自己fetch-registry:false# 不拉取注册表service-url:defaultZone:http://localhost:8761/eureka/
  4. 主类

    importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublicclassEurekaServerApplication{publicstaticvoidmain(String[]args){SpringApplication.run(EurekaServerApplication.class,args);}}
  5. 启动:访问 http://localhost:8761,看到 Eureka 控制台。

第三步:搭建示例微服务(user-service 和 order-service)

每个微服务都需要注册到 Eureka。

  1. 创建 user-service 子模块(添加 spring-boot-starter-web、spring-cloud-starter-netflix-eureka-client)。

  2. pom.xml(类似 order-service):

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId># 后续接入配置中心</dependency></dependencies>
  3. application.yml

    server:port:8081# order-service 用 8082spring:application:name:user-serviceeureka:client:service-url:defaultZone:http://localhost:8761/eureka/
  4. 主类

    importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublicclassUserServiceApplication{publicstaticvoidmain(String[]args){SpringApplication.run(UserServiceApplication.class,args);}}
  5. 添加一个简单 Controller(测试用):

    importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;@RestControllerpublicclassUserController{@GetMapping("/users")publicStringgetUsers(){return"User List from user-service";}}
  6. 启动:在 Eureka 控制台看到 user-service 和 order-service 已注册。

第四步:搭建 Spring Cloud Config 配置中心

Config Server 支持 Git/Nacos 等后端存储统一配置。

  1. 创建 config-server 子模块(添加 spring-cloud-config-server)。

  2. pom.xml

    <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency></dependencies>
  3. application.yml(用本地文件存储为例,生产用 Git):

    server:port:8888spring:application:name:config-servercloud:config:server:native:search-locations:classpath:/config# 本地配置目录
  4. 主类

    importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServerpublicclassConfigServerApplication{publicstaticvoidmain(String[]args){SpringApplication.run(ConfigServerApplication.class,args);}}
  5. 添加配置文件(resources/config/user-service.yml):

    message:"Hello from Config Server!"
  6. 接入微服务:在 user-service 的 bootstrap.yml(优先级更高):

    spring:cloud:config:uri:http://localhost:8888
  7. 测试:在 user-service Controller 用 @Value(“${message}”) String msg; 输出配置值。

第五步:搭建 Spring Cloud Gateway 网关

Gateway 基于 Reactor,支持动态路由。

  1. 创建 api-gateway 子模块(添加 spring-cloud-starter-gateway、spring-cloud-starter-netflix-eureka-client)。

  2. pom.xml

    <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies>
  3. application.yml

    server:port:8080spring:application:name:api-gatewaycloud:gateway:discovery:locator:enabled:true# 启用 Eureka 发现routes:-id:user-routeuri:lb://user-service# lb: 表示负载均衡predicates:-Path=/users/**-id:order-routeuri:lb://order-servicepredicates:-Path=/orders/**eureka:client:service-url:defaultZone:http://localhost:8761/eureka/
  4. 主类

    importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublicclassApiGatewayApplication{publicstaticvoidmain(String[]args){SpringApplication.run(ApiGatewayApplication.class,args);}}
  5. 测试:访问 http://localhost:8080/users → 路由到 user-service:8081/users

第六步:整合与测试(全链路验证)
  1. *启动顺序*:Eureka → Config Server → 微服务 → Gateway
  2. 验证注册:Eureka 控制台看到所有服务。
  3. 验证配置:微服务日志/接口输出 Config Server 的配置。
  4. 验证路由:通过 Gateway 访问微服务(负载均衡自动)。
  5. 高可用扩展:Eureka 集群(多节点 defaultZone 互指);Gateway 加限流/熔断(Resilience4J)。
第七步:常见坑 & 优化(企业级经验)
问题解决方案
Eureka 不注册检查 @EnableEurekaClient 和 service-url
Config 拉取失败用 bootstrap.yml;检查 uri
Gateway 路由 404路径匹配(/** vs /*);lb:// 拼写
配置刷新不生效加 spring-cloud-starter-bus-amqp(消息总线)
性能瓶颈用 Nacos 替换 Eureka(更现代)
安全加 Spring Security + JWT

恭喜!你已搭建完整微服务链路。想深入(如 Nacos 替换、OpenFeign 调用、Resilience4J 熔断、Docker 部署)?直接告诉我,我再展开代码。c

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/12 11:57:09

【CTF Writeup】Crypto题型之AES加密算法破解与实战

前言 一、AES-CBC模式原理 AES-CBC模式需满足&#xff1a; 加密前需对明文进行分组&#xff08;每组16字节&#xff09;&#xff0c;不足则填充&#xff1b; 需要一个密钥key&#xff08;16/24/32字节&#xff0c;对应AES-128/192/256&#xff09;和初始化向量IV&#xff08…

作者头像 李华
网站建设 2026/4/13 4:13:18

2000亿美元!2026年全球游戏行业的新格局与新变量

2000亿美元&#xff01;2026年全球游戏行业的新格局与新变量 2026年&#xff0c;全球游戏市场规模预计突破2000亿美元&#xff08;约2050-2100亿美元&#xff09;&#xff0c;从2025年的1888-1970亿美元增长3-7%。 这标志着后疫情低迷期的复苏&#xff08;增长率从2022-2024的…

作者头像 李华
网站建设 2026/4/10 14:47:34

鸿蒙APP开发从入门到精通:ArkUI组件库详解与常用组件实战

鸿蒙APP开发从入门到精通&#xff1a;ArkUI组件库详解与常用组件实战 鸿蒙&#xff08;HarmonyOS NEXT&#xff09;是华为推出的分布式操作系统&#xff0c;其APP开发框架以ArkUI为核心&#xff0c;采用ArkTS&#xff08;基于TypeScript的扩展语言&#xff09;进行声明式开发。…

作者头像 李华
网站建设 2026/4/13 23:06:41

Router_T000_ConceptMECE

startmindmap* 汇报总图** 动机价值*** 故事钩子*** 价值主张** 现状基线*** 基线速览*** SafeDreamer*** UNISafe外盾*** SPOWL** 问题缺陷*** 主流缺陷*** OOD幻觉*** 外盾不学*** 固定阈值** 创新方案*** 核心创新*** Risk-Bellman*** ucert内生*** 不改环境奖*** 插入点** …

作者头像 李华
网站建设 2026/4/15 0:27:16

Java计算机毕设之基于springboot+vue的游戏装备账号销售商城平台系统基于springboot的游戏售卖商城系统(完整前后端代码+说明文档+LW,调试定制等)

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华