因需项目对并发要求比较高、提高Tomcat效率、使用tomcat apr模式、今天在这记录下使用过程、apr全称为apache portable runtime、这里套用下wiki对apr的解释。

The Apache Portable Runtime (APR) is asupporting library fortheApache web server. It provides asetofAPIs that map totheunderlying operating system(OS).[] Where theOS does notsupport aparticular function, APRwillprovideanemulation. ThusprogrammerscanusetheAPRtomakeaprogramtrulyportableacrossplatforms.APR originally formed apart ofApache HTTP Server, but theApache Software Foundation spun itoff intoaseparate project. Other applications can use ittoachieve platformindependence.

很好理解就是为apache server准备的一套基于操作系统底层的类库。

APR原理

APR的整体模式还是非阻塞IO,实现的线程模型也是按照NIO的标准模型实现的,从官方文档(http://apr.apache.org/docs/apr/1.6/modules.html)可以看到APR根据不同操作系统,分别用c重写了大部分IO和系统线程操作模块,这就是为什么APR在不改动代码的情况下能够提升。

下面这些就是APR重写的模块:

Memory allocation andmemorypool functionality
Atomic operations
Dynamic library handling
File I/O
Command-argument parsing
Locking
Hash tables andarrays
Mmap functionalityNetwork sockets andprotocols
Thread, process andmutex functionality
Shared memory functionality
Time routinesUser andgroup ID services

spring boot 开启APR模式

在Springboot中内嵌的Tomcat默认启动开启的是NIO模式,这里如果我们要在linux内核的系统上使用APR模式,那么需要安装一些lib库,可以通过rpm -q | grep apr来查看是否安装了apr,如果安装了则不再需要安装,如果未安装则需要安装下列库:

)openssl,需要版本大于1.0.2,如果不使用https openssl也可以不安装,就是在启动的时候会报openssl的错误,直接忽视就可以了;
)apr,可以去官网下载1..X最新版进行下载http://apr.apache.org/download.cgiapr-util,在同一个页面进行下载,最新版本为1.6.X版本tomcat-native,在tomcat中自带了安装包,可以在tomcat的bin目录下找到tomcat-native.tar;
下载最新&解压安装包apr
wget http://mirrors.hust.edu.cn/apache//apr/apr-1.6.3.tar.gzwget http://mirrors.hust.edu.cn/apache//apr/apr-util-1.6.1.tar.gz

tar -zxvf apr-1.6..tar.gztar -zxvf apr-util-1.6..tar.gz

安装APR

# cd apr-1.6.

检查是否符合安装条件并配置安装参数,检查是否缺失类库,一般来说如果安装的不是精简版系统都是能顺利通过的# 

./configure --prefix=/usr/local/apr# make & make install

如果不设置安装路径,那么系统默认的安装路径为/usr/local/apr/lib

安装apr-util

# cd apr-util-1.6.

安装apr-util需要配置apr路径和jvm路径,否则会报错找不到apr

./configure --with-apr=/usr/local/apr --prefix=/usr/local/apr-utils --with-java-home=/usr/java/jdk1.8.0_ && make && make install

安装tomcat-native

# cd tomcat/bin

# tar -zxvf tomcat-native.tar.gz#cdtomcat-native-1.2.-src/native/

# ./configure --with-apr=/usr/local/apr --with-java-home=/usr/java/jdk1.8.0_ --with-ssl=/usr/local/include/node/openssl --with-ssl=yes && make && make install

配置Apr

vi /etc/profile

在profile最前面加上

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib

source /etc/profile

新增APRConfig类

网上大部分讲解配置tomcat apr的文章,都只是讲了如何在独立tomcat服务上如何配置apr,只需要修改server.xml中的connnector 的protocol就可以了,对于springboot会稍微复杂些,需要增加一个apr配置类在启动的时候修改Embed的tomcat connector网络接入协议。

packagecom.ochain.data2chain.gateway.config;

importorg.apache.catalina.LifecycleListener;
importorg.apache.catalina.core.AprLifecycleListener;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
importorg.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
/**
*
* @authoryueli
* @date: 2018年9月10日下午4:44:08
*/@ConfigurationpublicclassAPRConfig{
@Value("${tomcat.apr:false}")privateboolean enabled; @BeanpublicEmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory();
if(enabled) {
LifecycleListener arpLifecycle = new AprLifecycleListener();
container.setProtocol("org.apache.coyote.http11.Http11AprProtocol");
container.addContextLifecycleListeners(arpLifecycle);
}
returncontainer;
}
}
启动springboot

启动成功后回发现日志输出如下的信息

2018-09-1015:31:19,032-InitializingProtocolHandler["http-apr-8081"]
2018-09-1015:31:19,051-StartingProtocolHandler["http-apr-8081"]
2018-09-1015:31:19,080-Tomcatstartedonport(s): 8081(http)

问题:

1:启动springboot 报如下错误
org.springframework.boot.context.embedded.tomcat.ConnectorStartFailedException: Connector configured tolisten onport 8081 failed tostart
...
***************************
APPLICATION FAILED TOSTART
*************************** Description: The Tomcat connector configured tolisten onport 8081 failed tostart. Theport may already be inuse orthe connector may be misconfigured. Caused by: org.apache.catalina.LifecycleException: The configured protocol [org.apache.coyote.http11.Http11AprProtocol] requires the APR/native library which is notavailable

从上面错误来看真正的原因是系统找不到apr的lib库

解决方法:

在启动命令添加制定par库

java -Djava.library.path=/usr/apr/lib -jar xxxx-0.0.1-SNAPSHOT.jar

