SpringCloud升级之路2020.0.x版-4.maven依赖回顾以及项目框架结构
本系列代码地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford
我们先来回顾下 maven 依赖中一个重要原则:最短路径原则。这在之后我们的使用中会经常用到。
举一个例子,假设我们以 spring-boot-parent
作为 parent:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.9</version>
</parent>
我们想用想用 elasticsearch
作为搜索引擎,在项目中添加了依赖
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.10.2</version>
</dependency>
写好代码,一跑,报类不存在异常:
java.lang.NoClassDefFoundError: org/elasticsearch/common/xcontent/DeprecationHandler
at com.lv.springboot.datasource.ClientUTis.main(ClientUTis.java:13)
Caused by: java.lang.ClassNotFoundException: org.elasticsearch.common.xcontent.DeprecationHandler
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
看下依赖mvn dependency:tree
,发现依赖的elasticsearch
版本是:
org.elasticsearch.client:elasticsearch-rest-high-level-client:7.0.1
|--org.elasticsearch:elasticsearch:5.6.16
|--org.elasticsearch.client:elasticsearch-rest-client:7.0.1
|--org.elasticsearch.plugin:parent-join-client:7.0.1
|--org.elasticsearch.plugin:aggs-matrix-stats-client:7.0.1
|--org.elasticsearch.plugin:rank-eval-client:7.0.1
|--org.elasticsearch.plugin:lang-mustache-client:7.0.1
可能读者会感觉很奇怪,明明指定了elasticsearch
的依赖了啊,而且是项目的根 pom,依赖不是最短路径原则么?不应该以这个依赖为准么?
仔细分析,原来 SpringBoot的DependencyManagement 中,org.elasticsearch:elasticsearch
已经被包含了(以下为节选):
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.9.RELEASE</version>
<properties>
<elasticsearch.version>5.6.16</elasticsearch.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
spring-boot 其实已经考虑到用户可能要换版本了,所以将版本放入了 <properties/>
,properties 也具有最短路径原则,所以可以通过在你的项目根 pom 中的 properties 增加相同 key 修改版本:
<properties>
<elasticsearch.version>7.10.2</elasticsearch.version>
</properties>
所有可以这么替换的属性, spring-boot 官方文档已经列出了,参考官方文档附录:Version Properties
也可以通过 dependencyManagement 的最短路径原则,通过在你的项目根 pom 中的增加想修改依赖的 dependencyManagement 即可:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.10.2</version>
</dependency>
</dependencies>
</dependencyManagement>
最后,可以记住下面的原则,就知道项目的依赖到底是哪个版本啦:
Maven依赖可以分为如下几部分:
- 直接依赖,就是本项目 dependencies 部分的依赖
- 间接依赖,就是本项目 dependencies 部分的依赖所包含的依赖
- 依赖管理,就是本项目 dependency management 里面的依赖
- parent 的直接依赖
- parent 的间接依赖
- parent 的依赖管理
- bom 的直接依赖(一般没有)
- bom 的间接依赖(一般没有)
- bom 的依赖管理
可以这么理解依赖:
- 首先,将 parent 的直接依赖,间接依赖,还有依赖管理,插入本项目,放入本项目的直接依赖,间接依赖还有依赖管理之前
- 对于直接依赖,如果有 version,那么就依次放入 DependencyMap 中。如果没有 version,则从依赖管理中查出来 version,之后放入 DependencyMap 中。key 为依赖的 groupId + artifactId,value为version,后放入的会把之前放入的相同 key 的 value 替换
- 对于每个依赖,各自按照 1,2 加载自己的 pom 文件,但是如果第一步中的本项目 dependency management 中有依赖的版本,使用本项目 dependency management的依赖版本,生成 TransitiveDependencyMap,这里面就包含了所有的间接依赖。
- 所有间接依赖的 TransitiveDependencyMap, 对于项目的 DependencyMap 里面没有的 key,依次放入项目的 DependencyMap
- 如果 TransitiveDependencyMap 里面还有间接依赖,那么递归执行3, 4。
由于是先放入本项目的 DependencyMap,再去递归 TransitiveDependencyMap,这就解释了 maven 依赖的最短路径原则。
Bom 的效果基本和 Parent 一样,只是一般限制中,Bom 只有 dependencyManagement 没有 dependencies
如下图所示,我们会抽象出如下几个依赖:
- 所有项目的 parent:以某版本 Spring Boot 作为 parent,管理 Spring Cloud 依赖,并且包括一些公共依赖,还有单元测试依赖。如果以后我们想修改 Spring Boot 或者 Spring Cloud 版本,就在这里修改。并且,指定了所有项目编译配置。
- Spring Framework Common:所有使用了 Spring 或者 Spring Boot 的公共依赖,一般我们编写 starter,或者编写一些工具包,不需要 Spring Cloud 的特性,就会添加这个依赖。
- Spring Cloud Common:添加了 Spring Framework Common 的依赖。我们的微服务分为主要基于 spring-webmvc 的同步微服务项目以及主要基于 spring-webflux 的异步微服务项目,其中有一些公共的依赖和代码,就放在了这个项目中。
- Spring Cloud WebMVC:添加了 Spring Cloud Common 的依赖。基于 spring-webmvc 的同步微服务项目需要添加的核心依赖。
- Spring Cloud WebFlux:添加了 Spring Cloud Common 的依赖。基于 spring-webflux 的异步微服务项目需要添加的核心依赖。
我们在微服务项目中主要使用的依赖为:
- 对于纯工具包,只使用了 Spring 与 Spring Boot 的特性的,添加 Spring Framework Common 的依赖。
- 对于基于 spring-webmvc 的同步微服务项目,添加 Spring Cloud WebMVC 的依赖。
- 对于基于 spring-webflux 的异步微服务项目,添加 Spring Cloud WebFlux 的依赖。
本小节我们回顾了并深入理解了 maven 依赖最短路径原则,然后给出了我们项目框架的结构,主要对外提供了三种依赖:只使用了 Spring 与 Spring Boot 的特性的依赖,对于基于 spring-webmvc 的同步微服务项目的依赖以及对于基于 spring-webflux 的异步微服务项目的依赖。下一节我们将对这些项目模块的 pom 文件进行详细分析。
微信搜索“我的编程喵”关注公众号,每日一刷,轻松提升技术,斩获各种offer:
SpringCloud升级之路2020.0.x版-4.maven依赖回顾以及项目框架结构的更多相关文章
- SpringCloud升级之路2020.0.x版-1.背景
本系列为之前系列的整理重启版,随着项目的发展以及项目中的使用,之前系列里面很多东西发生了变化,并且还有一些东西之前系列并没有提到,所以重启这个系列重新整理下,欢迎各位留言交流,谢谢!~ Spring ...
- SpringCloud升级之路2020.0.x版-41. SpringCloudGateway 基本流程讲解(1)
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 接下来,将进入我们升级之路的又一大模块,即网关模块.网关模块我们废弃了已经进入维护状态的 ...
- SpringCloud升级之路2020.0.x版-6.微服务特性相关的依赖说明
本系列代码地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford spring-cl ...
- SpringCloud升级之路2020.0.x版-10.使用Log4j2以及一些核心配置
本系列代码地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford 我们使用 Log4 ...
- SpringCloud升级之路2020.0.x版-43.为何 SpringCloudGateway 中会有链路信息丢失
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 在开始编写我们自己的日志 Filter 之前,还有一个问题我想在这里和大家分享,即在 Sp ...
- SpringCloud升级之路2020.0.x版-5.所有项目的parent与spring-framework-common说明
本系列代码地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford 源代码文件:htt ...
- SpringCloud升级之路2020.0.x版-29.Spring Cloud OpenFeign 的解析(1)
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 在使用云原生的很多微服务中,比较小规模的可能直接依靠云服务中的负载均衡器进行内部域名与服务 ...
- SpringCloud升级之路2020.0.x版-34.验证重试配置正确性(1)
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 在前面一节,我们利用 resilience4j 粘合了 OpenFeign 实现了断路器. ...
- SpringCloud升级之路2020.0.x版-2.微服务框架需要考虑的问题
本系列为之前系列的整理重启版,随着项目的发展以及项目中的使用,之前系列里面很多东西发生了变化,并且还有一些东西之前系列并没有提到,所以重启这个系列重新整理下,欢迎各位留言交流,谢谢!~ 上图中演示了一 ...
随机推荐
- Kubernetes Pod中容器的Liveness、Readiness和Startup探针
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 探针的作用 在Kubernetes的容器生命周期管理中,有三种探针,首先要知道,这探针是属于容器的,而不是Pod: 存 ...
- release模式下打断点调试 配置选项
最近调试一个离职的同事留下的工程,DEBUG模式下顺利,RELEASE的时候就崩溃了,显示为"帧不在模块中"--简直一头雾水 于是我修改配置,为了能够在Release模式中打断点调 ...
- Raspberry Pi:树莓派开发板配置USB启动系统
准备材料 树莓派4B U盘 TF卡 树莓派基础镜像2020-08-20稳定版(这个系统是必须的并拷录在TF卡) Kali树莓派系统(这个是我想要学习的系统,大家可以准备自己的系统,拷录在U盘的) SD ...
- 资源:HTML调色板
调色板路径 https://encycolorpedia.cn/
- nginx反向代理tcp协议的80端口
需求:内网有一台mqtt协议服务器,需要将外网的mqtt请求通过一台服务器代理到内网的mqtt服务器上.而这台代理服务器不会开放出了80之外的端口,所以只能使用80端口来转发mqtt请求. 步骤:1. ...
- Spring Boot中文文档(官方文档翻译 基于1.5.2.RELEASE)
作者:Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, Andy Wilkinson, Marcel Overdijk, ...
- 使用命令行操作MySQL 及 语法
在使用之前先要确保服务中的MySQL 已启动,否则会报错:ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061 ...
- [Kong] key-auth实现对API请求的密钥认证
目录 1. 配置密钥验证插件 2. 确认插件配置正确 3. 创建cunsumer 4. 给cunsumer提供关键凭证 5. 验证 6. 小结 [前言]: 下面我们将配置key-auth插件以向服务添 ...
- 学C记录(理解递归问题之汉诺塔)
汉诺游戏规则如下: 1.有三根相邻的柱子,标号为A,B,C. 2.A柱子上从下到上按金字塔状叠放着n个不同大小的圆盘. 3.现在把所有盘子一个一个移动到柱子B上,并且每次移动同一根柱子上都不能出现大盘 ...
- C语言:渔夫打鱼晒网问题
//如果一个渔夫从 2011 年 1 月 1 日开始三天打渔,两天晒网,编程实现当输入 2011 1 月 1 日以后的任意一天,输出该渔夫是在打渔还是在晒网. #include <stdio.h ...