头图
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: https://github.com/macrozheng/mall

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 log collection plugin
docker-maven-plugin spotify->fabric8 Application Maven plugin packaged as a Docker image

upgrade process

There are some problems encountered during the upgrade process, which are sorted out here to give a reference to friends who want to upgrade this technology stack!

Support SpringBoot 2.7.0

I looked at the 2.3.0 version I used before. It was End of Support a year ago. It is still necessary to upgrade 2.7.0 .

Upgrade 2.7.0 version is not only a matter of changing the version number, because SpringBoot 2.6.x version has disabled circular dependencies by default. If there are too many circular dependencies in your project, you can only use the following Configuration is on.

 spring:
  main:
    allow-circular-references: true

Since it is officially banned, we still have to solve the circular dependency from the source. How to solve the circular dependency problem gracefully can refer to the section on resolving circular dependencies in mall-tiny upgrade support SpringBoot 2.7.0 . The mall project also uses this an elegant way.

Swagger uses Starter instead

In the previous project, the Swagger dependency was directly used for integration, and it was not used Starter , this time it was used instead.

 <!--Swagger-UI API文档生产工具-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

When upgrading SpringBoot 2.6.x version, Swagger actually has certain compatibility problems, and needs to add BeanPostProcessor this bean to the configuration. For details, please refer to After upgrading SpringBoot 2.6.x version, Swagger doesn't work anymore .

SpringSecurity usage upgrade

After upgrading the SpringBoot 2.7.0 version, an important class in SpringSecurity has been deprecated, which has been used as a configuration class WebSecurityConfigurerAdapter .

The new usage is very simple, no need to inherit WebSecurityConfigurerAdapter , just declare the configuration class directly, configure a method to generate SecurityFilterChainBean , and move the original HttpSecurity configuration This method is enough, and the mall project also adopts this new usage.

 /**
 * 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();
    }

}

For the usage of the latest version of Spring Security, please refer to the latest usage of Spring Security .

MyBatis upgrade

In the process of upgrading MyBatis, the driver version of MySQL was also upgraded, from 8.0.16 to 8.0.29 .

 <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.29</version>
</dependency>

Some friends suggested that after upgrading to this version, the inability to connect to the MySQL database on Linux is actually caused by the default use of SSL connection. Adding useSSL=false to the configuration file can solve the problem.

 spring:
  datasource:
    url: jdbc:mysql://db:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
    username: reader
    password: 123456

ELK log collection system upgrade

In fact, every time you upgrade the SpringBoot version, if you integrate Elasticsearch, you basically need to upgrade ES, and then the entire set of ELK components must be upgraded. This time, all of them have been upgraded to the 7.17.3 version.

Why upgrade this version? Because SpringBoot 2.7.0 uses Java SDK that is compatible with this version by default.

I have to say that ES Java SDK versions have poor compatibility. If you still use the previous 7.6.2 version, running the unit test code in mall-search will cause the following problems. Many small partners have some strange problems when using ES, which is probably a version compatibility problem.

Take a look at the upgraded log collection system, Kibana's interface is more modern!

MongoDB upgrade

MongoDB upgrade 5.0 usage is basically the same as before, but when deployed to the Docker environment, it is found that MongoDB 5.0 needs specific CPU support, so it has to be changed to 4.x version .

The image packaging plugin uses fabric8io instead

I have been using the spotify produced by docker-maven-plugin to package the application Docker 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 use the fabric8 produced by docker-maven-plugin , which is more powerful and updated in a timely manner.

Although the plug-in has been changed, the usage is still the same. After configuring the docker remote access address, double-click the package 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
https://www.macrozheng.com/mall/deploy/mall_deploy_windows.html
  • Deployment of mall in Linux environment (based on Docker container)
https://www.macrozheng.com/mall/deploy/mall_deploy_docker.html
  • Deployment of mall in Linux environment (based on Docker Compose)
https://www.macrozheng.com/mall/deploy/mall_deploy_docker_compose.html

Summarize

Today, I shared the upgrade content of the mall project and some problems encountered during the upgrade process. I have to say that SpringBoot is indeed a great framework. It has been upgraded to 2.7.0 across several major versions, and the code is almost unnecessary. change. 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, friends who think the project is helpful, please click Star to support it!

https://github.com/macrozheng/mall


macrozheng
1.1k 声望1.3k 粉丝