重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

在容器中启动Tomcat时,其日志时间与本地时间不一致

在容器启动时,tomcat的输出日志信息如下;

创新互联专注于企业成都全网营销、网站重做改版、涿鹿网站定制设计、自适应品牌网站建设、html5商城开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为涿鹿等各大城市提供网站开发制作服务。

docker logs 5ddb9eeb4815

Using CATALINA_BASE:   /opt/tomcat-8.5.11
Using CATALINA_HOME:   /opt/tomcat-8.5.11
Using CATALINA_TMPDIR: /opt/tomcat-8.5.11/temp
Using JRE_HOME:        /opt/jdk1.8.0_73
Using CLASSPATH:       /opt/tomcat-8.5.11/bin/bootstrap.jar:/opt/tomcat-8.5.11/bin/tomcat-juli.jar
Tomcat started.
22-Jul-2019 06:12:52.297 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.5.11
22-Jul-2019 06:12:52.299 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Jan 10 2017 21:02:52 UTC
22-Jul-2019 06:12:52.299 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.5.11.0
22-Jul-2019 06:12:52.299 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
22-Jul-2019 06:12:52.299 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            3.10.0-514.el7.x86_64
22-Jul-2019 06:12:52.299 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
22-Jul-2019 06:12:52.299 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /opt/jdk1.8.0_73/jre
22-Jul-2019 06:12:52.299 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_73-b02
22-Jul-2019 06:12:52.299 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
22-Jul-2019 06:12:52.299 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /opt/tomcat-8.5.11
22-Jul-2019 06:12:52.299 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /opt/tomcat-8.5.11
22-Jul-2019 06:12:52.299 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/opt/tomcat-8.5.11/conf/logging.properties
22-Jul-2019 06:12:52.299 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
22-Jul-2019 06:12:52.300 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
22-Jul-2019 06:12:52.300 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
22-Jul-2019 06:12:52.300 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/opt/tomcat-8.5.11
22-Jul-2019 06:12:52.300 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/opt/tomcat-8.5.11
22-Jul-2019 06:12:52.300 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/opt/tomcat-8.5.11/temp
22-Jul-2019 06:12:52.300 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
22-Jul-2019 06:12:52.566 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
22-Jul-2019 06:12:52.593 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
22-Jul-2019 06:12:52.606 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
22-Jul-2019 06:12:52.607 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
22-Jul-2019 06:12:52.611 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1015 ms
22-Jul-2019 06:12:52.663 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
22-Jul-2019 06:12:52.664 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.11
22-Jul-2019 06:12:52.682 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/tomcat-8.5.11/webapps/ROOT
22-Jul-2019 06:13:09.698 INFO [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [16,530] milliseconds.
22-Jul-2019 06:13:09.795 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /opt/tomcat-8.5.11/webapps/ROOT has finished in 17,113 ms
22-Jul-2019 06:13:09.795 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/tomcat-8.5.11/webapps/docs
22-Jul-2019 06:13:09.891 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /opt/tomcat-8.5.11/webapps/docs has finished in 96 ms
22-Jul-2019 06:13:09.891 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/tomcat-8.5.11/webapps/examples
22-Jul-2019 06:13:10.384 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /opt/tomcat-8.5.11/webapps/examples has finished in 493 ms
22-Jul-2019 06:13:10.385 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/tomcat-8.5.11/webapps/host-manager
22-Jul-2019 06:13:10.429 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /opt/tomcat-8.5.11/webapps/host-manager has finished in 43 ms
22-Jul-2019 06:13:10.429 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/tomcat-8.5.11/webapps/manager
22-Jul-2019 06:13:10.470 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /opt/tomcat-8.5.11/webapps/manager has finished in 41 ms
22-Jul-2019 06:13:10.487 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [http-nio-8080]
22-Jul-2019 06:13:10.494 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [ajp-nio-8009]
22-Jul-2019 06:13:10.495 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 17883 ms
22-Jul-2019 06:15:15.904 INFO [main] org.apache.catalina.core.StandardServer.await A valid shutdown command was received via the shutdown port. Stopping the Server instance.
22-Jul-2019 06:15:15.905 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8080"]
22-Jul-2019 06:15:15.957 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
22-Jul-2019 06:15:16.008 INFO [main] org.apache.catalina.core.StandardService.stopInternal Stopping service Catalina
22-Jul-2019 06:15:16.055 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8080"]
22-Jul-2019 06:15:16.056 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-nio-8009"]
22-Jul-2019 06:15:16.056 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8080"]
22-Jul-2019 06:15:16.060 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["ajp-nio-8009"]

进入容器内部查看其时间

在容器中启动Tomcat时,其日志时间与本地时间不一致

本地的CST时区,说明Docker与操作系统使用的是同一个时区

 说明:      

      (1) UTC
  整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated)。
  (2) GMT
  格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的×××格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。(UTC与GMT时间基本相同,本文中不做区分)
  (3) CST
  中国标准时间 (China Standard Time)
  代码如下:
  GMT + 8 = UTC + 8 = CST
  (4) DST
  夏令时(Daylight Saving Time) 指在夏天太阳升起的比较早时,将时钟拨快一小时,以提早日光的使用。(中国不使用)

