Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | About

5.25. Spring boot with Git version

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

請看下面的解決方案

5.25.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.25.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.25.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 $