查询服务器的ip及java获取环形IP的方法与静态类修改成懒加载解决shortUrl服务启动正常但是调用接口异常报错
1.查询服务器ip
ip addr
# 或简写
ip a
hostname -i
import java.net.InetAddress; ip = InetAddress.getLocalHost().getHostAddress(); 2.非回环内网IPv4 ip 修改代码: Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); while (nets != null && nets.hasMoreElements()) { NetworkInterface nif = nets.nextElement(); if (nif.isLoopback() || !nif.isUp()) { continue; } Enumeration<InetAddress> addrs = nif.getInetAddresses(); while (addrs.hasMoreElements()) { InetAddress addr = addrs.nextElement(); if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) { return addr.getHostAddress(); } } } private static final Map<Integer, String[]> MAP = new ImmutableMap.Builder<Integer, String[]>() .put(0, ApplicationContextUtil.getFirstCode()) .put(1, SECOND) .put(2, THIRD) .put(3, FOURTH) .put(4, FIFTH) .put(5, SIXTH) .build(); 3.静态类修改成懒加载: private static volatile Map<Integer, String[]> MAP; private static Map<Integer, String[]> getMap() { if (MAP == null) { synchronized (CodeUtil.class) { if (MAP == null) { MAP = new ImmutableMap.Builder<Integer, String[]>() .put(0, ApplicationContextUtil.getFirstCode()) .put(1, SECOND) .put(2, THIRD) .put(3, FOURTH) .put(4, FIFTH) .put(5, SIXTH) .build(); } } } return MAP; }
浙公网安备 33010602011771号