通过maven把Java服务的打包时间和版本配置到Eureka中

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();
}

页面效果:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值