CentOS7搭建FastDFS V5.11分布式文件系统-第三篇

1.测试

前面两篇博文已对FastDFS的安装和配置,做了比较详细的讲解。FastDFS的基础模块都搭好了,现在开始测试下载。

1.1 配置客户端

同样的,需要修改客户端的配置文件:

/etc/fdfs/client.conf
base_path=/opt/fastdfs_tracker #tracker服务器文件路径
tracker_server=172.20.132.57:22122 #tracker服务器IP地址和端口号
http.tracker_server_port=8080 # tracker 服务器的 http端口号,必须和tracker的设置对应起来

修改完成的文件为:

connect_timeout=30
network_timeout=60
base_path=/opt/fastdfs_tracker
tracker_server=172.20.132.57:22122
log_level=info
use_connection_pool = false
connection_pool_max_idle_time = 3600
load_fdfs_parameters_from_tracker=false
use_storage_id = false
storage_ids_filename = storage_ids.conf
http.tracker_server_port=8080
View Code

1.2 模拟上传

确定图片位置后,我们输入上传图片命令:

/usr/bin/fdfs_upload_file  /etc/fdfs/client.conf  /root/1.jpg  #这后面放的是图片的位置

成功后会返回图片的路径:

[root@sybmfw12-lnsy fastdfs-5.11]# /usr/bin/fdfs_upload_file  /etc/fdfs/client.conf  /root/2.png
group1/M00/00/00/rBSEOVoC2QCAJVdoAAFeVJMpopw987.png

组名:group1 
磁盘:M00 
目录:00/00 
文件名称:rBSEOVoC2QCAJVdoAAFeVJMpopw987.png

我们上传的图片会被上传到我们创建的storage_data目录下,让我们去看看:

[root@sybmfw12-lnsy ~]# cd /opt/fastdfs_storage_data/data/
[root@sybmfw12-lnsy data]# ls
00  04  08  0C  10  14  18  1C  20  24  28  2C  30  34  38  3C  40  44  48  4C  50  54  58  5C  60  64  68  6C  70  74  78  7C  80  84  88  8C  90  94  98  9C  A0  A4  A8  AC  B0  B4  B8  BC  C0  C4  C8  CC  D0  D4  D8  DC  E0  E4  E8  EC  F0  F4  F8  FC
01  05  09  0D  11  15  19  1D  21  25  29  2D  31  35  39  3D  41  45  49  4D  51  55  59  5D  61  65  69  6D  71  75  79  7D  81  85  89  8D  91  95  99  9D  A1  A5  A9  AD  B1  B5  B9  BD  C1  C5  C9  CD  D1  D5  D9  DD  E1  E5  E9  ED  F1  F5  F9  FD
02  06  0A  0E  12  16  1A  1E  22  26  2A  2E  32  36  3A  3E  42  46  4A  4E  52  56  5A  5E  62  66  6A  6E  72  76  7A  7E  82  86  8A  8E  92  96  9A  9E  A2  A6  AA  AE  B2  B6  BA  BE  C2  C6  CA  CE  D2  D6  DA  DE  E2  E6  EA  EE  F2  F6  FA  FE
03  07  0B  0F  13  17  1B  1F  23  27  2B  2F  33  37  3B  3F  43  47  4B  4F  53  57  5B  5F  63  67  6B  6F  73  77  7B  7F  83  87  8B  8F  93  97  9B  9F  A3  A7  AB  AF  B3  B7  BB  BF  C3  C7  CB  CF  D3  D7  DB  DF  E3  E7  EB  EF  F3  F7  FB  FF

