安装,使用mac的包管理工具Homebrew可以进行快速安装
brew install tomcat-native
导入运行库
安装成功之后brew会输出安装的路径,一般是在/usr/local/opt/
目录下。
进入目录/usr/local/opt/tomcat-native/lib/
,查看libtcnative-1.dylib
文件是否存在,注意libtcnative-1.dylib
是替身文件,不是源文件,源文件为libtcnative-1.0.dylib
。
建立软连接ln -s /usr/local/opt/tomcat-native/lib/libtcnative-1.dylib /Library/Java/Extensions
。(只要是java.library.path有的就行,这里为/Library/Java/Extensions
。也可以在启动springboot的时候使用-Djava.library.path=库文件路径
,不过会清除前面配置的路径,可能会出现一些问题,不推荐使用)
PS:经过测试,软连接之后运行库下的文件名应该为libtcnative-1.dylib
,否则会找不到库文件。文件名错误的话启动springboot就会报下面的错:
The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
SpringBoot配置Tomcat使用APR
@Configuration
public class APRConfig {
@Bean
public ServletWebServerFactory servletWebServerFactory() {
TomcatServletWebServerFactory container = new TomcatServletWebServerFactory();
LifecycleListener arpLifecycle = new AprLifecycleListener();
container.setProtocol("org.apache.coyote.http11.Http11AprProtocol");
container.addContextLifecycleListeners(arpLifecycle);
return container;
}
}