`
jayjayjays
  • 浏览: 217053 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • web
阅读更多

--  发布时间: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

 

分享到:
评论

相关推荐

    电力日负荷曲线预测程序和数据集(预测未来一天的负荷曲线)

    电力日负荷曲线预测程序和数据集(预测未来一天的负荷曲线)

    勾正科技向新而生智赢未来-2024年H1中国家庭智能大屏行业发展白皮书83页.pdf

    勾正科技向新而生智赢未来-2024年H1中国家庭智能大屏行业发展白皮书83页.pdf

    成绩分析问题-总文件压缩包(代码+所有磁盘文件)

    题目2.2(成绩分析问题):设计并实现一个成绩分析系统,们能够实现录入、保存一个班级学生多门课程的成绩,并成绩进行分析等功能。

    源代码-非零坊ASP友情链接 v5.0.zip

    更多毕业设计https://cv2022.blog.csdn.net/article/details/124463185

    java-springboot+vue应急救援物资管理系统源码.zip

    系统选用B/S模式,后端应用springboot框架,前端应用vue框架, MySQL为后台数据库。 本系统基于java设计的各项功能,数据库服务器端采用了Mysql作为后台数据库,使Web与数据库紧密联系起来。 在设计过程中,充分保证了系统代码的良好可读性、实用性、易扩展性、通用性、便于后期维护、操作方便以及页面简洁等特点。

    鸿蒙应用开发领域中DevEco Studio的安装、使用技巧及性能分析工具详细介绍

    内容概要:本文主要介绍了鸿蒙原生应用开发过程中可能遇到的内存问题以及相应的解决方案。针对这些问题,华为提供的 DevEco Studio 包含了性能分析工具 DevEco Profiler,提供两种场景化的分析模板——Snapshot Insight 和 Allocation Insight,支持实时监控、ArkTS 和 Native 内存的深度分析。这使得开发者能够有效识别、定界定位并优化内存问题,大幅提升应用的稳定性和性能。此外,文章还介绍了 DevEco Studio 强大的模拟器功能,该模拟器能仿真各类设备及场景,包括GPS定位、导航和低电量管理,极大提高了开发效率和测试灵活性。最后,文中详细列出了常见的快捷键,并给出了保持 DevEco Studio 与 Android Studio 快捷键同步的方法。 适合人群:专注于鸿蒙生态系统内的应用开发的技术人员,特别是有一定经验的中级至高级程序员。 使用场景及目标:本文旨在帮助开发者更好地理解和掌握 DevEco Studio 的强大工具链,尤其是解决开发过程中经常遇见的内存管理和多设备兼容问题,目标是优化开发流程,减少调测时间,增强产品的质量和用户体验。 阅读建议:开发者可通过鸿蒙官方提供的资源链接下载最新版本的 DevEco Studio 并探索相关技术博客,以获得最新的技术和使用技巧。建议在实践中逐步熟悉各个功能模块,并积极利用性能分析工具和模拟器来解决现实中的问题。

    我是谁啊我耽误 的耽误是

    我是谁

    精美导航引导页HTML源码 自适应手机/电脑,无后台

    精美导航引导页HTML源码,自适应手机/电脑,无后台,上传网站根目录就能用,首页内容在index里面修改 可以双页切换,亲测可用,搭建简单,附带修改教程

    hap手机软件包测试用

    hap手机软件包测试,测试使用

    电气工程领域的Altium Designer电子线路CAD训练-从基础入门到PCB设计实践

    内容概要:本文档是一份针对自动化专业的《电子线路CAD训练》实习报告,详细介绍了通过使用Altium Designer冬春软件进行电子线路的原理图设计、元件库文件设计、PCB板设计及元件封装库设计的过程。文档首先概述了训练的目的和重要性,随后逐步讲解Altium Designer Winter的安装与配置,然后重点展示了具体元件的设计细节,如温度传感器、AD输入通道、四双向模拟开关等的实际应用。此外,还详细阐述了自动布线和手动布线的具体步骤与注意事项,最后通过对此次实习的回顾,强调了本次训练对于提升电路设计能力和后续学习的支持。 适用人群:本报告适用于正在学习自动化及相关专业的在校大学生或从事电气工程领域的工程师和技术人员。 使用场景及目标:旨在帮助读者深入了解电子线路CAD的基础理论知识及其实际应用场景,特别是在Altium Designer环境下的操作流程。目标在于强化学生或技术人员的专业技能,以便他们能够在未来的工作或研究中有更强的设计能力。同时,该报告也可作为相关课程的教学材料。 其他说明:附录部分提供了完整的电路原理图和详细的元器件列表,供读者进一步理解和参照练习。

    2019年 金融网点分县统计数据.zip

    “2019年金融网点分县统计数据”提供了中国县域金融机构布局的详细信息,覆盖国有大型商业银行、股份制商业银行、城市商业银行及农村商业银行的网点分布特征。截至2019年底,全国银行网点总量为197,719个,其中县域地区分布87,003个,占比44%;市区网点110,716个,占比56%。 从银行类型看,国有大型商业银行县域网点数量最多(46,481个),但分布不均,如交通银行县域网点仅占9.01%,而邮政储蓄银行县域覆盖率高达59%。股份制商业银行县域网点仅占10%,主要集中于华东地区(73%)。农村商业银行县域网点占比60%(34,525个),华北和华中地区占其总量的53%。 区域分布上,华中地区县域网点占比最高(57.66%),其次是华东(34%)和西南(46%);华南地区县域网点最少,仅占7%。国有大行在华东地区县域网点占比32%,农村商业银行则集中在华北(32%)和华中(21%)。 该数据为研究金融资源城乡配置、普惠金融发展及区域经济差异提供了基础支撑。例如,国有大行2019年县域网点数量较前一年增加,反映其下沉服务趋势;而农村金融机构通过人缘地缘优势持续优化县域服务。数据格式包含分银行、分地区的统计表格,适用于量化分析金融网络覆盖与经济社会发展的关联性。

    GFP-ATOMIC参数的含义

    GFP-ATOMIC参数的含义

    ollama国内源,bash使用

    ollama国内源,bash使用

    电动汽车制造商迁移至Snowflake的数据平台现代化解决方案与实操

    内容概要:本文详细介绍了一家电动汽车(EV)制造商面临的数据处理挑战以及为解决这些问题所采取的举措——将现有数据平台迁移到Snowflake云平台上。文中阐述了制造商目前遇到的问题,如查询速度慢、运营成本高、难以整合结构化及非结构化的数据来源,并提出了具体的改进方向和技术细节。为了帮助潜在技术人员更好地理解和准备相关技术测试,还提供了一个详细的步骤指南来构建数据管道。具体要求分为两大部分:一是在当前架构上进行操作演示,二是利用Snowflake完成未来状态架构搭建并做技术示范,同时提供了预期产出物列表、所需技能概述及观众构成等关键信息。 适用人群:对于想要深入理解数据仓库迁移流程及其技术实施的专业人士非常有价值,特别适合作为数据工程师、数据科学家和其他IT专业人士参与面试的技术评估资料。 使用场景及目标:旨在展示候选人在构建现代数据工程基础设施方面的技术和创新能力。此外还可以作为内部培训材料供团队成员提高技能,或者为计划类似转型项目的企业决策层提供借鉴参考,从而优化其自身的数据管理策略和架构规划。 其他说明:演示时间被安排为60分钟,其中包括用例讲解(5分钟)、架构讨论(10分钟

    自动封装javaBean的工具类

    自动封装javaBean的工具类

    源代码-飞翔非主流ASP爬虫伪静态缓存版 v2.0.zip

    更多毕业设计https://cv2022.blog.csdn.net/article/details/124463185

    源代码-简洁快速趣味的开源ASP论坛 GBABOOK BBS v1.01 for SQL Server.zip

    更多毕业设计https://cv2022.blog.csdn.net/article/details/124463185

    wireshark log for ethercat io

    wireshark log for ethercat io

    TM1629A 驱动程序和数据手册.rar

    TM1629A 驱动程序和数据手册.rar

    Java-SpringBoot+vue的婚恋交流和信息管理系统毕业设计源码.zip

    系统选用B/S模式,后端应用springboot框架,前端应用vue框架, MySQL为后台数据库。 本系统基于java设计的各项功能,数据库服务器端采用了Mysql作为后台数据库,使Web与数据库紧密联系起来。 在设计过程中,充分保证了系统代码的良好可读性、实用性、易扩展性、通用性、便于后期维护、操作方便以及页面简洁等特点。

Global site tag (gtag.js) - Google Analytics