Spring Cloud Gateway 源代碼初始化構建

開局

自從到新公司、換了新語言之後,很長一段時間沒有研究源代碼了,也很長一段時間沒有用eclipse和java寫代碼了。以前看過少量的源代碼,也不太精通,所以基本上算是對開源代碼的小白入門者吧。

所以在看一些源代碼時遇到的問題,其他想看源代碼提升個人代碼能力的同學應該也會同樣遇到。

這段時間計劃把網關相關的幾個源代碼和相關能力的實現邏輯整理熟悉起來。基本上是:spring cloud gateway(java)、 zuul(java)、 mosn(go)、grpc-gateway(go)、kong(c++、lua)。

從 spring cloud gateway開始

源代碼地址

https://github.com/spring-cloud/spring-cloud-gateway

spring cloud 還是在用maven管理依賴,spring和spring boot已經在用gradle做依賴管理了。

spring cloud源代碼增加了kotlin的支持,包含兩組源代碼java的和kotlin的。

Un-Resolvable POM問題

spring cloud gateway項目根目錄下pom.xml提示的錯誤,找不到以下3個POM文件,但是從本地代碼倉庫看又能找到這幾個POM文件:

Multiple annotations found at this line:
- Project build error: Non-resolvable import POM: Failure to find org.springframework.data:spring-data-releasetrain:pom:Neumann-RC2 in https://repo.maven.apache.org/maven2 was cached in the local
repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced
- Project build error: Non-resolvable import POM: Failure to find org.springframework.session:spring-session-bom:pom:Dragonfruit-RC2 in https://repo.maven.apache.org/maven2 was cached in the local
repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced
- Project build error: Non-resolvable import POM: Failure to find org.springframework.integration:spring-integration-bom:pom:5.3.0.RC1 in https://repo.maven.apache.org/maven2 was cached in the local
repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced

從提示看,是maven到 https://repo.maven.apache.org/maven2 找不到上述3個POM文件,那我們先按照字面意思去看下:到這個地址https://repo.maven.apache.org/maven2/org/springframework/data/確實沒有找到對應的三個POM。

我們再看下這個網址https://repo.maven.apache.org/maven2,這個是maven的默認倉庫地址,需要到哪個倉庫地址纔能有這些POM文件呢?

咱們再看下spring cloud gateway提供的pom.xml文件,裏面提供了幾個倉庫地址:

<profile>
			<id>spring</id>
			<repositories>
				<repository>
					<id>spring-snapshots</id>
					<name>Spring Snapshots</name>
					<url>https://repo.spring.io/libs-snapshot-local</url>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
					<releases>
						<enabled>false</enabled>
					</releases>
				</repository>
				<repository>
					<id>spring-milestones</id>
					<name>Spring Milestones</name>
					<url>https://repo.spring.io/libs-milestone-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</repository>
				<repository>
					<id>spring-releases</id>
					<name>Spring Releases</name>
					<url>https://repo.spring.io/release</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>spring-snapshots</id>
					<name>Spring Snapshots</name>
					<url>https://repo.spring.io/libs-snapshot-local</url>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
					<releases>
						<enabled>false</enabled>
					</releases>
				</pluginRepository>
				<pluginRepository>
					<id>spring-milestones</id>
					<name>Spring Milestones</name>
					<url>https://repo.spring.io/libs-milestone-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</pluginRepository>
				<pluginRepository>
					<id>spring-releases</id>
					<name>Spring Releases</name>
					<url>https://repo.spring.io/libs-release-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>
		<profile>

倉庫地址有了,我們增加到maven的settings.xml即可,把spring cloud gateway pom.xml裏的倉庫拷貝到settings.xml的repositories裏,如下:

</profile>
		<profile>
			<id>spring</id>
			<repositories>
				<repository>
					<id>spring-snapshots</id>
					<name>Spring Snapshots</name>
					<url>https://repo.spring.io/libs-snapshot-local</url>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
					<releases>
						<enabled>false</enabled>
					</releases>
				</repository>
				<repository>
					<id>spring-milestones</id>
					<name>Spring Milestones</name>
					<url>https://repo.spring.io/libs-milestone-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</repository>
				<repository>
					<id>spring-releases</id>
					<name>Spring Releases</name>
					<url>https://repo.spring.io/release</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>spring-snapshots</id>
					<name>Spring Snapshots</name>
					<url>https://repo.spring.io/libs-snapshot-local</url>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
					<releases>
						<enabled>false</enabled>
					</releases>
				</pluginRepository>
				<pluginRepository>
					<id>spring-milestones</id>
					<name>Spring Milestones</name>
					<url>https://repo.spring.io/libs-milestone-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</pluginRepository>
				<pluginRepository>
					<id>spring-releases</id>
					<name>Spring Releases</name>
					<url>https://repo.spring.io/libs-release-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>
  </profiles>

