个人网站性能优化经历(10)网站增加访客记录

Posted exodus3

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了个人网站性能优化经历(10)网站增加访客记录相关的知识,希望对你有一定的参考价值。

自己搭建了一个基于SpringBoot+Spring Security+MyBatis+mysql+Redis+Thymeleaf的博客网站
上线个人云服务器后,发现服务器访问慢。个人服务器是1核2G的,1M宽带,虽然服务器是低配的,但是可以通过优化代码,中间件等手段,来提升性能。

这篇主要讲网站增加访客记录的功能

1、设计表字段,新增visit表

id主键,city城市,url访问博客的url,visTime访问时间,browserType浏览器类型(360浏览器,谷歌浏览器,百度浏览器,IE浏览器),platformType平台类型(安卓,苹果,电脑)

2、建表语句

CREATE TABLE `visit` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ip` varchar(255) DEFAULT NULL,
  `city` varchar(255) DEFAULT NULL,
  `url` varchar(255) DEFAULT NULL,
  `visitTime` varchar(255) DEFAULT NULL,
  `browserType` varchar(255) DEFAULT NULL,
  `platformType` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16081 DEFAULT CHARSET=utf8;

3、实体类

@Data
public class Visit {

    private Integer id;

    private String ip;

    private String city;

    private String url;

    private String visitTime;

    private String browserType;//浏览器类型

    private String platformType;//平台类型

    public Visit(){}

    public Visit(String browserType, String platformType){
        this.browserType = browserType;
        this.platformType = platformType;
    }

}

4、写个根据IP地址获取详细的地域信息的工具类

http://ip.taobao.com/service/getIpInfo.php 这个调用淘宝的访问ip具体信息的工具类,调完这个接口,对结果进行封装,地区划分国家,省份,城市(其中,对编码unicode设置为中文的)

package com.zxl.utils;

import org.apache.commons.lang.StringUtils;

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;

/**
 * 根据IP地址获取详细的地域信息
 */
public class AddressUtil {

    public String getAddresses(String content, String encodingString)
            throws UnsupportedEncodingException {
        String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
        String returnStr = this.getResult(urlStr, content, encodingString);
        if (returnStr != null) {
            // 处理返回的省市区信息
            //System.out.println(returnStr);
            String[] temp = returnStr.split(",");
            if (temp.length <= 3) {
                return "未知城市";  // 无效IP,局域网测试
            }
            String country = (temp[2].split(":"))[1].replaceAll("\\"", "");
            country = decodeUnicode(country);// 国家
            if (country.equals("XX") || country == "") {
                country = "";
            }
            String province = (temp[4].split(":"))[1].replaceAll("\\"", "");
            province = decodeUnicode(province);// 省份
            if (province.equals("XX") || province == "") {
                province = "";
            } else {
                province = province + "省";
            }
            String area = (temp[3].split(":"))[1].replaceAll("\\"", "");
            area = decodeUnicode(area);// 地区
            if (area.equals("XX") || StringUtils.isEmpty(area)) {
                area = "";
            } else {
                area = area + "区/县";
            }
            String city = (temp[5].split(":"))[1].replaceAll("\\"", "");
            city = decodeUnicode(city);// 城市
            if (city.equals("XX") || city == "" || city.equals("内网IP")) {
                city = "";
            } else {
                city = city + "市";
            }
            String isp = (temp[7].split(":"))[1].replaceAll("\\"", "");
            isp = decodeUnicode(isp);// ISP
            if (isp.equals("XX") || isp == "") {
                isp = "";
            }
            String str = province + city + isp;
            if (str.equals("") || str == "") {
                str = country;
            } else {
                str = province + city + area + " " + isp;
            }
            return str;
        }
        return "未知城市";
    }

    private String getResult(String urlStr, String content, String encoding) {
        URL url = null;
        HttpURLConnection connection = null;
        try {
            url = new URL(urlStr);
            connection = (HttpURLConnection) url.openConnection();// 新建连接实例
            connection.setConnectTimeout(2000);// 设置连接超时时间,单位毫秒
            connection.setReadTimeout(2000);// 设置读取数据超时时间,单位毫秒
            connection.setDoOutput(true);// 是否打开输出流 true|false
            connection.setDoInput(true);// 是否打开输入流true|false
            connection.setRequestMethod("POST");// 提交方法POST|GET
            connection.setUseCaches(false);// 是否缓存true|false
            connection.connect();// 打开连接端口
            DataOutputStream out = new DataOutputStream(
                    connection.getOutputStream());// 打开输出流往对端服务器写数据
            out.writeBytes(content);// 写数据,也就是提交你的表单 name=xxx&pwd=xxx
            out.flush();// 刷新
            out.close();// 关闭输出流
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), encoding));// 往对端写完数据对端服务器返回数据
            // ,以BufferedReader流来读取
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            reader.close();
            return buffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();// 关闭连接
            }
        }
        return "";
    }

    /**
     * unicode 转换成 中文
     */
    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 '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException(
                                        "Malformed      encoding.");
                        }
                    }
                    outBuffer.append((char) value);
                } 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();
    }
	
}

5、编写个IP工具类,用来获取IP信息

public class IpUtil {

    // 获取IP
    public static String getIp(HttpServletRequest httpRequest) {
        String ipAddress = httpRequest.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0
                || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = httpRequest.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0
                || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = httpRequest.getHeader("WL-Proxy-Client-IP");
        }
        if(null == ipAddress || 0 == ipAddress.length() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = httpRequest.getHeader("X-Real-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0
                || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = httpRequest.getRemoteAddr();
            if (ipAddress.equals("127.0.0.1")
                    || ipAddress.equals("0:0:0:0:0:0:0:1")) {
                // 根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ipAddress = inet.getHostAddress();
            }
        }
        // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
        if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
            // = 15
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }

}

6、新增访客记录方法

接下来就是具体的操作了,每次访问文章的时候,调insert方法,这个insert方法就是把访客的ip,地址,访问的文章等信息插入到visit这个表里

public void insert(HttpServletRequest request){
	Visit visit = new Visit();
	visit.setIp(IpUtil.getIp(request));
	TimeUtil timeUtil = new TimeUtil();
	visit.setVisitTime(timeUtil.getFormatDateForSix());
	int visitTimes = visitMapper.findVisitTimes(timeUtil.getFormatDateForThree(), IpUtil.getIp(request));
	String userAgent = request.getHeader("user-agent");

	try{
		if (visitTimes == 0) {
			visit.setPlatformType(UserAgentUtil.getUserAgent(
					userAgent).getPlatformType());
			visit.setBrowserType(UserAgentUtil.getUserAgent(
					userAgent).getBrowserType());
			visit.setUrl(request.getRequestURL().toString());
			visit.setCity(n

以上是关于个人网站性能优化经历(10)网站增加访客记录的主要内容,如果未能解决你的问题,请参考以下文章

10种优化页面加载速度的方法——转

个人网站性能优化经历其他优化

个人网站性能优化经历网站添加实用功能

个人网站性能优化经历访问文章功能优化

个人网站性能优化经历CDN优化过程

个人网站性能优化经历Redis优化过程