[root@sybmfw12-lnsy data]# ls 00/
00 04 08 0C 10 14 18 1C 20 24 28 2C 30 34 38 3C 40 44 48 4C 50 54 58 5C 60 64 68 6C 70 74 78 7C 80 84 88 8C 90 94 98 9C A0 A4 A8 AC B0 B4 B8 BC C0 C4 C8 CC D0 D4 D8 DC E0 E4 E8 EC F0 F4 F8 FC
01 05 09 0D 11 15 19 1D 21 25 29 2D 31 35 39 3D 41 45 49 4D 51 55 59 5D 61 65 69 6D 71 75 79 7D 81 85 89 8D 91 95 99 9D A1 A5 A9 AD B1 B5 B9 BD C1 C5 C9 CD D1 D5 D9 DD E1 E5 E9 ED F1 F5 F9 FD
02 06 0A 0E 12 16 1A 1E 22 26 2A 2E 32 36 3A 3E 42 46 4A 4E 52 56 5A 5E 62 66 6A 6E 72 76 7A 7E 82 86 8A 8E 92 96 9A 9E A2 A6 AA AE B2 B6 BA BE C2 C6 CA CE D2 D6 DA DE E2 E6 EA EE F2 F6 FA FE
03 07 0B 0F 13 17 1B 1F 23 27 2B 2F 33 37 3B 3F 43 47 4B 4F 53 57 5B 5F 63 67 6B 6F 73 77 7B 7F 83 87 8B 8F 93 97 9B 9F A3 A7 AB AF B3 B7 BB BF C3 C7 CB CF D3 D7 DB DF E3 E7 EB EF F3 F7 FB FF
[root@sybmfw12-lnsy data]# ls 00/00/

rBSEOVoC2QCAJVdoAAFeVJMpopw987.png

果然通过刚刚返回的路径,我们成功找到了图片。

我们仔细看一下,实际文件存储路径下有创建好的多级目录。data下有256个1级目录,每级目录下又有256个2级子目录,总共65536个文件,新写的文件会以hash的方式被路由到其中某个子目录下,然后将文件数据直接作为一个本地文件存储到该目录中。

如果要访问刚上传的图片,我们得需要结合nginx来实现

1.4 HTTP请求不能访问文件的原因

我们在使用FastDFS部署一个分布式文件系统的时候,通过FastDFS的客户端API来进行文件的上传、下载、删除等操作。同时通过FastDFS的HTTP服务器来提供HTTP服务。但是FastDFS的HTTP服务较为简单,无法提供负载均衡等高性能的服务,所以FastDFS的开发者——淘宝的架构师余庆同学,为我们提供了Nginx上使用的FastDFS模块(也可以叫FastDFS的Nginx模块)。 
FastDFS通过Tracker服务器,将文件放在Storage服务器存储,但是同组之间的服务器需要复制文件,有延迟的问题.假设Tracker服务器将文件上传到了172.20.132.57,文件ID已经返回客户端,这时,后台会将这个文件复制到172.20.132.57,如果复制没有完成,客户端就用这个ID在172.20.132.57取文件,肯定会出现错误。这个fastdfs-nginx-module可以重定向连接到源服务器取文件,避免客户端由于复制延迟的问题,出现错误。 
正是这样,FastDFS需要结合nginx,所以取消原来对HTTP的直接支持。

2. FastDFS的nginx模块安装

在安装nginx之前要安装nginx所需的依赖lib:

yum -y install pcre pcre-devel  
yum -y install zlib zlib-devel  
yum -y install openssl openssl-devel

2.2 安装nginx并添加fastdfs-nginx-module

解压nginx,和fastdfs-nginx-module:

tar -zxvf nginx-1.10.3.tar.gz
unzip fastdfs-nginx-module-master.zip

解压后进入nginx目录编译安装nginx,并添加fastdfs-nginx-module:

./configure --prefix=/usr/local/nginx --add-module=/root/fastdfs-nginx-module-master/src    #解压后fastdfs-nginx-module所在的位置

如果配置不报错的话,就开始编译:

make
make install

如果报错的话,很可能是版本的原因,在我的第二篇博文中提供了我测试成功不报错的版本下载。 
nginx的默认目录是/usr/local/nginx,安装成功后查看:

[root@sybmfw12-lnsy ~]# ll /usr/local/nginx/
drwx------. 2 nobody root 4096 11月  8 13:10 client_body_temp
drwxr-xr-x. 2 root   root 4096 11月  8 15:22 conf
drwx------. 2 nobody root 4096 11月  8 13:10 fastcgi_temp
drwxr-xr-x. 2 root   root 4096 11月  8 13:00 html
drwxr-xr-x. 2 root   root 4096 11月  8 13:10 logs
drwx------. 2 nobody root 4096 11月  8 13:10 proxy_temp
drwxr-xr-x. 2 root   root 4096 11月  8 13:00 sbin
drwx------. 2 nobody root 4096 11月  8 13:10 scgi_temp
drwx------. 2 nobody root 4096 11月  8 13:10 uwsgi_temp

2.3 配置storage nginx

修改nginx.conf:

修改监听端口 listen 9999, 新增location

server {
        listen       9991;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location ~/group1/M00 {
            root /opt/fastdfs_storage/data;
            ngx_fastdfs_module;
        }

        location = /50x.html {
            root   html;
        }
}
View Code

 然后进入FastDFS安装时的解压过的目录,将http.conf和mime.types拷贝到/etc/fdfs目录下:

[root@sybmfw12-lnsy conf]# cp http.conf  mime.types  /etc/fdfs/
[root@sybmfw12-lnsy conf]# cp mime.types  /etc/fdfs/

另外还需要把fastdfs-nginx-module安装目录中src目录下的mod_fastdfs.conf也拷贝到/etc/fdfs目录下:

[root@sybmfw12-lnsy src]# cp mod_fastdfs.conf  /etc/fdfs/

对刚刚拷贝的mod_fastdfs.conf文件进行修改:

/etc/fdfs/mod_fastdfs.conf
base_path=/opt/fastdfs_storage  #保存日志目录
 tracker_server=172.20.132.57:22122 #tracker服务器的IP地址以及端口号
 storage_server_port=23000 #storage服务器的端口号
 url_have_group_name = true #文件 url 中是否有 group 名
 store_path0=/opt/fastdfs_storage_data   #存储路径
 group_count = 1 #设置组的个数

在文件的最后,设置group

[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/opt/fastdfs_storage_data

创建M00至storage存储目录的符号连接:

ln  -s  /opt/fastdfs_storage_data/data/ /opt/fastdfs_storage_data/data/M00

启动nginx:

/usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=8142
[root@sybmfw12-lnsy src]# curl localhost:9991
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

恭喜你,storage的nginx已配置成功。接下来,我们还要继续配置tracker的nginx。

2.4 配置tracker nginx

在nginx.cfg里在添加一个虚拟主机

   upstream fdfs_group1 {
        server 127.0.0.1:9991;
    }
   server {
        listen       80;
        server_name  localhost;
       location /group1/M00 {
            proxy_pass http://fdfs_group1;
        }
      error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

重启nginx

/usr/local/nginx/sbin/nginx -s reload
ngx_http_fastdfs_set pid=8141
[root@sybmfw12-lnsy ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
View Code

2.6 HTTP测试

可以成功访问。

现在我们再去访问一下,原来我们上传过的文件: 

172.20.132.57/group1/M00/00/00/rBSEOVoCsSWAZG_WAAFeVJMpopw658.png

 

这个时候已经能成功访问。感谢你能坚持到现在,FastDFS已部署完成。 

 贴上完整nginx配置

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 {
        listen       9991;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location ~/group1/M00 {
            root /opt/data/fastdfs_storage/data;
            ngx_fastdfs_module;
        }

        location = /50x.html {
            root   html;
        }
}
upstream fdfs_group1 {
       server 127.0.0.1:9991;
}

    server {
        listen       80;
        server_name  localhost;
       location /group1/M00 {
            proxy_pass http://fdfs_group1;
        }
      error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}
View Code

 

posted @ 2017-11-08 21:09  程序员同行者  阅读(2798)  评论(2编辑  收藏  举报