Spring Cloud 简明教程
Spring Cloud - Dependency Management
在本教程中,我们将使用 Spring Cloud 构建我们的第一个应用程序。让我们在使用 Spring Boot 作为基本框架时来了解 Spring Cloud 应用程序的项目结构和依赖设置。
In this chapter, we will build our very first application using Spring Cloud. Let’s go over the project structure and the dependency setup for our Spring Cloud Application while using Spring Boot as the base framework.
Core Dependency
Spring Cloud 组有多个包列为依赖关系。在本教程中,我们将使用 Spring Cloud 组的多个包。为了避免这些包之间的任何兼容性问题,让我们使用下面给出的 Spring Cloud 依赖关系管理 POM −
Spring Cloud group has multiple packages listed as dependency. In this tutorial, we will be using multiple packages from the Spring Cloud group. To avoid any compatibility issue between these packages, let us use Spring Cloud dependency management POM, given below −
<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 用户可以通过使用以下内容来实现相同的功能 −
The Gradle user can achieve the same by using the following −
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')"
}
}
Project Architecture and Structure
本教程中,我们将使用餐馆示例 -
For this tutorial, we will use the case of a Restaurant −
-
Restaurant Service Discovery − Used for registering the service address.
-
Restaurant Customer Service − Provides Customer information to the client and other services.
-
Restaurant Service − Provides Restaurant information to the client. Uses Customer service to get city information of the customer.
-
Restaurant Gateway − Entry point for our application. However, we will use this only once in this tutorial for simplicity sake.
下图展示了项目架构 -
On a high level, here is the project architecture −
我们还将拥有以下项目结构。注意,我们将在后续章节查看文件。
And we will have the following project structure. Note that we will look at the files in the upcoming chapters.
Project POM
出于简化考虑,我们将使用基于 Maven 的构建。以下是基 POM 文件,我们将在本教程中使用该文件。
For simplicity sake, we will be using Maven-based builds. Below is the base POM file, which we will use for this tutorial.
<?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>
Points to note -
Points to note −
-
The POM dependency management section almost includes all the projects which we require.We will add the dependency section as and when we require.
-
We will use Spring Boot as the base Framework for the development of our application and that is why you see it listed as a dependency.