09.1、Spring Boot项目初始化
Spring Boot 项目初始化
欢迎来到第 9 章的学习。在前面的章节中,我们学习了 OpenTelemetry 的基础知识、Grafana Stack 的安装和配置。现在我们要学习如何在实际的后端应用中集成 OpenTelemetry。
今天我们要使用 Java Spring Boot 构建 ShoeHub 电商系统的后端服务,并集成 OpenTelemetry 进行可观察性追踪。
本节将学习:如何使用 Spring Initializr 创建项目、项目结构说明、以及关键依赖介绍。
使用 Spring Initializr 创建项目
使用 Spring Initializr 创建项目的步骤是什么?
第一步:访问 Spring Initializr。 访问 https://start.spring.io/,或者使用 IDE 集成的工具。
第二步:配置项目信息。 Project:Maven 或 Gradle,Language:Java,Spring Boot:3.2.x,Group:com.shoehub,Artifact:order-service,Package name:com.shoehub.orderservice,Packaging:Jar,Java:17。
第三步:选择依赖。 Spring Web、Spring Data JPA、MySQL Driver、Lombok、Spring Boot Actuator。这些依赖是构建后端服务的基础。
第四步:生成并下载项目。 点击 Generate,解压项目文件。
项目信息示例: Group:com.shoehub,Artifact:order-service,Package:com.shoehub.orderservice,Spring Boot:3.2.0,Java:17。
项目结构
Spring Boot 项目结构是什么?
主要目录: src/main/java(Java 源代码)、src/main/resources(资源文件)、src/test/java(测试代码)、pom.xml(依赖管理)。
关键文件: OrderServiceApplication.java(Spring Boot 主类)、application.properties(应用配置)、pom.xml(依赖管理)。
包结构: com.shoehub.orderservice(主包),controller(Controller 层)、service(Service 层)、repository(Repository 层)、model(Model 层)。
初始代码:
文件路径:
pom.xml<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <relativePath/> </parent> <groupId>com.shoehub</groupId> <artifactId>order-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>order-service</name> <description>Order Service for ShoeHub</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
文件路径:
src/main/java/com/shoehub/orderservice/OrderServiceApplication.javapackage com.shoehub.orderservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class OrderServiceApplication { public static void main(String[] args) { SpringApplication.run(OrderServiceApplication.class, args); } }
文件路径:
src/main/resources/application.properties# App configuration spring.application.name=order-service server.port=8080 # Actuator provisioning management.endpoints.web.exposure.include=health,info,metrics
标准的项目结构便于维护和团队协作。
关键依赖介绍
关键依赖有哪些呢?
第一个是 Spring Web。 Web 应用支持,包含 Spring MVC、内嵌 Tomcat,用于构建 REST API。
第二个是 Spring Data JPA。 数据持久化,包含 JPA、Hibernate,用于数据库操作。
第三个是 MySQL Driver。 MySQL 数据库驱动,用于连接 MySQL 数据库。
第四个是 Lombok。 简化代码,注解:@Data、@Getter、@Setter,用于减少样板代码。
第五个是 Spring Boot Actuator。 监控端点,端点:/actuator/health、/actuator/metrics,用于应用监控。
依赖关系: Spring Boot 是核心框架,Spring Web 提供 Web 支持,Spring Data JPA 提供数据持久化支持,MySQL Driver 提供数据库连接支持,Lombok 和 Actuator 提供辅助功能。
这些依赖是构建后端服务的基础。
本节小结
在本节中,我们学习了 Spring Boot 项目初始化:
第一个是使用 Spring Initializr 创建项目。 访问 https://start.spring.io/,配置项目信息,选择依赖,生成项目。
第二个是项目结构。 标准的 Spring Boot 目录结构,src/main/java(源代码)、src/main/resources(资源文件)。
第三个是关键依赖。 Spring Web、Spring Data JPA、MySQL Driver、Lombok、Actuator。这些依赖是构建后端服务的基础。
第四个是项目准备就绪。 项目已创建,依赖已配置,可以开始开发了。
初始化流程: 访问 Spring Initializr → 配置项目信息 → 选择依赖 → 生成项目 → 导入 IDE → 项目准备就绪。
这就是 Spring Boot 项目初始化。项目已准备就绪,可以开始开发了。
在下一节,我们将学习添加 OpenTelemetry 依赖。学习如何添加 OpenTelemetry 相关依赖。