給你們聊一聊雲收藏從 Spring Boot 1.0 升級到 2.0 所踩的坑java
先給你們曬一下雲收藏的幾個數據,做爲一個 Spring Boot 的開源項目(https://github.com/cloudfavorites/favorites-web)目前在 Github 上面已經有1600多個 Star,若是按照 SpringBoot 標籤進行篩選的話也能夠排到第五位。mysql
當雲收藏1.0開發完成以後,同步將雲收藏部署到了服務器上,申請了一個域名www.favorites.ren方便你們使用,到目前爲止:網站的註冊用戶4000多人,共計收藏文章100000多條,在百度上搜索:雲收藏,排在第一的就是雲收藏的官網。2年多的時間這個數據其實也並非很耀眼,可是做爲一個學習 Spring Boot 的開源軟件來說,已經不錯了。git
雲收藏的部署之路也挺曲折,剛開始的時候部署在我之前公司的服務器上,後來離職的時候在阿里雲買了個1核1G的雲服務器,由於安裝了 Mysql、Redis、還有其它小軟件致使服務器很是卡,那段時間訪問雲收藏的時候須要等待2-3秒纔會有響應。程序員
終於有一天本身也不能忍了,花錢把服務器升級到2核2G,訪問速度雖有所提高但仍是很不理想,那段時間工做很忙也沒時間優化。網站的 Bug 也是一片,有時候還會忽然中斷服務幾個小時,流失了一大批用戶,甚至有人在 Github 上面留言說:看來微笑哥已經放棄雲收藏了,我看了以後只能苦笑。github
到了今年 Spring Boot 2.0 發佈的時候,我就計劃着把雲收藏全面升級到2.0,順便作一些優化讓訪問速度快一點。但一拖就是2個月,終於在前幾個週末抽出了一點時間,將雲收藏升級到了 Spring Boot 2.0 同時修復了一批顯而易見的 Bug ,使用 Nginx 將靜態圖片等資源作了代理,當這些工做徹底作完的時候,雲收藏的訪問速度明顯獲得了提高,你們能夠訪問www.favorites.ren體驗一下。web
將雲收藏從 Spring Boot 1.0 升級到 2.0 的時候也遇到了一些問題,在修改的過程當中記錄下來,今天整理一下分享出來,方便後續升級的朋友少踩一些坑。spring
一、第一個問題:啓動類報錯sql
Spring Boot 部署到 Tomcat 中去啓動時須要在啓動類添加SpringBootServletInitializer
,2.0 和 1.0 有區別。數據庫
// 1.0 import org.springframework.boot.web.support.SpringBootServletInitializer; // 2.0 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class UserManageApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(UserManageApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(UserManageApplication.class, args); } }
這個問題好解決只須要從新導包就行。express
二、日誌類報錯:Spring Boot 2.0 默認不包含 log4j,建議使用 slf4j 。
import org.apache.log4j.Logger; protected Logger logger = Logger.getLogger(this.getClass());
改成:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; protected Logger logger = LoggerFactory.getLogger(this.getClass());
這個也比較好改動,就是須要替換的文件比較多。
三、Spring Boot 2.0 去掉了findOne()
方法。
之前的findOne()
方法其實就是根據傳入的 Id 來查找對象,因此在 Spring Boot 2.0 的 Repository 中咱們能夠添加findById(long id)
來替換使用。
例如:
User user=userRepository.findOne(Long id)
改成手動在userRepository
手動添加findById(long id)
方法,使用時將findOne()
調用改成findById(long id)
User user=userRepository.findById(long id)
delete()
方法和findOne()
相似也被去掉了,可使用deleteById(Long id)
來替換,還有一個不一樣點是deleteById(Long id)
默認實現返回值爲void
。
Long deleteById(Long id);
改成
//delete 改成 void 類型 void deleteById(Long id);
固然咱們還有一種方案能夠解決上述的兩種變化,就是自定義 Sql,可是沒有上述方案簡單不建議使用。
@Query("select t from Tag t where t.tagId = :tagId") Tag getByTagId(@Param("tagId") long tagId);
四、雲收藏升級到 2.0 以後,插入數據會報錯,錯誤信息以下:
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [PRIMARY]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement .... Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement ... Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '299' for key 'PRIMARY'
這個問題稍稍花費了一點時間,報錯提示的是主鍵衝突,跟蹤數據庫的數據發現並無主鍵衝突,最後才發現是 Spring Boot 2.0 須要指定主鍵的自增策略,這個和 Spring Boot 1.0 有所區別,1.0 會使用默認的策略。
@Id @GeneratedValue(strategy= GenerationType.IDENTITY) private long id;
改動也比較簡單,須要在全部的主鍵上面顯示的指明自增策略。
五、Thymeleaf 3.0 默認不包含佈局模塊。
這個問題比較尷尬,當我將 Pom 包升級到 2.0 以後,訪問首頁的時候一片空白什麼都沒有,查看後臺也沒有任何的報錯信息,首先嚐試着跟蹤了 http 請求,對比了一下也沒有發現什麼異常,在查詢 Thymeleaf 3.0 變化時才發現:Spring Boot 2.0 中spring-boot-starter-thymeleaf
包默認並不包含佈局模塊,須要使用的時候單獨添加,添加布局模塊以下:
<dependency> <groupId>nz.net.ultraq.thymeleaf</groupId> <artifactId>thymeleaf-layout-dialect</artifactId> </dependency>
改完以後再訪問首頁,一切正常,可是回頭查看日誌信息發現有一個告警信息:
2018-05-10 10:47:00.029 WARN 1536 --- [nio-8080-exec-2] n.n.u.t.decorators.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. 2018-05-10 10:47:00.154 WARN 1536 --- [nio-8080-exec-2] n.n.u.t.expressions.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.
跟蹤地址看了一下,大概的意思是之前佈局的標籤已通過期了,推薦使用新的標籤來進行頁面佈局,解決方式也比較簡單,修改之前的佈局標籤 layout:decorator
爲 layout:decorate
便可。
六、分頁組件PageRequest
變化。
在 Spring Boot 2.0 中 ,方法new PageRequest(page, size, sort)
已通過期再也不推薦使用,推薦使用如下方式來構建分頁信息:
Pageable pageable =PageRequest.of(page, size, Sort.by(Sort.Direction.ASC,"id"));
跟蹤了一下源碼發現PageRequest.of()
方法,內部仍是使用的new PageRequest(page, size, sort)
,只是最新的寫法更簡潔一些。
public static PageRequest of(int page, int size, Sort sort) { return new PageRequest(page, size, sort); }
七、關聯查詢時候組合返回對象的默認值有變化。
在使用 Spring Boot 1.0 時,使用 Jpa 關聯查詢時咱們會構建一個接口對象來接收結果集,相似以下:
public interface CollectView{ Long getId(); Long getUserId(); String getProfilePicture(); String getTitle(); }
在使用 Spring Boot 1.0 時,若是沒有查詢到對應的字段會返回空,在 Spring Boot 2.0 中會直接報空指針異常,對結果集的檢查會更加嚴格一些。
八、其它優化
前段時間在學習 Docker ,給雲收藏添加了 Docker 、Docker Compose 支持讓部署的時候更簡單一些;同時修復了一些 bug,對於明顯很消耗資源的功能進行了改進,部分功能添加了容錯性;本次部署的時候使用了 Nginx 做爲反向代理,由於使用了 WebJars 暫時不能使用 Nginx 代理 Js,因此將除過 Js 之外的其它資源都配置了緩存,;數據庫由 Mysql 換成了 Mariadb。
以上就是雲收藏從 Spring Boot 1.0 到 2.0 所作的一些小改進,作完這些工做以後驚喜的發現雲收藏的訪問速度比之前快了不少,雖然還有很大的優化空間,但平常使用基本上不會體驗到太大的延遲。Spring Boot 2.0 中 Thymeleaf 默認使用了 3.0 ,數據庫鏈接池默認使用了 Hikari ,這兩個組件在性能上有很大的提高,同時也是提高雲收藏訪問速度的因素之一。
將來雲收藏還會持續升級,後續會規劃一些面向程序員的新功能,敬請期待!