Due
Some time ago found FlagCounter counter to the right of the blog suddenly gone, and saw a garden blog blocked FlagCounter news, a bit puzzled. So on FlagCounter I watched a website, found the recent visit from the new countries actually from Taiwan. After another round Baidu, see bloggers issued a statement saying because the state refused to use FlagCounter the position. So I quickly emptied the bulletin board, but unfortunately there were no alternatives to think simply write one yourself.
Development environment and the online environment
- SpringBoot 2.1.8
- Redis 5.0.x
- Domain name + SSL certificate (blog Park requires https)
Use to interface and open source data
- www.taobao.com/help/getip.php used to obtain an IP address
- http://ip-api.com/json/ used to resolve IP location (only HTTP, so the request needs to be transmitted through the rear end)
- https://github.com/mukeshsolanki/country-picker-android flag Source
Development of ideas
Taobao interface to obtain an IP address visitors at the front end, back-end pass through user access. Backend requests country code to parse the ip address ip-api, and recorded Redis database. Meanwhile the visitor's request to the back-end data and displayed on the page.
Key Code
package com.qf;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Set;
@CrossOrigin
@RestController
@Transactional
public class Controller {
@Autowired
private StringRedisTemplate redisTemplate;
private RestTemplate restTemplate = new RestTemplate();
// 接口隐藏
@RequestMapping("xxxxxx")
public String getVisitor(){
// 取出排名前20的国家以及total(总数),所以是0-20共21个
Set<ZSetOperations.TypedTuple<String>> typedTupleSet = redisTemplate.opsForZSet().reverseRangeWithScores("visitor", 0, 20);
return JSON.toJSON(typedTupleSet).toString();
}
// 接口隐藏
@RequestMapping("xxxxxx")
public void addVisitor(@PathVariable String ip){
// 请求ip-api接口,获取ip所属地信息
IPaddr iPaddr = restTemplate.getForObject("http://ip-api.com/json/"+ip, IPaddr.class, new HashMap<>());
String code = iPaddr.getCountryCode();
// 转换为GB/T 2659-2000标准
if (code.equals("HK") || code.equals("TW") || code.equals("MO")) {
code = "CN";
}
// 更新Redis中的记录
redisTemplate.opsForZSet().incrementScore("visitor", code, 1);
redisTemplate.opsForZSet().incrementScore("visitor", "total", 1);
}
}