用springboot 写一个流程定时器先获取公网ip然后发送到weblook,第一次先把ip存一下,如果内存中ip跟当前ip不同再发送给weblook
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class MyTimer {
private String previousIp = null;
@Scheduled(fixedRate = 60000) //定时器每60秒执行一次
public void sendPublicIpToWeblook() {
try {
InetAddress inetAddress = InetAddress.getLocalHost();
String publicIp = inetAddress.getHostAddress();
if (previousIp == null || !previousIp.equals(publicIp)) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getForObject("http://weblook.com/updatePublicIp?ip=" + publicIp, String.class);
previousIp = publicIp;
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}