SpringBoot获取项目ip和端口号

本文来编写一个例子说下SpringBoot获取项目ip和端口号

文章目录


测试程序

本文来编写一个例子说下SpringBoot获取项目ip和端口号

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) {
            e.printStackTrace();
        }
        return "http://"+address.getHostAddress() +":"+this.serverPort;
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        this.serverPort = event.getWebServer().getPort();
    }

}

程序结果

在这里插入图片描述

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在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.csdn.net/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.csdn.net/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.csdn.net/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 ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值