因需项目对并发要求比较高、提高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. CSS(七):浮动

    一.float属性取值:left:左浮动right:右浮动none:不浮动 先看下面的一个例子: <!DOCTYPE html> <html lang="en"& ...

  2. EasyUI Pagination 分页分页布局定义 显示按钮布局

    //分页布局定义.该属性自版本 1.3.5 起可用.//布局项目包括一个或多个下列值://1.list:页面尺寸列表.//2.sep:页面按钮分割.//3.first:第一个按钮.//4.prev:前 ...

  3. jconsole监控tomcat

    一.专业术语 GC垃圾回收机制:当需要分配的内存空间不再使用的时候,JVM将调用垃圾回收机制来回收内存空间. JMX(Java Management Extensions,即Java管理扩展)是一个为 ...

  4. Linux命令之乐--nmap

    Nmap是一款非常强大的实用工具,可用于:检测活在网络上的主机(主机发现)检测主机上开放的端口(端口发现或枚举)检测到相应的端口(服务发现)的软件和版本检测操作系统,硬件地址,以及软件版本检测脆弱性的 ...

  5. axios如何进行跨域以及对返回格式为回调函数字符串的处理

    自从vue2.0开始不对vue-resouce进行维护了,转而用axios进行代替,axios的官方文档写的很详细,附上链接一枚:http://www.jianshu.com/p/df464b26ae ...

  6. 使用PHP,jsonp,jquery实现跨域

    html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  7. Android 防破解技术简介

    Android 防破解技术简介 这几年随着互联网的不断发展,Android App 也越来越多!但是随之而来的问题也越来越多,这其中比较令人头疼的问题就是:有些不法分子利用反编译技术破解 App,修改 ...

  8. NIO中几个非常重要的技术点

    参考:http://ifeve.com/selectors/ 参考:https://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.h ...

  9. js/vue 高德地图绘制驾车路线图

    地图容器: // css要给此容器设置宽高 <div class="map_container"></div> 画图 data{ return { Clng ...

  10. JAVA基础之JDBC开发、JSTL语法、EL表达式与数据分页

    一.直接使用JDBC开发的问题 1.当表中的列很多时,需要写很长的SQL语句 还需要写大量 setXXX() 设置参数语句 读取数据时还需要写大量setXXXX()设置属性语句 2.非常容易出错,而且 ...