Java微服务框架之Undertow
一、Undertow简介:
Undertow 是红帽公司(RedHat)的开源产品,是 WildFly8(JBoos) 默认的 Web 服务器。
官网API给出一句话概述Undertow:
Undertow is a flexible performant web server written in java, providing both blocking and non-blocking API’s based on NIO.
译文: Undertow是一个用java编写的灵活的高性能Web服务器,提供基于NIO的阻塞和非阻塞API。
官网API总结特点:
Lightweight(轻量级)
Undertow非常轻量级,Undertow核心jar包在1Mb以下。 它在运行时也是轻量级的,有一个简单的嵌入式服务器使用少于4Mb的堆空间
HTTP Upgrade Support(支持http升级)
支持HTTP升级,允许多个协议通过HTTP端口进行多路复用
Web Socket Support(支持WebScoket)
Undertow提供对Web Socket的全面支持,包括JSR-356支持
Servlet 3.1
Undertow提供对Servlet 3.1的支持,包括对嵌入式servlet的支持。 还可以在同一部署中混合Servlet和本机Undertow非阻塞处理程序
Embeddable(可嵌入的)
Undertow可以嵌入在应用程序中或独立运行,只需几行代码
6. Flexible(灵活性)
Undertow框架jar包: undertow-core.jar undertow-servlet.jar
二、Undertow示例:
1.官网给出一个Undertow Web 服务器使用异步IO的方式向界面输出字符串
1 import io.undertow.Undertow; 2 import io.undertow.server.HttpHandler; 3 import io.undertow.server.HttpServerExchange; 4 import io.undertow.util.Headers; 5 6 public class HelloWorldServer { 7 public static void main(String[] args) { 8 Undertow server=Undertow.builder() 9 .addHttpListener(8080, "localhost").setHandler(new HttpHandler(){ //设置HttpHandler的回调方法10 @Override11 public void handleRequest(HttpServerExchange exchange)12 throws Exception { 13 exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");14 exchange.getResponseSender().send("This is my first insert server!");15 }16 }).build();17 server.start();18 }19 }
运行后打开浏览器输入 ,则页面输出“Hello World”字符串
2.Undertow来部署Servlet
1 import io.undertow.Handlers; 2 import io.undertow.Undertow; 3 import io.undertow.server.HttpHandler; 4 import io.undertow.server.handlers.PathHandler; 5 import io.undertow.servlet.Servlets; 6 import io.undertow.servlet.api.DeploymentInfo; 7 import io.undertow.servlet.api.DeploymentManager; 8 import io.undertow.servlet.api.ServletContainer; 9 import io.undertow.servlet.api.ServletInfo;10 11 import javax.servlet.ServletException;12 13 import org.wildfly.undertow.quickstart.servlet.MyServlet;14 15 public class ServletServer {16 17 public static void main(String[] args) {18 19 /*20 * 创建ServletInfo,Servelt的最小单位。是对javax.servlet.Servlet具体实现的再次封装。21 * 注意:ServletInfo的name必须是唯一的22 */23 ServletInfo servletInfo1 = Servlets.servlet("MyServlet",24 MyServlet.class);25 // 创建servletInfo的初始化参数26 servletInfo1.addInitParam("message", "This is my first MyServlet!");27 // 绑定映射为/myServlet28 servletInfo1.addMapping("/myServlet");29 /**30 * 创建包部署对象,包含多个servletInfo。可以认为是servletInfo的集合31 */32 DeploymentInfo deploymentInfo1 = Servlets.deployment();33 // 指定ClassLoader34 deploymentInfo1.setClassLoader(ServletServer.class.getClassLoader());35 // 应用上下文(必须与映射路径一致,否则sessionId会出现问题,每次都会新建)36 deploymentInfo1.setContextPath("/myapp");37 // 设置部署包名38 deploymentInfo1.setDeploymentName("myServlet.war");39 // 添加servletInfo到部署对象中40 deploymentInfo1.addServlets(servletInfo1);41 /**42 * 使用默认的servlet容器,并将部署添加至容器43 * 容器,用来管理DeploymentInfo,一个容器可以添加多个DeploymentInfo44 */45 ServletContainer container = Servlets.defaultContainer();46 /**47 * 将部署添加至容器并生成对应的容器管理对象48 * 包部署管理。是对添加到ServletContaint中DeploymentInfo的一个引用,用于运行发布和启动容器49 */50 DeploymentManager manager = container.addDeployment(deploymentInfo1);51 // 实施部署52 manager.deploy();53 /**54 * 分发器:将用户请求分发给对应的HttpHandler55 */56 PathHandler pathHandler = Handlers.path();57 /**58 * servlet path处理器,DeploymentManager启动后返回的Servlet处理器。59 */60 HttpHandler myApp=null;61 try {62 //启动容器,生成请求处理器63 myApp=manager.start();64 } catch (ServletException e) {65 throw new RuntimeException("容器启动失败!");66 }67 //绑定映射关系68 pathHandler.addPrefixPath("/myapp", myApp);69 70 Undertow server=Undertow.builder().71 //绑定端口号和主机72 addHttpListener(8081, "localhost")73 //设置分发处理器74 .setHandler(pathHandler).build();75 //启动server76 server.start();77 }78 }
自定义MyServlet
1 import java.io.IOException; 2 import java.io.PrintWriter; 3 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 public class MyServlet extends HttpServlet {10 11 private static final long serialVersionUID = 2378494112650465478L;12 13 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {14 doPost(req, resp);15 }16 17 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {18 PrintWriter writer = resp.getWriter();19 writer.write(""+this.getInitParameter("message")+"
");20 writer.close();21 }22 23 }
如下图,是本人抽象出Undertow生成应用的架构:
示例运行:
在浏览器地址栏里输入:http://localhost:8081/myapp/myServlet,界面上会显示ServletInfo的初始化参数message数据
Undertow jar包:
Undertow 官网API地址: