Spring Cloud Gateway 源代码初始化构建

Posted 不去天涯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud Gateway 源代码初始化构建相关的知识,希望对你有一定的参考价值。

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>

以上是关于Spring Cloud Gateway 源代码初始化构建的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud Alibaba - 24 Gateway-路由断言(Predicate)过滤器(Filter)初体验

Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)

最全面的改造Zuul网关为Spring Cloud Gateway(包含Zuul核心实现和Spring Cloud Gateway核心实现)

spring cloud alibaba gateway  nacos  503错误代码

Spring Cloud Gateway 源代码初始化构建

Spring Cloud Gateway 源代码初始化构建