Spring Boot切换为APR模式

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包

链接:https://pan.baidu.com/s/1nvC9UYy2rzhtArQOkNEF4w 
提取码:xjmj

 

下载最新&解压安装包apr

wget http://mirrors.hust.edu.cn/apache//apr/apr-1.7.0.tar.gz

wget http://mirrors.hust.edu.cn/apache//apr/apr-util-1.6.1.tar.gz

tar xf apr-1.7.0.tar.gztar

tar xf apr-util-1.6.1.tar.gz

 

安装APR

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

./configure --prefix=/usr/local/apr && make && make install 
如果不设置安装路径,那么系统默认的安装路径为/usr/local/apr/lib

 

安装apr-util

cd apr-util-1.6.1

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

 

安装tomcat-native

http://archive.apache.org/dist/tomcat/tomcat-connectors/native/1.2.14/source/ 
tomcat-native下载地址,低版本启动会报错

tar xf tomcat-native-1.2.14.tar.gz

cd tomcat-native-1.2.14-src/native/

./configure --with-apr=/usr/local/apr/bin/apr-1-config --with-java-home=/usr/local/jdk && make && make install

 

配置Apr

vim /etc/profile

source /etc/profile

 
  1. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib
  2. export LD_RUN_PATH=$LD_RUN_PATH:/usr/local/apr/lib
 

新增APRConfig类

 
  1. packagecom.ochain.data2chain.gateway.config;
  2. importorg.apache.catalina.LifecycleListener;
  3. importorg.apache.catalina.core.AprLifecycleListener;
  4. importorg.springframework.beans.factory.annotation.Value;
  5. importorg.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
  6. importorg.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
  7. importorg.springframework.context.annotation.Bean;
  8. importorg.springframework.context.annotation.Configuration;
  9. /**
  10. *
  11. * @authoryueli
  12. * @date: 2020年7月10日下午3:32:08
  13. */@ConfigurationpublicclassAPRConfig{
  14. @Value("${tomcat.apr:false}")privateboolean enabled;
  15. @BeanpublicEmbeddedServletContainerFactory servletContainer() {
  16. TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory();
  17. if(enabled) {
  18. LifecycleListener arpLifecycle = new AprLifecycleListener();
  19. container.setProtocol("org.apache.coyote.http11.Http11AprProtocol");
  20. container.addContextLifecycleListeners(arpLifecycle);
  21. }
  22. returncontainer;
  23. }
  24. }
 

启动springboot

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

 
  1. 2020-07-1015:35:19,032-InitializingProtocolHandler["http-apr-8081"]
  2. 2020-07-1015:35:19,051-StartingProtocolHandler["http-apr-8081"]
  3. 2020-07-1015:35:19,080-Tomcatstartedonport(s): 8081(http)
 

启动及安装报错

 

安装提示No such file or directory

yum install -y expat-devel* 
安装后确实可以正常make

这里要注意,如果只yum search expat只会找到expat包,要yum search expat*才会看到还有expat-devel的包

 

系统找不到apr的lib库

 
  1. org.springframework.boot.context.embedded.tomcat.ConnectorStartFailedException: Connector configured tolisten onport 8081 failed tostart
  2. ...
  3. ***************************
  4. APPLICATION FAILED TOSTART
  5. ***************************
  6. Description:
  7. The Tomcat connector configured tolisten onport 8081 failed tostart. Theport may already be inuse orthe connector may be misconfigured.
  8. Caused by: org.apache.catalina.LifecycleException: The configured protocol [org.apache.coyote.http11.Http11AprProtocol] requires the APR/native library which is notavailable
 

在启动命令添加制定par库

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

 

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

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

安装 expat-devel 库

yum install -y expat-devel 
安装后再次编辑

 

内网的安装方式(上方APR包里有)

https://launchpad.net/ubuntu/+source/expat/2.0.1-7.2ubuntu1.4

./configure

make && make install 
然后回到apr-util-1.6.1目录重新make即可

 

容器内无法编译(缺少GCC)

apk update

apk add build-base

 

posted @ 2020-07-30 18:11  L1n  阅读(768)  评论(0编辑  收藏  举报