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 平台:多租户数据隔离,按需扩缩容。
- 高并发活动:针对热点服务单独部署集群。
通过上述实践,开发者可以构建出灵活、健壮的后端系统。关键在于合理划分服务边界,并规范服务间的交互协议。
本文涵盖了从架构概念到代码落地的完整流程。掌握这些核心组件,你就具备了构建企业级微服务系统的基础能力。后续可进一步探索网关路由、链路追踪等进阶主题。


