做者:韓數java
Github:github.com/hanshuaikan…git
微信公衆平臺:碼上Marsongithub
完成日期:2019-07-02日web
jdk:1.8spring
springboot版本:2.1.6.RELEASE編程
小馬哥: Java 微服務實踐 - Spring Boot / Spring Cloud購買連接:segmentfault
segmentfault.com/ls/16500000…tomcat
Github:github.com/hanshuaikan…springboot
微信公衆號:碼上marson服務器
HelloController.java代碼以下:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String helloWord(){
return "Hello World";
}
}
複製代碼
**前言:**在Springboot中,Tomcat做爲Springboot的一個組件由Spring驅動,支持jar包直接運行web項目簡化了咱們在本地工程環境下的開發流程。而Springboot固然做爲一個靈活的框架,也爲開發者們提供了其餘的選擇,如jetty,Undertow和Tomcat,已經2.0+版本新增的Netty Web Server。
由於Springboot默認採用的就是Tomcat容器,因此如何配置Tomcat做爲Web 容器這裏再也不多講。
Eclipse Jetty Web Server提供了一個HTTP服務器和Servlet容器,可以提供來自獨立實例或嵌入式實例的靜態和動態內容。從jetty-7開始,jetty web服務器和其餘核心組件由Eclipse Foundation託管。jetty支持:
異步HTTP服務器
基於標準的Servlet容器(最新版本支持Servlet3.1規範)
websocket服務器
http / 2服務器
異步客戶機(http/1.一、http/二、websocket)
OSGI、JNDI、JMX、JASPI、AJP支持
如何從Tomcat切換到Jetty容器,Springboot官方文檔中已經提供了方法,只需將spring-boot-starter-web依賴中的Tomcat依賴排除掉,引入spring-boot-starter-jetty依賴便可,xml代碼以下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 排除Tomcat依賴項-->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
複製代碼
重啓Springboot項目,訪問http://127.0.0.1:8080/ ,屏幕上顯示Hello World ,正常。
Undertow 是紅帽公司開發的一款基於 NIO 的高性能 Web 嵌入式服務器
Untertow 的特色:
輕量級:它是一個 Web 服務器,但不像傳統的 Web 服務器有容器概念,它由兩個核心 Jar 包組成,加載一個 Web 應用能夠小於 10MB 內存。
Servlet3.1 支持:它提供了對 Servlet3.1 的支持。
WebSocket 支持:對 Web Socket 徹底支持,用以知足 Web 應用巨大數量的客戶端。
嵌套性:它不須要容器,只需經過 API 便可快速搭建 Web 服務器。
切換方式和Jetty一致,代碼以下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
複製代碼
前言:嵌入式Reactive Web 容器做爲springboot2.0的新特性,通常來講並非系統默認的選擇。在某些狀況下函數式編程的確會大大提升咱們的開發效率。而jetty,tomcat,和undertow也響應的對Reactive作了支持。
注:當spring-boot-starter-web和spring-boot-starter-webflux同時存在時,spring-boot-starter-webflux其實是會被默認忽略掉的,真正其做用的是spring-boot-starter-web,因此在使用spring-boot-starter-webflux的時候,咱們須要把spring-boot-starter-web註釋掉。修改後的pom文件以下。
<dependencies>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-web</artifactId>-->
<!--<exclusions>-->
<!--<!– Exclude the Tomcat dependency –>-->
<!--<exclusion>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
<!--</exclusion>-->
<!--</exclusions>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
複製代碼
如今新增一個MyRoute類,經過函數式編程咱們編寫一個helloword路由:
@Configuration
public class MyRoute {
@Bean
public RouterFunction<ServerResponse> helloWord(){
return route(GET("/"),
request -> ok().body(Mono.just("helloworld"),String.class)
);
}
}
複製代碼
重啓Springboot項目,訪問http://127.0.0.1:8080/ ,屏幕上顯示helloworld ,正常。
同理,只須要簡單修改pom文件就能夠了。
<dependencies>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-web</artifactId>-->
<!--<exclusions>-->
<!--<!– Exclude the Tomcat dependency –>-->
<!--<exclusion>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
<!--</exclusion>-->
<!--</exclusions>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
複製代碼
<dependencies>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-web</artifactId>-->
<!--<exclusions>-->
<!--<!– Exclude the Tomcat dependency –>-->
<!--<exclusion>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
<!--</exclusion>-->
<!--</exclusions>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>
複製代碼
本文較爲簡單的記錄了springboot開發中不一樣Web 容器的切換,並簡單的闡述了每一個容器的特色,就像戀愛同樣,合適纔是最好的,本系列文章屬於Spring學習筆記中boot部分的學習筆記,markdown筆記文件以及代碼會陸續開源到Github上,歡迎你們下載和點Star,若是筆記中有疏漏或者錯誤的地方,還請各位大佬批評改正。