自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

炫酷,SpringBoot+Echarts實(shí)現(xiàn)用戶訪問(wèn)地圖可視化(附源碼)

開(kāi)發(fā) 后端
在常見(jiàn)的電商、新聞、社交網(wǎng)站等,合理運(yùn)用運(yùn)營(yíng)成本才能最大化輸出自己的產(chǎn)品,其中最常見(jiàn)的功能就有針對(duì)不同訪問(wèn)熱度的城市制定不同的運(yùn)營(yíng)手段,因此我們掌握用戶城市分布情況至關(guān)重要。

SpringBoot+Echarts用戶訪問(wèn)地圖可視化

意義

  •  在常見(jiàn)的電商、新聞、社交網(wǎng)站等,合理運(yùn)用運(yùn)營(yíng)成本才能最大化輸出自己的產(chǎn)品,其中最常見(jiàn)的功能就有針對(duì)不同訪問(wèn)熱度的城市制定不同的運(yùn)營(yíng)手段,因此我們掌握用戶城市分布情況至關(guān)重要。
  •  pc端與移動(dòng)端不同,無(wú)法依托手機(jī)自帶的gps定位到用戶所在城市,只能通過(guò)ip來(lái)進(jìn)行判斷所在地理位置。

根據(jù)ip獲取城市的方式

  •  淘寶、新浪等常年提供根據(jù)ip獲取城市的接口,但是隔一段時(shí)間會(huì)出現(xiàn)接口地址更改的情況,也有一定的限流
  •  開(kāi)源純真ip庫(kù):不斷迭代更新ip庫(kù)內(nèi)容,一般場(chǎng)景下足以使用,自主可控。(下載qqwry.dat庫(kù))

思路

首先需要獲取用戶請(qǐng)求的ip地址,我們對(duì)該方法進(jìn)行簡(jiǎn)單封裝: 

  1. public class IPUtil {  
  2.     public static String getIpAddress(HttpServletRequest request) {  
  3.         String ip = request.getHeader("x-forwarded-for");  
  4.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
  5.             ip = request.getHeader("Proxy-Client-IP");  
  6.         }  
  7.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
  8.             ip = request.getHeader("WL-Proxy-Client-IP");  
  9.         }  
  10.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
  11.             ip = request.getHeader("HTTP_CLIENT_IP");  
  12.         }  
  13.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
  14.             ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
  15.         }  
  16.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
  17.             ip = request.getRemoteAddr();  
  18.         }  
  19.         return ip;  
  20.     }  

封裝純真ip的解析工具,根據(jù)ip獲取請(qǐng)求地址所在城市,github有大量實(shí)現(xiàn)版本,我們這里不做贅述,具體代碼見(jiàn)文末源碼 

  1. //篇幅較長(zhǎng),截取的主要方法,詳細(xì)在源碼地址查看      
  2. public IPZone findIP(final String ip) {  
  3.         final long ipNum = toNumericIP(ip);  
  4.         final QIndex idx = searchIndex(ipNum);  
  5.         if (idx == null) {  
  6.             return new IPZone(ip); 
  7.          }  
  8.         return readIP(ip, idx);  

自定義攔截器,對(duì)用戶的登錄請(qǐng)求進(jìn)行攔截,在此處判斷請(qǐng)求ip所在城市,并進(jìn)行計(jì)數(shù)。我們這里只是簡(jiǎn)單邏輯的說(shuō)明,在生產(chǎn)上時(shí)應(yīng)該用redis來(lái)存放計(jì)數(shù),并且專門提供一個(gè)rest接口來(lái)推送當(dāng)前各城市訪問(wèn)數(shù)量情況,再由前端配合,隔一段時(shí)間發(fā)起一次請(qǐng)求,例如隔一小時(shí)請(qǐng)求一次該rest接口,從而進(jìn)行前端數(shù)據(jù)的展示。 

  1. /**  
  2.  * 登錄攔截器  
  3.  */  
  4. @Slf4j  
  5. public class MyLoginInterceptor implements HandlerInterceptor {  
  6.     private static final String LOGIN_PATH = "/user/login" 
  7.     private static Map<String, AtomicInteger> visitCount;  
  8.     private static final QQWry qqWry;  
  9.     static {  
  10.         visitCount = new HashMap<>(31);  
  11.         qqWry = new QQWry();  
  12.     } 
  13.     //展示訪問(wèn)數(shù)量不是精確指標(biāo),如果要做到完全正確需要使用鎖,防止計(jì)數(shù)存在并發(fā)問(wèn)題  
  14.     @Override  
  15.     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {  
  16.         log.info("【MyLoginInterceptor】調(diào)用了:{}", request.getRequestURI());  
  17.         if (request.getRequestURI().equals(LOGIN_PATH)) {  
  18.             String ipAddress = IPUtil.getIpAddress(request);  
  19.             String province = qqWry.findIP(ipAddress).getMainInfo();  
  20.             if (visitCount.containsKey(province)) {  
  21.                 visitCount.put(province,new AtomicInteger(visitCount.get(province).incrementAndGet()));  
  22.             } else {  
  23.                 visitCount.put(province,new AtomicInteger());  
  24.             }  
  25.         }  
  26.         return true;  
  27.     }  
  28.     @Override  
  29.     public void postHandle(HttpServletRequest request, HttpServletResponse response,  
  30.                            Object handler, ModelAndView modelAndView) throws Exception {}  
  31.     @Override  
  32.     public void afterCompletion(HttpServletRequest request, HttpServletResponse response,  
  33.                                 Object handler, Exception ex){}  

注冊(cè)自定義的攔截器 

  1. @Configuration  
  2. public class WebMvcConfig implements WebMvcConfigurer {  
  3.     @Override  
  4.     public void addInterceptors(InterceptorRegistry registry) {  
  5.         registry.addInterceptor(new MyLoginInterceptor());  
  6.     }  

登錄controller模擬邏輯,注意:如果想看效果圖需要自己寫線程用不同的虛擬ip進(jìn)行訪問(wèn)url,從而達(dá)到在不同城市訪問(wèn)接口的效果。 

  1. @RestController("user") 
  2. public class LoginController { 
  3.     @GetMapping("login")  
  4.     public String login() {  
  5.         //登錄邏輯  
  6.         return "success";  
  7.     }  

最終效果

前后端源碼

  1. https://github.com/Motianshi/distribute-tool  

 

責(zé)任編輯:龐桂玉 來(lái)源: Java知音
相關(guān)推薦

2022-09-29 11:16:21

Python數(shù)據(jù)可視化

2020-03-11 14:39:26

數(shù)據(jù)可視化地圖可視化地理信息

2022-04-13 09:01:53

Echart5繪制地圖

2018-03-21 12:13:47

工具數(shù)據(jù)開(kāi)發(fā)

2021-12-30 12:02:52

Python可視化代碼

2013-10-22 10:37:47

谷歌數(shù)據(jù)可視化

2019-07-26 09:19:32

數(shù)據(jù)可視化架構(gòu)

2022-08-17 09:01:16

數(shù)據(jù)可視化大數(shù)據(jù)

2014-01-17 10:36:39

2020-03-01 14:01:22

Echarts數(shù)據(jù)可視化圖表

2022-03-01 10:29:44

Kubernetes容器

2019-05-20 08:20:40

數(shù)據(jù)集數(shù)據(jù)可視化數(shù)據(jù)

2017-10-14 13:54:26

數(shù)據(jù)可視化數(shù)據(jù)信息可視化

2017-10-11 18:17:06

大數(shù)據(jù)數(shù)據(jù)可視化前后端

2021-04-19 09:00:54

Python批量下載視頻下載器

2010-08-04 10:48:17

路由器

2021-03-17 08:07:56

Python可視化工具

2017-09-05 08:35:09

Python可視化地圖

2017-09-01 19:49:50

Python工具地圖

2021-03-18 08:11:18

PythonDash工具
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)