springboot切换使用undertow容器的方式

目录
  • springboot切换使用undertow容器
    • maven引入jar
    • undertow的基本配置
    • 一个特别的报错警告
    • 验证成功
    • 分享感觉
  • springboot替换默认容器
    • undertow简介
    • 性能比对
    • 项目中使用undertow 1.引入依赖

springboot切换使用undertow容器

maven引入jar

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 默认是使用的tomcat -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- undertow容器支持 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

undertow的基本配置

#undertow容器配置开始
# 是否打开 undertow 日志,默认为 false
server.undertow.accesslog.enabled=false
# 设置访问日志所在目录
server.undertow.accesslog.dir=logs
# 指定工作者线程的 I/0 线程数,默认为 2 或者 CPU 的个数
server.undertow.threads.io=8
# 指定工作者线程个数,默认为 I/O 线程个数的 8 倍
server.undertow.threads.worker=256
# 设置 HTTP POST 内容的最大长度,默认不做限制
server.undertow.max-http-post-size=4MB
# 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理;
server.undertow.buffer-size=1024
# 是否分配的直接内存(NIO直接分配的堆外内存)
server.undertow.direct-buffers=true
#undertow容器配置结束

其他配置可以先看springboot的autoconfig配置类这块的配置:

org.springframework.boot.autoconfigure.web包下的ServerProperties、servlet、embedded的undertowxxx类

一个特别的报错警告

解决使用undertow容器报io.undertow.websockets.jsr -

UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used

处理:

新增一个component注解的类,具体如下:

@Component
public class UndertowPoolCustomizer implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
    @Override
    public void customize(UndertowServletWebServerFactory factory) {
        factory.addDeploymentInfoCustomizers(deploymentInfo -> {
            WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
            webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, 1024));
            deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo);
        });
    }
}

验证成功

看到Undertow started xxx就是使用undertow容器启动成功了。

分享感觉

网传undertow比tomcat、jetty都快省资源,还是费阻塞nio等等,实际上可能就没有什么感觉。

我其实用postman测试了以前的一些接口,感觉接口返回秒回,就是感觉快了。

后来运行2天(没有配置undertow,默认配置)有点小卡,然后,早上把配置改成上面的发布,再观察几天试试。

springboot替换默认容器

undertow简介

Undertow 是红帽公司开发的一款基于 NIO 的高性能 Web 嵌入式服务器

Undertow 被设计成为完全可嵌入式,所以也叫做可嵌入式容器,可以很好的嵌入在SpringBoot中

性能比对

使用jmeter进行压测比较

tomcat压测结果

将tomcat容器换成jetty容器进行测试

将jetty容器修改为undertow

从吞吐量看undertow要强于前两个

项目中使用undertow 1.引入依赖

在官网上可以看到undertow主要有两个版本

2.1

The current stable Servlet 4.0 branch, requires JDK8 or above

1.4

The current stable Servlet 3.1 branch, supports JDK7

可以根据自己的servlet和jdk版本进行选择,我们这里使用2.1版本

<dependency>
    <groupId>io.undertow</groupId>
    <artifactId>undertow-core</artifactId>
    <version>2.1.0.Final</version>
</dependency>
<dependency>
    <groupId>io.undertow</groupId>
    <artifactId>undertow-servlet</artifactId>
    <version>2.1.0.Final</version>
</dependency>
<dependency>
    <groupId>io.undertow</groupId>
    <artifactId>undertow-websockets-jsr</artifactId>
    <version>2.1.0.Final</version>
</dependency>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 关于springboot-starter-undertow和tomcat的区别说明

    目录 什么是tomcat tomcat的作用 javaweb项目都需要tomcat? Java前后端分离的核心思想 springboot内置的tomcat undertow和tomcat的区别 部署jar和war包 springboot下比较tomcat与undertow性能 第一步 第二步 第三步 第四步 第五步 什么是tomcat 在说undertow和tomcat区别之前,先说下tomcat是什么(如果知道了可以跳过哦!) Tomcat:免费开源,轻量级应用服务器,在中小型系统和并发访问用

  • SpringBoot如何使用Undertow做服务器

    目录 使用Undertow做服务器 说明 NIO(非阻塞式输入输出) 快速开始 undertow警告Buffer pool was not set on WebSocketDeploymentInfo 使用Undertow做服务器 说明 undertow,jetty和tomcat可以说是javaweb项目当下最火的三款服务器,tomcat是apache下的一款重量级的服务器,不用多说历史悠久,经得起实践的考验. 然而:当下微服务兴起,spring boot ,spring cloud 越来越热的

  • 记一次springboot中用undertow的坑

    目录 springboot中用undertow的坑 本文实验的环境如下 环境准备 使用springboot2.2.11.springboot2.2.12.springboot2.2.13 如果是生产采用了上述几个版本的sringboot springboot需放弃Tomcat,选择Undertow吗? SpringBoot中的Tomcat容器 SpringBoot设置Undertow Tomcat与Undertow的优劣对比 springboot中用undertow的坑 场景:准备基于sprin

  • springboot切换使用undertow容器的方式

    目录 springboot切换使用undertow容器 maven引入jar undertow的基本配置 一个特别的报错警告 验证成功 分享感觉 springboot替换默认容器 undertow简介 性能比对 项目中使用undertow 1.引入依赖 springboot切换使用undertow容器 maven引入jar <dependency>     <groupId>org.springframework.boot</groupId>     <artif

  • SpringBoot配置嵌入式Servlet容器和使用外置Servlet容器的教程图解

    配置嵌入式Servlet容器 SpringBoot默认使用Tomcat作为嵌入式的Servlet容器: 问题? 1).如何定制和修改Servlet容器的相关配置: 1.修改和server有关的配置(ServerProperties[也是EmbeddedServletContainerCustomizer]): server.port=8081 server.context-path=/crud server.tomcat.uri-encoding=UTF-8 //通用的Servlet容器设置 s

  • springboot配置嵌入式servlet容器的方法

    配置嵌入式Servlet容器 springboot默认tomcat为嵌入式servlet容器,所以不用在配置tomcat. 1.如何定制修改servlet容器? 1.在applicatio.properties里修改和server有关的配置(推荐) 如: server.tomcat server.tomcat.connection-timeout= 连接超时时间 server.tomcat.uri-encoding=UTF-8 修改编码 server.servlet.XXX 通用servlet容

  • 浅谈SpringBoot @Autowired的两种注入方式

    Autowired有两种注入方式 by type by name 默认使用的是byType的方式向Bean里面注入相应的Bean.例如: @Autowired private UserService userService; 这段代码会在初始化的时候,在spring容器中寻找一个类型为UserService的bean实体注入,关联到userService的引入上. 但是如果UserService这个接口存在多个实现类的时候,就会在spring注入的时候报错,例如: public class Us

  • SpringBoot自动装配Condition的实现方式

    目录 1. 简介 2. 定义 2.1 @Conditional 2.2 Condition 3. 使用说明 3.1 创建项目 3.2 测试 3.3 小结 4. 改进 4.1 创建注解 4.2 修改UserCondition 5. Spring内置条件注解 1. 简介 @Conditional注解在Spring4.0中引入,其主要作用就是判断条件是否满足,从而决定是否初始化并向容器注册Bean. 2. 定义 2.1 @Conditional @Conditional注解定义如下:其内部只有一个参数

  • 关于Springboot如何获取IOC容器

    目录 Springboot项目中获取IOC容器的方式 方法一(不实用,不推荐): 方法二(强烈推荐): 总结 Springboot项目中获取IOC容器的方式 在Springboot项目中如果要获取IOC容器目前有两种方法. 方法一(不实用,不推荐): 在Springboot项目中都会存在一个SpringApplication的启动类,我们通过以下代码启动IOC容器. SpringApplication.run(Application.class, args); 其实run方法会将创建的IOC容器

  • SpringBoot 动态配置Profile环境的方式

    下面的例子是通过修改开发环境和生产环境的动态配置的端口号的示例: 开发环境端口号是 8081 生产环境端口号是 8082 springboot的配置方式 springboot的配置有两种:properties和yaml或yml properties方式配置 1.在application.properties配置环境 创建两个application-xx.properties 一个application-dev.properties #开发环境 一个application-pro.properti

  • springboot如何关掉tomcat容器

    目录 springboot关掉tomcat容器 springboot使用第三方tomcat 1.改pom 2.再加一个启动类 3.打war包 springboot关掉tomcat容器 有的时候需要对外提供的并不是HTTP服务,而是RPC服务,但是又想使用springboot提供的便利支持. 这个时候需要关掉RPC服务,然后在main函数中自己添加守护线程 public static void main(String[] args) { SpringApplication app = new Sp

  • SpringBoot加载配置文件的实现方式总结

    目录 一.简介 二.代码实践 2.1.通过@value注解实现参数加载 2.2.通过@ConfigurationProperties注解实现参数加载 2.3.通过@PropertySource注解实现配置文件加载 2.4.通过自定义环境处理类,实现配置文件的加载 2.5.最后,我们来介绍一下yml文件读取 三.小结 一.简介 在实际的项目开发过程中,我们经常需要将某些变量从代码里面抽离出来,放在配置文件里面,以便更加统一.灵活的管理服务配置信息.比如,数据库.eureka.zookeeper.r

  • SpringBoot中@Import注解的使用方式

    目录 一. @Import引入普通类 二. @Import引入配置类(@Configuration修饰的类) 三 .@Import引入ImportSelector的实现类 3.1 静态import场景(注入已知的类) 3.2 动态import场景(注入指定条件的类) 四. @Import引入ImportBeanDefinitionRegistrar的实现类 前言: @Import注解用来帮助我们把一些需要定义为Bean的类导入到IOC容器里面.下面我们就对@Import注解的使用做一个简单的总结

随机推荐