全球机房遇到的 Date类型 比较时间区间解决方案

 

 

日本机房展示:

************************日本机房**********************



//查询当前时间对应的时区
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | JST    |
| time_zone        | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)




//获取当前时间戳:
mysql> SELECT UNIX_TIMESTAMP(NOW());
+-----------------------+
| UNIX_TIMESTAMP(NOW()) |
+-----------------------+
|            1619603014 |
+-----------------------+
1 row in set (0.00 sec)


//
mysql> select * from XXXX    where game_id =5643 order by version asc;
+-----+--------+---------+-------------+-----------+---------------------+---------------------+---------------------+---------------------+--------+---------+-----------------+
| id  | status | XXXXXXX| limit_state | limit_num | begin_time          | end_time            | ctime               | mtime               | source | version | remark          |
+-----+--------+---------+-------------+-----------+---------------------+---------------------+---------------------+---------------------+--------+---------+-----------------+
|  89 |      1 |    5643 |           1 |         5 | 2021-03-30 14:00:00 | 2021-03-30 15:00:00 | 2021-03-30 12:47:09 | 2021-03-30 12:47:09 |      5 |       1 | 管理台配置      |
|  90 |      1 |    5643 |           1 |         5 | 2021-03-30 15:32:04 | 2021-03-30 16:32:06 | 2021-03-30 15:32:21 | 2021-03-30 15:32:21 |      5 |       2 | 管理台配置      |
|  91 |      1 |    5643 |           1 |         5 | 2021-03-30 16:34:20 | 2021-03-30 17:34:22 | 2021-03-30 16:34:24 | 2021-03-30 16:35:01 |      5 |       3 | 管理台配置      |
|  92 |      0 |    5643 |           1 |         5 | 2021-03-30 18:18:24 | 2021-03-31 00:45:59 | 2021-03-30 17:46:41 | 2021-03-30 18:18:24 |      5 |       4 | 管理台配置      |
|  93 |      0 |    5643 |           1 |         3 | 2021-03-30 18:43:32 | 2021-03-31 12:00:00 | 2021-03-30 18:18:54 | 2021-03-30 18:43:32 |      5 |       5 | 管理台配置      |
|  94 |      0 |    5643 |           0 |         5 | 2021-03-30 20:01:55 | 2021-03-30 21:43:35 | 2021-03-30 18:43:47 | 2021-03-30 20:01:55 |      5 |       6 | 管理台配置      |
|  95 |      0 |    5643 |           1 |         3 | 2021-03-31 11:08:46 | 2021-03-31 19:02:00 | 2021-03-30 20:02:15 | 2021-03-31 11:08:46 |      5 |       7 | 管理台配置      |
|  96 |      0 |    5643 |           1 |         5 | 2021-04-13 17:16:55 | 2021-04-13 20:51:13 | 2021-04-13 15:51:30 | 2021-04-13 17:16:55 |      5 |       8 | 管理台配置      |
|  97 |      0 |    5643 |           1 |         4 | 2021-04-13 18:04:07 | 2021-04-13 20:00:00 | 2021-04-13 17:17:27 | 2021-04-13 18:04:07 |      5 |       9 | 管理台配置      |
| 117 |      0 |    5643 |           1 |         4 | 2021-04-22 18:17:50 | 2021-04-22 18:00:00 | 2021-04-22 15:55:10 | 2021-04-22 18:17:50 |      5 |      10 | 管理台配置      |
| 118 |      0 |    5643 |           1 |         3 | 2021-04-22 18:17:47 | 2021-04-22 20:00:00 | 2021-04-22 15:55:45 | 2021-04-22 18:17:47 |      5 |      11 | 管理台配置      |
| 124 |      0 |    5643 |           1 |         2 | 2021-04-22 18:33:08 | 2021-04-22 21:19:22 | 2021-04-22 18:20:25 | 2021-04-22 18:33:08 |      5 |      12 | 管理台配置      |
| 126 |      0 |    5643 |           1 |         2 | 2021-04-22 19:31:12 | 2021-04-22 20:21:31 | 2021-04-22 19:09:11 | 2021-04-22 19:31:12 |      5 |      13 | 管理台配置      |
| 127 |      1 |    5643 |           1 |      2146 | 2021-04-22 19:31:41 | 2021-04-30 00:10:15 | 2021-04-22 19:31:59 | 2021-04-26 21:58:51 |      5 |      14 | 管理台配置      |
+-----+--------+---------+-------------+-----------+---------------------+---------------------+---------------------+---------------------+--------+---------+-----------------+
14 rows in set (0.01 sec)

 

