探讨关于门户网站中的大型计数器问题解决方案(Inber原作)

--  发布时间:2005-10-20 22:06:01

--  计数器系统

有一计数器系统要求如下: 
1性能要求:每天访问量要求符合100000/天  且有30-40  个站点统计  同时进行 
2刷新量,纯IP统计(0-24  每个时间段内)各自形成每天报表 
本人的解决方案:
第一次读数据库,之后的数据都存在APLICATION中进行加一操作定时更新数据库 三四十个站点同时进行。

问题:发现Aplication 有不稳定的情况(同一Aplication  有时候一会儿变成这个数据 一会儿变成那个数据,之间的差值会虽时间长而变大,但是还是最大的那个正确,就是检查不出来怀疑数据访问量大的时候Tomcat 没有处理好Aplication这个东西)如果把这个程序单独每个统计站布署一个就不会出现这种情况。注:我的程序不存在性能瓶经基本全在内存中操作速度很快服务器硬件也没有问题。

谁能用JAVA来实现统一计数实现我如上的条件,或者能解决一下我的问题所在,本人将以高分数表示感谢,希望写过大型计数器的朋友进入

本人写了一个Servlet  不怕大家见笑抛砖引玉:
以下是本人前100行代码(程序有不完善的地方见笑。。。)
import java.sql.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.PageContext;

import java.io.*;
import java.util.Date;
import java.text.DateFormat;
import com.inberkong.util.DataBaseConnection;

