Spring Boot中的嵌入式服务器配置

简介: Spring Boot中的嵌入式服务器配置

Spring Boot中的嵌入式服务器配置

今天我们来探讨一下如何在Spring Boot中配置嵌入式服务器。

一、Spring Boot中的嵌入式服务器简介

Spring Boot内置了几种常见的服务器,如Tomcat、Jetty和Undertow。默认情况下,Spring Boot使用Tomcat作为嵌入式服务器。通过嵌入式服务器,Spring Boot应用可以以独立的Java应用程序运行,无需外部的Web服务器。

二、配置嵌入式服务器

1. 修改默认端口

默认情况下,Spring Boot使用端口8080。可以在application.propertiesapplication.yml文件中配置端口号。

application.properties

server.port=9090

application.yml

server:
  port: 9090

2. 配置Tomcat嵌入式服务器

可以通过编程方式配置Tomcat嵌入式服务器的属性。例如,配置最大线程数和连接超时时间。

代码示例

package cn.juwatech.config;

import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TomcatConfig {
   

    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> customizer() {
   
        return factory -> {
   
            factory.setPort(9090);
            factory.setContextPath("/myapp");
            factory.addConnectorCustomizers(connector -> {
   
                connector.setProperty("maxThreads", "200");
                connector.setProperty("connectionTimeout", "20000");
            });
        };
    }
}

3. 使用Jetty作为嵌入式服务器

如果希望使用Jetty作为嵌入式服务器,可以在pom.xml中引入相应的依赖,并排除Tomcat依赖。

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <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>

4. 使用Undertow作为嵌入式服务器

类似地,可以在pom.xml中引入Undertow的依赖,并排除Tomcat依赖。

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <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>

5. 配置SSL

可以通过配置SSL来增强嵌入式服务器的安全性。需要在application.propertiesapplication.yml文件中配置SSL相关的属性。

application.properties

server.port=8443
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=yourpassword
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias=tomcat

三、嵌入式服务器高级配置

1. 自定义Servlet、Filter和Listener

可以通过@Bean方式自定义Servlet、Filter和Listener。

代码示例

package cn.juwatech.config;

import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfig {
   

    @Bean
    public ServletRegistrationBean<Servlet> myServlet() {
   
        return new ServletRegistrationBean<>(new HttpServlet() {
   
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
                resp.getWriter().write("Hello, this is my custom servlet!");
            }
        }, "/myServlet");
    }

    @Bean
    public FilterRegistrationBean<Filter> myFilter() {
   
        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter((request, response, chain) -> {
   
            System.out.println("This is my custom filter");
            chain.doFilter(request, response);
        });
        registrationBean.addUrlPatterns("/*");
        return registrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean<ServletContextListener> myListener() {
   
        return new ServletListenerRegistrationBean<>(new ServletContextListener() {
   
            @Override
            public void contextInitialized(ServletContextEvent sce) {
   
                System.out.println("This is my custom listener");
            }

            @Override
            public void contextDestroyed(ServletContextEvent sce) {
   
            }
        });
    }
}

四、总结

Spring Boot的嵌入式服务器配置为我们提供了灵活性和便利性。无论是使用默认的Tomcat,还是替换为Jetty或Undertow,都可以通过简单的配置来实现。同时,通过配置SSL、自定义Servlet、Filter和Listener等高级配置,可以进一步增强应用的功能和安全性。冬天不穿秋裤,天冷也要风度,注意咯,我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编!

相关文章
|
2月前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
2月前
|
编译器 开发工具 C语言
交叉编译器环境配置与boa嵌入式web服务器移植问题
交叉编译器环境配置与boa嵌入式web服务器移植问题
82 0
|
6天前
|
运维 Java 测试技术
Spring运维之boo项目表现层测试加载测试的专用配置属性以及在JUnit中启动web服务器发送虚拟请求
Spring运维之boo项目表现层测试加载测试的专用配置属性以及在JUnit中启动web服务器发送虚拟请求
13 3
|
6天前
|
Java Maven
springboot项目打jar包后,如何部署到服务器
springboot项目打jar包后,如何部署到服务器
18 1
|
2月前
|
Java 关系型数据库 MySQL
保姆级教程——将springboot项目部署到阿里云服务器包含环境配置(小白包会)
本文档详细介绍了将SpringBoot项目部署到阿里云服务器的步骤。首先,通过Xshell连接服务器,使用公网IP地址。接着,下载JDK的Linux版本,使用XFTP上传并解压,配置环境变量。然后,安装MySQL 5.7,包括下载YUM源、安装、启动服务以及修改root密码和开启远程访问。最后,将SpringBoot项目打包成jar,上传至服务器,使用`java -jar`命令运行,通过`nohup`确保服务持续运行。配置安全组以允许远程访问。
|
2月前
|
算法 安全 Java
服务器启动 SpringBoot 后访问特别慢的解决方案
服务器启动 SpringBoot 后访问特别慢的解决方案
70 1
|
6天前
|
Java
springboot自定义拦截器,校验token
springboot自定义拦截器,校验token
20 6
|
4天前
|
Java 数据库连接 数据库
Spring Boot 集成 MyBatis-Plus 总结
Spring Boot 集成 MyBatis-Plus 总结
|
3天前
|
NoSQL 搜索推荐 Java
使用Spring Boot实现与Neo4j图数据库的集成
使用Spring Boot实现与Neo4j图数据库的集成
|
6天前
|
Java 关系型数据库 MySQL
Mybatis入门之在基于Springboot的框架下拿到MySQL中数据
Mybatis入门之在基于Springboot的框架下拿到MySQL中数据
15 4