최근에 프로젝트의 특정 기능은 IP 주소를 기반으로 타사 인터페이스에서 상세한 지리적 위치를 얻어야합니다. Sina, Taobao 및 Tencent를 포함하여 인터넷에서 많은 예를 찾았습니다. 타오 바오를 시도했습니다. 적은 순서라면 괜찮습니다. 괜찮습니다. 그러나 100,000 수준에 도달하면 속도가 느려서 시스템이 충돌하게됩니다. 다음 예는 Sina의 예입니다. 예제는 각 프로젝트에 적합하지 않으며 수정해야합니다.
/** ipSearchUrl = http : //int.dpool.sina.com.cn/iplookup/iplookup.php? format = js & ip = (Sina의 인터페이스 주소) 프로젝트에서는 Sina의 인터페이스 주소가 직접 작성되지 않지만 속성 파일을 읽습니다. */ public static string getipinfobysina (문자열 ip) {// 속성 파일을 읽습니다 (속성 파일 키-값 및 형식) 최종 문자열 prop_ipsearchurl = "ipsearchUrl"; 최종 문자열 ret_success = "1"; 최종 문자열 ret = "ret"; 최종 문자열 주 = "지방"; Final String City = "City"; 최종 문자열 지구 = "지구"; 최종 문자열 ISP = "ISP"; 문자열 iPaddress = ""; if (stringUtils.isblank (ip)) {return null; } string url = SystemParamPropertyUtils.getSystemParamKeyValue (prop_ipsearchUrl); // 이것은 속성 파일에서 URL을 가져 오는 것입니다. path = url+ip; // "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip="+ip; httpclient httpclient = new httpclient (); Map <String, String> Parammap = new Hashmap <String, String> (); String impleatipinfo = ""; try {remoteipinfo = httpclientutil.request (httpclient, Path, Parammap, "Sina"); } catch (예외 e) {e.printstacktrace (); } if (stringUtils.isNotBlank (remotePinfo)) {string _ret = searchValue (remoteipinfo, ret); if (ret_success.equals (_ret)) {String provincename = searchValue (remoteipinfo, province); String CityName = SearchValue (Remoteipinfo, City); String District = SearchValue (remoteipinfo, District); 문자열 isp = searchValue (remoteipinfo, isp); iPaddress = ProvinceName+CityName+District+ISP; }}} return iPaddress; } private static string searchValue (String remoteipinfo, String key) {String _value = ""; if (stringUtils.isnotblank (remoteipinfo)) {_value = stringUtils.SubStringBetween if (stringUtils.isnotblank (_value)) {_value = unicodeutils.decodeunicode (_value); }} return _value; } Sina 인터페이스 주소를 매우 빨리 읽었습니다. 약 90,000, 10 분 동안 테스트했습니다. 타오 바오의 경우 1 시간 이상, 5 만 개 이상. 읽기 IP를 맵에 저장 한 다음 다음에 읽는 경우지도에서 직접 꺼내는 또 다른 트릭이 있습니다. 훨씬 빠릅니다. 이것은 많은 문제로 이어질 것입니다. 로그 파일이 수백만 또는 수백만 수준에 도달하면 어떻게 해결 하는가? Taobao와 같은 예의 경우 각 주문은 1 초 안에 주문 수가 다릅니다. 나는 그것을 해결하는 방법을 모른다. 위대한 하나님은 나에게 대답하는 것을 알고 있습니다.
다음은 Taobao입니다.
import java.io.bufferedReader; import java.io.dataOutputStream; import java.io.ioException; import java.io.inputStreamReader; import java.io.unsupportedEncodingException; import java.net.httpurlConnection; import java.net.url을 기반으로 한 지리적 정보 얻기; @Author lwl * @datejan 26, 2016 * /public class addressUtils { /** * @param content * 요청 된 매개 변수 형식은 다음과 같습니다. name = xxx & pwd = xxx * @param 인코딩 * 서버 측 요청 인코딩. 예를 들어, GBK, UTF-8 등 * @return * @Throws UnSupportedEncodingException */public String getAddresses (문자열 내용, String encodingstring)는 UnSupportedEncodingException {// pconline interface urlstr = "http://ip.taobao.com/service/service/getipinfo.ppo.php"; // IP가 http://whois.pconline.com.cn string returnstr = this.getresult (urlstr, content, encodingstring)에서 위치한 지방, 도시 및 지구의 정보를 얻습니다. if (returnstr! = null) {// 반환 된 지방 및 시립 정보 시스템을 처리합니다. 문자열 [] temp = returnstr.split ( ","); if (temp.length <3) {return "0"; // invalid ip, lan test} 문자열 영역 = (temp [5] .split ( ":")) "; Country = DecodeUnicode (Country); // Country Break; 사례 3 : 면적 = (temp [i] .split ( ":")) 지역 = decodeUnicode (지역); // 지방 브레이크; 사례 7 : city = (temp [i] .split ( ":")) 카운티 = DecodeUnicode (카운티); // 지역 브레이크; 사례 11 : ISP = (Temp [i] .split ( ":")) StringBuffer (Country). Append (City). Append (County) .ToString (return null) /**; GBK, UTF-8 등 */ 개인 문자열 getResult (String urlstr, 문자열 인코딩) {url url = null; Connection.setConnectTimeout (2000); // 연결 시간 초과 시간을 milliseconds connection.setReadTimeout (33000)에서 설정하십시오. /// milliseconds connection.setDoOutput (true)에서 데이터를 읽는 시간 초과 설정; // 출력 스트림을 열는지 | false connection.setdoinput (true); false.setRequestMod (seterequestOd); 메소드 포스트 | 연결 get connection.setUeCaches (false); // 캐시 true true true true | false connection.connect (); // 연결을 열면 포트 데이터 아웃 스 스트림 out = new DataOutputStream (connection.getOutputStream ()); // 출력 스트림을 열고 동료 서버에 데이터를 씁니다. out.flush (); // refresh Out.close (); // 출력 스트림 bufferedReader reader = new bufferedReader (new inputStreamReader (connection.getInputStream (), 인코딩); // 후 피어 서버에 데이터를 작성한 후 데이터를 반환하고 버퍼링 리더 스트림을 사용하여 StringBuffer Buffer (); 문자열 line = ""; while ((line = reader.readline ())! = null) {buffer.append (line); } reader.close (); return buffer.toString (); } catch (ioexception e) {e.printstacktrace (); } round {if (connection! = null) {connection.disconnect (); // 연결을 닫습니다}} return null; } / ** * 유니 코드 중국어로 변환 * * @author fanhui 2007-3-15 * @param thestring * @return * / public static string decodeunicode (String thestring) {char achar; int len = thestring.length (); StringBuffer outbuffer = New StringBuffer (Len); for (int x = 0; x <len;) {achar = thestring.charat (x ++); if (achar == '//') {achar = thestring.charat (x ++); if (achar == 'u') {int value = 0; for (int i = 0; i <4; i ++) {achar = thestring.charat (x ++); switch (achar) {case '0': case '1': case '2': case '3': case '4': case '6': case '7': case '8': case '9': value = (value << 4) + achar - '0'; 부서지다; CASE 'A': CASE 'B': CASE 'C': CASE 'D': CASE 'E': CASE 'F': value = (value << 4) + 10 + Achar- 'a'; 부서지다; CASE 'A': CASE 'B': CASE 'C': CASE 'D': CASE 'E': CASE 'F': value = (value << 4) + 10 + Achar- 'a'; 부서지다; CASE 'A': CASE 'B': CASE 'C': CASE 'D': CASE 'E': CASE 'F': value = (value << 4) + 10 + Achar- 'a'; 부서지다; 기본값 : New New OregalArgumentException을 던지십시오 ( "Ollformed Encoding"); }} outbuffer.append ((char) 값); } else {if (achar == 't') {achar = '/t'; } else if (achar == 'r') {achar = '/r'; } else if (achar == 'n') {achar = '/n'; } else if (achar == 'f') {achar = '/f'; } outbuffer.append (Achar); }} else {outbuffer.append (Achar); }} return outbuffer.toString (); } // test public static void main (string [] args) {wasseUtils addressUtils = new AddressUtils (); // 테스트 IP 219.136.134.157 중국 = 중국 = 광동성 = 광저우시 = yuexiu 지구 = 통신 문자열 IP = "125.70.11.136"; 문자열 주소 = ""; try {address = jand } catch (UnsupportedEncodingException e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } system.out.println (주소); // 출력 결과는 다음과 같습니다. Guangdong Province, Guangzhou City, Yuexiu District}}