maven 打包普通java配置_通过maven把Java服务的打包时间和版本配置到Eureka中-Go语言中文社区...

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

}

页面效果:

108003c063acd4b0895067154fd74ca4.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值