java实现网站访问量统计

来源:互联网 发布:js输出时间格式 编辑:程序博客网 时间:2024/06/05 05:19

【写在前面的废话】


【前段时间给学校做网站,老师提了一个能统计访问量的需求,当时因为时间原因没做,最近恰逢十一假期,先把后台实现了】
【其实这东西完全可以集成第三方来做,这几天无聊,还是自己实现一下后台】


1.先说说需求吧

老师当时提的需求是在访问网站是给用户一个提示,类似于“您是第8888位访客”这种东西,上个周突然想到可以做一个平台,帮助所有入驻该平台的网站统计他们的访问量。

2.技术应用

因为最近在搭springboot+mybatis的框架玩,所以这个直接用了这两个框架,前期仅仅实现了统计,没进行存储,所以不熟悉这两个框架的朋友也能看得懂。另外采用前后端分离开发,先把后端接口开发完了,集成了swagger。对于这部分用到的框架,其实主要是最近在搭这几个框架才用,要不然直接写.java即可实现。

3.设计思路

总体就是在前端页面加一点东西,每次加载完页面后携带key调用一次写好的后台接口,后台接口比如:127.0.0.1:1188/border/website/count?key=110101,url中的key值就标识了这是哪个页面或者网站,因为想实现的是多个网站共同入驻。后台主线程来接收请求。另外有一个Timer线程起一个定时器,以后可能会做定时把数据存一下什么的操作。还有一个RunnableThreadWebCount线程负责来统计网站访问次数,这个线程中有一个Map<K,V>,存储了网站的key值和访问次数。

4.代码来了

【1】springboot启动

package com.zhao.myfirstboot;import com.zhao.myfirstboot.util.RunnableThreadWebCount;import com.zhao.myfirstboot.util.Timers;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.EnableAspectJAutoProxy;@SpringBootApplication@EnableAspectJAutoProxy@MapperScan("com.zhao.myfirstboot.dao")public class MyfirstbootApplication {    public static void main(String[] args) {        SpringApplication.run(MyfirstbootApplication.class, args);        //计数线程        RunnableThreadWebCount runnableThreadWebCount=new RunnableThreadWebCount();        runnableThreadWebCount.run();        //计时器线程        Timers timers=new Timers();        timers.run();    }}

【2】这是用来接收请求的controller

package com.zhao.myfirstboot.controller;import com.zhao.myfirstboot.util.RunnableThreadWebCount;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.web.bind.annotation.*;/** * 网站访问控制层 * Created by zhaoxudong on 2017/9/30. */@RestController@RequestMapping(value = "/border/website")@Api(value = "WebsiteController" ,description = "网站访问")public class WebsiteController {    @ApiOperation(value = "count", httpMethod = "GET")    @RequestMapping(value = "/count/")    @ResponseBody    public int count(@RequestParam("key") String key) {        return RunnableThreadWebCount.addCount(key);    }}

【3】计数线程

package com.zhao.myfirstboot.util;import java.util.HashMap;/** * Created by zhaoxudong on 2017/10/1. */public class RunnableThreadWebCount implements Runnable{    private static HashMap<String,Integer> map=new HashMap<String,Integer>();    @Override    public void run() {        System.out.println("计数线程已启动------");        map.put(new String("110101"), 0);    }    public static void showthread(){        System.out.println("------------计数线程运行中--");    }    public static int addCount(String key){        Integer count=map.get(key);        if(count==null){            return 0;        }        count++;        map.put(key,count);        System.out.println("网站访问人数:"+count);        return count;    }}

【4】计时器线程
这部分只是一个简单的打印,后面会改成存储

package com.zhao.myfirstboot.util;import java.util.Timer;import java.util.TimerTask;/** * Created by zhaoxudong on 2017/10/1. */public class Timers implements Runnable{    @Override    public void run() {        System.out.println("计时器线程已启动----------");        TimerTask task = new TimerTask() {            @Override            public void run() {                RunnableThreadWebCount.showthread();            }        };        Timer timer = new Timer();        timer.scheduleAtFixedRate(task, 1000, 3000);    }}

5.结果

【1】springboot启动
启动

【2】用swagger测一下
swagger

【3】直接访问接口试试
浏览器直接输入localhost:1188/border/count?key=110101访问,多次刷新
B

该项目github地址:
https://github.com/hellozhaoxudong/webcount.git

【注】:这样这个使用java统计网站访问量基本功能就实现了,最近再完善一下,实现多个网站共同入驻统计并定时存储。

原创粉丝点击