阅读更多
为了比对MFS, 在系统中装了FastDFS,以下是部署笔记。
1. 准备机器
Tracker: 192.168.3.127
Tracker: 192.168.3.131
Storage: 192.168.3.142 group1
192.168.3.143 group1
Storage:192.168.3.140 group2
192.168.3.141 group2
安装tracker
1. 下载 FastDFS_v5.03.tar.gz http://heanet.dl.sourceforge.net/project/fastdfs/FastDFS%20Server%20Source%20Code/FastDFS%20Server%20with%20PHP%20Extension%20Source%20Code%20V5.03/FastDFS_v5.03.tar.gz
注:从5.0.4之后版本,缺少文件,需要安装libfastcommon 依赖包。(可以检查FastDFS\common\logger.h是否存在判断是否缺少文件)
tar zxvf FastDFS_v5.03.tar.gz
cd FastDFS
./make.sh
./make.sh install
2. 修改配置
vim /etc/fdfs/tracker.conf
修改 base_path=/home/zjport/fastdfs
mkdir /home/zjport/fastdfs
3. 启动tracker
/usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
查看:netstat -unltp| grep fdfs
查看日志: cat /home/zjport/fastdfs/logs/trackerd.log
4. 设置开启启动
vim /etc/rc.d/rc.local
添加 /usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
安装storage(3.140,3.141,3.142,3.143)
1. 与安装tracker一致
2. vim /etc/fdfs/storage.conf
修改 base_path=/home/zjport/storage
store_path0=/home/zjport/storage
tracker_server=192.168.3.127:22122
tracker_server=192.168.3.131:22122
group_name=group2(142,143 为group1, 140,141 为group2)
mkdir /home/zjport/storage
3. 启动storage
/usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf start
查看:netstat -unltp| grep fdfs
4. 查看是否连上服务器: /usr/local/bin/fdfs_monitor /etc/fdfs/storage.conf
看到 192.168.3.142 ACTIVE 即可
5. 开机自动启动
vim /etc/rc.d/rc.local
添加: /usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf start
6. 将3.140.3.141.3.143 都装上
Storage安装nginx(3.140,141,142,143)
1. 拷贝lib包并解压
tar zxf nginx-1.4.7.tar.gz
tar zxf fastdfs-nginx-module_v1.16.tar.gz
tar zxf pcre-8.39.tar.gz
tar zxf zlib-1.2.8.tar.gz
2. 安装nginx
cd nginx-1.4.7
./configure --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs/fastdfs-nginx-module/src --with-pcre=/usr/local/fastdfs/pcre-8.39/ --with-zlib=/usr/local/fastdfs/zlib-1.2.8
make
make install
3. 安装模块
cp /usr/local/fastdfs/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
4. 确认nginx安装目录
cd /usr/local/nginx
5. 修改nginx配置,添加storage信息
vim /usr/local/nginx/conf/nginx.conf
将 server 段中的 listen 端口号改为 8080 :
listen 8080;
在 server 段中添加:
location ~/group[1-2]/M00 {
root /home/zjport/storage/data;
ngx_fastdfs_module;
}
vim /etc/fdfs/mod_fastdfs.conf
修改 base_path=/home/zjport/storage
store_path0=/home/zjport/storage
tracker_server=192.168.3.127:22122
tracker_server=192.168.3.131:22122
group_name=group2(142,143 为group1, 140,141 为group2)
url_have_group_name = true
http.need_find_content_type=true
group_count = 2
添加
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/home/zjport/storage
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/home/zjport/storage
建立存储链接
ln -s /home/zjport/storage/data /home/zjport/storage/data/M00
chmod -R 777 /home/zjport/
6. 运行nginx
/usr/local/nginx/sbin/nginx
查看启动日志: cat /usr/local/nginx/logs/error.log
7. 设置开机启动
vim /etc/rc.d/rc.local
添加 /usr/local/nginx/sbin/nginx
Tracker安装nginx(3.127)
1. 拷贝lib包并解压
tar zxf nginx-1.4.7.tar.gz
tar zxf ngx_cache_purge-2.3.tar.gz
tar zxf pcre-8.39.tar.gz
tar zxf zlib-1.2.8.tar.gz
2. 安装
cd nginx-1.4.7
./configure --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs/ngx_cache_purge-2.3 --with-pcre=/usr/local/fastdfs/pcre-8.39/ --with-zlib=/usr/local/fastdfs/zlib-1.2.8
make
make install
3. 修改配置
vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 2; #根据 CPU 核心数而定
events {
worker_connections 65535; # 最大链接数
use epoll; # 新版本的 Linux 可使用 epoll 加快处理性能
}
http {
# 设置缓存参数
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
# 设置缓存存储路径、存储方式、分配内存大小、磁盘最大空间、缓存期限
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2
keys_zone=http-cache:500m max_size=10g inactive=30d;
proxy_temp_path /var/cache/nginx/proxy_cache/tmp;
# 设置 group1 的服务器
upstream fdfs_group1 {
server 192.168.3.142:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.3.143:8080 weight=1 max_fails=2 fail_timeout=30s;
}
# 设置 group2 的服务器
upstream fdfs_group2 {
server 192.168.3.140:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.3.141:8080 weight=1 max_fails=2 fail_timeout=30s;
}
server {
# 设置服务器端口
listen 8080;
# 设置 group1 的负载均衡参数
location /group1/M00 {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache http-cache;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
proxy_pass http://fdfs_group1;
expires 30d;
}
# 设置 group2 的负载均衡参数
location /group2/M00 {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache http-cache;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
proxy_pass http://fdfs_group2;
expires 30d;
}
# 设置清除缓存的访问权限
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 192.168.3.0/24;
proxy_cache_purge http-cache $1$is_args$args;
}
}
}
4. 创建存储目录
mkdir /home/zjport/storage
mkdir -p /var/cache/nginx/proxy_cache
chmod -R 777 /var/cache/
chmod -R 777 /home/zjport/
5. 修改配置
vim /etc/fdfs/client.conf
base_path=/home/zjport/storage # 日志存放路径
tracker_server=192.168.3.127:22122 #tracker 服务器 IP 地址和端口号
tracker_server=192.168.3.131:22122
http.tracker_server_port=8080 #tracker 服务器的 http 端口号
6. 运行nginx
/usr/local/nginx/sbin/nginx
7. 上传文件测试
/usr/local/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/nginx/s.png
返回: group1/M00/00/00/wKgDj1eFq8SAVScTAACE1CpbOR0868.png
8. 使用浏览器访问
http://192.168.3.127:8080/group1/M00/00/00/wKgDj1eFq8SAVScTAACE1CpbOR0868.png
9. 设置开机启动
vim /etc/rc.d/rc.local
添加 /usr/local/nginx/sbin/nginx
10. 查看缓存情况
ll /var/cache/nginx/proxy_cache/ -R
清除缓存只需在url前加purge就行(nginx中有配置).
http://192.168.3.127:8080/purge/group1/M00/00/00/wKgDj1eFq8SAVScTAACE1CpbOR0868.png
监控
/usr/local/bin/fdfs_monitor /etc/fdfs/client.conf
JAVA 客户端调用
1. 下载 fastdfs_client_java._v1.25.tar.gz
http://ufpr.dl.sourceforge.net/project/fastdfs/Java%20Client%20API%20Source%20Code/Java%20Client%20API%20Source%20Code%20V1.25/fastdfs_client_java._v1.25.tar.gz
2. 解压并导入eclipse
3. 运行 Test1.java 测试, 注意修改TrackerGroup的地址 和 fdfs_client.conf 配置