public class CounterServlet extends HttpServlet
{
private String counterSite;
//private int count=0;

private  Connection con=null;

private String pString ="";


private final int WRITE_TIME=6;//定时写数据库的间隔时长以秒为单位

private ServletContext application=null;
  
public  void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
{

response.setContentType("text/html;charset=gb2312");
String theIP=request.getRemoteAddr();//得到访问者IP;
Date time1=new java.util.Date();//获得系统当前时间
    String Referer=request.getHeader("Referer");//得到来访者地址
   
    if(Referer==null)
    {
     System.out.print("No Referer value");
    }
   // String Referer="http://61.163.234.223/zhaotie/test1.htm";///' target="_blank" >http://61.163.234.223/zhaotie/test1.htm";//摸拟得到来访者地址(检验数据开始要把此项打开)    

    

    String [] siteAddress=Referer.split("/");
counterSite=siteAddress[0]+"//"+siteAddress[2];//得到来访者的网址域名

PrintWriter out =response.getWriter();

String  sql="";

Date tm=new java.util.Date();//获得系统当前时间
String sqltm=new Timestamp(tm.getTime()).toString();//将当前时间转换成sql server可以识别的对象
String st2=sqltm;
st2=st2.substring(0,19);//2005-08-14 18:41:33
String d1=st2.substring(0,10);
     
application = getServletConfig().getServletContext();


try
{
 

  if(application.getAttribute(counterSite+"_checkAccount")==null)//初始计数器的状态.
  {

        
    //application.setAttribute(counterSite,new Integer(count));//计数器
   
    Statement stmt=null;
       try{
        //如果数据库缓冲池无连接则重新启动TOMCATE服务   
           stmt=con.createStatement();
       }catch(SQLException ee)
       {
        destroy();
        Runtime rn=Runtime.getRuntime();
        Process p=null;
        try{
        
        p=rn.exec("tomcat5.exe stop");
        for(int i=0;i<1999999999;i++)
        {
        ;
        }
        p=rn.exec("taskkill /F /IM tomcat5w.exe /T");
        p=rn.exec("taskkill /F /IM tomcat5.exe /T");
        for(int j=0;j<1999999999;j++)
        {
        ;
        }
        p=rn.exec("tomcat5.exe start");
      
       
        }catch(Exception e){
        System.out.println("Error exec notepad");
        }
       

以下为101-200行代码
       System.out.print(ee+""+new java.util.Date());
       
       
       }
    ResultSet rs=null;      
    application.setAttribute(counterSite+"Date1",new java.util.Date());//站点定时时间
     sql="select id,R_TODAY_SUM,R_TOM_SUM,R_THIS_MONTH_SUM,R_TOTAL_SUM,R_TOTAL_DAY,R_COUNT_DATE from inber_count_account where ACCOUNT_SIT_ADD like /'%"+counterSite+"%/'";
    
     //out.print(sql);
     rs=stmt.executeQuery(sql);
     if(rs.next())
     {

     
     application.setAttribute(counterSite+"_checkAccount",new String("ok"));
     application.setAttribute(counterSite+"_id",new Integer(rs.getInt(1)));
    
     application.setAttribute(counterSite+"_R_TODAY_SUM",new Integer(rs.getInt(2)));    
     application.setAttribute(counterSite+"_R_TOM_SUM",new Integer(rs.getInt(3)));    
     application.setAttribute(counterSite+"_R_THIS_MONTH_SUM",new Integer(rs.getInt(4)));    
     application.setAttribute(counterSite+"_R_TOTAL_SUM",new Integer(rs.getInt(5)));    
     application.setAttribute(counterSite+"_R_TOTAL_DAY",new Integer(rs.getInt(6)));    

//****
     application.setAttribute(counterSite+"_R_COUNT_DATE",new String(rs.getString("R_COUNT_DATE")));    

     }
     rs.close();
     stmt.close();
    
  }
        else
        {
        

   if((application.getAttribute(counterSite+"_checkAccount").toString()).equals("ok"))//校验此计数器账号引用是否存在
   {
   
    Date accessTime=null;   
  

//刷新率 start----------------------------------

       application.setAttribute(counterSite+"_R_TODAY_SUM",new Integer(Integer.parseInt((application.getAttribute(counterSite+"_R_TODAY_SUM").toString()))+1));    
       application.setAttribute(counterSite+"_R_THIS_MONTH_SUM",new Integer(Integer.parseInt((application.getAttribute(counterSite+"_R_THIS_MONTH_SUM").toString()))+1));    
       application.setAttribute(counterSite+"_R_TOTAL_SUM",new Integer(Integer.parseInt((application.getAttribute(counterSite+"_R_TOTAL_SUM").toString()))+1));
        
        
      
       String d2=application.getAttribute(counterSite+"_R_COUNT_DATE").toString();
       //当日期改变时,清除当天的计数
       String tYear,tMonth,tDay;
       tYear=d2.substring(0,4);
       tMonth=d2.substring(5,7);
       tDay=d2.substring(8,10);

       if(!(d1.equals(d2)))
       {


       int todaysum=Integer.parseInt((application.getAttribute(counterSite+"_R_TODAY_SUM").toString()));
       int totalday=Integer.parseInt((application.getAttribute(counterSite+"_R_TOTAL_DAY").toString()));

       application.setAttribute(counterSite+"_R_TOM_SUM",new Integer(todaysum));
       application.setAttribute(counterSite+"_R_TODAY_SUM",new Integer(0));
       application.setAttribute(counterSite+"_R_TOTAL_DAY",new Integer(totalday+1));   
       application.setAttribute(counterSite+"_R_COUNT_DATE",new String(d1));


      
      
try{
                    CallableStatement cstmt4=null;
              
         pString = "{call p_setDetailList3(/'"+tYear+"/', /'"+tMonth+"/', /'"+tDay+"/',"+todaysum+", /'"+d2+"/',"+Integer.parseInt((application.getAttribute(counterSite+"_id").toString()))+")}";
         cstmt4=con.prepareCall(pString);
         cstmt4.executeUpdate();
         cstmt4.close();     
      }
      catch(Exception ee)
      {
       System.out.println("call p_setDetailList3 is wrong:"+ee);
      }

      
       }
      
      
       String cMonth=d1.substring(5,7);
       //当月改变时清除当月的计数,并计数当月第一个浏览者.
       if(!(cMonth.equals(tMonth)))
       {
       application.setAttribute(counterSite+"_R_THIS_MONTH_SUM",new Integer(0));
       application.setAttribute(counterSite+"_R_COUNT_DATE",new String(d1));       
       }    
      
  


//刷新率 end----------------------------------
201-3百行以后的代码


   //count=Integer.parseInt((application.getAttribute(counterSite).toString()));
  


定时写数据库同时 初始化数据 start/
   Date time2=new java.util.Date();//获得系统当前时间
   Date t1=(Date)application.getAttribute(counterSite+"Date1");
         int secondValue=(int)(((double)time2.getTime()-(double)t1.getTime())/1000);

 
          if(secondValue>=WRITE_TIME)//
          {//定时写数据库同时 初始化数据


          int siteid=Integer.parseInt((application.getAttribute(counterSite+"_id").toString()));
    int a1=Integer.parseInt((application.getAttribute(counterSite+"_R_TODAY_SUM").toString()));
    int a2=Integer.parseInt((application.getAttribute(counterSite+"_R_TOM_SUM").toString()));
    int a3=Integer.parseInt((application.getAttribute(counterSite+"_R_THIS_MONTH_SUM").toString()));
    int a4=Integer.parseInt((application.getAttribute(counterSite+"_R_TOTAL_SUM").toString()));
    int a5=Integer.parseInt((application.getAttribute(counterSite+"_R_TOTAL_DAY").toString()));
    String a6=application.getAttribute(counterSite+"_R_COUNT_DATE").toString();
   

try{
                    CallableStatement cstmt5=null;         
         
       pString = "{call p_INBER_COUNT_ACCOUNT_EDIT2("+siteid+","+a1+","+a2+","+a3+","+a4+","+a5+",/'"+a6+"/')}";
       cstmt5=con.prepareCall(pString);
       cstmt5.executeUpdate();
       cstmt5.close();
      }
      catch(Exception ee)
      {
       System.out.println("call p_INBER_COUNT_ACCOUNT_EDIT2 is wrong:"+ee);
      }


          //count=0;
        
    //application.setAttribute(counterSite,new Integer(count));//得到初始值,
    application.setAttribute(counterSite+"Date1",new java.util.Date());//初始时间为系统当前时间
    //application.setAttribute(counterSite,new Integer(count));//计数器
   
定时写数据库同时 初始化数据 end//
      
          }
//检验数据2开始///
/*out.print("secondValue="+secondValue+"<BR>");
    out.print("counter="+count+"<BR>"+"counterSite="+counterSite+"<BR>");  
   
*/
//检验数据2结束///  
   }
  
   else
   {//校验此计数器账号引用是否存在
   }
     
        }

  //System.out.println(count+":"+counterSite+" "+theIP); 
  // count++;
  

   //application.setAttribute(counterSite,new Integer(count)); 
  
 


} catch(SQLException e)
{
   System.out.println("cc:"+e);

out.close();
   }
   

    /**
     *处理Post请求
     */

    /**
     *获得初始化参数
     */
    public void init() throws ServletException
    {
     try{
      int ss=0;
    
     con=DataBaseConnection.getConnection();

   
   
  }
  catch (Exception e)
  {

    System.out.println("Error(con error):"+e);
  }
    }
     public void destroy()
    {
    
    
          int siteid=Integer.parseInt((application.getAttribute(counterSite+"_id").toString()));
  
    int a1=Integer.parseInt((application.getAttribute(counterSite+"_R_TODAY_SUM").toString()));
    int a2=Integer.parseInt((application.getAttribute(counterSite+"_R_TOM_SUM").toString()));
    int a3=Integer.parseInt((application.getAttribute(counterSite+"_R_THIS_MONTH_SUM").toString()));
    int a4=Integer.parseInt((application.getAttribute(counterSite+"_R_TOTAL_SUM").toString()));
    int a5=Integer.parseInt((application.getAttribute(counterSite+"_R_TOTAL_DAY").toString()));
    String a6=application.getAttribute(counterSite+"_R_COUNT_DATE").toString();
  try{
                   CallableStatement cstmt6=null;   
         
       pString = "{call p_INBER_COUNT_ACCOUNT_EDIT2("+siteid+","+a1+","+a2+","+a3+","+a4+","+a5+",/'"+a6+"/')}";
       cstmt6=con.prepareCall(pString);
       cstmt6.executeUpdate();
       cstmt6.close();   
         
          System.out.println("when the programe stoped execute this code automatic!");
          con.close();

        }
  catch (SQLException e)
  {

  System.out.println("desdory error:"+e);
  }
    }
   

}

   
    
   =====================

天哪... 没看懂

大概你是遇上并发操作的问题了. 所有的应用服务器都有并发的问题, 主要还是看的应用怎么去处理了. 要注意一下几点

1. 数据库操作得用事务, 而且避免脏读. 如果想避免别人并发操作你的数据, 可以使用锁
2. 对象上注意是否线程安全, 避免多个线程同时写变量
3. 更新数据使用相对值, 不要用绝对值. 如: update table set a = a + @value where ...

100000/天访问不算大, 如果没有并发峰值限制. 不需要太特殊得处理方法

http://community.csdn.net/Expert/topic/4332/4332118.xml?temp=.1796991

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值