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

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

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

观心发布于 2026/3/29更新于 2026/6/1118 浏览
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. 实际应用场景
  • 免费图片AI生成工具免费生成了解详情
  • Magick API 一键接入全球大模型注册送1000万token查看
  • 免费图片视频在线生成30秒,将你的创意变成现实开始设计
  • X/Twitter免费视频下载器免登陆无限额度免费视频解析下载了解详情
  • 100+免费在线小游戏爽一把
极客日志微信公众号二维码

微信扫一扫,关注极客日志

微信公众号「极客日志V2」,在微信中扫描左侧二维码关注。展示文案:极客日志V2 zeeklog

更多推荐文章

查看全部
  • Django REST Framework 企业级 API 架构实战
  • Whisper.cpp 量化版本清单与 ggml 格式模型下载
  • C++ 测试与调试:确保代码质量与稳定性
  • 大龄零基础转行网络安全行业的可行性与学习路径
  • 基于 OpenCV 的 Python 自动扫雷实现
  • Vivado 项目 Git 版本管理实战指南
  • C++ 设计模式详解:创建型、结构型与行为型实战
  • 基于 Spark 与 Django 的电影票房预测及推荐系统
  • 9款免费AI论文写作工具实测与使用指南
  • 前端 math.js 快速上手入门教程
  • Java Lambda 与匿名内部类为何不能修改外部变量?final 机制解析
  • Claude Code 本地环境配置与使用指南
  • 黑客、骇客、红客、蓝客与飞客的定义及区别解析
  • FastAPI:Python 高性能 Web 框架核心特性与性能测试
  • VS Code 内置聊天与 GitHub Copilot Chat 的区别及语言设置
  • Java 调用 CPLEX:安装配置与示例代码
  • 解决 JavaScript 精度丢失:BigNumber.js 核心用法与场景
  • 数据结构:外部排序原理与优化方法
  • C/C++按位取反操作详解与代码示例
  • Neo4j 5.26 版本安装、下载与配置步骤

相关免费在线工具

  • 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