Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get ip address in java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to get the ip address in java? for this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Package com.ysma.jobs.util;import com.ysma.jobs.common.component.RedisManager;import com.ysma.jobs.common.constants.CacheKeyConstants;import com.ysma.jobs.common.dingtalk.ChatbotSend;import com.ysma.jobs.service.XxlService;import lombok.extern.slf4j.Slf4j;import org.apache.commons.lang.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.DependsOn;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;import java.net.InetAddress Import java.net.NetworkInterface;import java.net.UnknownHostException;import java.util.Enumeration;import java.util.concurrent.TimeUnit;/** * IP address acquisition tool * @ Date: 2019-6-17 14:26 * modified by ysma 2019-06-28 * / @ DependsOn (value = {"redisManager", "chatbotSend", "xxlService"}) @ Component@Slf4jpublic class IpAdressUtil {@ Autowired private ChatbotSend chatbotSend; @ Autowired private XxlService xxlService; @ Autowired private RedisManager redisManager / * * define an ip variable for static so that it is visible on the full stack * / private static volatile String MACHINE_IP; private final String OS_WINDOWS = "windows"; private final String OS_MAC = "mac"; public String getServerIp () {if (StringUtils.isEmpty (MACHINE_IP)) {log.error ("IpAddressUtil.getServerIp to get server ip,MACHINE_IP: {}", MACHINE_IP); return null } return MACHINE_IP;} / * synchronously specify the ip address of the execution machine to redis, so that all machines can share this information, and cooperate with * @ param ip ip address * / private void setJobRunIP (String ip) {String key = CacheKeyConstants.JOB_DEFAULT_RUN_IP; boolean result = redisManager.set (key, ip) Log.info ("IpAddressUtil.setJobRunIP set job running ip runIp: {}, result: {}", ip, result) } / * * obtain the local IP address * the judgment of the operating system can only determine what kind of operating system it is. However, there will be deviations in the version of the operating system * this requirement only involves IP, so ignore the deviation of the operating system version and continue to use * / @ PostConstruct public void initIp () {/ / Thread initIpThread = new Thread (()-> {/ / 1. Local ip address initialization boolean goOn = true; do {MACHINE_IP = getMachineIp (); try {goOn = StringUtils.isEmpty (MACHINE_IP) If (goOn) {/ / pin alarm until the ip address chatbotSend.sendMsg is obtained ("Please note that the scheduler application has not obtained the ip address! try again in 5 seconds"); TimeUnit.SECONDS.sleep (5) }} catch (Exception ex) {log.error ("IpAddressUtil.initIp Exception Vendor V", ex);}} while (goOn); / / until the local ip is obtained to stop / / 2. Distributed specified execution of ip initialization try {String executeIps = xxlService.getExecuteIps (); String [] split = executeIps.split (",") If (StringUtils.isEmpty (executeIps)) {log.error ("IpAddressUtil.initIp xxl does not configure the ip address information for the specified operation. Native ip is specified here by default, so that the last machine ip started will get the execution permission"); setJobRunIP (MACHINE_IP);} else {setJobRunIP (split [0]) } Object runIp = redisManager.get (CacheKeyConstants.JOB_DEFAULT_RUN_IP); log.info ("IpAddressUtil.initIp native ip: {}, specify to run ip: {}", MACHINE_IP, runIp = = null? ": runIp.toString ()) } catch (Exception ex) {log.error ("IpAddressUtil.initIp Exception Venture V", ex);}}); initIpThread.setName ("init-ip-thread"); initIpThread.start () } / * determine which operating system * / private boolean isThisOS (String prefixName) {String osName = System.getProperty ("os.name"); return osName.toLowerCase (). StartsWith (prefixName);} / * obtain the IP address of the machine * / private String getMachineIp () {String ipString = null Try {if (isThisOS (OS_WINDOWS)) {ipString = InetAddress.getLocalHost (). GetHostAddress (); log.info ("IpAddressUtil.getMachineIp windows os, ip: {}", ipString);} else {Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces (); boolean findFlag = false While (netInterfaces.hasMoreElements ()) {if (findFlag) {break;} NetworkInterface ni = netInterfaces.nextElement (); Enumeration ips = ni.getInetAddresses () While (ips.hasMoreElements ()) {InetAddress address = ips.nextElement (); if (address.isSiteLocalAddress () & &! address.isLoopbackAddress () / / 127. It all starts with the lookback address & &! address.getHostAddress () .contains (":") {ipString = address.getHostAddress (); findFlag = true; break } log.info ("IpAddressUtil.getMachineIp {} os, ip: {}", isThisOS (OS_MAC)? "Mac": "linux/unix", ipString);}} catch (Exception ex) {log.error ("IpAddressUtil.getMachineIp error", ex);} return ipString }} this is the answer to the question about how to obtain the ip address in java. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report