ClusterUtils.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package com.its.common.cluster.utils;
  2. import io.netty.channel.Channel;
  3. import io.netty.channel.EventLoopGroup;
  4. import io.netty.channel.epoll.Epoll;
  5. import io.netty.channel.epoll.EpollEventLoopGroup;
  6. import io.netty.channel.epoll.EpollServerSocketChannel;
  7. import io.netty.channel.epoll.EpollSocketChannel;
  8. import io.netty.channel.nio.NioEventLoopGroup;
  9. import io.netty.channel.socket.ServerSocketChannel;
  10. import io.netty.channel.socket.SocketChannel;
  11. import io.netty.channel.socket.nio.NioServerSocketChannel;
  12. import io.netty.channel.socket.nio.NioSocketChannel;
  13. import io.netty.util.concurrent.DefaultThreadFactory;
  14. import java.net.*;
  15. import java.time.Instant;
  16. import java.time.ZoneId;
  17. import java.time.format.DateTimeFormatter;
  18. import java.util.ArrayList;
  19. import java.util.Enumeration;
  20. public class ClusterUtils {
  21. public static final String OS_NAME = System.getProperty("os.name");
  22. private static boolean isLinuxPlatform = false;
  23. private static boolean isWindowsPlatform = false;
  24. private ClusterUtils() {
  25. }
  26. public static String getAddress(Channel ch) {
  27. String localIp = "local-unknown";
  28. String remoteIp = "remote-unknown";
  29. int localPort = 0;
  30. int remotePort = 0;
  31. InetSocketAddress localAddr = (InetSocketAddress)ch.localAddress();
  32. if (localAddr != null) {
  33. localIp = localAddr.getAddress().getHostAddress();
  34. localPort = localAddr.getPort();
  35. }
  36. InetSocketAddress remoteAddr = (InetSocketAddress)ch.remoteAddress();
  37. if (remoteAddr != null) {
  38. remoteIp = remoteAddr.getAddress().getHostAddress();
  39. remotePort = remoteAddr.getPort();
  40. }
  41. return "[Local #(" + localIp + ":" + localPort + ") Remote #(" + remoteIp + ":" + remotePort + ")]";
  42. }
  43. public static String getRemoteAddress(Channel ch) {
  44. String ip = getRemoteIpAddress(ch);
  45. int port = getRemotePort(ch);
  46. return "[Remote #(" + ip + ":" + port + ")]";
  47. }
  48. public static String getLocalAddress(Channel ch) {
  49. String ip = getLocalIpAddress(ch);
  50. int port = getLocalPort(ch);
  51. return "[Local #(" + ip + ":" + port + ")]";
  52. }
  53. public static String getRemoteIpAddress(Channel ch) {
  54. String ip = "255.255.255.255";
  55. InetSocketAddress inetAddr = (InetSocketAddress)ch.remoteAddress();
  56. if (inetAddr != null) {
  57. ip = inetAddr.getAddress().getHostAddress();
  58. }
  59. return ip;
  60. }
  61. public static long getRemoteIpAddressToLong(Channel ch) {
  62. String[] ipAddressInArray = getRemoteIpAddress(ch).split("\\.");
  63. long result = 0L;
  64. for(int i = 0; i < ipAddressInArray.length; ++i) {
  65. int power = 3 - i;
  66. int ip = Integer.parseInt(ipAddressInArray[i]);
  67. result += (long)((double)ip * Math.pow((double)256.0F, (double)power));
  68. }
  69. return result;
  70. }
  71. public static int getRemotePort(Channel ch) {
  72. int port = 0;
  73. InetSocketAddress inetAddr = (InetSocketAddress)ch.remoteAddress();
  74. if (inetAddr != null) {
  75. port = inetAddr.getPort();
  76. }
  77. return port;
  78. }
  79. public static String getLocalIpAddress(Channel ch) {
  80. String ip = "127.0.0.1";
  81. InetSocketAddress inetAddr = (InetSocketAddress)ch.localAddress();
  82. if (inetAddr != null) {
  83. ip = inetAddr.getAddress().getHostAddress();
  84. }
  85. return ip;
  86. }
  87. public static int getLocalPort(Channel ch) {
  88. int port = 0;
  89. InetSocketAddress inetAddr = (InetSocketAddress)ch.localAddress();
  90. if (inetAddr != null) {
  91. port = inetAddr.getPort();
  92. }
  93. return port;
  94. }
  95. public static boolean isEpollAvailable() {
  96. return Epoll.isAvailable();
  97. }
  98. public static EventLoopGroup newEventLoopGroup(int nThreads, String threadPoolName) {
  99. if (isEpollAvailable()) {
  100. return threadPoolName.isEmpty() ? new EpollEventLoopGroup(nThreads) : new EpollEventLoopGroup(nThreads, new DefaultThreadFactory("epo" + threadPoolName));
  101. } else {
  102. return threadPoolName.isEmpty() ? new NioEventLoopGroup(nThreads) : new NioEventLoopGroup(nThreads, new DefaultThreadFactory("nio" + threadPoolName));
  103. }
  104. }
  105. public static Class<? extends SocketChannel> getSocketChannel() {
  106. return isEpollAvailable() ? EpollSocketChannel.class : NioSocketChannel.class;
  107. }
  108. public static Class<? extends ServerSocketChannel> getServerSocketChannel() {
  109. return isEpollAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class;
  110. }
  111. public static String getTcpAddress(Channel ch) {
  112. String localIp = "local-unknown";
  113. String remoteIp = "remote-unknown";
  114. int localPort = 0;
  115. int remotePort = 0;
  116. InetSocketAddress localAddr = (InetSocketAddress)ch.localAddress();
  117. if (localAddr != null) {
  118. localIp = localAddr.getAddress().getHostAddress();
  119. localPort = localAddr.getPort();
  120. }
  121. InetSocketAddress remoteAddr = (InetSocketAddress)ch.remoteAddress();
  122. if (remoteAddr != null) {
  123. remoteIp = remoteAddr.getAddress().getHostAddress();
  124. remotePort = remoteAddr.getPort();
  125. }
  126. return "[Local #(" + localIp + ":" + localPort + ") Remote #(" + remoteIp + ":" + remotePort + ")]";
  127. }
  128. public static String getLocalAddress() {
  129. Enumeration<NetworkInterface> enumeration = null;
  130. try {
  131. enumeration = NetworkInterface.getNetworkInterfaces();
  132. } catch (SocketException var7) {
  133. return null;
  134. }
  135. ArrayList<String> ipv4Result = new ArrayList<>();
  136. ArrayList<String> ipv6Result = new ArrayList<>();
  137. while(enumeration.hasMoreElements()) {
  138. NetworkInterface networkInterface = (NetworkInterface)enumeration.nextElement();
  139. Enumeration<InetAddress> en = networkInterface.getInetAddresses();
  140. while(en.hasMoreElements()) {
  141. InetAddress address = (InetAddress)en.nextElement();
  142. if (!address.isLoopbackAddress()) {
  143. if (address instanceof Inet6Address) {
  144. ipv6Result.add(normalizeHostAddress(address));
  145. } else {
  146. ipv4Result.add(normalizeHostAddress(address));
  147. }
  148. }
  149. }
  150. }
  151. if (!ipv4Result.isEmpty()) {
  152. for(String ip : ipv4Result) {
  153. if (!ip.startsWith("127.0") && !ip.startsWith("192.168")) {
  154. return ip;
  155. }
  156. }
  157. return (String)ipv4Result.get(ipv4Result.size() - 1);
  158. } else if (!ipv6Result.isEmpty()) {
  159. return (String)ipv6Result.get(0);
  160. } else {
  161. InetAddress localHost;
  162. try {
  163. localHost = InetAddress.getLocalHost();
  164. } catch (UnknownHostException var6) {
  165. return null;
  166. }
  167. return normalizeHostAddress(localHost);
  168. }
  169. }
  170. public static String normalizeHostAddress(InetAddress localHost) {
  171. return localHost instanceof Inet6Address ? "[" + localHost.getHostAddress() + "]" : localHost.getHostAddress();
  172. }
  173. public static SocketAddress string2SocketAddress(String addr) {
  174. String[] s = addr.split(":");
  175. InetSocketAddress isa = new InetSocketAddress(s[0], Integer.parseInt(s[1]));
  176. return isa;
  177. }
  178. public static String socketAddress2String(SocketAddress addr) {
  179. StringBuilder sb = new StringBuilder();
  180. InetSocketAddress inetSocketAddress = (InetSocketAddress)addr;
  181. sb.append(inetSocketAddress.getAddress().getHostAddress());
  182. sb.append(":");
  183. sb.append(inetSocketAddress.getPort());
  184. return sb.toString();
  185. }
  186. public static String parseChannelRemoteAddr(Channel channel) {
  187. if (null == channel) {
  188. return "";
  189. } else {
  190. SocketAddress remote = channel.remoteAddress();
  191. String addr = remote != null ? remote.toString() : "";
  192. if (addr.length() > 0) {
  193. int index = addr.lastIndexOf("/");
  194. return index >= 0 ? addr.substring(index + 1) : addr;
  195. } else {
  196. return "";
  197. }
  198. }
  199. }
  200. public static String parseSocketAddressAddr(SocketAddress socketAddress) {
  201. if (socketAddress != null) {
  202. String addr = socketAddress.toString();
  203. if (addr.length() > 0) {
  204. return addr.startsWith("/") ? addr.substring(1) : addr;
  205. }
  206. }
  207. return "";
  208. }
  209. public static String timeToString(long timestamp) {
  210. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
  211. .withZone(ZoneId.systemDefault());
  212. return formatter.format(Instant.ofEpochMilli(timestamp));
  213. }
  214. public static String timeToStringYmd(long timestamp) {
  215. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
  216. .withZone(ZoneId.systemDefault());
  217. return formatter.format(Instant.ofEpochMilli(timestamp));
  218. }
  219. static {
  220. if (OS_NAME != null && OS_NAME.toLowerCase().contains("linux")) {
  221. isLinuxPlatform = true;
  222. }
  223. if (OS_NAME != null && OS_NAME.toLowerCase().contains("windows")) {
  224. isWindowsPlatform = true;
  225. }
  226. }
  227. }