Start Tomcat in a container, which logs the time and local time inconsistency

When the container starts, tomcat output log information is as follows;

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"]

Into the interior of the container to see its time

Pictures .png

CST local time zone, indicating Docker and operating system used is the same time zone

 Description:      

      (1) UTC
  entire planet divided into 24 zones seasons, each time zone has its own local time. In the case of international radio communications, for the sake of consistency, the use of a single time, when called Universal Coordinated (UTC, Universal Time Coordinated).
  (2) GMT
  Greenwich Mean Time (Greenwich Mean Time) refers to standard time in the UK the London suburb of Greenwich Observatory ×××, because the prime meridian is defined by where the warp. (UTC substantially the same time and GMT, herein, no distinction)
  (. 3) CST
  China Standard Time (China Standard Time)
  code is as follows:
  GMT + UTC + =. 8. 8 = CST
  (. 4) the DST
  daylight saving time (Daylight Saving Time) refers to the sun rises earlier than the clock ahead one hour in the summer, in order to advance the use of daylight. (China does not use)


Because Tomcat is started using the default time zone when the JVM due to start, do not start JVM using the system to provide default time zone you? Answer: No, the default time zone is UTC

Based on this phenomenon, I write test code in the container:

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

Compile and run

javac -g Test.java
# java Test

Look at the following screenshot:

Pictures .png

Just a difference of 8 hours

In fact, like to find reasons to solve

First, modify the default JVM startup time zone provided, the second is to modify the Tomcat startup script

I am here to choose Tomcat startup script to modify the parameters

In the $ Tomcat_HOME / bin / catalina.sh add the following:

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

Finally, start Tomcat, normal, see the following:

Pictures .png


Method Two:

Use tzselect be modified

# 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.

Select 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

Select 9

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

Option 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

Option 1

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

cat > /etc/timezone << EOF

Asia / Shanghai

EOF


The previous catalina.sh script comment out the last start Tomcat, the time zone is normal

See below:


Pictures .png


Finally, these can be configured information, repackaged into a new image, and then posted to the repos

Guess you like

Origin blog.51cto.com/3388803/2422474