跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客我的书AI学习GitHub 精选镜像AI 生图工具UI配色美学关于
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
Javajava

Spring Boot 微服务架构设计与实战

微服务架构将应用拆分为独立服务,显著提升系统的扩展性与维护效率。基于 Spring Boot 结合 Spring Cloud 演示核心实践:搭建 Eureka 注册中心实现服务发现,配置 Config Server 统一管理外部化配置,利用 Ribbon 实现客户端负载均衡。通过产品与订单服务的交互示例,展示服务间通信流程及测试方法,帮助开发者快速掌握微服务落地方案。

观心发布于 2026/3/29更新于 2026/9/1753 浏览
Spring Boot 微服务架构设计与实战

Spring Boot 微服务架构设计与实战

微服务架构图

微服务架构概述

在 Java 开发领域,微服务架构已成为构建高可用、可扩展系统的主流选择。简单来说,微服务是一种将应用程序拆分为一组独立服务的架构风格,每个服务运行在自己的进程中,并通过轻量级机制(通常是 HTTP API)进行通信。

这种架构的核心优势在于:

  • 独立部署:各服务可单独发布,互不影响。
  • 技术异构:不同服务可根据需求选择最适合的技术栈。
  • 弹性扩展:只需对负载高的特定服务进行扩容。
  • 容错性提升:单个服务故障不会导致整个系统瘫痪。

常见的微服务生态包括 Spring Cloud、Netflix OSS,并常配合 Docker 容器化与 Kubernetes 编排工具使用。

集成 Spring Cloud Eureka

服务注册与发现是微服务的基础。Spring Cloud Eureka 提供了成熟的服务治理方案。下面我们以一个典型的产品服务(Provider)和订单服务(Consumer)为例,演示如何搭建 Eureka 环境。

1. 搭建服务注册中心

首先创建 Eureka Server 项目。在 pom.xml 中引入核心依赖:

<dependencies>
    <!-- Eureka Server 依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <!-- 测试依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR12</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

配置 application.properties,关闭自身注册行为,仅作为注册中心:

# 服务器端口
server.port=8761
# Eureka Server 配置
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.instance.hostname=localhost

启动类需添加 @EnableEurekaServer 注解:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

2. 创建服务提供者 (Product Service)

服务提供者需要注册到 Eureka。依赖配置如下:

<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.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

配置文件指定应用名称及注册中心地址:

server.port=8081
spring.application.name=product-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=true

启动类添加 @EnableEurekaClient:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ProductServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }
}

控制器示例:

import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/products")
public class ProductController {
    private List<Product> products = new ArrayList<>();

    public ProductController() {
        products.add(new Product(1L, "P001", "手机", 1000.0, 100));
        products.add(new Product(2L, "P002", "电脑", 5000.0, 50));
    }

    @GetMapping("/")
    public List<Product> getAllProducts() {
        return products;
    }

    @GetMapping("/{id}")
    public Product getProductById(@PathVariable Long id) {
        return products.stream()
                .filter(p -> p.getId().equals(id))
                .findFirst()
                .orElse(null);
    }
}

3. 创建服务消费者 (Order Service)

消费者同样需要注册到 Eureka,以便被其他服务发现。依赖中额外加入 Ribbon 用于负载均衡:

<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-netflix-ribbon</artifactId>
    </dependency>
</dependencies>

配置与启动类类似,只需修改 spring.application.name=order-service 和端口 8082。

关键是在调用产品接口时使用 @LoadBalanced 的 RestTemplate,这样可以通过服务名直接调用:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/api/orders")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    private List<Order> orders = new ArrayList<>();

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    // 调用远程服务示例
    @GetMapping("/{id}/product/{productId}")
    public Product getProductById(@PathVariable Long id, @PathVariable Long productId) {
        // 通过服务名调用,而非 localhost
        String url = "http://product-service/api/products/" + productId;
        ResponseEntity<Product> response = restTemplate.getForEntity(url, Product.class);
        return response.getBody();
    }
}

统一配置管理

随着服务增多,分散的配置文件难以维护。Spring Cloud Config 允许我们将配置集中存储在 Git 仓库中。

