Give everyone a tan cloud, several data collection as a Spring Boot open source project (https://github.com/cloudfavorites/favorites-web) is currently in making the above have more than 1600 Star, If filtered by the SpringBoot tag, it could be number five.

When cloud collection 1.0 development is completed, synchronous cloud collection deployed to the server, applied for a domain name www.favorites.ren for everyone to use, so far: website registered users more than 4000, a total of more than 100,000 articles, in Baidu search: cloud collection, ranked the first is cloud collection official website. In fact, this data is not very dazzling in more than two years, but as an open source software learning Spring Boot, it is already good.

Collection of cloud deployment is a winding road, the beginning of the deployment of company’s server, before I later in ali cloud when leaving the company to purchase a nuclear 1 g cloud server, because install Mysql, Redis, and other small software cause the server is very card, during a visit to cloud collection time need to wait for 2 to 3 seconds will have response.

Finally, one day, I could not bear it, so I spent money to upgrade the server to 2 core 2G. Although the access speed was improved, it was still not ideal. I was busy and had no time to optimize at that time. The site also has a number of bugs, including sudden outages of service for a few hours and the loss of a large number of users. Someone even posted a message on Github saying, “It seems brother Smiler has given up cloud collection. I can only smile after reading it.

When Spring Boot 2.0 comes out this year, I plan to upgrade my cloud favorites to 2.0, with some optimizations to make access faster. However, after two months of delay, I finally took some time to upgrade cloud collection to Spring Boot 2.0 and fix a number of obvious bugs, including the use of Nginx to proxy static images and other resources. When these works are completely finished, the access speed of cloud collection has been significantly improved. You can check it out at www.favorites.ren.

When upgrading cloud collection from Spring Boot 1.0 to 2.0, I also encountered some problems. I recorded them in the process of modification. Today, I sorted them out and shared them to facilitate the subsequent upgrade of friends to step on some pits.

1, the first problem: startup class error

Spring Boot deployed to Tomcat startup need add SpringBootServletInitializer at startup class, 2.0 and 1.0 is there a difference.


     
  1. / / 1.0

  2. import org.springframework.boot.web.support.SpringBootServletInitializer;

  3. / / 2.0

  4. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

  5. @SpringBootApplication

  6. public class UserManageApplication extends SpringBootServletInitializer {

  7.    @Override

  8.    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

  9.        return application.sources(UserManageApplication.class);

  10.    }

  11.    public static void main(String[] args) throws Exception {

  12.        SpringApplication.run(UserManageApplication.class, args);

  13.    }

  14. }

Copy the code

This problem is easy to solve by just redirecting the package.

Spring Boot 2.0 does not include log4j by default. Slf4j is recommended.


     
  1. import org.apache.log4j.Logger;

  2. protected Logger logger = Logger.getLogger(this.getClass());

Copy the code

To:


     
  1. import org.slf4j.Logger;

  2. import org.slf4j.LoggerFactory;

  3. protected Logger logger =  LoggerFactory.getLogger(this.getClass());

Copy the code

This is also easier to change, but there are more files to replace.

Spring Boot 2.0 removes the findOne() method.

FindById (longid) can be added to Spring Boot 2.0’s Repository instead of findOne(), which finds objects based on the passed Id.

Such as:


     
  1. User user=userRepository.findOne(Long id)

Copy the code

Add the findById(longid) method manually in userRepository and change the findOne() call to findById(longid).

                                
     
  1. User user=userRepository .findById( long id)

Copy the code

The delete() method, similar to findOne(), has also been removed and can be replaced with deleteById(Longid), except that deleteById(Longid) returns void by default.

                                    
     
  1. Long deleteById(Long id);

Copy the code

Instead of


     
  1. //delete changes to void

  2. void deleteById(Long id);

Copy the code

Of course, we have a solution to solve the above two changes, is the custom Sql, but without the solution is simply not recommended.


     
  1. @Query("select t from Tag t where t.tagId = :tagId")

  2. Tag getByTagId(@Param("tagId") long tagId);

Copy the code

4. After cloud Collection is upgraded to 2.0, an error will be reported when data is inserted. The error message is as follows:


     
  1. org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [PRIMARY]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

  2. .

  3. Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement

  4. .

  5. Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '299' for key 'PRIMARY'

