大型网站页面静态化解决方案
2011-01-27 11:00
483 查看
在开发大型网站时,避免不了处理大量的页面静态化操作,这样方便加快网站访问速度与流量分流,那么如何来实现呢?其实说白了比较简单,网站静态化主要包括以下几方面的工作:
多个文件服务器读写,这里可采用SMB协议
页面静态化,可采用freemarker开源框架
如果考虑到大量的读写请求,则将请求分布式或采用调度的办法来解决
第一点我们首先应该考虑文件服务器与静态页面的映射关系,即什么文件应该读写到哪台服务器,这个关系最简单的办法是随机映射,然后将映射关系保存到数据库中即可,SMB常用的操作代码如下:
package org.manggo.test;
import java.net.MalformedURLException;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
public class SmbTest {
public static boolean exists(String filepath, String username, String pwd) throws MalformedURLException{
SmbFile file = new SmbFile(“smb://” + username + “:” + pwd + “@” + filepath);
try {
return file.exists();
} catch (Exception ex) {
return false;
}
}
public static boolean fileRename(String filepath, String newFilename, String username, String pwd) throws MalformedURLException, SmbException {
SmbFile f = new SmbFile(“smb://” + username + “:” + pwd + “@” + filepath);
if (f.isFile()) {
String str = filepath.substring(0, filepath.lastIndexOf(“/”));
str = “smb://” + username + “:” + pwd + “@” + str + “/” + newFilename;
f.renameTo(new SmbFile(str));
} else if (f.isDirectory()) {
String str = filepath.substring(0, filepath.length() – 1);
str = filepath.substring(0, str.lastIndexOf(“/”));
str = “smb://” + username + “:” + pwd + “@” + str + “/” + newFilename;
f.renameTo(new SmbFile(str));
}
return true;
}
public static void mkdir(String dir, String username, String pwd) {
try {
SmbFile f = new SmbFile(“smb://” + username + “:” + pwd + “@” + dir);
if (!f.exists())
f.mkdir();
} catch (Exception ex) {
}
}
public static void mkfile(String filepath, String username, String pwd) {
try {
SmbFile f = new SmbFile(“smb://” + username + “:” + pwd + “@” + filepath);
if (!f.exists())
f.createNewFile();
} catch (Exception ex) {
}
}
public static void mkfile(String filepath, String username, String pwd, String content) {
try {
SmbFile f = new SmbFile(“smb://” + username + “:” + pwd + “@” + filepath);
if (!f.exists())
f.createNewFile();
//writeFile(filepath, content, username, pwd);
} catch (Exception ex) {
}
}
public static boolean isdir(String filepath, String username, String pwd) throws MalformedURLException, SmbException {
String dir = “smb://” + username + “:” + pwd + “@” + filepath;
SmbFile f = new SmbFile(dir);
return f.isDirectory();
}
public static void main(String[] args) {
SmbTest.mkdir(“192.168.100.188/wwwroot/test”, “hongquanli”, “87483281″);
try{
SmbFile smbFile=new SmbFile(“smb://hongquanli:87483281@192.168.100.188/wwwroot/index.htm”);
//通过 smbFile.isDirectory();isFile()可以判断smbFile是文件还是文件夹
int length=smbFile.getContentLength();//得到文件的大小
byte buffer[] = new byte[length] ;
SmbFileInputStream in = new SmbFileInputStream(smbFile) ; //建立smb文件输入流
while((in.read(buffer)) != -1){
System.out.write(buffer);
System.out.println(buffer.length);
}
in.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
多个文件服务器读写,这里可采用SMB协议
页面静态化,可采用freemarker开源框架
如果考虑到大量的读写请求,则将请求分布式或采用调度的办法来解决
第一点我们首先应该考虑文件服务器与静态页面的映射关系,即什么文件应该读写到哪台服务器,这个关系最简单的办法是随机映射,然后将映射关系保存到数据库中即可,SMB常用的操作代码如下:
package org.manggo.test;
import java.net.MalformedURLException;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
public class SmbTest {
public static boolean exists(String filepath, String username, String pwd) throws MalformedURLException{
SmbFile file = new SmbFile(“smb://” + username + “:” + pwd + “@” + filepath);
try {
return file.exists();
} catch (Exception ex) {
return false;
}
}
public static boolean fileRename(String filepath, String newFilename, String username, String pwd) throws MalformedURLException, SmbException {
SmbFile f = new SmbFile(“smb://” + username + “:” + pwd + “@” + filepath);
if (f.isFile()) {
String str = filepath.substring(0, filepath.lastIndexOf(“/”));
str = “smb://” + username + “:” + pwd + “@” + str + “/” + newFilename;
f.renameTo(new SmbFile(str));
} else if (f.isDirectory()) {
String str = filepath.substring(0, filepath.length() – 1);
str = filepath.substring(0, str.lastIndexOf(“/”));
str = “smb://” + username + “:” + pwd + “@” + str + “/” + newFilename;
f.renameTo(new SmbFile(str));
}
return true;
}
public static void mkdir(String dir, String username, String pwd) {
try {
SmbFile f = new SmbFile(“smb://” + username + “:” + pwd + “@” + dir);
if (!f.exists())
f.mkdir();
} catch (Exception ex) {
}
}
public static void mkfile(String filepath, String username, String pwd) {
try {
SmbFile f = new SmbFile(“smb://” + username + “:” + pwd + “@” + filepath);
if (!f.exists())
f.createNewFile();
} catch (Exception ex) {
}
}
public static void mkfile(String filepath, String username, String pwd, String content) {
try {
SmbFile f = new SmbFile(“smb://” + username + “:” + pwd + “@” + filepath);
if (!f.exists())
f.createNewFile();
//writeFile(filepath, content, username, pwd);
} catch (Exception ex) {
}
}
public static boolean isdir(String filepath, String username, String pwd) throws MalformedURLException, SmbException {
String dir = “smb://” + username + “:” + pwd + “@” + filepath;
SmbFile f = new SmbFile(dir);
return f.isDirectory();
}
public static void main(String[] args) {
SmbTest.mkdir(“192.168.100.188/wwwroot/test”, “hongquanli”, “87483281″);
try{
SmbFile smbFile=new SmbFile(“smb://hongquanli:87483281@192.168.100.188/wwwroot/index.htm”);
//通过 smbFile.isDirectory();isFile()可以判断smbFile是文件还是文件夹
int length=smbFile.getContentLength();//得到文件的大小
byte buffer[] = new byte[length] ;
SmbFileInputStream in = new SmbFileInputStream(smbFile) ; //建立smb文件输入流
while((in.read(buffer)) != -1){
System.out.write(buffer);
System.out.println(buffer.length);
}
in.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
相关文章推荐
- 通用大型网站页面静态化解决方案
- 通用大型网站页面静态化解决方案
- 通用大型网站页面静态化解决方案
- 通用大型网站页面静态化解决方案
- 大型网站提速关键技术(页面静态化,memcached,MySql优化)(一)
- 型网站页面静态化解决方案 freemarker
- 大型网站提速关键技术(页面静态化,memcached,MySql优化)(二)
- 大型网站提速关键技术(页面静态化,memcached,MySql优化)(三)
- 大型网站架构(页面静态化、图片服务器分离、负载均衡)
- 大型网站架构提速关键技术(页面静态化、memcached、Mysql优化)
- 大型网站性能优化 二 页面(HTML)优化的方法
- 关于大型网站技术演进的思考(十三)--网站静态化处理—CSI(5)
- 大型网站瓶颈及解决方案
- 网站架构(页面静态化,图片服务器分离,负载均衡)方案全解析
- [Apache]网站页面静态化与Apache调优(图)
- 网站页面静态化方案
- 网站架构(页面静态化,图片服务器分离,负载均衡)方案全解析
- 关于大型网站技术演进的思考(九)--网站静态化处理--总述(1)
- 搭建网站: wamp打开LocalHost以后显示IIS7页面的问题解决方案
- 转: 大型网站应对高负载和并发的解决方案