Spring Cloud - 依赖管理
在本章中,我们将使用 Spring Cloud 构建我们的第一个应用程序。让我们回顾一下 Spring Cloud 应用程序的项目结构和依赖关系设置,同时使用 Spring Boot 作为基础框架。
核心依赖
Spring Cloud 组有多个列为依赖项的包。在本教程中,我们将使用 Spring Cloud 组中的多个包。为了避免这些包之间的任何兼容性问题,让我们使用 Spring Cloud 依赖管理 POM,如下所示 -
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Gradle 用户可以通过使用以下命令来实现相同的目的 -
buildscript {
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.10.RELEASE"
}
}
apply plugin: "io.spring.dependency-management"
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:
'Hoxton.SR8')"
}
}
项目架构与结构
在本教程中,我们将使用餐厅的案例 -
餐厅服务发现- 用于注册服务地址。
餐厅客户服务- 向客户提供客户信息和其他服务。
餐厅服务- 向客户提供餐厅信息。使用客户服务获取客户的城市信息。
Restaurant Gateway - 我们应用程序的入口点。但是,为了简单起见,我们将在本教程中仅使用一次。
在较高的层面上,这是项目架构 -
我们将有以下项目结构。请注意,我们将在接下来的章节中查看这些文件。
项目POM
为了简单起见,我们将使用基于 Maven 的构建。下面是我们将在本教程中使用的基本 POM 文件。
<?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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorials.point</groupId>
<artifactId>spring-cloud-eureka-client</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
注意事项-
POM依赖管理部分几乎包含了我们需要的所有项目。我们将在需要时添加依赖部分。
我们将使用 Spring Boot 作为开发应用程序的基础框架,这就是为什么您会看到它被列为依赖项。