Java Spring boot - 如何获取运行端口和IP地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51628943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:10:46  来源:igfitidea点击:

Spring boot - how to get running port and ip address

javaspringspring-boot

提问by Jessie

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.

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

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

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

回答by GolamMazid Sajib

If you want to get it after application running try with this:

如果你想在应用程序运行后得到它,试试这个:

@Component
public class ApplicationLoader implements ApplicationRunner {

    @Autowired
    private Environment environment;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(environment.getProperty("java.rmi.server.hostname"));
        System.out.println(environment.getProperty("local.server.port"));
        System.out.println(InetAddress.getLocalHost().getHostAddress());
    }
}

You can get port many ways:

您可以通过多种方式获取端口:

@Value("${local.server.port}")
private int serverPort;

or

或者

@LocalServerPort
private int serverPort;

回答by Emdadul Sawon

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

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

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

Or with the annotation @LocalServerPort

或者用注释 @LocalServerPort

@LocalServerPort
private int port;

And host address with following

和主机地址如下

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

回答by tsarenkotxt

When you pass any parameters from the script, you can get this at runtime:

当你从脚本中传递任何参数时,你可以在运行时得到这个:

private String jmxRemote = System.getProperty("com.sun.management.jmxremote"); 
private String jmxRemotePort = System.getProperty("com.sun.management.jmxremote.port");

Get properties either the JVM itself or any -D options you may have passed at the command line

获取 JVM 本身或您可能在命令行中传递的任何 -D 选项的属性

Get ip:

获取ip:

// for example 127.0.0.1 is localhost ip
private String ip = InetAddress.getLoopbackAddress().getHostAddress();

Get port:

获取端口:

@Value("${local.server.port}")
private int serverPort;

or by:

或通过:

@LocalServerPort
private int serverPort;

or by:

或通过:

@Autowired
private Environment environment;

public void doWork(){
    String serverPort = environment.getProperty("local.server.port");
    // do something
}

Also you can get all properties from Environment - JVM / system / environment / all passed arguments

您还可以从 Environment - JVM / system / environment / 所有传递的参数中获取所有属性