1414 */
1515package com .aizuda .snailjob .server ;
1616
17+ import cn .hutool .core .util .StrUtil ;
1718import com .aizuda .snailjob .server .common .rpc .server .grpc .GrpcServer ;
1819import lombok .extern .slf4j .Slf4j ;
1920import org .springframework .boot .ApplicationRunner ;
3334public class SnailJobServerApplication {
3435
3536 public static void main (String [] args ) {
36- TimeZone .setDefault (TimeZone .getTimeZone ("Asia/Shanghai" ));
37+ // 初始化默认时区
38+ initDefaultTimeZone ();
39+ // 启动SnailJob服务
3740 SpringApplication .run (SnailJobServerApplication .class , args );
3841 }
3942
@@ -47,7 +50,7 @@ public ApplicationRunner nettyStartupChecker(GrpcServer grpcServer,
4750 ServletWebServerFactory serverFactory ) {
4851 return args -> {
4952 // 判定Grpc
50- boolean started = grpcServer .isStarted ();
53+ boolean started = grpcServer .isStarted ();
5154 // 最长自旋10秒,保证 grpcHttpServer启动完成
5255 int waitCount = 0 ;
5356 while (!started && waitCount < 100 ) {
@@ -65,4 +68,27 @@ public ApplicationRunner nettyStartupChecker(GrpcServer grpcServer,
6568 }
6669 };
6770 }
71+
72+ /**
73+ * 从环境变量读取时区配置,如果没有则使用系统默认时区
74+ * fixed 避免固定上海时区导致部署国际化服务的机房时,时区如何设置调整都为上海时区时间进行触发的问题
75+ */
76+ private static void initDefaultTimeZone () {
77+ String timezone = System .getenv ("TZ" );
78+
79+ if (StrUtil .isNotBlank (timezone )) {
80+ try {
81+ TimeZone .setDefault (TimeZone .getTimeZone (timezone .trim ()));
82+ log .info ("--------> System timezone set to: {} from environment variable TZ" , timezone );
83+ } catch (Exception e ) {
84+ log .warn ("--------> Invalid timezone '{}' from environment variable TZ, " +
85+ "using system default timezone. Error: {}" ,
86+ timezone , e .getMessage ());
87+ }
88+ return ;
89+ }
90+
91+ log .info ("--------> Environment variable TZ not set, using system default timezone: {}" ,
92+ TimeZone .getDefault ().getID ());
93+ }
6894}
0 commit comments