1、在主pom.xml文件中配置时间戳和版本信息
<version>1.0.0</version>
版本信息还有待优化,需要改为放到每个jar中的pom文件中;
<properties>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
<time>${maven.build.timestamp}</time>
</properties>
时间戳是使用maven自带的时间戳,时区是GMT;北京时间=GMT时间+8小时
在网上找了两个可以使用GMT+8的时间戳,具体如下,但是都没达到GMT+8的效果,还有待研究;
a、使用buildnubmer-maven-plugin;
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<timestampFormat>yyyyMMdd</timestampFormat>
</configuration>
<executions>
<execution>
<goals>
<goal>create-timestamp</goal>
</goals>
</execution>
</executions>
<inherited>false</inherited>
</plugin>
默认属性是timestamp,在打包plugin中引用该属性
<finalName>
${project.artifactId}-${project.version}_${timestamp}
</finalName>
b、使用build-helper-maven-plugin;
<build>
<finalName>ProjectName-${current.time}</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>timestamp-property</id>
<goals>
<goal>timestamp-property</goal>
</goals>
</execution>
</executions>
<configuration>
<name>current.time</name>
<pattern>yyyyMMdd-HHmmss</pattern>
<timeZone>GMT+8</timeZone>
</configuration>
</plugin>
</plugins>
</build>
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();
}
页面效果: