文章目錄
- 1、前置
- 1、前置介紹, 環境, 以及對應的依賴說明
- 2、建立項目
-
- 1. 建立父項目
- 2. 建立子項目
- 3、配置pom檔案
-
- 1.父項目pom.xml
- 2.子項目pom.xml
- 4、以服務發現做測試
- 5、個人總結
1、前置
1、前置介紹, 環境, 以及對應的依賴說明 (重點)
2、建立項目 (主要是給基礎像我一樣薄弱的同學使用)
3、配置對應的pom檔案 (重點)
4、以服務發現進行測試
5、個人總結
1、前置介紹, 環境, 以及對應的依賴說明
環境說明:
- JDK:1.8
- maven: 3.6.3(本來我用的是3.3.9, 至于為什麼更換了版本, 這個後面會說明, 版本是否更換不影響)
- Spring Boot: 2.2.5.RELEASE
- Spring Cloud: Hoxton.SR3
- Spring Cloud Alibaba: 2.2.1.RELEASE
- 開發工具: IntelliJ IDEA 2020.2 x64
- 系統: Windows 10
搭建項目前, 還是要說一下Spring Boot、 Cloud、Cloud Alibaba三者對應的關系
- Spring Cloud 基于 Spring Boot
- Spring Cloud Alibaba 是 Spring Cloud 的子項目
先把這個順序理清楚了, 我們在搭建項目的時候, 以及在後面進行功能的開發,以及所碰到的問題, 包的配置方面, 都能夠做到思維更清晰
Spring相關版本不清楚的, 可以參考我本專欄的第一篇文章, 有做詳細說明
- 選擇maven
- next finish (不做任何勾選)
- 删除src
- new module
- 選擇spring initializr, 預設選擇, next
- 選擇對應的jdk版本, 打包方式
- 圖檔有做講解
- next
- 留一個src和pom檔案
- 這裡需要說一下spring對應的三個依賴從哪裡擷取, 或者說我為什麼這麼寫, 網上搜尋出來的往往各種各樣的, 自己有時都不知該如何進行選擇, 我們需要有一個自己的抉擇标準
- 這個項目中的, 父項目的pom.xml的作用主要是用來做版本控制使用, 統一jar包版本
- springCloud: 我是從官方找尋的:https://spring.io/projects/spring-cloud
- springCloudAlibaba: 我是從官方找尋的:https://spring.io/projects/spring-cloud-alibaba
- 基于3、4, 以及上面說有的spring三者的關系,我知道我還缺一個springBoot的
- 最終我是在nacos的GitHub進行确定了以下的用法:https://github.com/alibaba/spring-cloud-alibaba/blob/master/pom.xml. 這個gitHub進入方式: nacos官網-Nacos Spring Cloud 快速開始-劃到最下面有一個Spring Cloud Alibaba (這裡要重點說明一下, 官方給的GitHub, 我們也要注意是否過時, 版本對否偏低, 比如nacos完整示例代碼請參考,就是已經很久沒有進行更新過的項目了)
<?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>org.example</groupId>
<artifactId>untitledTest</artifactId>
<packaging>pom</packaging>
<version>${revision}</version>
<!-- 指定子產品, 指定對應的子項目 -->
<modules>
<module>demo</module>
</modules>
<properties>
<!-- 指定項目版本 -->
<revision>1.0-SNAPSHOT</revision>
<!-- 指定SpringBoot版本 -->
<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
<!-- 指定SpringCloud版本 -->
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<!-- 指定SpringCloudAlibaba版本 -->
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
<!-- 指定JDK版本 -->
<java-jdk.version>1.8</java-jdk.version>
</properties>
<!-- dependencyManagement: 對所依賴jar包進行版本管理的管理器,聲明依賴,并不實作引入.
1.不在子項目中聲明依賴,是不會從父項目中繼承下來的
2.隻有在子項目中寫了該依賴項,并且沒有指定具體版本,才會從父項目中繼承該項
3.子項目中指定了版本号,那麼會使用子項目中指定的jar版本。
主要: 主要管理版本
-->
<dependencyManagement>
<dependencies>
<!-- 聲明依賴 springBoot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 聲明依賴 springCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 聲明依賴 springCloudAlibaba -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- 應用父項目, 由于父項目已經指定了版本, 我們在引用的時候就不需要在對版本進行指定(這裡不指定版本, 是指比如:spring-cloud-alibaba-dependencies下面包含的包), 檢視方式 ctrl+滑鼠左鍵, 或者通過包的依賴圖進行檢視
- 引入父項目的版本, 我們寫的是${revision}, Windows的可能會報一個紅線, 隻是IDEA沒有識别, 網上有說更新maven到3.5.0以上, 我試了一下沒有起到效果, 不知道是不是我的方式不對, 也是我上面環境介紹中maven版本為什麼有兩個導緻的, 解決方法:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 引入父項目 -->
<parent>
<groupId>org.example</groupId>
<artifactId>untitledTest</artifactId>
<version>${revision}</version>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>${revision}</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<!-- 父項目中我們已經指定了對應的版本, 這裡直接使用就行, 不用在引用版本 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 服務注冊與發現 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4、以服務發現做測試
上面的pom.xml檔案中, 我們已經導入了服務發現的包
這裡直接運作
server:
# 端口
port: 18082
spring:
application:
name: testName
cloud:
nacos:
discovery:
# 指定nacos server的位址
server-addr: 127.0.0.1:8848
2021-03-13 00:50:53.059 ERROR 17696 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'
解決方式:
修改右下角字元集 UTF-8
2021-03-13 00:53:52.714 WARN 17132 --- [ main] c.a.c.n.registry.NacosServiceRegistry : No service to register for nacos client..
解決方式,指定:
spring:
application:
name: testName
- 其實本篇部落格隻是簡單的介紹,有很多地方都沒有寫出來, 還需要大家自行在深入研究一下. 比如我們項目的版本我是通過revision進行标記, 是否可以采用revision_test了?答案在第二點列舉的文檔中有說明
- 基于pom檔案, 裡面有很多節點, 這裡也隻是列舉了部分, 大家可以查閱官方文檔: http://maven.apache.org/pom.html#The_Super_POM, 以及父pom.xml還可以擴充很多, 大家可以查閱一下官方GitHub給的案列
- 本篇文章中也是查閱了很多文章, 網上尋找了很多列子, 最後影響自己決定使用哪種方法, 都是通過官方文檔或者官方示列
- 父pom.xml檔案裡面還可以指定dependencies節點, 子產品都會使用到的jar, 都可以在父子產品進行指定, 子子產品進行引入就行
- 個人建議, 既然已經做到微服務了, 對pom檔案的結構一定要多了解一點, 深入一點, 對自己會有很大的幫助的
- 以官方文檔為樹幹, 已網上文章為樹枝進行擴充