如何配置Spring Boot应用程序侦听的TCP/IP端口,使其不使用默认端口8080。
当前回答
希望这篇文章能有所帮助
application.properties=> server.port=8090 application.yml=> server port:8090
其他回答
根据所使用的应用程序配置文件,有三种方法可以做到这一点
a)如果您正在使用应用程序。属性文件集
server.port = 8090
b)如果您正在使用应用程序。yml文件设置YAML格式的服务器端口属性,如下所示
server:
port: 8090
c)也可以在main方法中将属性设置为System属性
System.setProperty("server.port","8090");
正如Spring文档中解释的那样,有几种方法可以做到这一点:
您可以在命令行中设置端口(例如8888)
-Dserver。Port =8888或——server.port=8888
例如:java -jar -Dserver。端口= 8888 test.jar
或者在application.properties中设置端口
server.port=${port:4588}
或(在申请中)。Yml和yaml语法)
server:
port: ${port:4588}
如果在命令行中设置了-Dport(或-Dserver.port)传递的端口,则将考虑该端口。如果不是,则端口默认为4588。
如果你想在属性文件中强制端口,不管环境变量是什么,你只需要写:
server.port=8888
如文档中所说,要么设置服务器。端口作为系统属性使用命令行选项jvm -Dserver。端口=8090或添加应用程序。/src/main/resources/ with中的属性
server.port=8090
随机使用端口:
server.port=0
类似地,添加应用程序。Yml /src/main/resources/
server:
port: 8090
您还可以使用SERVER_PORT环境变量来配置Spring Boot端口。只需设置环境变量并重新启动应用程序:
set SERVER_PORT=9999 // on windows machine
export SERVER_PORT=9999 // on linux
请注意,如果你没有在系统范围内设置这些环境变量,你应该在同一个会话上运行引导应用程序。
由于Spring Boot提供了各种配置外部化机制(通过各种PropertySource实现和/或按顺序连接到Environment对象的处理器),您可以通过以下方法设置jar存档之外的任何属性:
Pass property through command line argument as application argument java -jar <path/to/my/jar> --server.port=7788 From property in SPRING_APPLICATION_JSON (Spring Boot 1.3.0+) Define environment variable in U*IX shell: SPRING_APPLICATION_JSON='{"server.port":7788}' java -jar <path/to/my/jar> By using Java system property: java -Dspring.application.json='{"server.port":7788}' -jar <path/to/my/jar> Pass through command line argument: java -jar <path/to/my/jar> --spring.application.json='{"server.port":7788}' Define JVM system property java -Dserver.port=7788 -jar <path/to/my/jar> Define OS environment variable U*IX Shell SERVER_PORT=7788 java -jar <path/to/my/jar> Windows SET SERVER_PORT=7788 java -jar <path/to/my/jar> Place property in ./config/application.properties configuration file server.port=7788 and run: java -jar <path/to/my/jar> Place property in ./config/application.yaml server: port: 7788 and run: java -jar <path/to/my/jar> Place property in ./application.properties server.port=7788 and run: java -jar <path/to/my/jar> Place property in ./application.yaml server: port: 7788 and run: java -jar <path/to/my/jar>
您可以将上述方法组合在一起,列表中的前一个配置优先于后一个配置。
例如:
SERVER_PORT=2266 java -Dserver.port=5566 -jar <path/to/my/jar> --server.port=7788
服务器将在端口7788上启动并监听。
这非常有用,在PropertySources中提供优先级较低的默认属性(通常打包在存档中或编码在源代码中),然后在运行时环境中覆盖它。这就是Spring Boot的设计理念:
坚持己见,但当需求开始偏离默认值时,要迅速离开。
SERVER_NAME到server.name的转换由放松绑定完成。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何找到哪个程序正在使用端口80在Windows?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder