Linux JVM, Tomcat, MySQL, log4j optimized configuration

Source: Internet
Author: User

Side dishes have been afraid of the operating system, before also very little contact, this venture to buy a Linux cloud host, because wood someone help, can only do their own optimization server ....

The cloud host configuration of the side dishes is roughly: CENTEOS6 (32-bit), 4 core cpu,4g memory, 5M exclusive bandwidth.

First, the JVM memory optimization scheme.

To optimize the server, start with the JVM, allocate the JVM memory, and configure the scenario as follows:

Declare-x java_opts= "-server-xms1280m-xmx1280m-xmn512m-xss1024k-xx:+aggressiveopts-xx:+useparallelgc-xx:+ Usebiasedlocking "

The-server is to allow the JVM to run in service mode, which can improve the efficiency of the JVM, and the rest will not worry more.

-XMX is the maximum memory, if it is 32 operating system, 2G memory above, set to 1280G is optimal, set too large will cause the JVM to crash, which is caused by the limitations of the 32-bit JVM itself.

-XMN is the smallest memory, officially recommended for maximum memory of 3/8, here to take down the whole, 521M

-xms to initialize memory, directly with the maximum memory, so as to avoid duplication of memory allocation, reduce efficiency, directly up to the largest, the JVM also no longer expand memory, save time.

-XSS is the memory that each thread consumes, that is, the stack size of each thread, too small to overflow, too large to reduce the number of threads created because the maximum capacity is limited. The maximum memory here is 1280M, theoretically can create 1000 left and right threads, almost support 500 concurrent access (is concurrent access oh, that is, 500 users at the same time click), has been quite ok.

-xx:+aggressiveopts, speeding up compilation.

-XX:+USEPARALLELGC, optimize garbage collection.

-xx:+usebiasedlocking, optimize the lock mechanism.

Add the above configuration to the \bin\catalina.sh file in the Tomcat installation directory, where it doesn't matter, as long as it's cygwin=false before the sentence.

Second, Tomcat server optimization scheme

After the JVM is done, the next step is to configure Tomcat, which is configured as follows:

1<ConnectorPort= "80"Protocol= "http/1.1"23Maxhttpheadersize= "8192"45 Maxthreads= ""  Minsparethreads= " Maxsparethreads= "6 7 = "false"  Redirectport= "8443"  Acceptcount= "200" 8 9  connectiontimeout< Span style= "color: #0000ff;" >= "50000"  Disableuploadtimeout= "true" />                

Among these configurations, we are concerned with:

MaxThreads Maximum number of threads, set to 1000 can create 1000 threads, that is, 1000 concurrent transactions, approximately can support 500 full concurrent access, this is a pretty scary number, you can imagine, 500 users simultaneously click, what is the probability.

Minsparethreads minimum number of spare threads, it can be understood that, once the server is started, create so many threads ready for use, so the settings are too large to waste resources, too small will reduce efficiency, this will be based on the reader's server configuration and specific traffic to measure, not generalize, My settings here are only a reference, the server access is about hundreds of thousands of PV per day, and relatively centralized.

Maxsparethreads the maximum number of standby threads, when the number of threads created exceeds this number, if some threads are idle, these are recycled until the total number of threads is less than this value.

Acceptcount the number of threads to wait (note that no new threads were created, just booked) when the number of threads started by Tomcat reached maximum. This value also needs to be set according to the actual situation, if the setting is small, will cause a lot of requests are rejected directly, if the set of large, will cause a lot of requests time-out, the reader understand it!

Enablelookups turns off Tomcat DNS queries, saving bandwidth and eliminating the need for too much understanding.

Disableuploadtimeout This flag allows the servlet container to use a different, longer connection timeout when a servlet executes. The end result is to give the servlet a longer time to complete its execution, or to have a longer time-out when the data is uploaded, in short, to ensure that each request will be completed successfully.

Copy this phrase into the \conf\server.xml file in the Tomcat installation directory.

Note that this configuration is inherently present in server.xml and may need to be fused with the previous configuration of the reader, and not be directly covered blindly ...

Third, MySQL optimization program

MySQL optimization, side dishes learned is to modify the maximum number of connections, because MySQL default maximum number of connections is 100, for a slightly larger project, is certainly not enough.

Project-Side Configuration

The maximum number of connections, first set in the program, if your project uses a JNDI configuration of the data source, then go to the Tomcat installation directory in the \conf\server.xml file or \conf\context.xml file to find the following configuration (side dishes just for example , the inside configuration is casually written, the key is to find the resource node):

1<ResourceName= "Demo"2Auth= "Container"3Type= "Javax.sql.DataSource" Maxactive=" 500 " Span style= "color: #008080;" > 5  Maxidle= ""  6  Maxwait= " 7 = "sa"  8  Password= ""  9 = "Org.hsqldb.jdbcDriver" 10  Url= "jdbc:hsqldb:hsql://localhost/runqiandb" />  

Here's what we need to focus on:

