zoukankan      html  css  js  c++  java
  • springboot使用tomcat apr模式

    因需项目对并发要求比较高、提高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).[2] 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,如果安装了则不再需要安装,如果未安装则需要安装下列库:

    1)openssl,需要版本大于1.0.2,如果不使用https openssl也可以不安装,就是在启动的时候会报openssl的错误,直接忽视就可以了;
    2)apr,可以去官网下载1.6.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.3.tar.gztar -zxvf apr-util-1.6.1.tar.gz

    安装APR

    # cd apr-1.6.3
    
    检查是否符合安装条件并配置安装参数,检查是否缺失类库,一般来说如果安装的不是精简版系统都是能顺利通过的# 
    
    ./configure --prefix=/usr/local/apr# make & make install
    
    如果不设置安装路径,那么系统默认的安装路径为/usr/local/apr/lib

    安装apr-util

    # cd apr-util-1.6.1
    
    安装apr-util需要配置apr路径和jvm路径,否则会报错找不到apr
    
    ./configure --with-apr=/usr/local/apr --prefix=/usr/local/apr-utils --with-java-home=/usr/java/jdk1.8.0_171 && make && make install

    安装tomcat-native

    # cd tomcat/bin
    
    # tar -zxvf tomcat-native.tar.gz#cdtomcat-native-1.2.17-src/native/
    
    # ./configure --with-apr=/usr/local/apr --with-java-home=/usr/java/jdk1.8.0_171 --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

    安装后再次编辑

     

  • 相关阅读:
    AsyncTask异步加载和HttpURLConnection网络请求数据
    The Vertu of the Dyamaund钻石
    英文finaunce金融
    Middle English finaunce金融
    金融finaunce财经
    英语fraunce法兰西
    France Alternative forms Fraunce
    python关于try except的使用方法
    java实现在线预览--poi实现word、excel、ppt转html
    static 关键字有什么作用
  • 原文地址:https://www.cnblogs.com/yueli/p/9668088.html
Copyright © 2011-2022 走看看