注意一定要把新增加的profile設置爲激活的profile,在settings.xml增加如下配置:

	<activeProfiles>
		<activeProfile>spring</activeProfile>
	</activeProfiles>

maven lifecycle問題

Multiple annotations found at this line:
- Plugin execution not covered by lifecycle configuration: io.spring.javaformat:spring-javaformat-maven-plugin:0.0.9:apply (execution: default, phase:
validate)
- Plugin execution not covered by lifecycle configuration: io.spring.javaformat:spring-javaformat-maven-plugin:0.0.9:validate (execution: default, phase:
validate)
- Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:flatten-maven-plugin:1.2.2:flatten (execution: flatten, phase: process-
resources)

對於這個問題stackoverflow上給了一個可以用的方法:https://stackoverflow.com/questions/6352208/how-to-solve-plugin-execution-not-covered-by-lifecycle-configuration-for-sprin。 就像其他人評論的那樣,既然這個plugin被放在了配置文件裏,那麼他一定有某種用途,使用增加pluginManagement的方式只是一種掩耳盜鈴的臨時解決方法。

另外一種不需要改動源代碼文件的臨時解決辦法就是在eclipse裏配置爲忽略plugin lifecycle error:
在這裏插入圖片描述
在進一步的解決方案就是針對上述出問題的plugin配置各自所需的lifecyclemapping,後續有時間再研究研究怎麼做。

兩個插件的功能

大致查了下2個插件的功能,都是開源社區通用的插件,後續還是有必要研究一下。
flatten-maven-plugin: pom文件按照版本進行精簡的插件,比如原pom裏包含了一些變量、一些開發相關的依賴、構建信息可以在發佈版中自動清理掉;
spring-javaformat-maven-plugin:按照spring項目的統一規範格式化代碼,參見

maven裏mirror和repository的關係

mirror是repository的鏡像,一般用在原repository訪問不了或者響應比較慢的情況,或者公司內部有統一的鏡像管理,需要都通內部私服的情況。常見的mirror配置:

使用阿里雲鏡像替代官方鏡像:

<mirror>
		 <id>alimaven</id>
		<name>aliyun maven</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
		<mirrorOf>central</mirrorOf>
	</mirror>

配置公司nexus私服:

<mirror>
      <id>company-nexus</id>
      <mirrorOf>*</mirrorOf> <!--所有倉庫都鏡像到私服; =central時,僅maven中央倉庫鏡像到私服-->
      <name>company-nexus</name>
      <url>http://yourhost:8081/nexus/content/groups/public/</url>
    </mirror>

也可以把私服配置成一個repository:


        <repository>
			<id>company-nexus</id>
			<name>company-nexus</name>
			<url>http://yourhost:8081/nexus/content/groups/public/</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
			<releases>
				<enabled>true</enabled>
			</releases>

詳見:http://maven.apache.org/guides/mini/guide-mirror-settings.html

爲什麼最前邊的POM只到maven中央倉庫找

這裏有個問題,也值得再思考一下。實際上spring cloud gateway的根目錄pom.xml文件裏已經配置spring依賴所需的全部倉庫地址,而且默認情況下按說name=spring的profile是激活態纔對,爲什麼maven update project的時候和select profile的時候都會報錯?

難道spring cloud gateway開發的大神們沒有考慮到項目首次開發的便利性,環境的自洽性?感覺可能我哪裏還是沒有理解到位,哪裏打開方式不對。

做了有一個嘗試,是在spring cloud gateway的根目錄pom.xml裏增加一行激活name=spring的profile的配置maven update project就能正常工作了。

<profile>
			<id>spring</id>
			<repositories>
				<repository>
					<id>spring-snapshots</id>
					<name>Spring Snapshots</name>
					<url>https://repo.spring.io/libs-snapshot-local</url>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
					<releases>
						<enabled>false</enabled>
					</releases>
				</repository>
				<repository>
					<id>spring-milestones</id>
					<name>Spring Milestones</name>
					<url>https://repo.spring.io/libs-milestone-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</repository>
				<repository>
					<id>spring-releases</id>
					<name>Spring Releases</name>
					<url>https://repo.spring.io/release</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>spring-snapshots</id>
					<name>Spring Snapshots</name>
					<url>https://repo.spring.io/libs-snapshot-local</url>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
					<releases>
						<enabled>false</enabled>
					</releases>
				</pluginRepository>
				<pluginRepository>
					<id>spring-milestones</id>
					<name>Spring Milestones</name>
					<url>https://repo.spring.io/libs-milestone-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</pluginRepository>
				<pluginRepository>
					<id>spring-releases</id>
					<name>Spring Releases</name>
					<url>https://repo.spring.io/libs-release-local</url>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章