Spring boot with Git version
阿新 • • 發佈:2022-05-02
本文節選自《Netkiller Java 手札》
5.23. Spring boot with Git version
Spring boot 每次升級打包發給運維操作,常常運維操作不當致使升級失敗,開發怎樣確認線上的jar/war包與升級包一致呢?
請看下面的解決方案
5.23.1. CommonRestController 公共控制器
所有 RestController將會整合 CommonRestController
package cn.netkiller.api.rest; import org.springframework.http.HttpStatus; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus; public class CommonRestController { @RequestMapping("ping") @ResponseStatus(HttpStatus.OK) public String welcome() { return "PONG"; } @RequestMapping("commit") public String commit() { return "$Id$"; } @RequestMapping("auth") @ResponseStatus(HttpStatus.OK) public String auth(@AuthenticationPrincipal final UserDetails user) { return String.format("%s: %s %s", user.getUsername(), user.getPassword(), user.getAuthorities()); } }
5.23.2. VersionRestController 測試控制器
我們建立一個RestController並繼承CommonRestController用來測試
package cn.netkiller.api.rest; @RestController @RequestMapping("/public/version") public class VersionRestController extends CommonRestController { private static final Logger logger = LoggerFactory.getLogger(VersionRestController.class); public VersionRestController() { // TODO Auto-generated constructor stub } @RequestMapping("welcome") @ResponseStatus(HttpStatus.OK) public String welcome() { return "Welcome to RestTemplate version 1.0."; } }
5.23.3. 建立 .gitattributes 檔案
# vim .gitattributes
src/main/java/cn/netkiller/api/rest/CommonRestController.java ident
使用curl命令呼叫commit介面可以顯示當前war/jar最後一次提交的版本號碼(你同樣可以使用IE瀏覽器)
curl https://api.netkiller.cn/public/version/commit.json
$Id: 929bc9e4c90b4d68c25dc693618f23b33fd6ba0f $