SpringBoot获取项目IP和端口号

该博客介绍了如何在Spring Boot项目中创建一个`ServerConfig`类,该类实现了`ApplicationListener<WebServerInitializedEvent>`接口,用于监听Web服务器的初始化事件。当服务器启动并完成初始化时,`onApplicationEvent`方法被调用,获取到服务器的端口号。通过`getUrl`方法,可以获取服务器的本地IP地址和端口,方便构建应用的访问URL。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搬运-原文链接

方法一:
在项目中创建一个config类,并实现ApplicationListener接口

import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Component
public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> {
    private int serverPort;
    public String getUrl() {
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
        return address.getHostAddress() +":"+this.serverPort;
    }
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        this.serverPort = event.getWebServer().getPort();
    }
}

输出:127.0.0.1:8080

方法二:
通过controller层方法参数中的 HttpServletRequest对象
在这里插入图片描述
方法三:
如果一些方法中没有 HttpServletRequest对象,可以通过 RequestContextHolder类的静态方法获取当前的request
在这里插入图片描述

要在Spring Cloud中获取IP端口,可以使用以下步骤: 1. 首先,确保在项目的pom.xml文件中添加了spring-cloud-starter-loadbalancer依赖项。这可以通过以下方式完成: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> ``` 这个依赖项将提供负载均衡功能,以便在获取IP端口时使用。 2. 其次,在应用程序的配置文件中,可以使用以下属性来配置Eureka实例IP地址: ```properties eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port} eureka.instance.prefer-ip-address=true ``` 这将配置Eureka实例的ID为IP地址端口号的组合,并设置优先使用IP地址。 3. 最后,可以创建一个IpConfiguration类来获取本机端口号。这可以通过实现ApplicationListener接口并监听WebServerInitializedEvent事件来完成。以下是一个示例IpConfiguration类的代码: ```java import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Component public class IpConfiguration implements ApplicationListener<WebServerInitializedEvent> { private int serverPort; @Override public void onApplicationEvent(WebServerInitializedEvent event) { this.serverPort = event.getWebServer().getPort(); } public int getPort() { return this.serverPort; } } ``` 这个类将在应用程序启动时获取本机端口号,并提供一个getPort方法来获取端口号。 请注意,以上步骤适用于Spring Boot版本2.1.3,并且假设您已经正确配置了Eureka负载均衡器。 #### 引用[.reference_title] - *1* [Spring Cloud根据服务名获取服务的ip端口](https://blog.youkuaiyun.com/weixin_43888891/article/details/126755927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [spring cloud配置注册中心显示服务的ip地址端口](https://blog.youkuaiyun.com/web15687102624/article/details/126504257)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [SpringBoot / SpringCloud获取启动端口号](https://blog.youkuaiyun.com/weixin_42639445/article/details/89013405)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值