parent
68e78bdc7b
commit
04b66da1ba
@ -0,0 +1,177 @@
|
||||
package com.xjs.utils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Ip工具类
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-01-15
|
||||
*/
|
||||
public class IPUtil {
|
||||
|
||||
/**
|
||||
* 获取外网ip
|
||||
* @return
|
||||
*/
|
||||
public static String getV4IP() throws Exception {
|
||||
String ip = null;
|
||||
// 第一种方式
|
||||
try {
|
||||
ip = IPUtil.getNowIP1();
|
||||
ip.trim();
|
||||
} catch (Exception e) {
|
||||
System.out.println("getPublicIP - getNowIP1 failed ~ ");
|
||||
}
|
||||
if (!StringUtils.isEmpty(ip))
|
||||
return ip;
|
||||
// 第二种方式
|
||||
try {
|
||||
ip = IPUtil.getNowIP2();
|
||||
ip.trim();
|
||||
} catch (Exception e) {
|
||||
System.out.println("getPublicIP - getNowIP2 failed ~ ");
|
||||
}
|
||||
if (!StringUtils.isEmpty(ip))
|
||||
return ip;
|
||||
// 第三种方式
|
||||
try {
|
||||
ip = IPUtil.getNowIP3();
|
||||
ip.trim();
|
||||
} catch (Exception e) {
|
||||
System.out.println("getPublicIP - getNowIP3 failed ~ ");
|
||||
}
|
||||
if (!StringUtils.isEmpty(ip))
|
||||
return ip;
|
||||
// 第四种方式
|
||||
try {
|
||||
ip = IPUtil.getNowIP4();
|
||||
ip.trim();
|
||||
} catch (Exception e) {
|
||||
System.out.println("getPublicIP - getNowIP4 failed ~ ");
|
||||
}
|
||||
if (!StringUtils.isEmpty(ip))
|
||||
return ip;
|
||||
return ip;
|
||||
}
|
||||
|
||||
// 方法1
|
||||
private static String getNowIP1() throws IOException {
|
||||
String ip = null;
|
||||
String chinaz = "http://ip.chinaz.com";
|
||||
StringBuilder inputLine = new StringBuilder();
|
||||
String read = "";
|
||||
URL url = null;
|
||||
HttpURLConnection urlConnection = null;
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
url = new URL(chinaz);
|
||||
urlConnection = (HttpURLConnection) url.openConnection();
|
||||
in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
|
||||
while ((read = in.readLine()) != null) {
|
||||
inputLine.append(read + "\r\n");
|
||||
}
|
||||
Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
|
||||
Matcher m = p.matcher(inputLine.toString());
|
||||
if (m.find()) {
|
||||
String ipstr = m.group(1);
|
||||
ip = ipstr;
|
||||
}
|
||||
} finally {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
if (StringUtils.isEmpty(ip)) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
// 方法2
|
||||
private static String getNowIP2() throws IOException {
|
||||
String ip = null;
|
||||
BufferedReader br = null;
|
||||
try {
|
||||
URL url = new URL("https://v6r.ipip.net/?format=callback");
|
||||
br = new BufferedReader(new InputStreamReader(url.openStream()));
|
||||
String s = "";
|
||||
StringBuffer sb = new StringBuffer("");
|
||||
String webContent = "";
|
||||
while ((s = br.readLine()) != null) {
|
||||
sb.append(s + "\r\n");
|
||||
}
|
||||
webContent = sb.toString();
|
||||
int start = webContent.indexOf("(") + 2;
|
||||
int end = webContent.indexOf(")") - 1;
|
||||
webContent = webContent.substring(start, end);
|
||||
ip = webContent;
|
||||
} finally {
|
||||
if (br != null)
|
||||
br.close();
|
||||
}
|
||||
if (StringUtils.isEmpty(ip)) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
// 方法3
|
||||
private static String getNowIP3() throws IOException {
|
||||
String ip = null;
|
||||
String objWebURL = "https://ip.900cha.com/";
|
||||
BufferedReader br = null;
|
||||
try {
|
||||
URL url = new URL(objWebURL);
|
||||
br = new BufferedReader(new InputStreamReader(url.openStream()));
|
||||
String s = "";
|
||||
String webContent = "";
|
||||
while ((s = br.readLine()) != null) {
|
||||
if (s.indexOf("我的IP:") != -1) {
|
||||
ip = s.substring(s.indexOf(":") + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (br != null)
|
||||
br.close();
|
||||
}
|
||||
if (StringUtils.isEmpty(ip)) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
// 方法4
|
||||
private static String getNowIP4() throws IOException {
|
||||
String ip = null;
|
||||
String objWebURL = "https://bajiu.cn/ip/";
|
||||
BufferedReader br = null;
|
||||
try {
|
||||
URL url = new URL(objWebURL);
|
||||
br = new BufferedReader(new InputStreamReader(url.openStream()));
|
||||
String s = "";
|
||||
String webContent = "";
|
||||
while ((s = br.readLine()) != null) {
|
||||
if (s.indexOf("互联网IP") != -1) {
|
||||
ip = s.substring(s.indexOf("'") + 1, s.lastIndexOf("'"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (br != null)
|
||||
br.close();
|
||||
}
|
||||
if (StringUtils.isEmpty(ip)) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.xjs.common.client.api.roll;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.annotation.ApiLog;
|
||||
import com.xjs.common.client.factory.RollIPFeignFactory;
|
||||
import com.xjs.copywriting.domain.RequestBody;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.cloud.openfeign.SpringQueryMap;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import static com.xjs.consts.ApiConst.ROLL_IP;
|
||||
import static com.xjs.consts.ApiConst.ROLL_IP_URL;
|
||||
|
||||
/**
|
||||
* rollip信息查询接口api调用
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-01-15
|
||||
*/
|
||||
@FeignClient(name = "rollIP", url = ROLL_IP_URL, fallbackFactory = RollIPFeignFactory.class)
|
||||
public interface RollIPFeignClient {
|
||||
|
||||
@GetMapping()
|
||||
@ApiLog(name = ROLL_IP,
|
||||
url = ROLL_IP_URL,
|
||||
method = "Get")
|
||||
JSONObject IpApi(@SpringQueryMap RequestBody requestBody);
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.xjs.common.client.factory;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.common.client.api.roll.RollIPFeignClient;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @since 2022-01-15
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class RollIPFeignFactory implements FallbackFactory<RollIPFeignClient> {
|
||||
|
||||
@Override
|
||||
public RollIPFeignClient create(Throwable cause) {
|
||||
log.error("api模块roll IP服务调用失败:{},执行降级处理", cause.getMessage());
|
||||
return requestBody -> {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("error", R.FAIL);
|
||||
return jsonObject;
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.xjs.weather.controller;
|
||||
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.log.annotation.Log;
|
||||
import com.ruoyi.common.security.annotation.RequiresLogin;
|
||||
import com.xjs.weather.domain.IPInfoVo;
|
||||
import com.xjs.weather.service.IPService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* ip信息控制器
|
||||
* @author xiejs
|
||||
* @since 2022-01-15
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("ipInfo")
|
||||
@Api(tags = "业务模块-IP信息")
|
||||
@Log4j2
|
||||
public class IPController {
|
||||
|
||||
@Autowired
|
||||
private IPService ipService;
|
||||
|
||||
|
||||
@GetMapping
|
||||
@ApiOperation("获取IP信息")
|
||||
@Log(title = "获取IP")
|
||||
@RequiresLogin
|
||||
public R<IPInfoVo> getIPApiData() {
|
||||
return R.ok(ipService.getIPApiData());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.xjs.weather.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* IP信息实体
|
||||
* @author xiejs
|
||||
* @since 2022-01-15
|
||||
*/
|
||||
@Data
|
||||
public class IPInfoVo implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* ip地址
|
||||
*/
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* 省份
|
||||
*/
|
||||
private String province;
|
||||
|
||||
/**
|
||||
* 省份Id
|
||||
*/
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 城市
|
||||
*/
|
||||
private String city;
|
||||
|
||||
/**
|
||||
* 城市id
|
||||
*/
|
||||
private String cityId;
|
||||
|
||||
/**
|
||||
* 网络服务商名称 例如 电信
|
||||
*/
|
||||
private String isp;
|
||||
|
||||
/**
|
||||
* 拼接好的描述信息
|
||||
*/
|
||||
private String desc;
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.xjs.weather.factory;
|
||||
|
||||
/**
|
||||
* 获取ip的API接口工厂
|
||||
* @author xiejs
|
||||
* @since 2022-01-15
|
||||
*/
|
||||
public interface IPFactory<T> {
|
||||
|
||||
/**
|
||||
* 获取ip工厂方法
|
||||
* @return T
|
||||
*/
|
||||
T IpApi();
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.xjs.weather.factory.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.common.redis.service.RedisService;
|
||||
import com.xjs.common.client.api.roll.RollIPFeignClient;
|
||||
import com.xjs.config.RollProperties;
|
||||
import com.xjs.copywriting.domain.RequestBody;
|
||||
import com.xjs.utils.IPUtil;
|
||||
import com.xjs.weather.domain.IPInfoVo;
|
||||
import com.xjs.weather.factory.IPFactory;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static com.xjs.consts.RedisConst.IP_INFO;
|
||||
|
||||
/**
|
||||
* roll IP信息查询API工厂实现
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-01-15
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class RollIPFactory implements IPFactory<IPInfoVo> {
|
||||
|
||||
@Autowired
|
||||
private RollProperties rollProperties;
|
||||
@Autowired
|
||||
private RollIPFeignClient rollIPFeignClient;
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Override
|
||||
public IPInfoVo IpApi() {
|
||||
RequestBody requestBody = new RequestBody();
|
||||
try {
|
||||
requestBody.setIp(IPUtil.getV4IP());
|
||||
} catch (Exception e) {
|
||||
requestBody.setIp("127.0.0.1");
|
||||
}
|
||||
requestBody.setApp_id(rollProperties.getApp_id());
|
||||
requestBody.setApp_secret(rollProperties.getApp_secret());
|
||||
JSONObject jsonObject = rollIPFeignClient.IpApi(requestBody);
|
||||
if (!jsonObject.containsKey("error") && jsonObject.getInteger("code") == 1) {
|
||||
JSONObject data = jsonObject.getJSONObject("data");
|
||||
return data.toJavaObject(IPInfoVo.class);
|
||||
} else {
|
||||
log.error("天行全网热搜服务调用成功,但返回异常");
|
||||
if (redisService.hasKey(IP_INFO)){
|
||||
return (IPInfoVo) redisService.getCacheObject(IP_INFO);
|
||||
}else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.xjs.weather.service;
|
||||
|
||||
import com.xjs.weather.domain.IPInfoVo;
|
||||
|
||||
/**
|
||||
* ip api服务接口
|
||||
* @author xiejs
|
||||
* @since 2022-01-15
|
||||
*/
|
||||
public interface IPService {
|
||||
|
||||
/**
|
||||
* 获取IP的数据
|
||||
* @return IPInfoVo
|
||||
*/
|
||||
IPInfoVo getIPApiData();
|
||||
|
||||
}
|
Loading…
Reference in new issue