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的方式向界面输出字符串

 import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers; public class HelloWorldServer {
public static void main(String[] args) {
Undertow server=Undertow.builder()
.addHttpListener(8080, "localhost").setHandler(new HttpHandler(){//设置HttpHandler的回调方法
@Override
public void handleRequest(HttpServerExchange exchange)
throws Exception {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
exchange.getResponseSender().send("This is my first insert server!");
}
}).build();
server.start();
}
}

   运行后打开浏览器输入 http://localhost:8080 ,则页面输出“Hello World”字符串

2.Undertow来部署Servlet

 import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.PathHandler;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;
import io.undertow.servlet.api.ServletContainer;
import io.undertow.servlet.api.ServletInfo; import javax.servlet.ServletException; import org.wildfly.undertow.quickstart.servlet.MyServlet; public class ServletServer { public static void main(String[] args) { /*
* 创建ServletInfo,Servelt的最小单位。是对javax.servlet.Servlet具体实现的再次封装。
* 注意:ServletInfo的name必须是唯一的
*/
ServletInfo servletInfo1 = Servlets.servlet("MyServlet",
MyServlet.class);
// 创建servletInfo的初始化参数
servletInfo1.addInitParam("message", "This is my first MyServlet!");
// 绑定映射为/myServlet
servletInfo1.addMapping("/myServlet");
/**
* 创建包部署对象,包含多个servletInfo。可以认为是servletInfo的集合
*/
DeploymentInfo deploymentInfo1 = Servlets.deployment();
// 指定ClassLoader
deploymentInfo1.setClassLoader(ServletServer.class.getClassLoader());
// 应用上下文(必须与映射路径一致,否则sessionId会出现问题,每次都会新建)
deploymentInfo1.setContextPath("/myapp");
// 设置部署包名
deploymentInfo1.setDeploymentName("myServlet.war");
// 添加servletInfo到部署对象中
deploymentInfo1.addServlets(servletInfo1);
/**
* 使用默认的servlet容器,并将部署添加至容器
* 容器,用来管理DeploymentInfo,一个容器可以添加多个DeploymentInfo
*/
ServletContainer container = Servlets.defaultContainer();
/**
* 将部署添加至容器并生成对应的容器管理对象
* 包部署管理。是对添加到ServletContaint中DeploymentInfo的一个引用,用于运行发布和启动容器
*/
DeploymentManager manager = container.addDeployment(deploymentInfo1);
// 实施部署
manager.deploy();
/**
* 分发器:将用户请求分发给对应的HttpHandler
*/
PathHandler pathHandler = Handlers.path();
/**
* servlet path处理器,DeploymentManager启动后返回的Servlet处理器。
*/
HttpHandler myApp=null;
try {
//启动容器,生成请求处理器
myApp=manager.start();
} catch (ServletException e) {
throw new RuntimeException("容器启动失败!");
}
//绑定映射关系
pathHandler.addPrefixPath("/myapp", myApp); Undertow server=Undertow.builder().
//绑定端口号和主机
addHttpListener(8081, "localhost")
//设置分发处理器
.setHandler(pathHandler).build();
//启动server
server.start();
}
}

 
自定义MyServlet
 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { private static final long serialVersionUID = 2378494112650465478L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
} protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.write("<p style='color:red;text-align:center;'>"+this.getInitParameter("message")+"</p>");
writer.close();
} }

如下图,是本人抽象出Undertow生成应用的架构:


示例运行:

在浏览器地址栏里输入:http://localhost:8081/myapp/myServlet,界面上会显示ServletInfo的初始化参数message数据


Undertow jar包:点击下载

Undertow 官网API地址:http://undertow.io/index.html