Copy the code

Spring Boot 2.0 needs to specify an increment policy for the primary key. This is different from Spring Boot 1.0, which uses the default policy.


     
  1. @Id

  2. @GeneratedValue(strategy= GenerationType.IDENTITY)

  3. private long id;

Copy the code

The change is also relatively simple, requiring that the increment policy be specified on all primary keys.

Thymeleaf 3.0 does not include the layout module by default.

This problem is quite embarrassing. When I upgraded the Pom package to 2.0, when I visited the home page, there was nothing, and there was no error message in the background. First, I tried to trace the HTTP request, and I did not find any exception after comparing it, but I found it when I checked the changes of Thymeleaf 3.0: In Spring Boot 2.0, the spring-boot-starter-Thymeleaf package does not contain layout modules by default. You can add layout modules as follows:


     
  1. <dependency>

  2.   <groupId>nz.net.ultraq.thymeleaf</groupId>

  3.   <artifactId>thymeleaf-layout-dialect</artifactId>

  4. </dependency>

Copy the code

After the modification, I went to the home page and found that everything was normal. However, WHEN I checked the log information, I found an alarm message:


     
  1. The 10:47:00 2018-05-10. 1536-029 WARN [nio - 8080 - exec - 2] N.N.U.T.D ecorators. DecoratorProcessor: The layout:decorator/data-layout-decorator processor has been deprecated and will be removed in the next major version of the layout dialect. Please use layout:decorate/data-layout-decorate instead to future-proof your code. See https://github.com/ultraq/thymeleaf-layout-dialect/issues/95 for more information.

  2. The 2018-05-10 10:47:00. 1536-154 WARN [nio - 8080 - exec - 2] N.N.U.T.E xpressions. ExpressionProcessor: Fragment expression "layout" is being wrapped as a Thymeleaf 3 fragment expression (~{... }) for backwards compatibility purposes. This wrapping will be dropped in the next major version of the expression processor, so please rewrite as a Thymeleaf 3 fragment expression to future-proof your code. See https://github.com/thymeleaf/thymeleaf/issues/451 for more information.

Copy the code

Decorator :decorate :decorate :decorator decorator :decorate :decorate

6. The PageRequest component changes.

In Spring Boot 2.0, the method newPageRequest(Page,size,sort) has expired and is no longer recommended. It is recommended to build paging information as follows:


     
  1. Pageable pageable =PageRequest.of(page, size, Sort.by(Sort.Direction.ASC,"id"));

Copy the code

The PageRequest() method still uses newPageRequest(page,size,sort) internally, but the latest version is more concise.

                                                
     
  1. public static PageRequest of(int page, int size, Sort sort ) {

  2. return new PageRequest(page , size, sort);

  3. }

Copy the code

7. The default value of the combination object is changed during associated query.

With Spring Boot 1.0, when using Jpa associative query we would build an interface object to receive the result set, similar to the following:


     
  1. public interface CollectView{

  2.   Long getId();

  3.   Long getUserId();

  4.   String getProfilePicture();

  5.   String getTitle();

  6. }

Copy the code

In Spring Boot 1.0, null is returned if no corresponding field is found. In Spring Boot 2.0, a null pointer exception is reported directly, and the check on the result set is stricter.

8. Other optimizations

Some time ago, I learned Docker and added Docker and Docker Compose support to cloud collection to make the deployment easier. At the same time, some bugs have been fixed, and some functions have been improved to consume resources. Some functions have been added with fault tolerance. In this deployment, Nginx was used as the reverse proxy. Because WebJars were used, Nginx Js could not be used temporarily, so all resources except Js were cached. The database is changed from Mysql to Mariadb.

These are some of the small improvements that cloud Collection has made from Spring Boot 1.0 to 2.0. After doing this, I was pleasantly surprised to find that cloud Collection is much faster than before. Although there is still a lot of room for optimization, daily use will not experience much latency. Thymeleaf uses 3.0 by default in Spring Boot 2.0, and the database connection pool uses Hikari by default. These two components provide significant performance improvements and are also one of the factors that improve cloud collection access speed.

Cloud collection will continue to upgrade in the future, and some new functions for programmers will be planned in the future, so stay tuned!

Recommended reading: Spring Boot,Our first open source software

END