Spring boot - 如何获取运行端口和IP地址 [英] Spring boot - how to get running port and ip address

查看:162
本文介绍了Spring boot - 如何获取运行端口和IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在启动 Spring Boot 应用程序时通过 shell 脚本传递端口.想知道如何在应用程序中获取运行端口和系统IP地址以打印在日志文件中.

I am passing the port via shell script while starting the spring boot application. Would like to know how to get the running port and system ip address in the application to print in log file.

脚本:-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9890

script: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9890

推荐答案

您可以通过以下方式在任何组件类中自动装配端口号

You can autowire port number in any component class in following way

// Inject which port we were assigned
@Value("${local.server.port}")
int port;

或者使用注解@LocalServerPort

@LocalServerPort
private int port;

和主机地址如下

String ip = InetAddress.getLocalHost().getHostAddress()

这篇关于Spring boot - 如何获取运行端口和IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