SpringBoot內嵌Tomcat開啓APR模式(運行環境爲Centos7)

網上查到的一些springboot內嵌的tomcat開啓apr的文章,好像使用的springboot版本較老,在SpringBoot 2.0.4.RELEASE中已經行不通了。本身整理了一下,供參考。java

運行環境:Centos 7,JDK 1.8linux

1.pom.xml代碼以下:web

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>syb</groupId>
    <artifactId>tomcat.apr.sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>tomcat.apr.sample</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 2. 查看內嵌的tomcat版本,如從eclipse中查看,以下圖:spring

3. 去tomcat官網,下載該版本的tomcat,以下圖:apache

4. 將tomcat的壓縮包上傳到linux系統,解壓,而後進入${tomcat-home}/bin目錄,以下圖:tomcat

5. 將上圖中紅框內文件「tomcat-native.tar.gz」解壓開,而後進入「${tomcat-home}/bin/tomcat-native-1.2.18-src/native」目錄。springboot

6. 按順序執行以下命令,注意,其中java-home根據實際狀況進行修改,其中prefix爲tomcat apr庫的安裝位置,可根據本身的須要進行修改。eclipse

yum -y install gcc

yum -y install apr-devel openssl-devel ./configure --with-apr=/usr/bin/apr-1-config \ --with-java-home=/usr/java/jdk1.8.0_162/ \ --with-ssl=yes \ --prefix=/usr/local/tomcat-apr make && make install

 執行完成後,結果以下圖,表示tomcat apr庫安裝成功,庫的位置爲/usr/local/tomcat-apr/lib:maven

7. 配置環境變量,打開/etc/profile文件,在結尾處,添加以下內容:spring-boot

export LD_LIBRARY_PATH=/usr/local/tomcat-apr/lib

 

8. 使環境變量生效,執行以下命令:

source /etc/profile

 

 9. 程序代碼以下,SpringBoot 2.0.4內嵌的tomcat,默認使用的是nio模式,若要開啓apr模式,須要作一些定製操做。

package syb.tomcat.apr.sample; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.context.annotation.Bean; @SpringBootApplication public class App { /** * 開啓apr模式 * @return */ @Bean public ConfigurableServletWebServerFactory webServerFactory() { TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.setProtocol("org.apache.coyote.http11.Http11AprProtocol"); return factory; } public static void main(String[] args) { SpringApplication.run(App.class, args); } }

 

 10. 將程序打包,放到linux機器,而後執行以下命令運行:

java -jar tomcat.apr.sample-0.0.1-SNAPSHOT.jar

 結果以下圖,能夠從中看到,已經開啓了apr模式:

相關文章
相關標籤/搜索