2:apr-util致命错误:expat.h:没有那个文件或目录

xml/apr_xml.c:35:19: 致命错误:expat.h:
没有那个文件或目录#include<expat.h>^
编译中断。make[1]: *** [xml/apr_xml.lo] 错误1make[1]:
离开目录“/home/apr/apr-util-1.6.1”缺少expat库

解决方法:

安装 expat-devel 库

yum install -y  expat-devel

安装后再次编辑

springboot使用tomcat apr模式的更多相关文章

  1. tomcat运行模式APR安装

    centos6.2下,Tomcat运行模式apr安装过程,如下: 一.安装apr [root@vmT227-m5 /]# cd /usr/local/ [root@vmT227-m5 local]# ...

  2. Tomcat 8.5 apr 模式配置

    tomcat APR模式配置 一.环境 操作系统:Ubutnu 14 ubuntu@ubuntu:~$ uname -a Linux ubuntu 4.4.0-31-generic #50~14.04 ...

  3. 基于APR模式的Tomcat8环境部署

    1.版本信息 组件名 版本号 jdk 1.8.111 tomcat 8.5.9 apr 1.6.3 apr-iconv 1.2.2 apr-util 1.6.2 tomcat-native 1.2.1 ...

  4. SpringBoot内嵌Tomcat开启APR模式(运行环境为Centos7)

    网上查到的一些springboot内嵌的tomcat开启apr的文章,好像使用的springboot版本较老,在SpringBoot 2.0.4.RELEASE中已经行不通了.自己整理了一下,供参考. ...

  5. tomcat bio nio apr 模式性能测试

    转自:tomcat bio nio apr 模式性能测试与个人看法 11.11活动当天,服务器负载过大,导致部分页面出现了不可访问的状态.那后来主管就要求调优了,下面是tomcat bio.nio.a ...

  6. Tomcat在Linux服务器上的BIO、NIO、APR模式设置

    一.BIO.NIO.AIO 先了解四个概念: 同步 : 自己亲自出马持银行卡到银行取钱(使用同步IO时,Java自己处理IO读写). 异步 : 委托一小弟拿银行卡到银行取钱,然后给你(使用异步IO时, ...

  7. liunx tomcat 运行模式apr

    tomcat运行模式一共三种:bio.nio.apr 网上对这三种运行模式的解释外: bio运行模式:阻塞式I/O操作,表示Tomcat使用的是传统的Java I/O操作(即java.io包及其子包) ...

  8. 配置Tomcat apr运行模式

    tomcat中一共有三种运行模式,分别是:bio,nio,apr bio是阻塞式IO操作,使用的是传统的java i/o处理方式,对于每一个请求都要创建一个线程来进行处理,所以开销较大不适合处理高并发 ...

  9. TOMCAT开启APR模式

    Tomcat支持三种接收请求的处理方式:BIO.NIO.ARP. BIO模式:阻塞式I/O操作,表示Tomcat使用传统Java I/O操作.默认情况下,Tomcat7以下版本使用BIO模式运行,由于 ...

随机推荐

  1. iis 导入和导出配置——iis管理

    首先我们打开服务器管理器,一般服务器都在左下角的任务栏中,直接点击即可打开 2 打开WEB服务器(IIS),选择IIS根目录,找到右边的共享管理 3 打开共享管理后,我们在右侧的操作中找到导出配置,选 ...

  2. C语言 百炼成钢21

    //题目57:编写一个业务函数,实现字符串(前后各有三个空格,单词前后也均有空格) //" i am student, you are teacher " , //各个单词首字符大 ...

  3. gsoap 学习 1-由wsdl文件生成h头文件

    开始前先看一下用户向导吧 http://www.cs.fsu.edu/~engelen/soap.html 中左侧点击Documentation 英语水平确实有限,有些内容可能说的不准确,敬请参考向导 ...

  4. UVa 11178:Morley’s Theorem(两射线交点)

    Problem DMorley’s TheoremInput: Standard Input Output: Standard Output Morley’s theorem states that ...

  5. jquery合并表格中相同文本的相邻单元格

    <!DOCTYPE HTML> <html> <head>   <title>Example</title>   <meta char ...

  6. UESTC 1511(差分约束)

    题目链接:http://acm.uestc.edu.cn/problem.php?pid=1511 思路:我们可以等到这样的5个关系式: k=1:dsit[a]-dist[b]>=0&& ...

  7. Python3x 爬取妹子图

    思路:1.get_totalpages(url)  通过[性.感.美.女.图]获得该版块的总页数 [首页1234567891011下一页末页共 21页1034条] 2.get_sercoverurl( ...

  8. POJ2139 Six Degrees of Cowvin Bacon [Floyd]

    水题,随手敲过 一看就是最短路问题,a,b演同一场电影则他们的距离为1 默认全部两两原始距离无穷,到自身为0 输入全部数据处理后floyd 然后照它说的求平均分离度 再找最小的,×100取整输出 #i ...

  9. shell脚本学习总结04--终端信息的获取和设置

    tput tput 命令将通过 terminfo 数据库对您的终端会话进行初始化和操作.通过使用 tput,您可以更改几项终端功能,如移动或更改光标.更改文本属性,以及清除终端屏幕的特定区域. 文本属 ...

  10. C语言函数的概念

    在<我们对函数进行了简单的解释,函数(Function)是一段可以重复使用的代码,这是从整体上对函数的认识. C语言本身带了很多库函数,并分门别类地放在了不同的头文件中,使用时只要引入对应的头文 ...