是因为Tomcat启动时,使用了JVM启动时的默认时区所致,难道JVM启动不是使用系统提供的默认时区吗?答案:不是的,默认是UTC时区

基于这种现象,本人在容器中写了测试的代码:

public class Test{
   public static void main(String[] args){
      System.out.println(new java.util.Date());
      System.out.println(System.getProperty("user.timezone"));
   }
}

进行编译,然后运行

javac -g Test.java
# java Test

看如下截图:

在容器中启动Tomcat时,其日志时间与本地时间不一致

正好相差8个小时

找到原因其实就好解决了

一是修改JVM启动提供的默认时区,二是修改Tomcat启动脚本

我这里选择修改Tomcat启动脚本的参数

在$Tomcat_HOME/bin/catalina.sh 中添加如下:

export JAVA_OPTS="$JAVA_OPTS -server -Dfile.encoding=UTF-8 -Duser.timezone=GMT+08"

最后启动Tomcat,正常,见如下:

在容器中启动Tomcat时,其日志时间与本地时间不一致

方法二:

使用tzselect进行进行修改

# tzselect

 1) Africa
 2) Americas
 3) Antarctica
 4) Arctic Ocean
 5) Asia
 6) Atlantic Ocean
 7) Australia
 8) Europe
 9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.

选择5

Please select a country.
 1) Afghanistan           18) Israel                35) Palestine
 2) Armenia               19) Japan                 36) Philippines
 3) Azerbaijan            20) Jordan                37) Qatar
 4) Bahrain               21) Kazakhstan            38) Russia
 5) Bangladesh            22) Korea (North)         39) Saudi Arabia
 6) Bhutan                23) Korea (South)         40) Singapore
 7) Brunei                24) Kuwait                41) Sri Lanka
 8) Cambodia              25) Kyrgyzstan            42) Syria
 9) China                 26) Laos                  43) Taiwan
10) Cyprus                27) Lebanon               44) Tajikistan
11) East Timor            28) Macau                 45) Thailand
12) Georgia               29) Malaysia              46) Turkmenistan
13) Hong Kong             30) Mongolia              47) United Arab Emirates
14) India                 31) Myanmar (Burma)       48) Uzbekistan
15) Indonesia             32) Nepal                 49) Vietnam
16) Iran                  33) Oman                  50) Yemen
17) Iraq                  34) Pakistan

选择9

Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time

选择1

The following information has been given:

        China
        Beijing Time

Therefore TZ='Asia/Shanghai' will be used.
Local time is now:      Mon Jul 22 15:55:01 CST 2019.
Universal Time is now:  Mon Jul 22 07:55:01 UTC 2019.
Is the above information OK?
1) Yes
2) No

选择1

# cp /etc/timezone /etc/timezone.bak

cat > /etc/timezone << EOF

Asia/Shanghai

EOF

将之前的catalina.sh脚本注释掉,最后启动Tomcat,时区也是正常的

见下图:

在容器中启动Tomcat时,其日志时间与本地时间不一致

最后,可以将这些配置好的信息,重新打包成新镜像,然后发布到repos中


分享文章:在容器中启动Tomcat时,其日志时间与本地时间不一致
转载注明:http://cqcxhl.cn/article/ghhdep.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP