Centos 服务器Nginx优化

1.查看当前centos的cpu 信息及内存信息

[root@localhost ~]# cat /proc/cpuinfo |grep "name" |cut -f2 -d: |uniq -c
8 Intel(R) Xeon(R) CPU E7-4809 v4 @ 2.10GHz
[root@localhost ~]# cat /proc/cpuinfo | grep "physical"| sort |uniq -c
8 address sizes : 42 bits physical, 48 bits virtual
4 physical id : 0
4 physical id : 1
[root@localhost ~]# getconf LONG_BIT
64
[root@localhost ~]# cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 79
model name : Intel(R) Xeon(R) CPU E7-4809 v4 @ 2.10GHz
stepping : 1
microcode : 0xb00001f
cpu MHz : 2094.952
cache size : 20480 KB
physical id : 0
siblings : 4
core id : 0
cpu cores : 4
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 20
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch epb invpcid_single fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 invpcid rtm rdseed adx smap xsaveopt dtherm arat pln pts
bogomips : 4189.90
clflush size : 64
cache_alignment : 64
address sizes : 42 bits physical, 48 bits virtual
power management:

2.nginx CPU优化

服务器四核配置如下:

worker_processes  4;
worker_cpu_affinity 0001 0010 0100 1000;

八核如下:

worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 1000000;

重启服务:

systemctl restart nginx

查看状态

systemctl status nginx.service

3.nginx 文件数优化 见 ulimit优化

worker_rlimit_nofile 65535;

4.Nginx事件处理模型
events {
use epoll;
worker_connections 65535;
multi_accept on;
}

Centos7 处理服务器端口

1.查看防火墙状态
firewall-cmd --state
2.查看防火墙是否开机自启
systemctl is-enabled firewalld.service;echo $?
3.开启防火墙
systemctl start firewalld.service
4.关闭防火墙
systemctl stop firewalld.service
5.防火墙设置开机自启
systemctl enable firewalld.service
6.防火墙重启
systemctl restart firewalld.service

7.开启防火墙端口
开端口命令:firewall-cmd --zone=public --add-port=80/tcp --permanent

然后重启防火墙:systemctl restart firewalld.service

检验防火墙是否开启
netstat -ntlp
firewall-cmd --list-ports

8.查看已经开启的端口信息
firewall-cmd --zone=public --list-ports

Mysql 百万数据limit 查询

SELECT * FROM db_storage limit 1085031,20
查询时间为0.486s

SELECT * FROM db_storage WHERE ID >= (select id from db_storage limit 1085031, 1) limit 20

SELECT a.* FROM db_storage a INNER JOIN (SELECT id FROM db_storage LIMIT 1085031,20) b ON a.id = b.id;

上面两个类似,为0.27s左右,此处需要优化pagehelper 逻辑

Mysql数据库在服务器上优化

[client]
port=3306
socket=/tmp/mysql/mysql.sock
default-character-set=utf8

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

[mysqld]
port=3306
user=mysql
socket=/tmp/mysql/mysql.sock
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
log-error=error.log
#允许最大连接数
max_connections=200
#允许连接失败的次数,这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10

# innodb 优化
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 缓存池
innodb_buffer_pool_size=8G
# 脏页占innodb_buffer_pool_size的比例时,触发刷脏页到磁盘
# 25%~50%
innodb_max_dirty_pages_pct=30
# 限制Innodb能打开的表的数据,如果库里的表特别多的情况,请增加这个。这个值默认是300
innodb_open_files = 500
# 此参数确定写日志文件所用的内存大小,以M为单位。缓冲区更大能提高性能,但意外的故障将会丢失数据。MySQL开发人员建议设置为1-8M之间
innodb_log_buffer_size = 2M

#开启记录慢查询 设置慢查询界定时间为1秒
long_query_time=1
slow-query-log=On
slow_query_log_file="mysql_slow_query.log"

transaction_isolation = READ-COMMITTED
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
# lower_case_table_names = 1 这个不能要

#skip-grant-tables

在linux中使用yum安装mysql 安装完成后可以通过下面的语句查询各个路径。

show global variables like "%dir%";

springboot 以jar包方式在windows服务中运行的初始配置

1.在任务管理器中可以看到java运行的进程名

2.初始化winsw的名称及winsw.xml配置信息

 

@echo off

setlocal & pushd

set /p project_name=Jar名称(英文):

set /a sum=0

 set /a jarName=null

echo Jar名称:%project_name%

set filepath=%cd%

if not exist "%filepath%%project_name%_java.exe" (   
    goto copy 
) else ( 
    goto check_winsw 
)

:copy
    echo -----------------------------重命名java.exe----------------------------------
    echo 项目名称对应的java文件不存在
    echo 复制Java.exe 到当前文件夹并命名
    copy "%JAVA_HOME%binjava.exe" "%filepath%%project_name%_java.exe"
    if not exist "%filepath%%project_name%_java.exe" ( 
        echo 文件复制失败,JAVA_HOME环境变量不存在 
        echo --------------------------------------------------------------------------
        pause
        goto :eof
    ) else ( 
        echo --------------------------------------------------------------------------
        goto :check_winsw 
    )


:check_winsw
    echo -----------------------------检查winsw.exe----------------------------------
    if not exist "%filepath%%project_name%_win.exe" if not exist "%filepath%winsw.exe" ( 
        echo --------------------------------winsw 文件不存在------------------------------
        pause
        goto :eof
    )
    if not exist "%filepath%%project_name%_win.exe" ( 
        echo --------------------------------%project_name%_win.exe 文件不存在------------
        echo --------------------------------重命名winsw.exe 文件不存在------------
        ren "%filepath%winsw.exe" "%project_name%_win.exe"
        pause
        goto :check_winsw
    )
    goto :check_winsw_xml

:check_winsw_xml
    echo -----------------------------检查winsw.xml----------------------------------
    if not exist "%filepath%%project_name%_win.xml" if not exist "%filepath%winsw.xml" ( 
        echo --------------------------------winsw.xml 文件不存在------------------------------
        pause
        goto :eof
    )
    if not exist "%filepath%%project_name%_win.xml" ( 
        echo --------------------------------%project_name%_win.xml 文件不存在------------
        echo --------------------------------重命名winsw.xml 文件不存在------------
        ren "%filepath%winsw.xml" "%project_name%_win.xml"
        pause
        goto :check_winsw_xml
    )
    goto :renameJar

:renameJar
    for /r "%filepath%" %%a in (*.jar) do (
        SET "jarName=%%~nxa"
        echo %%~dpa%%~nxa
        echo %%~dpa
        echo %%~nxa
        set /a sum +=1
    )
    echo -------------------------------- 共%sum% 个jar------------------------------
    if %sum% NEQ 1 (
        echo --------------------------------Jar 包不存在或者数量大于1------------------------------
        pause
        goto :eof
    ) else (
        ren "%filepath%%jarName%" "%project_name%.jar"
        echo --------------------------------Jar 包重命名成功------------------------------
    )
    goto :configXml

:configXml
    echo -------------------------------------配置xml文件--------------------------------------------
    setlocal enabledelayedexpansion

    for /f "skip=2 tokens=1-4 delims=^<^>" %%i in ('find /i "id" %project_name%_win.xml') do (
        set id=%%k
        echo id当前值为!id!
    )

    for /f "skip=2 tokens=1-4 delims=^<^>" %%i in ('find /i "name" %project_name%_win.xml') do (
        set name=%%k
        echo name当前值为!name!
    )

    for /f "skip=2 tokens=1-4 delims=^<^>" %%i in ('find /i "description" %project_name%_win.xml') do (
        set description=%%k
        echo description当前值为!description!
    )

    for /f "skip=2 tokens=1-4 delims=^<^>" %%i in ('find /i "executable" %project_name%_win.xml') do (
        set executable=%%k
        echo executable当前值为!executable!
    )

    for /f "skip=2 tokens=1-4 delims=^<^>" %%i in ('find /i "arguments" %project_name%_win.xml') do (
        set arguments=%%k
        echo arguments当前值为!arguments!
    )

    pause
    chcp 65001
    for /f "tokens=* delims=" %%a in (%project_name%_win.xml) do (
        set "content=%%a"
        call set "content=%%content:<id>!id!</id>=<id>!project_name!</id>%%"
        call set "content=%%content:<name>!name!</name>=<name>!project_name!</name>%%"
        call set "content=%%content:<description>!description!</description>=<description>!project_name!_winsw</description>%%"
        call set "content=%%content:<executable>!executable!</executable>=<executable>!project_name!_java</executable>%%"
        call set "content=%%content:<arguments>!arguments!</arguments>=<arguments>-Dfile.encoding=utf8 -jar !project_name!.jar</arguments>%%"
        echo !content!>>result.xml
    )
    del %project_name%_win.xml
    ren result.xml %project_name%_win.xml
    chcp 936

endlocal & popd
pause

 

运行后输入项目名称,要求是英文,不要有空格
执行过程:

1.复制JAVA_HOME 中的java.exe到当前目录,并将名称修改成项目名称
2.检查winsw.exe信息,并进行命名
3.检查winsw.xml信息,并进行命名
4.判断当前目录jar包的数量,有且只允许有一个
5.重命名当前的jar文件
6.修改.xml文件中的信息

 

下面就是 winsw install 及winsw start

WINSW 的使用注意事项

1.WINSW 的xml 文件最好为ANSI编码(windows电脑上)

2.WINSW使用时最好重新命名下winsw.exe和winsw.xml。相同名称情况下可能会有冲突,我有两个没有改名字,第三个服务死活启动不了。