Spring Cloud – 依赖管理
在本章中,我们将使用 Spring Cloud 构建我们的第一个应用程序。在使用 Spring Boot 作为底层框架的情况下,我们来了解 Spring Cloud 应用程序的项目结构和依赖性设置。
核心依赖性
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')"
}
}
项目架构和结构
对于本教程,我们将用一家餐厅的案例 −
餐厅服务发现 − 用于注册服务地址。
餐厅客户服务 − 向客户端和其它服务提供客户信息。
餐厅服务 − 向客户端提供餐厅信息。使用客户服务获取客户的城市信息。
餐厅网关 − 我们的应用程序的入口点。但是,出于简单考虑,在本教程中,我们仅会使用一次。
大致上,下面是项目架构 −
并且我们将使用以下项目结构。请注意,我们将在即将到来的章节中查看文件。
项目 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 作为开发应用程序的底层框架,这就是你看到它被列为依赖性部分的原因。
广告