Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

2.15. Spring boot with Git version

Spring boot 每次升級打包發給運維操作,常常運維操作不當致使升級失敗,開發怎樣確認線上的jar/war包與升級包一致呢?

請看下面的解決方案

2.15.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());
	}
}
			
			

2.15.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.";
	}

}
			
			

2.15.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 $