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 餐厅的案例 −
Restaurant Service Discovery − 用于注册服务地址。
Restaurant Customer Service − 向客户和其他服务提供客户信息。
Restaurant Service − 向客户提供餐厅信息。 使用客户服务获取客户的城市信息。
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 作为基础框架来开发我们的应用程序,这就是为什么您会看到它被列为依赖项。

