Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | Email

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 $