Overview
本教程介绍如何使用 Spring Boot、Spring Cloud 和MongoDB构建微服务架构。具体来说,本教程将引导您创建多个共同构成完整应用程序的服务。
您可以在以下 GitHub 存储库中找到完整的源代码:
Spring Boot
Spring Boot 是一个构建在 Spring 框架之上的框架。它添加了自动配置、默认和生产就绪功能,以简化构建基于 Spring 的 Java 应用程序,包括与 Spring Data MongoDB 的集成。有关更多信息,请参阅Spring Boot 文档。
Spring Cloud
Spring Cloud 是用于构建分布式系统和微服务的工具集合。它提供配置管理、服务发现和智能路由等功能。有关更多信息,请参阅Spring Cloud 文档。
Tutorial
本教程介绍如何执行以下操作:
验证先决条件
克隆示例存储库
设置配置服务器
设置服务注册表
配置API网关
创建MongoDB微服务
测试 REST API
验证先决条件。
开始之前,请确保已安装以下软件:
Java Development Kit (JDK) 21 或更高版本。您可以从Oracle网站下载JDK。
Git
MongoDB (两个部署用于本地开发)。要学习如何在本地设立MongoDB ,请参阅使用Docker创建本地Atlas部署指南。
克隆示例存储库。
通过运行以下命令来克隆微服务应用程序和配置存储库:
git clone git@github.com:mongodb-developer/microservices-architecture-mongodb.git git clone git@github.com:mongodb-developer/microservices-architecture-mongodb-config-repo.git
按照 README.md 文件中的说明启动 microservices-architecture-mongodb 存储库的每项服务。
设置配置服务器。
配置服务器将微服务的所有配置文件存储在单个存储库中。
配置服务器的配置在 microservices-architecture-mongodb/config-server/src/main/resources/application.properties文件中定义:
spring.application.name=config-server server.port=8888 spring.cloud.config.server.git.uri=${HOME}/Work/microservices-architecture-mongodb-config-repo spring.cloud.config.label=main
此配置指定存储微服务配置的 Git存储库的位置以及要使用的分支。
配置服务器应用程序在位于 microservices-architecture-mongodb/config-server/src/main/java/com/mongodb/configserver/目录中的以下Java类中定义:
package com.mongodb.configserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
注意
@EnableConfigServer 注解可在您的 Spring Boot应用程序中配置服务器功能。
设置服务注册表。
服务注册表跟踪正在运行的微服务及其位置。其他服务使用此信息与其所需的微服务进行通信。
服务注册表配置在以下属性文件中定义:
spring.application.name=service-registry server.port=8761 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
最后两个属性可防止服务注册表向自身注册。
服务注册表应用程序在位于 microservices-architecture-mongodb/service-registry/src/main/java/com/mongodb/serviceregistry/目录中的以下Java类中定义:
package com.mongodb.serviceregistry; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; public class ServiceRegistryApplication { public static void main(String[] args) { SpringApplication.run(ServiceRegistryApplication.class, args); } }
注意
@EnableEurekaServer 注解可启用 Spring Boot应用程序中的服务注册表功能。
配置API网关。
API网关提供访问权限所有微服务的单一入口点。网关在多个微服务之间分发请求,并处理安全、监控和其他问题。
API网关配置在 microservices-architecture-mongodb/api-gateway/src/main/resources/application.yml文件中定义:
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: company-service uri: lb://company-service predicates: - Path=/api/company/**,/api/companies - id: employee-service uri: lb://employee-service predicates: - Path=/api/employee/**,/api/employees eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8761/eureka/ instance: hostname: localhost
此配置定义公司和员工服务的路由,并将网关注册到服务注册表。
创建MongoDB微服务。
此应用程序包括两个微服务:公司服务和员工服务。每个微服务都连接到自己的MongoDB实例以保持独立性。
公司服务配置在 microservices-architecture-mongodb-config-repo/company-service.properties文件中定义:
spring.data.mongodb.uri=${MONGODB_URI_1:mongodb://localhost:27017} spring.threads.virtual.enabled=true management.endpoints.web.exposure.include=* management.info.env.enabled=true info.app.name=Company Microservice info.app.java.version=21 info.app.type=Spring Boot server.port=8081 eureka.client.register-with-eureka=true eureka.client.fetch-registry=true eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ eureka.instance.hostname=localhost
员工服务配置在 microservices-architecture-mongodb-config-repo/employee-service.properties文件中定义:
spring.data.mongodb.uri=${MONGODB_URI_2:mongodb://localhost:27018} spring.threads.virtual.enabled=true management.endpoints.web.exposure.include=* management.info.env.enabled=true info.app.name=Employee Microservice info.app.java.version=21 info.app.type=Spring Boot server.port=8082 eureka.client.register-with-eureka=true eureka.client.fetch-registry=true eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ eureka.instance.hostname=localhost
注意
对于本地开发,公司服务通过端口 27017 连接到MongoDB ,员工服务通过端口 27018 连接到MongoDB 。对于生产,请使用单独的MongoDB Atlas集群,并使用 MONGODB_URI_1 和 MONGODB_URI_2 环境变量设立连接字符串。
测试 REST API。
启动所有服务后,请确保以下服务正在运行,以验证微服务架构是否正常运行:
端口 8888 上的配置服务器
端口 8761 上的服务注册表
端口 8080 上的API网关
端口 8081 上的公司服务
端口 8082 上的员工服务
另请确保您有两个MongoDB实例在端口 27017 和 27018 上运行,或者有两个MongoDB Atlas集群。
通过运行以下命令来运行测试脚本:
./2_api-tests.sh
该脚本创建公司和员工,然后使用REST API检索它们。该脚本返回的输出如下所示:
DELETE Companies 2 DELETE Employees 2 POST Company 'MongoDB' POST Company 'Google' GET Company 'MongoDB' by 'id' { "id": "661aac7904e1bf066ee8e214", "name": "MongoDB", "headquarters": "New York", "created": "2009-02-11T00:00:00.000+00:00" } GET Company 'Google' by 'name' { "id": "661aac7904e1bf066ee8e216", "name": "Google", "headquarters": "Mountain View", "created": "1998-09-04T00:00:00.000+00:00" } GET Companies [ { "id": "661aac7904e1bf066ee8e214", "name": "MongoDB", "headquarters": "New York", "created": "2009-02-11T00:00:00.000+00:00" }, { "id": "661aac7904e1bf066ee8e216", "name": "Google", "headquarters": "Mountain View", "created": "1998-09-04T00:00:00.000+00:00" } ] POST Employee Maxime POST Employee Tim GET Employee 'Maxime' by 'id' { "id": "661aac79cf04401110c03516", "firstName": "Maxime", "lastName": "Beugnet", "company": "Google", "headquarters": "Mountain View", "created": "1998-09-04T00:00:00.000+00:00", "joined": "2018-02-12T00:00:00.000+00:00", "salary": 2468 } GET Employee 'Tim' by 'id' { "id": "661aac79cf04401110c03518", "firstName": "Tim", "lastName": "Kelly", "company": "MongoDB", "headquarters": "New York", "created": "2009-02-11T00:00:00.000+00:00", "joined": "2023-08-23T00:00:00.000+00:00", "salary": 13579 } GET Employees [ { "id": "661aac79cf04401110c03516", "firstName": "Maxime", "lastName": "Beugnet", "company": "Google", "headquarters": "Mountain View", "created": "1998-09-04T00:00:00.000+00:00", "joined": "2018-02-12T00:00:00.000+00:00", "salary": 2468 }, { "id": "661aac79cf04401110c03518", "firstName": "Tim", "lastName": "Kelly", "company": "MongoDB", "headquarters": "New York", "created": "2009-02-11T00:00:00.000+00:00", "joined": "2023-08-23T00:00:00.000+00:00", "salary": 13579 } ]
员工服务查询公司服务以检索公司详细信息。以下代码显示了员工服务如何通过服务注册表与公司服务进行通信:
private CompanyDTO getCompany(String company) { String url = "http://company-service/api/company/name/"; CompanyDTO companyDTO = restTemplate.getForObject(url + company, CompanyDTO.class); if (companyDTO == null) { throw new EntityNotFoundException("Company not found: ", company); } return companyDTO; }
该URL通过名称而不是IP解决和端口引用公司服务,这确认了服务注册表正常工作。
其他资源
要学习;了解有关使用 Spring 和MongoDB构建微服务的更多信息,请参阅以下资源: