老项目中附件存储,直接上传到tomcat项目目录下。并使得tomcat变为有状态的,难以扩展所以决定搭建一个分布式文件管理系统。

背景理论

http://blog.csdn.net/poechant/article/details/6977407

搭建过程

1. 服务结构

Tracker 10.2.3.238 Centos6

Group1-Storage1 10.2.3.237 Centos6

Group1-Storage2 10.2.3.239 Centos6

2. 准备条件


master(解压后是libfastcommon)和FastDFS_v5.05.tar.gz 是每个节点必须安装的组件。

nginx和fastdfs-nginx-module_v1.16.tar和ngx_cache_purge-2.3.tar 用来支持http方式下载

3. 安装

所有节点安装依赖包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
yum install -y zlib zlib-devel pcre pcre-devel gcc gcc-c++ openssl openssl-devel libevent libevent-devel perl unzip
下载master文件
cd /usr/local/
wget https://github.com/happyfish100/libfastcommon/archive/master.zip
unzip master
cd libfastcommon-master/
./make.sh
./make.sh install
安装Fastdfs
cd /usr/local/
wget http://sourceforge.net/projects/fastdfs/files/FastDFS%20Server%20Source%20Code/FastDFS%20Server%20with%20PHP%20Extension%20Source%20Code%20V5.05/FastDFS_v5.05.tar.gz/download
tar zxf FastDFS_v5.05.tar.gz
cd FastDFS
./make.sh
./make.sh install
cd conf
cp * /etc/fdfs/
cd /etc/fdfs/
rm -rf *.sample

安装Tracker节点10.2.3.238

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
创建tarcker存储数据路径
mkdir -p /export/fastdfs/tracker
配置tarcker配置文件
cp /etc/fdfs/
vim tracker.conf
base_path=/export/fastdfs/tracker
port=22122 #设置tracker的端口号,一般采用22122这个默认端口
http.server_port=8080 #设置http端口号 注意,这个配置在fastdfs5.05这个版本中
启动tarcker服务
/usr/bin/fdfs_tarckerd /etc/fdfs/tracker.conf
查看启动状态
netstat -npl | grep fdfs
没有问题添加开机启动
vi /etc/rc.d/rc.local
添加如下命令
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

Storage安装节点10.2.3.237

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
创建storage存储数据路径
mkdir -p /export/fastdfs/storage
p配置storage配置文件
cp /etc/fdfs/
vim storage.conf
base_path=/export/fastdfs/storage
store_path0=/export/fastdfs/storage
port=23001 #设置storage的端口号,默认是23000,同一个组的storage端口号必须
store_path_count=1 #存储路径个数,需要和store_path个数匹配
tracker_server=10.2.3.238:22122
http.server_port=8080 #设置http端口号 注意,这个配置在fastdfs5.05这个版本中已经不用配置,不用管这个!
启动服务
/usr/bin/fdfs_storage /etc/fdfs/storage.conf
查看storage是否注册tracker
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
查看启动状态
netstat -npl | grep fdfs

Storage安装节点10.2.3.239 同237

优化

storage中安装nginx,主要是为了为提供http的访问服务,同时解决group中storage服务器的同步延迟问题。而tracker中安装nginx,主要是为了提供http访问的反向代理、负载均衡以及缓存服务

安装nginx

1
2
3
4
5
6
yum install -y gcc 这个前边在安装libfastcommon之前已经安装了
yum install -y gcc-c++ 这个前边在安装libfastcommon之前已经安装了
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

storage安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
cd /usr/local/
tar zxf fastdfs-nginx-module_v1.16.tar
tar nginx-1.10.2.tar
cd fastdfs-nginx-module/src
vim config #更改如下, 去掉local,并指定lib64(64系统)
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
CORE_LIBS="$CORE_LIBS -L/usr/lib64 -lfastcommon -lfdfsclient"
cp mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf
group_name=group1
base_path=/export/fastdfs/storage
store_path0=/export/fastdfs/storage
tracker_server=10.2.3.238:22122
url_have_group_name = true #是true 不是ture
cd nginx-1.10.2
./configure --prefix=/usr/local/nginx --add-module=../fastdfs-nginx-module/src
make
make install
cd /usr/local/nginx/conf
vim nginx.conf
listen 80;
location ~/group[1-3]/M00{
root /export/fastdfs/storage;
ngx_fastdfs_module;
}

当两个存储节点都安装完成停掉一个存储节点服务。


http访问

通过http访问这个节点。现在是可以访问到图片的

tracker安装nginx

tracker中nginx安装时比storage中的nginx安装时多安装一个nginx的缓存模块,同时在配置的时候有很大的不同。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
解压文件
cd /usr/local/
tar zxf nginx-1.10.2.tar
tar zxf fastdfs-nginx-module_v1.16.tar
tar zxf ngx_cache_purge-2.3.tar
cd fastdfs-nginx-module/src
vim config #更改如下, 去掉local,并指定lib64(64系统)
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
CORE_LIBS="$CORE_LIBS -L/usr/lib64 -lfastcommon -lfdfsclient"
cd nginx-1.10.2
./configure --add-module=../fastdfs-nginx-module/src --add-module=../ngx_cache_purge-2.3
make
make install
vim ../nginx/conf/nginx.conf
以下是配置文件内容
user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
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 /opt/cache/nginx/proxy_cache levels=1:2
keys_zone=http-cache:500m max_size=10g inactive=30d;
proxy_temp_path /opt/cache/nginx/proxy_cache/tmp;
upstream fdfs_group1 {
server 10.2.3.237:80 weight=1 max_fails=2 fail_timeout=30s;
server 10.2.3.239.135:80 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
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;
}
location ~/purge(/.*) {
allow 127.0.0.1;
allow 192.168.224.0/24;
deny all;
proxy_cache_purge http-cache $1$is_args$args;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

测试

1
2
3
4
5
6
7
8
9
配置客户端文件
vim /etc/fdfs/client.conf
base_path=/export/fastdfs/storage #存放路径
tracker_server=10.2.3.238:22122
http.tracker_server_port=8080 #tracker服务器的http端口号,注意,这个配置在fastdfs5.0.5中已经没有用了
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /home/zhang/test/txt


结合java使用
详见https://github.com/ErosZZH/FastdfsClient.git