因需项目对并发要求比较高、提高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. selenuim爬虫实战(日lofter.com)

    LOFTER是网易公司2011年8月下旬推出的一款轻博客产品. LOFTER专注于为用户提供简约.易用.有品质.重原创的博客工具.原创社区,以及有品质的手机博客应用. LOFTER首次采用独立域名,口 ...

  2. TensorFlow基础笔记(7) 图像风格化效果与性能优化进展

    参考 http://hacker.duanshishi.com/?p=1693http://blog.csdn.net/hungryof/article/details/53981959http:// ...

  3. 003很好的网络博客(TCP/IP)-很全

    http://www.cnblogs.com/obama/p/3292335.html 很全的计算机网络方面的资料.

  4. Lifecycle for overriding binding, validation, etc,易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试。

    Lifecycle for overriding binding, validation, etc,易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试. 它是一个典型的教科书式的mvc ...

  5. HttpServlet中,用来处理POST请求的方法是(选择1项)

    HttpServlet中,用来处理POST请求的方法是(选择1项) A. doHead B. doGet C. doPost D. doPut 解答:C

  6. 用 malloc 或 new 申请内存之后,应该立即检查指针值是否为 NULL

    用 malloc 或 new 申请内存之后,应该立即检查指针值是否为 NULL. 防止使用指针值为 NULL 的内存. #include <iostream> #include <s ...

  7. 静态内部类定义在类中,任何方法外,用static定义

    静态内部类:(注意:前三种内部类与变量类似,所以可以对照参考变量) 静态内部类定义在类中,任何方法外,用static定义. 静态内部类只能访问外部类的静态成员. 生成(new)一个静态内部类不需要外部 ...

  8. 让div也出现滑动框。

    <div id="box" style="height: 300px; width: 200px; border:1px solid #CCC; overflow: ...

  9. BT下载会损害硬盘吗

    简而言之,这个问题是否存在,取决于网络带宽的发展速度与硬件性能的发展速度.如果硬件发展的速度快, 网络带宽速度发展慢,那么对大多数人而言,当前的硬件在慢速的带宽下载BT不会造成任何的硬盘损坏.     ...

  10. C#正则表达式操作中使用LINQ

    比如:[程序员][代码]博客园 - 程序员的网上家园,代码改变世界 提取出来的Tag应该是:[程序员].[代码] Regex _regexTag = new Regex(@"^(\[[^\] ...