时间戳(Timestamp)

时间(Date)

1.数据库的时间会存在差异(原因:数据库的时区不一样)     

2.但是将其转化成时间戳的话在两个机房并不会有差异

阿里的DTS同步时间只会在展示层面使用日本时区来展示当时时间,时间戳是不变的.

 

************************日本机房**********************

//将本次连接的时区设置成 +8:00  东八区

mysql> set time_zone = '+8:00';
Query OK, 0 rows affected (0.00 sec)


//查看对应的时区

mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | JST    |
| time_zone        | +08:00 |
+------------------+--------+
2 rows in set (0.00 sec)


//直接查看时间戳,发现时间戳基本一致 

mysql> SELECT UNIX_TIMESTAMP(NOW());
+-----------------------+
| UNIX_TIMESTAMP(NOW()) |
+-----------------------+
|            1619603056 |
+-----------------------+
1 row in set (0.00 sec)



//
mysql> select * from xxxxxx   where game_id =5643 order by version asc;
+-----+--------+---------+-------------+-----------+---------------------+---------------------+---------------------+---------------------+--------+---------+-----------------+
| id  | status | xxxxxx| limit_state | limit_num | begin_time          | end_time            | ctime               | mtime               | source | version | remark          |
+-----+--------+---------+-------------+-----------+---------------------+---------------------+---------------------+---------------------+--------+---------+-----------------+
|  89 |      1 |    5643 |           1 |         5 | 2021-03-30 13:00:00 | 2021-03-30 14:00:00 | 2021-03-30 11:47:09 | 2021-03-30 11:47:09 |      5 |       1 | 管理台配置      |
|  90 |      1 |    5643 |           1 |         5 | 2021-03-30 14:32:04 | 2021-03-30 15:32:06 | 2021-03-30 14:32:21 | 2021-03-30 14:32:21 |      5 |       2 | 管理台配置      |
|  91 |      1 |    5643 |           1 |         5 | 2021-03-30 15:34:20 | 2021-03-30 16:34:22 | 2021-03-30 15:34:24 | 2021-03-30 15:35:01 |      5 |       3 | 管理台配置      |
|  92 |      0 |    5643 |           1 |         5 | 2021-03-30 17:18:24 | 2021-03-30 23:45:59 | 2021-03-30 16:46:41 | 2021-03-30 17:18:24 |      5 |       4 | 管理台配置      |
|  93 |      0 |    5643 |           1 |         3 | 2021-03-30 17:43:32 | 2021-03-31 11:00:00 | 2021-03-30 17:18:54 | 2021-03-30 17:43:32 |      5 |       5 | 管理台配置      |
|  94 |      0 |    5643 |           0 |         5 | 2021-03-30 19:01:55 | 2021-03-30 20:43:35 | 2021-03-30 17:43:47 | 2021-03-30 19:01:55 |      5 |       6 | 管理台配置      |
|  95 |      0 |    5643 |           1 |         3 | 2021-03-31 10:08:46 | 2021-03-31 18:02:00 | 2021-03-30 19:02:15 | 2021-03-31 10:08:46 |      5 |       7 | 管理台配置      |
|  96 |      0 |    5643 |           1 |         5 | 2021-04-13 16:16:55 | 2021-04-13 19:51:13 | 2021-04-13 14:51:30 | 2021-04-13 16:16:55 |      5 |       8 | 管理台配置      |
|  97 |      0 |    5643 |           1 |         4 | 2021-04-13 17:04:07 | 2021-04-13 19:00:00 | 2021-04-13 16:17:27 | 2021-04-13 17:04:07 |      5 |       9 | 管理台配置      |
| 117 |      0 |    5643 |           1 |         4 | 2021-04-22 17:17:50 | 2021-04-22 17:00:00 | 2021-04-22 14:55:10 | 2021-04-22 17:17:50 |      5 |      10 | 管理台配置      |
| 118 |      0 |    5643 |           1 |         3 | 2021-04-22 17:17:47 | 2021-04-22 19:00:00 | 2021-04-22 14:55:45 | 2021-04-22 17:17:47 |      5 |      11 | 管理台配置      |
| 124 |      0 |    5643 |           1 |         2 | 2021-04-22 17:33:08 | 2021-04-22 20:19:22 | 2021-04-22 17:20:25 | 2021-04-22 17:33:08 |      5 |      12 | 管理台配置      |
| 126 |      0 |    5643 |           1 |         2 | 2021-04-22 18:31:12 | 2021-04-22 19:21:31 | 2021-04-22 18:09:11 | 2021-04-22 18:31:12 |      5 |      13 | 管理台配置      |
| 127 |      1 |    5643 |           1 |      2146 | 2021-04-22 18:31:41 | 2021-04-29 23:10:15 | 2021-04-22 18:31:59 | 2021-04-26 20:58:51 |      5 |      14 | 管理台配置      |
+-----+--------+---------+-------------+-----------+---------------------+---------------------+---------------------+---------------------+--------+---------+-----------------+
14 rows in set (0.00 sec)

 