WildFly8(JBoss)默认web服务器-------Undertow的更多相关文章

  1. 高性能非阻塞 Web 服务器 Undertow

    Undertow 简介 Undertow是一个用java编写的.灵活的.高性能的Web服务器,提供基于NIO的阻塞和非阻塞API. Undertow的架构是组合式的,可以通过组合各种小型的目的单一的处 ...

  2. [ 转 ] 为 phpstorm 自定义默认 Web 服务器

    phpstorm自带web 服务器,可以直接执行调试,这个之前的文章专门讲过,可以看下. 同时你也可以选择在phpstorm集成apache服务器,下面是我自己的亲测的步骤. 如何修改apache默认 ...

  3. 转:Red Hat JBoss团队发布WildFly 8,全面支持Java EE 7并包含全新的嵌入式Web服务器

    原文来自于:http://www.infoq.com/cn/news/2014/02/wildfly8-launch Red Hat的JBoss部门今天宣布WildFly 8正式发布.其前身是JBos ...

  4. asp.net core 系列 18 web服务器实现

    一. ASP.NET Core Module 在介绍ASP.NET Core Web实现之前,先来了解下ASP.NET Core Module.该模块是插入 IIS 管道的本机 IIS 模块(本机是指 ...

  5. net core web服务器实现

    net core 系列 18 web服务器实现 一. ASP.NET Core Module 在介绍ASP.NET Core Web实现之前,先来了解下ASP.NET Core Module.该模块是 ...

  6. Visual Studio中用于ASP.NET Web项目的Web服务器

    当您在 Visual Studio 中开发 Web 项目时,需要 Web 服务器才能测试或运行它们. 利用 Visual Studio,您可以使用不同的 Web 服务器进行测试,包括 IIS Expr ...

  7. ASP.NET Core技术研究-全面认识Web服务器Kestrel

    因为IIS不支持跨平台的原因,我们在升级到ASP.NET Core后,会接触到一个新的Web服务器Kestrel.相信大家刚接触这个Kestrel时,会有各种各样的疑问. 今天我们全面认识一下ASP. ...

  8. WEB服务器:Apache、Tomcat、JBoss、WebLogic、Websphere、IIS的区别与关系

    1)Apache  免费,世界使用排名第一的Web服务器.它可以运行在几乎所有广泛使用的计算机平台上.Apache的特点是简单.速度快.性能稳定,并可做代理服务器来使用.Apache是以进程为基础的结 ...

  9. web服务器-nginx默认网站

    web服务器-nginx默认网站 一 默认网站 server { listen 80; server_name localhost; location / { root html; index ind ...

随机推荐

  1. AngularJS中Directive指令系列 - 基本用法

    参考: https://docs.angularjs.org/api/ng/service/$compile http://www.zouyesheng.com/angular.html Direct ...

  2. Python解包参数列表及 Lambda 表达式

    解包参数列表 当参数已经在python列表或元组中但需要为需要单独位置参数的函数调用解包时,会发生相反的情况.例如,内置的 range() 函数需要单独的 start 和 stop 参数.如果它们不能 ...

  3. HDU-6315:Naive Operations(线段树+思维)

    链接:HDU-6315:Naive Operations 题意: In a galaxy far, far away, there are two integer sequence a and b o ...

  4. 【第七章】MySQL数据库备份-物理备份

    一.数据库备份 备份的目的: 备份: 能够防止由于机械故障以及人为误操作带来的数据丢失,例如将数据库文件保存在了其它地方. 冗余: 数据有多份冗余,但不等备份,只能防止机械故障还来的数据丢失,例如主备 ...

  5. 5.airflow问题

    1. Traceback (most recent call last): File "/usr/bin/airflow", line 28, in <module> ...

  6. Android连接SQLServer详细教程(数据库+服务器+客户端)

    摘星 标签: android连接sql http://blog.csdn.net/haoxingfeng/article/details/9111105

  7. Programming Protocol-Independent Packet Processors

    引言 OpenFlow协议固定的包头域数目,使得南向协议过于死板. P4可以实现自定义包头,增加灵活性. P4是OpenFlow未来发展的方向. We propose P4 as a strawman ...

  8. Beta版冲刺前准备

    [团队概要] 团队项目名:小葵日记 团队名:日不落战队 队员及角色: 队员 角色 备注 安琪 前端工程师 队长 佳莹 前端工程师 智慧 后端工程师 章鹏 后端工程师 语恳 UI设计师 炜坤 前端工程师 ...

  9. HDU 5154 Harry and Magical Computer 有向图判环

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5154 题解: 有向图判环. 1.用dfs,正在访问的节点标记为-1,已经访问过的节点标记为1,没有访 ...

  10. <浪潮之巅>读书笔记

    <浪潮之巅>这本书通过介绍AT&T.IBM.微软.苹果.google等IT公司的发展历史,揭示科技工业的胜败规律,说明这些公司是如何在每一次科技革命浪潮到来时站在浪尖,实现跨越式发 ...