如何在运行时获取SPRING引导主机和端口地址? [英] How to get the SPRING Boot HOST and PORT address during run time?

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

问题描述

如何在运行时获取应用程序部署所在的主机和端口,以便可以在我的java方法中使用它?

How could I get the host and port where my application is deployed during run-time so that I can use it in my java method?

推荐答案

您可以通过 Environment net/InetAddress.html"rel =" noreferrer> porthost,您可以使用InternetAddress获得.

You can get this information via Environment for the port and the host you can obtain by using InternetAddress.

@Autowired
Environment environment;

......
public void somePlaceInTheCode() {
    // Port
    environment.getProperty("server.port");
    // Local address
    InetAddress.getLocalHost().getHostAddress();
    InetAddress.getLocalHost().getHostName();

    // Remote address
    InetAddress.getLoopbackAddress().getHostAddress();
    InetAddress.getLoopbackAddress().getHostName();
}

这篇关于如何在运行时获取SPRING引导主机和端口地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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