开发小技巧 - springboot项目启动控制台输出访问地址

打印 上一主题 下一主题

主题 853|帖子 853|积分 2559

在Spring Boot项目中,有时我们需要记录或输出访问的地址和IP,以便进行调试、监控或日志记录。以下是如何在Spring Boot中实现这一需求的方法: 1、编写获取所有访问地址工具类
  1. package com.example.utils;
  2. import cn.hutool.core.util.StrUtil;
  3. import org.springframework.boot.web.context.WebServerApplicationContext;
  4. import java.net.InetAddress;
  5. import java.net.NetworkInterface;
  6. import java.net.SocketException;
  7. import java.util.*;
  8. /**
  9. * IP地址工具类
  10. * @author 李东阳
  11. */
  12. public class IpAddressUtils {
  13.     /**
  14.      * 获取项目启动的IP地址
  15.      * 注: 仅限springboot项目
  16.      **/
  17.     public static List < String > getIpAddressOfStartUp(WebServerApplicationContext context) {
  18.         List < String > addressList = new ArrayList < > ();
  19.         try {
  20.             Enumeration < NetworkInterface > interfaces = NetworkInterface.getNetworkInterfaces();
  21.             for(NetworkInterface networkInterface: Collections.list(interfaces)) {
  22.                 Enumeration < InetAddress > inetAddresses = networkInterface.getInetAddresses();
  23.                 for(InetAddress inetAddress: Collections.list(inetAddresses)) {
  24.                     if(!inetAddress.isLoopbackAddress() && inetAddress.isSiteLocalAddress()) {
  25.                         String address = StrUtil.utf8Str(inetAddress.getHostAddress());
  26.                         int port = context.getWebServer().getPort();
  27.                         String ipAddress = StrUtil.format("http://{}:{}", address, port);
  28.                         addressList.add(ipAddress);
  29.                     }
  30.                 }
  31.             }
  32.             return addressList;
  33.         } catch (SocketException e) {
  34.             e.printStackTrace();
  35.             return addressList;
  36.         }
  37.     }
  38. }
复制代码
2、在启动类中使用
  1. package com.example;
  2. import cn.hutool.core.lang.Console;
  3. import com.example.utils.IpAddressUtils;
  4. import org.springframework.boot.ApplicationArguments;
  5. import org.springframework.boot.ApplicationRunner;
  6. import org.springframework.boot.SpringApplication;
  7. import org.springframework.boot.autoconfigure.SpringBootApplication;
  8. import org.springframework.boot.web.context.WebServerApplicationContext;
  9. import org.springframework.context.annotation.Bean;
  10. import java.util.List;
  11. /**
  12. * 项目启动器
  13. * @author lidy
  14. */
  15. @SpringBootApplication
  16. public class BlogApplication {
  17.     public static void main(String[] args) {
  18.         SpringApplication.run(BlogApplication.class, args);
  19.     }
  20.     /**
  21.      * 项目启动输出访问地址
  22.      */
  23.     @Bean
  24.     public ApplicationRunner applicationRunner(WebServerApplicationContext context) {
  25.         return (ApplicationArguments args) - > {
  26.             try {
  27.                 Console.log("===============项目启动成功 start===============");
  28.                 List < String > ipAddressList = IpAddressUtils.getIpAddressOfStartUp(context);
  29.                 ipAddressList.forEach(Console::log);
  30.                 Console.log("================项目启动成功 end================");
  31.             } catch (Exception e) {
  32.                 e.printStackTrace();
  33.             }
  34.         };
  35.     }
  36. }
复制代码
注:本案例中使用到了hutool工具来做控制台输出,需要在pom.xml引入如下如下坐标。
  1. <dependency>
  2.     <groupId>cn.hutool</groupId>
  3.     <artifactId>hutool-all</artifactId>
  4.     <version>5.8.22</version>
  5. </dependency>
复制代码
如果不想引入相关工具,可以使用其他控制台输出方案即可。3、效果展示:
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

灌篮少年

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表