分环境
前面提到了你自己开发、给测试小妹妹测试以及给运维人员发布,一共三个环境,而实际上一个软件系统的环境往往不止这些。
常用的环境有:dev、sit、uat、sandbox、pro。
为什么要有这么多环境呢?答案是形势所迫。随着软件开发的分工日益精细化和软件系统的日益复杂化,不同环境所承担的职责不同,但最终目的是一样的:提高效率、保证质量、节约成本、保证收益。
关于分环境的思想这里就不多讲了,下面要讲的一个问题是分环境是如何实现的?
分环境的实现方式有很多Spring Profile、Spring Boot等等都有不同的实现。
下面讲一个使用 maven profiles 实现分环境配置的方式。
比如我在不同的环境需要提供不同的配置文件,怎么实现呢?
首先在pom.xml增加如下几个环境的配置,并指定配置路径:
<profiles>
<!-- 分环境profile> --> <profile> <id>dev</id> <!-- 如果dev带上activeByDefault,会默认将dev下的配置复制到config目录下--> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <env>dev</env> <package.target>dev</package.target> <spring.profiles.active.value>dev</spring.profiles.active.value> <yui.skip>true</yui.skip> <config.path>src/main/resources/config/dev</config.path> </properties> </profile>
<!--sit--> <profile> <id>sit</id> <properties> <env>sit</env> <package.target>sit</package.target> <spring.profiles.active.value>sit</spring.profiles.active.value> <yui.skip>false</yui.skip> <config.path>src/main/resources/config/sit</config.path> </properties> </profile>
<!-- uat --> <profile> <id>uat</id> <properties> <env>uat</env> <package.target>uat</package.target> <spring.profiles.active.value>uat</spring.profiles.active.value> <yui.skip>false</yui.skip> <config.path>src/main/resources/config/uat</config.path> </properties> </profile>
<!--sandbox--> <profile> <id>sandbox</id> <properties> <env>sandbox</env> <package.target>sandbox</package.target> <spring.profiles.active.value>sandbox</spring.profiles.active.value> <yui.skip>false</yui.skip> <config.path>src/main/resources/config/sandbox</config.path> </properties> </profile>
<!--prod--> <profile> <id>prod</id> <properties> <env>prod</env> <package.target>prod</package.target> <spring.profiles.active.value>prod</spring.profiles.active.value> <yui.skip>false</yui.skip> <config.path>src/main/resources/config/prod</config.path> </properties> </profile>
</profiles>
然后在打包项目的时候通过-P
参数指定环境就行了。例如打包uat环境:
$ mvn install -Puat
首先就是慢,也可说浪费咖啡。很多大型项目每次从编译到拉文件都要半个多小时。
那怎么节省发布的时间,让我们早点下班呢?答案就是所有环境一个包。
5个环境就能节省2个小时,太值了!
怎么所有环境一个包呢?
首先在pom.xml配置maven-resources-plugin
插件,并指定copy-resources
的路径,把所有环境的配置都打到包里。
<!-- maven-resources-plugin --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/classes/config</outputDirectory> <resources> <resource> <directory>${config.path}/</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin>
然后正常使用mvn install
打包。
最后把要发布的包复制到指定环境机器的磁盘上以后,通过mv
命令把需要发布的环境的配置移动出来。例如发布sandbox环境:
mv config/sandbox/* config/
当然这个操作不是必须的,比如你在启动容器的时候指定了当前的环境,然后通过${spring.profile.active}
来指定当前读取哪个目录下的配置也可以。
根据每个环境打不同的包,发布一个新的feature到生产需要类似下面这样的流程:
打一个包发布所有环境以后,分支管理模式将改为:
上图演示了多环境多包发布
和多环境单包发布
的简要流程,下面做一下补充说明。
现在解决了打包慢的问题,但是怎么保证运维人员发布的代码版本跟我们功能所在的版本一致呢?
当然可以口头确认,结果就发生了上面的“惨案”。
那么我们能不能自己检查呢?那就要借助工具了。
git-commit-id-plugin
是一个插件,会根据当前分支的版本号生成一个git.properties
文件。
首先在pom.xml引入插件配置:
<!-- https://github.com/git-commit-id/maven-git-commit-id-plugin --><plugin> <groupId>pl.project13.maven</groupId> <artifactId>git-commit-id-plugin</artifactId> <version>${git-commit-id-plugin.version}</version> <executions> <execution> <id>get-the-git-infos</id> <goals> <goal>revision</goal> </goals> </execution> </executions> <configuration> <!-- 使properties扩展到整个maven bulid 周期, Ref: https://github.com/ktoso/maven-git-commit-id-plugin/issues/280 --> <injectAllReactorProjects>true</injectAllReactorProjects> <dateFormat>yyyy.MM.dd HH:mm:ss</dateFormat> <verbose>true</verbose> <!-- 是否生 git.properties 属性文件 --> <generateGitPropertiesFile>true</generateGitPropertiesFile> <!--git描述配置,可选;由JGit提供实现; --> <gitDescribe> <!--是否生成描述属性 --> <skip>false</skip> <!--提交操作未发现tag时,仅打印提交操作ID --> <always>false</always> <!--提交操作ID显式字符长度,最大值为:40;默认值:7; 0代表特殊意义;--> <abbrev>7</abbrev> <!--构建触发时,代码有修改时(即"dirty state"),添加指定后缀;默认值:""; --> <dirty>-dirty</dirty> <forceLongFormat>false</forceLongFormat> </gitDescribe> </configuration></plugin>
接着打包项目,你就可以看到自己编译的文件下面多了一个git.properties
文件:
#Generated by Git-Commit-Id-Plugin#Sat Apr 20 13:08:01 CST 2019git.branch=mastergit.build.host=DESKTOP-12GT5DQgit.build.time=2019.04.20 13\:08\:01git.build.user.email=ijiangtao@foxmail.comgit.build.user.name=ijiangtaogit.build.version=1.1.01.01-SNAPSHOTgit.closest.tag.commit.count=git.closest.tag.name=git.commit.id=67b60eeffa9deca877c2b9a28d52a40d3ea55444git.commit.id.abbrev=67b60eegit.commit.id.describe=67b60eegit.commit.id.describe-short=67b60eegit.commit.message.full=\u53D1\u9001\u91CD\u8981\u6570\u636E\u7ED9\u8001\u677F~~~~git.commit.message.short=\u53D1\u9001\u91CD\u8981\u6570\u636E\u7ED9\u8001\u677F~~~~git.commit.time=2019.04.20 12\:57\:50git.commit.user.email=ijiangtao@foxmail.comgit.commit.user.name=ijiangtaogit.dirty=falsegit.remote.origin.url=https\://github.com/javastudydemo/jsd-maven.gitgit.tags=git.total.commit.count=2
有了这个文件,我们就可以清晰地知道生产环境的代码是什么版本了。
需要特别注意的是,使用这个插件要保证你编译的项目是有.git目录的,因为这个插件要获取git的提交信息,如果不使用git进行版本管理的项目,编译会报错。
下面提供一个Controller来展示git的提交信息。
package net.ijiangtao.tech.maven.web.controller;
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.serializer.SerializerFeature;import com.wordnik.swagger.annotations.Api;import com.wordnik.swagger.annotations.ApiOperation;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
/** * @author ijiangtao */@Controller@Api(value = "", description = "git info from git-commit-id-plugin")public class GitCommitController {
/** * @return */ @RequestMapping("/git/commit/info") @ResponseBody @ApiOperation(value = "get commit info", httpMethod = "GET") public String showGitCommitInfo() { //git.properties ResourceBundle resourceBundle = ResourceBundle.getBundle("git", defaultIfNull(null, Locale.getDefault()));
Map<String, String> map = new TreeMap<>();
Enumeration<String> keysEnumeration = resourceBundle.getKeys();
while (keysEnumeration.hasMoreElements()) { String key = keysEnumeration.nextElement();
map.put(key, resourceBundle.getString(key)); }
return JSON.toJSONString(map, SerializerFeature.PrettyFormat); }
/** * @return */ @ApiOperation(value = "get commit id", httpMethod = "GET") @RequestMapping("/git/commit/id") @ResponseBody public String showGitCommitId() { //git.properties ResourceBundle resourceBundle = ResourceBundle.getBundle("git", defaultIfNull(null, Locale.getDefault())); return resourceBundle.getString("git.commit.id"); }
}
通过Jetty插件启动项目,并访问SwaggerUI
地址 http://localhost:8241/swagger/index.html ,最后通过http://localhost:8241/git/commit/info
请求到了我们的git提交信息。
一般我们为了版本回滚的方便,发布的时候会通过git commit id进行打包,可以通过机器对比两者是否一致,达到自动检查的目的,而不是每次需要人工检查。