因此可以明确和MySQL问题无关。转而研究 Date.getTime()方法

https://blog.csdn.net/levae1024/article/details/78607015

 

类  :abstract public class TimeZone implements Serializable, Cloneable
包名:rt.jar



static TimeZone getDefaultRef() {
        TimeZone defaultZone = defaultTimeZone;
        if (defaultZone == null) {
            // Need to initialize the default time zone.
            defaultZone = setDefaultZone();
            assert defaultZone != null;
        }
        // Don't clone here.
        return defaultZone;
    }

    private static synchronized TimeZone setDefaultZone() {
        TimeZone tz;
        // get the time zone ID from the system properties
        String zoneID = AccessController.doPrivileged(
                new GetPropertyAction("user.timezone"));

        // if the time zone ID is not set (yet), perform the
        // platform to Java time zone ID mapping.
        if (zoneID == null || zoneID.isEmpty()) {
            String javaHome = AccessController.doPrivileged(
                    new GetPropertyAction("java.home"));
            try {
                zoneID = getSystemTimeZoneID(javaHome);
                if (zoneID == null) {
                    zoneID = GMT_ID;
                }
            } catch (NullPointerException e) {
                zoneID = GMT_ID;
            }
        }

        // Get the time zone for zoneID. But not fall back to
        // "GMT" here.
        tz = getTimeZone(zoneID, false);

        if (tz == null) {
            // If the given zone ID is unknown in Java, try to
            // get the GMT-offset-based time zone ID,
            // a.k.a. custom time zone ID (e.g., "GMT-08:00").
            String gmtOffsetID = getSystemGMTOffsetID();
            if (gmtOffsetID != null) {
                zoneID = gmtOffsetID;
            }
            tz = getTimeZone(zoneID, true);
        }
        assert tz != null;

        final String id = zoneID;
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            @Override
                public Void run() {
                    //****************************************************
                    //javaHome 配置 使用默认的-Duser.timezone=Asia/Shanghai
                    //****************************************************
                    System.setProperty("user.timezone", id);
                    return null;
                }
            });

        defaultTimeZone = tz;
        return tz;
    }

 

 

发现一个问题:数据库使用的是JST

然后虚拟机或者容器上的-Duser.timezone=GMT+08    

catalina.sh里面发现配置有问题:

 

 

 

 

解决方案1:

因为数据库只会获取Date类型"2021-03-30 19:01:55" 然后按照<给到的时区>转换成时间戳,所以会出现问题。

因此

1)基于当前Mysql对应的时区配置java服务时区

&serverTimezone=GMT%2B8                (北京时间东八区   或者都是 日本时间

&serverTimezone=Asia/Shanghai             (上海时间              或者都是 日本时间)

 

2)javaHome 配置使用默认的时区

-Duser.timezone=Asia/Shanghai   或者是   -Duser.timezone=GMT+8  北京时间东八区   或者 都是  日本时间

 

解决方案2:

后续根据时间判断的字段统一使用long类型或者是BigInt类型来存储时间戳,而不是使用Date类型来存储

 

 

运维给出的解决方案:

海外日本集群新增应用时,在caster平台上配置项,一般是手动配置还是走 caster平台的"配置模板"。
关于日本集群时区不一致的问题,有2种方式解决:
1、手动改应用的配置项,挂载宿主机时区,然后重发生效。
2、在模板内改配置,下次发布直接生效。

你们这边要是用模板的话,就额外新增多个日本地区专用的模板(分开发语言)。要是不用模板的话,就在配置项内加一条配置。

 

 

 

 

 

 

非常不错的操作参考文章文章:

SpringBoot 时区问题

https://blog.csdn.net/qq_35425070/article/details/84339110?utm_term=spring%E8%AE%BE%E7%BD%AE%E6%97%B6%E5%8C%BA&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-5-84339110&spm=3001.4430

 

 

调试过程需要当心的相关问题:

Java时区bug,TimeZone.setDefault()只在当前线程和之后创建的子线程有效

https://blog.csdn.net/yanical/article/details/5386662

 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值