配置服务器

引入 spring-cloud-config-server 依赖,并启用配置功能:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

配置指向 Git 仓库:

server.port=8888
spring.cloud.config.server.git.uri=https://github.com/username/config-repo
spring.cloud.config.server.git.search-paths=config-repo

配置客户端

服务启动时自动拉取配置。注意在 Hoxton 版本后,推荐使用 bootstrap.properties 或 spring.config.import 来指定配置源:

# bootstrap.properties
spring.application.name=product-service
spring.cloud.config.uri=http://localhost:8888

实际业务配置则放在 Git 仓库中的 product-service-dev.properties 文件里,实现环境隔离。

服务间通信与负载均衡

除了 Eureka 注册,服务间的调用通常结合 Ribbon 实现客户端负载均衡。Ribbon 会自动从 Eureka 获取服务列表,并在多次请求间轮询分配流量,避免单点过载。

在实际项目中,我们还会遇到熔断降级的需求(如 Hystrix),但在基础架构设计中,确保服务发现与负载均衡稳定运行是首要任务。

实际应用场景

Spring Boot 微服务架构适用于多种场景:

  • 电商系统:拆分用户、商品、订单、支付等独立服务。
  • SaaS 平台:多租户数据隔离,按需扩缩容。
  • 高并发活动:针对热点服务单独部署集群。

通过上述实践,开发者可以构建出灵活、健壮的后端系统。关键在于合理划分服务边界,并规范服务间的交互协议。


本文涵盖了从架构概念到代码落地的完整流程。掌握这些核心组件,你就具备了构建企业级微服务系统的基础能力。后续可进一步探索网关路由、链路追踪等进阶主题。

目录

  1. Spring Boot 微服务架构设计与实战
  2. 微服务架构概述
  3. 集成 Spring Cloud Eureka
  4. 1. 搭建服务注册中心
  5. 服务器端口
  6. Eureka Server 配置
  7. 2. 创建服务提供者 (Product Service)
  8. 3. 创建服务消费者 (Order Service)
  9. 统一配置管理
  10. 配置服务器
  11. 配置客户端
  12. bootstrap.properties
  13. 服务间通信与负载均衡
  14. 实际应用场景

更多推荐文章

查看全部
  • FAST_LIO 与 FAST_LIO2 算法复现指南
  • MaaS 平台与阿里 QWQ 技术:AI 调参实战指南
  • 基于自然语言处理的论文辅助写作工具功能介绍
  • 银河麒麟 V11-2503 安装指南:6.6 内核与 AI 特性详解
  • Dify + Skill 本地部署大模型智能体:构建企业级 AI Agent 系统
  • Adaptive RAG 系统搭建:从向量检索到 Streamlit 前端全流程
  • 低代码不是终点:走向意图驱动的企业级开发新范式
  • 基于Q-learning算法的机器人迷宫路径规划研究
  • 神经网络概率语言模型(NPLM)阅读笔记
  • 利用 AI 实现一镜到底:将教材插图转为 VR 全景视频
  • OpenClaw 结合 cpolar 打造可远程访问的本地 AI 智能体
  • 双指针算法实战:移动零与复写零详解
  • C++ STL 常用容器入门与使用指南
  • 算法实战:位运算求解两数之和、唯一数字及缺失数字
  • Git 基础:认识三大区域与文件修改提交流程
  • Stable Diffusion WebUI 本地部署实战(Win11 + RTX 环境配置)
  • DeepSeek 深度使用指南:提示词工程与本地知识库搭建
  • ESP32 结合 MimiClaw 的 BLDC 无刷电机控制与本地智能机器人方案
  • 前端防录屏原理:EME 与 DRM 技术解析及实战
  • OpenDroneMap 快速入门:无人机影像处理与三维建模

相关免费在线工具

  • Keycode 信息

    查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online

  • Escape 与 Native 编解码

    JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online

  • JavaScript / HTML 格式化

    使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online

  • JavaScript 压缩与混淆

    Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online

  • Base64 字符串编码/解码

    将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online

  • Base64 文件转换器

    将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online