package com.xxx.app.service.impl;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.pisen.yunma2.server.service.IpInfoService;
import com.pisen.yunma2.server.vo.IPInfo;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author: fanxian
* @Date: 2024/10/22 15:13
**/
@Slf4j
@Service
public class IpInfoServiceImpl implements IpInfoService {
@Override
public IPInfo ipInfo(String ip) {
IPInfo ipInfo = new IPInfo(ip);
CompletableFuture.anyOf(
CompletableFuture.supplyAsync(()->fetchCountryCode(StrUtil.format("https://ipapi.com/ip_api.php?ip={}", ip), e->extractField(e, "country_code")))
.thenAccept(cCode -> ipInfo.setCountryCode(cCode)),
CompletableFuture.supplyAsync(()->fetchCountryCode(StrUtil.format("https://webapi-pc.meitu.com/common/ip_location?ip={}", ip), e->extractField(e, "nation_code")))
.thenAccept(cCode -> ipInfo.setCountryCode(cCode)),
CompletableFuture.supplyAsync(()->fetchCountryCode(StrUtil.format("https://ip-api.io/json/{}", ip), e->extractField(e, "country_code")))
.thenAccept(cCode -> ipInfo.setCountryCode(cCode)),
CompletableFuture.supplyAsync(()->fetchCountryCode(StrUtil.format("https://api.ipapi.is/?ip={}", ip), e->extractField(e, "country_code")))
.thenAccept(cCode -> ipInfo.setCountryCode(cCode))
).join();
return ipInfo;
}
private static Integer TIMEOUT = 1000;
public String fetchCountryCode(String url, Function<String,String> extractField) {
try {
log.info("fetching {}", url);
String resStr = HttpUtil.get(url, TIMEOUT);
String countryCode = extractField.apply(resStr);
Assert.notBlank(countryCode);
log.info("resp {} country code: {}", url, countryCode);
return countryCode.toUpperCase();
}catch (Exception e){
log.error("failed url: {}, error: {}", url, e.getMessage());
ThreadUtil.sleep(TIMEOUT+300, TimeUnit.MILLISECONDS); // 停一下,讓其他請(qǐng)求成功返回
return null;
}
}
private static @Nullable String extractField(String resStr, String fieldName) {
String regex = "\""+fieldName+"\":\\s*\"([^\"]*)\"";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(resStr);
if (matcher.find()) {
String nationCode = matcher.group(1);
System.out.println("extract field... " + fieldName +": " + nationCode);
return nationCode;
} else {
System.out.println(fieldName +" not found.");
return null;
}
}
public static void main(String[] args) throws IOException {
// String ip = "172.66.43.8"; // not china
String ip = "121.8.215.106"; // china
IpInfoServiceImpl ipInfoService = new IpInfoServiceImpl();
IPInfo ipInfo = ipInfoService.ipInfo(ip);
System.out.println("--->ipInfo: " + JSONUtil.toJsonStr(ipInfo));
}
}
CompletableFuture并發(fā)獲取IP的country_code
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門(mén)主儡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人惨缆,你說(shuō)我怎么就攤上這事糜值。” “怎么了坯墨?”我有些...
- 文/不壞的土叔 我叫張陵寂汇,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我捣染,道長(zhǎng)健无,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任液斜,我火速辦了婚禮累贤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘少漆。我一直安慰自己臼膏,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布示损。 她就那樣靜靜地躺著渗磅,像睡著了一般。 火紅的嫁衣襯著肌膚如雪检访。 梳的紋絲不亂的頭發(fā)上始鱼,一...
- 那天,我揣著相機(jī)與錄音脆贵,去河邊找鬼医清。 笑死,一個(gè)胖子當(dāng)著我的面吹牛卖氨,可吹牛的內(nèi)容都是我干的会烙。 我是一名探鬼主播,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼筒捺,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼柏腻!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起系吭,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤五嫂,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后肯尺,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體沃缘,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年蟆盹,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了孩灯。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站舔哪,受9級(jí)特大地震影響欢顷,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜捉蚤,卻給世界環(huán)境...
- 文/蒙蒙 一抬驴、第九天 我趴在偏房一處隱蔽的房頂上張望炼七。 院中可真熱鬧,春花似錦布持、人聲如沸豌拙。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)按傅。三九已至,卻和暖如春胧卤,著一層夾襖步出監(jiān)牢的瞬間唯绍,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓侧啼,卻偏偏與公主長(zhǎng)得像牛柒,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子痊乾,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 昨天都十點(diǎn)了皮壁,牛牛一直磨蹭要買(mǎi)的大巴車,消防車拿到床上哪审,小汽車蛾魄,那一堆,非要拿到床上湿滓,之前干凈的拿到床上無(wú)所謂滴须。我...
- 我忽然間明白一件事情,既然我能感受到某個(gè)音樂(lè)的感受叽奥,或者某個(gè)電影的非常不明顯的隱喻扔水,那其實(shí)也不用懷疑自己感受到的善...
- 前幾天陰雨綿綿,氣溫也下降的歷害朝氓,讓人有恍惚一夜入冬的感覺(jué)魔市。據(jù)說(shuō)今年的冬天很冷,也不知是不是真的赵哲。從時(shí)間上來(lái)說(shuō)待德,現(xiàn)...
- 以往的清晨,我都會(huì)在七點(diǎn)之前離開(kāi)床枫夺,起來(lái)運(yùn)動(dòng)或是坐下來(lái)看書(shū)将宪,運(yùn)動(dòng)是流了汗水的,但看書(shū)不見(jiàn)得能看的進(jìn)去,看書(shū)還是適合...