Not only that, SpringBoot 2.7.0 version was released, and I also upgraded the previous open source scaffolding project mall-tiny for the first time! Some friends proposed to upgrade the mall project, so I recently took the time to upgrade it! Not only the latest version of SpringBoot is supported, but the technology stacks used are basically upgraded to the latest! Today, I will share the upgrade content and some problems encountered during the upgrade process. You can refer to it!
SpringBoot actual e-commerce project mall (50k+star) address: github.com/macrozheng/…
Technology stack upgrade
The mall project is implemented with the current mainstream technologies. These mainstream technologies have basically been upgraded to the latest stable version. For the specific upgrade content, you can refer to the table below.
technology | Version | illustrate |
---|---|---|
SpringBoot | 2.3.0->2.7.0 | Container + MVC framework |
SpringSecurity | 5.1.4->5.7.1 | Authentication and Authorization Framework |
MyBatis | 3.4.6->3.5.9 | ORM framework |
MyBatisGenerator | 1.3.3->1.4.1 | Data layer code generation |
RabbitMQ | 3.7.14->3.10.5 | message queue |
Redis | 5.0->7.0 | Distributed cache |
MongoDB | 4.2.5->5.0 | NoSql database |
Elasticsearch | 7.6.2->7.17.3 | search engine |
LogStash | 7.6.2->7.17.3 | log collection tool |
Kibana | 7.6.2->7.17.3 | Log visualization tool |
Nginx | 1.10->1.22 | static resource server |
Druid | 1.1.10->1.2.9 | database connection pool |
MiniO | 7.1.0->8.4.1 | object storage |
Hutool | 5.4.0->5.8.0 | Java tool class library |
PageHelper | 5.2.0->5.3.0 | MyBatis physical paging plugin |
Swagger-UI | 2.9.2->3.0.0 | Documentation generation tool |
logstash-logback-encoder | 5.3->7.2 | Logstash日志收集插件 |
docker-maven-plugin | spotify->fabric8 | 应用打包成Docker镜像的Maven插件 |
升级过程
升级过程中遇到一些问题,这里整理了下,给想要升级这套技术栈的小伙伴一个参考!
支持SpringBoot 2.7.0
看了下之前使用的2.3.0
版本,一年前就End of Support
了,升级2.7.0
还是很有必要的。
升级2.7.0
版本不仅是改个版本号就行了,由于SpringBoot2.6.x
版本开始默认禁用了循环依赖,如果你的项目中循环依赖太多的话,只能使用如下配置开启了。
spring:
main:
allow-circular-references: true
既然官方都禁止使用了,我们还是从源头上解决循环依赖的好,如何优雅地解决循环依赖问题具体可以参考mall-tiny升级支持SpringBoot 2.7.0 中的解决循环依赖部分,mall项目也使用了这种优雅的方式。
Swagger改用Starter
之前项目中是直接使用Swagger依赖来集成的,并没有用Starter
,这次改用了它。
<!--Swagger-UI API文档生产工具-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
在升级SpringBoot 2.6.x
版本的时候,其实Swagger就有一定的兼容性问题,需要在配置中添加BeanPostProcessor
这个Bean,具体可以参考升级 SpringBoot 2.6.x 版本后,Swagger 没法用了 。
SpringSecurity用法升级
在升级SpringBoot2.7.0
版本后,SpringSecurity中有个重要的类被弃用了,那就是一直作为配置类使用的WebSecurityConfigurerAdapter
。
新用法非常简单,无需再继承WebSecurityConfigurerAdapter
,只需直接声明配置类,再配置一个生成SecurityFilterChainBean
的方法,把原来的HttpSecurity
配置移动到该方法中即可,mall项目也采用了这种新用法。
/**
* SpringSecurity 5.4.x以上新用法配置
* 为避免循环依赖,仅用于配置HttpSecurity
* Created by macro on 2022/5/19.
*/
@Configuration
public class SecurityConfig {
@Bean
SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
//省略HttpSecurity的配置
return httpSecurity.build();
}
}
最新版Spring Security用法具体可以参考Spring Security 最新用法 。
MyBatis升级
在升级MyBatis的过程中,也升级了MySQL的驱动版本,从8.0.16
升级到了8.0.29
。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
之前有小伙伴提出升级到该版本后,在Linux上无法连接到MySQL数据库,其实是因为默认使用了SSL连接导致的,在配置文件中添加useSSL=false
配置即可解决。
spring:
datasource:
url: jdbc:mysql://db:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
username: reader
password: 123456
ELK日志收集系统升级
其实每次升级SpringBoot版本,如果集成了Elasticsearch都基本要升级ES,然后整套ELK组件都得升级,这次全部升级到了7.17.3
版本。
为什么升级该版本呢?因为SpringBoot2.7.0
使用的Java SDK默认兼容该版本。
不得不说ES的Java SDK各版本兼容性很差,如果还是使用之前的7.6.2
版本的话,运行mall-search
中的单元测试代码会出现如下问题。很多小伙伴使用ES出现一些稀奇古怪的问题,大概率是版本兼容性问题。
看一眼升级后的日志收集系统,Kibana的界面更现代化了!
MongoDB升级
MongoDB升级5.0
用法基本和之前一致,但是在部署到Docker环境时发现,MongoDB5.0
居然需要特定CPU支持,只得改用4.x
版本了。
镜像打包插件改用fabric8io
I have been using it before, which is used to package spotify
the application Docker docker-maven-plugin
image and upload it to the server. On the official website, this plugin is basically not maintained, and some friends have reported problems with its use before.
Now I have switched to the one fabric8
produced docker-maven-plugin
, with more powerful functions and more timely updates.
Although the plug-in has been changed, the usage is still the same. After configuring the docker remote access address, package
you can directly double-click the command to package and upload the application image with one click.
Deployment Documentation Updates
The deployment documentation of the project has also been updated synchronously. For details, please refer to the following link.
- Deployment of mall in Windows environment
- Deployment of mall in Linux environment (based on Docker container)
- Deployment of mall in Linux environment (based on Docker Compose)
Summarize
Today, I shared the upgrade content of the mall project and some problems encountered in the upgrade process. I have to say that SpringBoot is indeed a great framework. It has been upgraded across several major versions 2.7.0
, and the code hardly needs to be changed. The SpringBoot 2.7 version is likely to become a nail-biting version, because the minimum Java 17 is required since SpringBoot 3.0, you can try to upgrade to this version!
Project source code address
Open source is not easy. If you think the project is helpful, please click and
Star
support it!