1、在主pom.xml文件中配置时间戳和版本信息
1.0.0
版本信息还有待优化,需要改为放到每个jar中的pom文件中;
yyyy-MM-dd HH:mm:ss
${maven.build.timestamp}
时间戳是使用maven自带的时间戳,时区是GMT;北京时间=GMT时间+8小时
在网上找了两个可以使用GMT+8的时间戳,具体如下,但是都没达到GMT+8的效果,还有待研究;
a、使用buildnubmer-maven-plugin;
org.codehaus.mojo
buildnumber-maven-plugin
1.4
yyyyMMdd
create-timestamp
false
默认属性是timestamp,在打包plugin中引用该属性
${project.artifactId}-${project.version}_${timestamp}
b、使用build-helper-maven-plugin;
ProjectName-${current.time}
org.codehaus.mojo
build-helper-maven-plugin
1.9.1
timestamp-property
timestamp-property
current.time
yyyyMMdd-HHmmss
GMT+8
2、在application.yml文件中配置Eureka
eureka:
client:
serviceUrl:
defaultZone: http://${app.config.eureka-server-host}:${app.config.eureka-server-port}/eureka/
healthcheck:
enabled: false
instance:
preferIpAddress: true
instance-id: ${spring.cloud.client.ipAddress}:${spring.application.name}:${server.port}:'@project.version@':'@time@'
metadata-map.time: '@time@'
metadata-map.version: '@project.version@'
其中metadata-map.time、metadata-map.version是Eureka的自定义元数据,在Eureka中使用如下:
DiscoveryClientInstanceInfo(final ServiceInstance instance) {
this.host = instance.getHost();
this.port = instance.getPort();
this.url = instance.getUri().toString();
this.version = instance.getMetadata().get("version");
if (instance.getMetadata() != null && instance.getMetadata().containsKey("time")) {
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
this.time = simple.format(modifyTime(instance.getMetadata().get("time")));
}
}
GMT时间格式转GMT+8时间方法如下:
private Date modifyTime(String date) {
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date oldDate = null;
try {
oldDate = simple.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(oldDate);
calendar.add(Calendar.HOUR_OF_DAY, +8);
return calendar.getTime();
}
页面效果: