`

通用大型网站页面静态化解决方案二(非原创)

 
阅读更多

在开发大型网站时,避免不了处理大量的页面静态化操作,这样方便加快网站访问速度与流量分流,那么如何来实现呢?其实说白了比较简单,网站静态化主要包括以下几方面的工作 
多个文件服务器读写,这里可采用SMB协议 
页面静态化,可采用freemarker开源框架 
如果考虑到大量的读写请求,则将请求分布式或采用调度的办法来解决 
第一点我们首先应该考虑文件服务器与静态页面的映射关系,即什么文件应该读写到哪台服务器,这个关系最简单的办法是随机映射,然后将映射关系保存到数据库中即可,SMB常用的操作代码如下: 

   1. public static boolean exists(String filepath,String username,String pwd) throws Exception 
   2. { 
   3. SmbFile file = new SmbFile("smb://"+username+":"+pwd+"@"+filepath); 
   4. try{ 
   5. return file.exists(); 
   6. }catch(Exception ex){ 
   7. return false; 
   8. } 
   9. } 
  10. public static boolean fileRename(String filepath,String newFilename,String username,String pwd) 
  11. { 
  12. try{ 
  13. SmbFile f=new SmbFile("smb://"+username+":"+pwd+"@"+filepath); 
  14. if(f.isFile()){ 
  15. String str=filepath.substring(0,filepath.lastIndexOf("/")); 
  16. str="smb://"+username+":"+pwd+"@"+str+"/"+newFilename; 
  17. f.renameTo(new SmbFile(str)); 
  18. }else if(f.isDirectory()){ 
  19. String str=filepath.substring(0,filepath.length()-1); 
  20. str=filepath.substring(0,str.lastIndexOf("/")); 
  21. str="smb://"+username+":"+pwd+"@"+str+"/"+newFilename; 
  22. f.renameTo(new SmbFile(str)); 
  23. } 
  24. return true; 
  25. }catch(Exception ex){ 
  26. return false; 
  27. } 
  28. } 
  29. public static void mkdir(String dir,String username,String pwd) 
  30. { 
  31. try{ 
  32. SmbFile f=new SmbFile("smb://"+username+":"+pwd+"@"+dir); 
  33. if(!f.exists()) 
  34. f.mkdir(); 
  35. }catch(Exception ex) 
  36. { 
  37. } 
  38. } 
  39. public static void mkfile(String filepath,String username,String pwd) 
  40. { 
  41. try 
  42. { 
  43. SmbFile f=new SmbFile("smb://"+username+":"+pwd+"@"+filepath); 
  44. if(!f.exists()) 
  45. f.createNewFile(); 
  46. }catch(Exception ex) 
  47. { 
  48. } 
  49. } 
  50. public static void mkfile(String filepath,String username,String pwd,String content) 
  51. { 
  52. try 
  53. { 
  54. SmbFile f=new SmbFile("smb://"+username+":"+pwd+"@"+filepath); 
  55. if(!f.exists()) 
  56. f.createNewFile(); 
  57. writeFile(filepath,content,username,pwd); 
  58. }catch(Exception ex) 
  59. { 
  60. } 
  61. } 
  62. public static boolean isdir(String filepath,String username,String pwd) throws Exception 
  63. { 
  64. String dir="smb://"+username+":"+pwd+"@"+filepath; 
  65. SmbFile f=new SmbFile(dir); 
  66. return f.isDirectory(); 
  67. } 

复制代码 
第二点,页面静态化可由freemarker生成,freemarker的使用比较简单,我这里不再啰嗦,重复说了 
第三点,调度中心,或把静态化的请求先保存到Task中,然后通过调度中心异步执行,可用我在博客中说道的另外一篇文章解决即可

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics