简体   繁体   English

我应该在基于 Spring-boot 的后端中使用无头模式吗?

[英]Should I use headless mode in Spring-boot based backend?

I'm working on a application based on a simple monolithic architecture:rest API backend with spring-boot + frontend with a Js framework.我正在开发一个基于简单单片架构的应用程序:rest API 后端与 spring-boot + 前端与 Js 框架。

I have been reading about the headless mode when running java apps and I would like to know if running the embedded tomcat with my spring-boot application in headless mode is a good idea.在运行 java 应用程序时,我一直在阅读有关无头模式的信息,我想知道在无头模式下使用我的 spring-boot 应用程序运行嵌入式 tomcat 是否是个好主意。

In general, running an application in a console requires some sort of terminal to write to.通常,在控制台中运行应用程序需要某种终端来写入。

In Unix-based systems, these terminals are referred to as tty .在基于 Unix 的系统中,这些终端称为tty

This implies the following:这意味着以下内容:

Whenever you open a connection to your server via SSH, you get a tty.每当您通过 SSH 打开与服务器的连接时,您都会得到一个 tty。 Your non-headless application will run in that tty until the tty is closed.您的非无头应用程序将在该 tty 中运行,直到 tty 关闭。 To avoid that, you'd need something like screen .为避免这种情况,您需要类似screen的东西。

For Windows systems: If you close the cmd window of the console/sign off the user, the application server will also stop running.对于 Windows 系统:如果关闭控制台的 cmd window/注销用户,应用服务器也将停止运行。

That's why productive installations are usually headless and service based.这就是为什么生产性安装通常是无头和基于服务的。

So much for the background, the official docs actually state the following:背景介绍了这么多, 官方文档其实state如下:

In addition to running Spring Boot applications by using java -jar, it is also possible to make fully executable applications for Unix systems.除了使用 java -jar 运行 Spring 引导应用程序之外,还可以为 Unix 系统制作完全可执行的应用程序。 A fully executable jar can be executed like any other executable binary or it can be registered with init.d or systemd.完全可执行的 jar 可以像任何其他可执行二进制文件一样执行,也可以在 init.d 或 systemd 中注册。 This helps when installing and managing Spring Boot applications in common production environments.这有助于在常见生产环境中安装和管理 Spring 引导应用程序。

This is described here .在此处进行了描述。

Generally you don't need to specify -Djava.awt.headless=true because since around Java 1.8 the JRE determines itself whether a graphics environment is available or not and switches into headless mode if no graphics environment is available.通常,您不需要指定-Djava.awt.headless=true ,因为从 Java 1.8 开始,JRE 会自行确定图形环境是否可用,如果没有图形环境可用,则切换到无头模式。

Relevant source in Java 11: https://github.com/openjdk/jdk11/blob/master/src/java.desktop/share/classes/java/awt/GraphicsEnvironment.java#L166 Java 11中的相关来源: https://github.com/openjdk/jdk11/blob/master/src/java.desktop/share/classes/java/awt/GraphicsEnvironment.java#L166

  • if you are running a Unix-like operating system and the DISPLAY environment variable is not set the headless is true如果您运行的是类 Unix 操作系统并且未设置DISPLAY环境变量,则headless为 true
  • on Windows it is assumed that a graphics device is always present and therefore headless is false在 Windows 上,假定图形设备始终存在,因此headless是错误的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM