1、后端获取公网IP ,然后获取地理信息

pull/254/head
xjs 4 years ago
parent 68e78bdc7b
commit 04b66da1ba

@ -41,6 +41,8 @@ public class ApiConst {
public static final String TIANXING_TOPSEARCHDOUYIN= "天行-抖音热搜榜";
public static final String ROLL_IP= "ROLL-IP信息";
@ -80,6 +82,10 @@ public class ApiConst {
public static final String TIANXING_TOPSEARCHDOUYIN_URL= "http://api.tianapi.com/douyinhot/index";
public static final String ROLL_IP_URL= "https://www.mxnzp.com/api/ip/aim_ip";

@ -27,14 +27,22 @@ public class RedisConst {
/**
* websocketkey
*/
public static final String WEBSOCKET= "WEBSOCKET";
public static final String WEBSOCKET = "WEBSOCKET";
/**
* ipkey
*/
public static final String IP_INFO = "IPInfo";
//-------------------有效时间-----------------------
public static final Integer TRAN_DICT_EXPIRE = 7; //天
public static final Integer TRAN_DICT_EXPIRE = 1; //小时
public static final Integer ONE_ENGLISH_EXPIRE = 3; //分钟
public static final Long HOT_EXPIRE = 10L; //分钟
public static final Long IP_INFO_EXPIRE = 30L; //分钟
}

@ -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;
}
}

@ -86,7 +86,7 @@ public class EnglishWordServiceImpl implements IEnglishWordService {
Map<String, Object> build = new HashMap<>();
build.put(hkey, englishWord);
redisService.setCacheMap(TRAN_DICT, build);
redisService.expire(TRAN_DICT, TRAN_DICT_EXPIRE, TimeUnit.DAYS);
redisService.expire(TRAN_DICT, TRAN_DICT_EXPIRE, TimeUnit.HOURS);
return englishWord;
}

@ -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;
/**
* rollipapi
*
* @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;
};
}
}

@ -40,4 +40,9 @@ public class RequestBody {
*
*/
private Integer count;
/**
* ip
*/
private String ip;
}

@ -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;
/**
* ipAPI
* @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 IPAPI
*
* @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();
}

@ -0,0 +1,44 @@
package com.xjs.weather.service.impl;
import com.ruoyi.common.redis.service.RedisService;
import com.xjs.exception.BusinessException;
import com.xjs.weather.domain.IPInfoVo;
import com.xjs.weather.factory.impl.RollIPFactory;
import com.xjs.weather.service.IPService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import static com.xjs.consts.RedisConst.IP_INFO;
import static com.xjs.consts.RedisConst.IP_INFO_EXPIRE;
/**
* ip api
* @author xiejs
* @since 2022-01-15
*/
@Service
public class IPServiceImpl implements IPService {
@Autowired
private RollIPFactory rollIPFactory;
@Autowired
private RedisService redisService;
@Override
public IPInfoVo getIPApiData() {
if (redisService.hasKey(IP_INFO)) {
return (IPInfoVo) redisService.getCacheObject(IP_INFO);
}else {
IPInfoVo ipInfoVo = rollIPFactory.IpApi();
if (Objects.nonNull(ipInfoVo)) {
redisService.setCacheObject(IP_INFO, ipInfoVo, IP_INFO_EXPIRE, TimeUnit.MINUTES);
return ipInfoVo;
}else {
throw new BusinessException("获取IP信息异常");
}
}
}
}
Loading…
Cancel
Save