0
点赞
收藏
分享

微信扫一扫

spring-boot项目获取当前实例的ip和端口

纽二 2023-02-04 阅读 76


import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Data
@RefreshScope
@Component
public class ServerConfig{

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

public String getIP() {
InetAddress address = null;
try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return address.getHostAddress();
}

}

 

举报

相关推荐

0 条评论