Maxactive refers to the maximum number of active connections is 500, which means that there can be up to 500 database connections at a time. Here also can be understood as concurrency, side dishes think 500 already many, after all, the data will automatically release the connection after reading, if your host configuration is not high enough, there are too many settings, such as what 3000, may cause MySQL startup failure.

Maxidle is the maximum number of idle connections, even if the database is not being used, so many connections are maintained so that they can be used directly to improve efficiency. Because the database connection creation process is quite time consuming. Refer to a lot of websites, generally are about 30, for reference only!

Maxwait is the maximum wait time, if the number of database connections is not enough, then wait so long, more than this time to timeout, set to 1 for infinite wait.

If your project uses a JDBC configuration, that is, there is a properties file in the project, what is commonly called jdbc.properties, config.properties, what is called a side dish is not known, then there will be similar to the following configuration:

Jdbc. Driverclass=com.mysql.jdbc.driver

Jdbc. Url=jdbc\:mysql\://localhost\:3306/xxx?useunicode\=true&amp;characterencoding\=utf8

Jdbc. User=xxx

Jdbc. Password=xxx

Jdbc. maxpoolsize=500

Jdbc. Minpoolsize=30

Jdbc. waittime=5000

Here Maxpoolsize, Minpoolsize, waittime respectively and the above maxactive, Maxidle, maxwait corresponding, but this is a side dish of the project, your project needs specific analysis ....

Server-side configuration

Once the project is configured, you will have to configure the MySQL server.

If your MySQL is a version prior to 5.0, including 5.0, then you can try to modify the maximum number of MySQL connections in the following ways.

Find the/usr/bin/mysqld_safe edit it in the Linux system, find the two lines that mysqld started, and add the parameters to the following:

-O max_connections=1000

Special description in Scarlet Letter:

If Test-z "$args"

Then

$NOHUP _niceness $ledir/$MYSQLD $defaults--basedir= $MY _basedir_version--datadir= $DATADIR $USER _option--pid-file=$ Pid_file--skip-external-locking- o max_connections=1000 >> $err _log 2>&1

Else

Ev Al "$NOHUP _niceness $ledir/$MYSQLD $defaults--basedir= $MY _basedir_version--datadir= $DATADIR $USER _option-- pid-file= $pid _file--skip-external-locking $args- o max_connections=1000 >> $err _log 2>&1 "

This code is not easy to find, and can not say clearly in which location. However, it is advisable to look forward from the very end.

Restart MySQL service: # service MySQL Restart

To view the maximum number of connections currently: MYSQLADMIN-UROOT-P variables

If your MySQL server is a version after 5.1, including 5.1, or if you do not use the method above, then you can try the following methods.

Find the/etc/my.cnf file in the Linux system, in the inside of the [Mysqld] node, add: max_connections=500 can, 500 refers to the maximum number of connections, you can change freely. For example:

[Mysqld]

Datadir=/home/mysql

Socket=/var/lib/mysql/mysql.sock

User=mysql

Default-character-set = UTF8

Default-collation = Utf8_general_ci

max_connections=500

That's it, restart MySQL.

Four, log optimization

The importance of the log needless to say, the project development, the log is generally directly output to the console, easy to view.

However, when the project is actually published, it cannot be done, the efficiency is not said, we can not view these logs.

Side dishes of the project with the log4j, it has a configuration file, called Log4j.properties, the configuration is roughly as follows:

Log4j.rootlogger = INFO, R

LOG4J.APPENDER.R = Org.apache.log4j.RollingFileAppender

Log4j.appender.r.file =/home/westjavatest/logs/ivlifelog4j.txt

Log4j.appender.r.maxfilesize = 500KB

Log4j.appender.r.maxbackupindex = 1

Log4j.appender.r.layout = Org.apache.log4j.PatternLayout

Log4j.appender.r.layout.conversionpattern=%-d{yyyy-mm-dd HH:MM:SS} [%c]-[%p]-%m%n

Here are some of the concerns we need:

Log4j.rootlogger = info, r, here the info indicates the level of the log, the general use of info can be, r is the output configuration, the name casually, and the bottom of the LOG4J.APPENDER.R corresponding to it.

Log4j.appender.r.file indicates the log output path, note that Linux under the directory/and under the Windows directory \ Reverse Oh.

Log4j.appender.r.maxfilesize refers to the size of each log file, which is set to no more than 500kb, and once it is exceeded, a. txt log file is recreated.

Other parameters are less important, interested readers can go to Google on their own ~

V. Concluding remarks

The optimal configuration of these servers, side dishes is also the first contact, spent a lot of time from the online collection, so tidy up, convenient like a side dish such as rookie.

These configurations, many of them are empirical data, specific parameters need to be based on the actual situation of your server, a certain comprehensive analysis, stress testing, only to get, the side dishes enumerated these are only general experience, for reference only.

Finally wish everyone to optimize smoothly!

Original from: http://www.cnblogs.com/iyangyuan/p/3330152.html Another thank Bo Master share.

Linux JVM, Tomcat, MySQL, log4j optimized configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.