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 作為開發應用程式的底層框架,這就是你看到它被列為依賴性部分的原因。
廣告