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. DSP28335声音降噪(未完成)

    1. 确定使用的模块是Webrtc-NS,采集声音的芯片TLV32AIC23,实际测试发现Webrtc-NS无法使用,所以改成FIR滤波器. 从时域特性上来看,数字滤波器还可以分为有限冲激响应数字滤波 ...

  2. rpmforge

    Could not retrieve mirrorlist http://mirrorlist.repoforge.org/el6/mirrors-rpmforge error was : PYCUR ...

  3. 【Unity3d】MenuItem修饰的方法无法触发的可能原因

    遇到了MenuItem修饰的方法无法触发的情况,顺利解决. 类放在Editor目录下,该类下其他方法被MenuItem修饰可以触发. 后来发现我修饰的方法和该类下另一个方法重名了. 改方法名,问题解决 ...

  4. c# 实体类怎么给LIST赋值,table转LIST

    /// <summary> /// 缓存客服集合信息 /// </summary> public class model { /// <summary> /// 客 ...

  5. Python数据可视化的10种技能

    今天我来给你讲讲Python的可视化技术. 如果你想要用Python进行数据分析,就需要在项目初期开始进行探索性的数据分析,这样方便你对数据有一定的了解.其中最直观的就是采用数据可视化技术,这样,数据 ...

  6. [leetcode]三数之和

    三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...

  7. Echarts简单图表

    一.实现要点 常用可视化图表库 Echarts HighCharts D3.js neo4j (NOSQL) 1.导入js库 <script type="text/javascript ...

  8. 当git遇上中文乱码

    git有个比较奇怪的问题,当目录或者文件名中出现了中文的时候,在执行git status 的时候,会返回一串unicode码,这段unicode码就读不懂了,必须解决. git status显示uni ...

  9. access数据库频繁读取操作会出现 System.Data.OleDb.OleDbException 的异常解决

    asp.net access数据库 本来想着打开一个access数据库连接后,不关闭,下次操作数据了,直接拿来用,谁知道连着测试64次后(大概这么多次),就会出现System.Data.OleDb.O ...

  10. 2.openldap安装

    1.安装步骤如下 获取软件包 安装软件包(rpm或者源码编译) 生产openldap配置文件及数据库文件 配置 添加目录树条目 加载slapd进程 验证 2.所需安装包说明 openldap,open ...