1. Overview
1.概述
This tutorial will discuss a feature added in Spring Boot 2.5 that enables the possibility to specify a prefix for system environment variables. This way, we can run multiple different Spring Boot applications in the same environment as all properties will expect a prefixed version.
本教程将讨论Spring Boot 2.5中增加的一个功能,即可以为系统环境变量指定前缀。这样,我们就可以在同一个环境中运行多个不同的Spring Boot应用程序,因为所有的属性都将期望有一个前缀的版本。
2. Environment Variable Prefixes
2.环境变量的前缀
We might need to run multiple Spring Boot applications in the same environment and often face the problem of environment variable names to assign to different properties.
我们可能需要在同一环境中运行多个Spring Boot应用程序,并且经常面临要分配给不同属性的环境变量名称问题。
We could use Spring Boot properties which, in a way, can be similar, but we also might want to set a prefix at the application level to leverage on the environment side.
我们可以使用Spring Boot properties在某种程度上,它可以类似,但我们也可能希望在应用层面设置一个前缀,以便在环境方面利用。
Let’s set up, as an example, a simple Spring Boot application, and modify an application property, for example, the tomcat server port, by setting this prefix.
让我们以一个简单的Spring Boot应用为例,通过设置这个前缀来修改一个应用属性,例如,Tomcat服务器端口。
2.1. Our Spring Boot Application
2.1.我们的Spring Boot应用
Let’s create a Spring Boot application to demonstrate this feature. First, let’s add a prefix to the application. We call it “prefix” to keep it simple:
让我们创建一个Spring Boot应用程序来演示这一功能。首先,让我们为该应用程序添加一个前缀。我们将其称为”prefix”以保持简单:
@SpringBootApplication
public class PrefixApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(PrefixApplication.class);
application.setEnvironmentPrefix("prefix");
application.run(args);
}
}
We can’t use, as a prefix, a word that already contains the underscore character (_). Otherwise, the application will throw an error.
我们不能使用一个已经包含下划线字符(_)的词作为前缀。否则,应用程序将抛出一个错误。
We also want to make an endpoint to check at what port our application is listening to:
我们还想做一个端点来检查我们的应用程序在监听什么端口。
@Controller
public class PrefixController {
@Autowired
private Environment environment;
@GetMapping("/prefix")
public String getServerPortInfo(final Model model) {
model.addAttribute("serverPort", environment.getProperty("server.port"));
return "prefix";
}
}
In this case, we are using Thymeleaf to resolve our template while setting our server port, with a simple body like:
在这种情况下,我们使用Thymeleaf来解决我们的模板,同时设置我们的服务器端口,其简单的主体是这样。
<html>
// ...
<body>
It is working as we expected. Your server is running at port : <b th:text="${serverPort}"></b>
</body>
</html>
2.2. Setting Environment Variables
2.2.设置环境变量
We can now set our environment variable like prefix_server_port to, for example, 8085. We can see how to set system environment variables, for instance, in Linux.
我们现在可以将我们的环境变量如prefix_server_port设置为,例如,8085。我们可以看看如何设置系统环境变量,例如,在Linux。
Once we set the environment variable, we expect the application to create properties based on that prefix.
一旦我们设置了环境变量,我们希望应用程序能根据这个前缀来创建属性。
In the case of running from an IDE, we need to edit the launch configuration and add the environment variable or pick it from environment variables that are already loaded.
在从IDE运行的情况下,我们需要编辑启动配置并添加环境变量,或者从已经加载的环境变量中挑选。
2.3. Running the Application
2.3.运行应用程序
We can now start the application from the command line or with our favorite IDE.
现在我们可以从命令行或用我们最喜欢的IDE启动该应用程序。
If we load with our browser the URL http://localhost:8085/prefix, we can see that the server is running and at the port, we prefixed earlier:
如果我们用浏览器加载URL http://localhost:8085/prefix,我们可以看到服务器正在运行,而且是在我们之前预设的端口。
It is working as we expected. Your server is running at port : 8085
The application will start using default environment variables if not prefixed.
如果不加前缀,应用程序将使用默认环境变量启动。
3. Conclusion
3.总结
In this tutorial, we have seen how to use a prefix for environment variables with Spring Boot. It can help, for example, if we want to run multiple Spring Boot applications in the same environment and assign different values to a property with the same name, such as the server port.
在本教程中,我们看到了如何用Spring Boot为环境变量使用前缀。例如,如果我们想在同一个环境中运行多个Spring Boot应用程序,并为一个同名的属性(如服务器端口)指定不同的值,那么它就会有帮助。
As always, the code presented in this article is available over on GitHub.
一如既往,本文介绍的代码可在GitHub上获得over。