PHP速学视频免费教程(入门到精通)
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
然而 springboot还支持其它的servlet容器 默认的tomcat只是其中一种
Jetty
适合用于长链接应用 例如聊天
Undertow
是一个高性能非阻塞的Servlet容器 并发性能非常好 但不支持jsp
若要切换 只需要在pom文件中排除自带的Tomcat启动器 再引入其它Servlet容器的启动器即可
spring-boot-starter-web里面引入了spring-boot-starter-tomcat 因此默认使用Tomcat
因此 只需排除原先的Tomcat 再引入要添加的Servlet容器的依赖 即可:
切换成Jetty:
<!-- 引入Web模块 --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <exclusions> <!-- 排除tomcat容器 --> <exclusion> <artifactid>spring-boot-starter-tomcat</artifactid> <groupid>org.springframework.boot</groupid> </exclusion> </exclusions></dependency><!-- 引入其它的Servlet容器 --><dependency> <artifactid>spring-boot-starter-jetty</artifactid> <groupid>org.springframework.boot</groupid></dependency>
Jetty启动成功
同理 切换成undertow:
<!-- 引入Web模块 --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <exclusions> <!-- 排除tomcat容器 --> <exclusion> <artifactid>spring-boot-starter-tomcat</artifactid> <groupid>org.springframework.boot</groupid> </exclusion> </exclusions></dependency><!-- 引入其它的Servlet容器 --><dependency> <artifactid>spring-boot-starter-undertow</artifactid> <groupid>org.springframework.boot</groupid></dependency>
Undertow启动成功
1.1、SpringBoot支持的Servlet种类及其切换
1)默认支持的webServer:Tomcat、Jrtty、Undertow
2)切换服务器
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <exclusions> <exclusion> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-tomcat</artifactid> </exclusion> </exclusions></dependency>
1.2、原理
1)SpringBoot应用启动发现当前是Web应用。web场景包-导入tomcat
2)web应用会创建一个web版的ioc容器 ServletWebServerApplicationContext
3)ServletWebServerApplicationContext 启动的时候寻找 ServletWebServerFactory(Servlet 的web服务器工厂---> Servlet 的web服务器)
4)SpringBoot底层默认有很多的WebServer工厂;
TomcatServletWebServerFactory
JettyServletWebServerFactory
UndertowServletWebServerFactory
5)底层直接会有一个自动配置类。
ServletWebServerFactoryAutoConfiguration导入了ServletWebServerFactoryConfiguration(配置类)
6)ServletWebServerFactoryConfiguration 配置类 根据动态判断系统中到底导入了那个Web服务器的包。(默认是web-starter导入tomcat包),容器中就有 TomcatServletWebServerFactory
7)TomcatServletWebServerFactory 创建出Tomcat服务器并启动;TomcatWebServer 的构造器拥有初始化方法initialize---this.tomcat.start();
8)内嵌服务器,就是手动把启动服务器的代码调用(tomcat核心jar包存在)
2.1、修改配置文件
通过对application.properties配置文件的设置,定制Servlet容器
#修改servlet容器 server.port=8000 #server.undertow.accesslog.dir=/tmp
2.2、实现 WebServerFactoryCustomizer
xxxxxCustomizer:定制化器,可以改变xxxx的默认规则
通过WebServerFactoryCustomizer修改 Servlet 容器的响应端口号:
import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.stereotype.Component; @Component public class CustomizationBean implements WebServerFactoryCustomizer<configurableservletwebserverfactory> { @Override public void customize(ConfigurableServletWebServerFactory server) { server.setPort(9000); } }</configurableservletwebserverfactory>
2.3、ConfigurableServletWebServerFactory
直接自定义 ConfigurableServletWebServerFactory 的接口实现类
public interface ConfigurableWebServerFactory extends WebServerFactory, ErrorPageRegistry { void setPort(int port); void setAddress(InetAddress address); void setErrorPages(Set extends ErrorPage> errorPages); void setSsl(Ssl ssl); void setSslStoreProvider(SslStoreProvider sslStoreProvider); void setHttp2(Http2 http2); void setCompression(Compression compression); void setServerHeader(String serverHeader); default void setShutdown(Shutdown shutdown) { } }
ConfigurableServletWebServerFactory 接口实现类(框架中默认实现类):
通过自定义 ConfigurableServletWebServerFactory 接口的实现类,即可定制Servlet容器
已抢7329个
抢已抢95408个
抢已抢14927个
抢已抢52643个
抢已抢195815个
抢已抢87481个
抢