问题
在大型组织中,一个中间件可能需要维护多个版本,不同的业务线使用了不同版本,而且可能长期不升级。这样就带来一个严重的问题:如果忽然发现某个bug影响了多个中间件版本,如何确认那些业务线需要进行升级修复呢?
方案1
维护一个列表,详细记录哪个业务线使用了哪个版本。
缺点: 列表维护工作量巨大,时间长了是否还跟真实情况同步难以保证。
方案2
包扫描,利用扫描工具,搜索各个业务线的代码,找出使用了目标中间件的业务。
缺点: 扫描需要比较长的时间,且获取所有业务线的代码也可能有公司制度上的障碍。
方案3
利用Spring Boot功能。
- 步骤一:POM里添加spring-boot-maven-plugin,并加入goal。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
- 步骤二:启用actuator
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 步骤三:重新编译、打包
mvn package
- 步骤四:调用info接口(http://localhost:8080/actuator/info)
{
"build": {
"version": "0.0.1-SNAPSHOT",
"artifact": "demo1",
"name": "demo1",
"group": "com.example",
"time": "2019-01-31T03:21:59.207Z"
}
}
可以看到包名和版本信息都有了。
但是大家需要注意,这个方案其实对Spring Boot程序有效,并不是针对中间件的。
针对中间件,还需要进行如下改进:
- 步骤五:在插件的配置里指定版本信息文件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<outputFile>${project.build.outputDirectory}/META-INF/demo-info.properties</outputFile>
</configuration>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
- 步骤六:自定义版本信息Bean
这个有点长,建议大家参考spring-boot-actuator-autoconfigure包里的InfoContributorAutoConfiguration类的做法。
核心思想就是,读取前面步骤五生成的文件,注入一个info Bean。