【注意】最后更新于 February 4, 2019,文中内容可能已过时,请谨慎使用。
Spring Boot Gradle Plugin简介
Spring Boot Gradle Plugin提供了Gradle构建工具对Spring Boot的支持,使用它不仅可以完成Spring Boot项目的打包(executable jar or distribution tar)、运行,还可以使用spring-boot-dependencies
提供的依赖管理功能。
如果你选择Spring Boot作为Java语言开发的微服务的开发框架的话,熟练使用Spring Boot Gradle Plugin,可以更好的管理微服务项目的工程化和构建,更进一步可以基于Spring Boot扩展出自己团队的微服务框架,基于Spring Boot Gradle Plugin扩展出团队自己的微服务Gradle插件,结合Jenkins等CI/CD系统以及Kubernetes这样的容器云平台,就可以更好的开发、构建、发布和治理和运维微服务。
Spring Boot Gradle Plugin Reference Guide是Spring Boot Gradle Plugin的官方文档,这里大致列举一些重点的内容,更多的内容可查看文档。
微服务运行时监控其构建信息
微服务架构有很多的优点,同时也有很多挑战。关于微服务的治理有很多公共的关注点,在微服务的监控上我们需要各式各样的Metrics指标,其中除了服务运行时的各种动态信息外
(如:服务运行时的性能指标、JVM信息、资源使用率等等),还需要可以随时了解服务的一些在构建时的静态信息(如服务的版本、服务基础框架的版本等等),这些静态信息也是十分重要的,例如微服务的基础框架某个版本中的某个依赖存在安全漏洞,就需要升级使用该版本基础框架的所有微服务,如果能从监控中得到这些信息,就可以轻松从线上几十或上百个微服务中找到受影响的那些服务了。
使用spring boot gradle plugin可以将这些构建信息在打包时写入到`META-INF/build-info.properties中,Spring Boot Actuator可以将它们作为Metrics暴露。
这里做一个测试,首先从https://start.spring.io生成一个基于Gradle的Spring Boot 2.1.x的项目,添加了Spring Boot Web和Spring Boot Actuator的依赖。
在build.gradle中加入如下内容:
1
2
3
4
5
6
7
|
ext {
baseFrameworkVersion = '0.1'
}
springBoot {
buildInfo()
}
|
使用gradle build -x test
构建项目,可以发现生成了文件build/resources/main/META-INF/build-info.properties
:
1
2
3
4
5
6
|
build.version=0.0.1-SNAPSHOT
build.baseFrameworkVersion=0.1
build.group=com.example
build.name=demo
build.artifact=demo
build.time=2019-02-04T14\:25\:18.302931Z
|
下面加入actuator对prometheus的支持:
build.gradle加:
1
|
implementation 'io.micrometer:micrometer-registry-prometheus'
|
application.yml加入:
1
2
3
4
5
6
7
8
9
|
management:
endpoints:
web:
exposure:
include: "health,info,prometheus"
server:
port: 8079
servlet:
context-path: /
|
重新打包并运行打包完成的jar包:
1
2
3
4
5
6
7
|
gradle clean build -x test
java -jar build/libs/demo-0.0.1-SNAPSHOT.jar
curl http://localhost:8079/actuator/info
{"build":{"version":"0.0.1-SNAPSHOT","artifact":"demo","baseFrameworkVersion":"0.1","name":"demo","time":"2019-02-04T14:52:09.837Z","group":"com.example"}}
|
可以看到经过上面的配置,打包运行时build info已经可以在/actuator/info
这个端点查看,我们使用的监控系统是Prometheus,所以还需要以Prometheus exporter metrics的形式暴露,这需要我们定制开发一个micrometer的MeterBinder。
下面编写micrometer的MeterBinder即BuildInfoMetrics,用来将build info暴露给Prometheus:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package com.example.demo;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.MeterBinder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.info.BuildProperties;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnResource(resources = {"META-INF/build-info.properties"})
public class BuildInfoMetrics implements MeterBinder {
@Autowired
private BuildProperties buildProperties;
@Override
public void bindTo(MeterRegistry registry) {
Gauge.builder("buildInfo", 1, c -> 1)
.tag("version", buildProperties.getVersion())
.tag("group", buildProperties.getGroup())
.tag("group", buildProperties.getArtifact())
.tag("baseFrameworkVersion", buildProperties.get("baseFrameworkVersion"))
.description("the build info").register(registry);
}
}
|
上面的代码,当META-INF/build-info.properties
存在时,Spring会自动注入BuildProperties
这个Bean给我们使用,注意这里使用了@ConditionalOnResource(resources = {"META-INF/build-info.properties"})
来避免IDE本地开发环境中没有META-INF/build-info.properties
文件的问题,因为这个文件是gradle build时才会生成。
重新打包并运行打包完成的jar包:
1
2
3
4
5
6
7
8
9
10
11
|
gradle clean build -x test
java -jar build/libs/demo-0.0.1-SNAPSHOT.jar
curl http://localhost:8079/actuator/prometheus
......
# HELP buildInfo the build info
# TYPE buildInfo gauge
buildInfo{baseFrameworkVersion="0.1",group="demo",version="0.0.1-SNAPSHOT",} 1.0
......
|
参考