现在的位置: 首页 > 综合 > 正文

FastDFS分布式文件服务器安装,及配置,测试

2013年06月14日 ⁄ 综合 ⁄ 共 9876字 ⁄ 字号 评论关闭

 一、FastDFS简介:

FastDFS是一款类Google FS的开源分布式文件系统,它用纯C语言实现,支持Linux、FreeBSD、AIX等UNIX系统。它只 能通过 专有API对文件进行存取访问,不支持POSIX接口方式,不能mount使用。准确地讲,Google FS以及FastDFS、mogileFS、 HDFS、TFS等类Google FS都不是系统级的分布式文件系统,而是应用级的分布式文件存储服务。

FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。

二、FastDFS安装

 
1
2
3
4
5
6
wget http://fastdfs.googlecode.com/files/FastDFS_v3.06.tar.gz
 
tar -zxvf FastDFS_v3.03.tar.gz
cd FastDFS
vim make.sh 将#WITH_HTTPD=1 修改成WITH_HTTPD=1 以支持 http
./make.sh && ./make.sh install

三、FastDFS配置

1、配置及启动Tracker Server

mkdir /home/fastdfs

vi /etc/fdfs/tracker.conf


base_path=/home/yuqing/fastdfs  修改为:base_path=/home/fastdfs


##include http.conf  修改为:#include http.conf


/usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf


检查tracker是否启动成功,可以查看如下文件/home/fastdfs/logs/trackerd.log

 

2、配置及启动Storage Server

mkdir /home/fastdfs/fdfs_storage

cd /etc/fdfs


vim storage.conf


或者 vim /etc/fdfs/storage.conf

base_path=/home/yuqing/fastdfs 修改为: /home/fastdfs/fdfs_storage

store_path0=/home/yuqing/fastdfs 修改为: store_path0=/home/fastdfs/fdfs_storage


tracker_server=192.168.209.121:22122 修改为 tracker_server=10.201.20.237:22122


##include http.conf  修改为:#include http.conf

 

/usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf

接下来会出现很多mkdir data path,这是系统在创建数据目录。

3.配置PHP扩展

 
1
2
3
4
5
6
7
8
9
10
11
12
13
cd php_client
 
phpize
 
./configure--with-php-config=/usr/local/php/bin/php-config
 
make && make install
 
cat fastdfs_client.ini >> /usr/local/php/etc/php.ini
 
/usr/local/apache2/bin/apachectl restart
 
cp fastdfs_test.php /home/wwwroot/

http://10.201.20.237/fastdfs_test.php

 

vim /usr/local/php/etc/php.ini 可以看到fastdfs_clicet.so已经加载到php.ini中

查看phpinfo()

 

四、测试以及使用FastDFS

1、FastDFS之配置client

vim /etc/fdfs/client.conf


base_path=/home/yuqing/fastdfs 修改为: base_path=/home/fastdfs


tracker_server=192.168.209.121:22122 修改为: tracker_server=10.201.20.237:22122


##include http.conf  修改为: #include http.conf

 

2、测试上传文件

用法:Usage: fdfs_test <config_file> upload <local_filename> [FILE | BUFF | CALLBACK]

测试:

cd /home/wwwroot/

fdfs_test /etc/fdfs/client.conf  upload  FlexPaper.zip

http://10.201.20.237:8080/group1/M00/00/00/CskU7U-c9hio4fAeACCD1c0mEbg924_big.zip

启动tracker服务:

/usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf

启动storager服务:

/usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf

监控:

/usr/local/bin/fdfs_monitor /etc/fdfs/storage.conf

 

PHP与FastDFS简单示例:

FastDFS.php

 
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
<?php
 
class FDFS{
 
    public  function __construct(){
        $this->tracker = fastdfs_tracker_get_connection();
        $this->server = fastdfs_connect_server($this->tracker['ip_addr'], $this->tracker['port']);
        $this->storage = fastdfs_tracker_query_storage_store();
 
        $this->server = fastdfs_connect_server($this->storage['ip_addr'], $this->storage['port']);
        if (!$this->server){
            error_log("errno1: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info());
            exit(1);
        }
 
        $this->storage['sock'] = $this->server['sock'];
 
    }
 
    public  function fdfs_upload($input_name){
        $file_tmp = $_FILES[$input_name]['tmp_name'];
        $real_name = $_FILES[$input_name]['name'];
        $file_name = dirname($file_tmp)."/".$real_name;
        //@copy($file_tmp, $file_name);
        @rename($file_tmp, $file_name);
 
        $file_info = fastdfs_storage_upload_by_filename($file_name, null, array(), null, $this->tracker, $this->storage);
        //print_r($file_info);die;
        if($file_info){
            $group_name = $file_info['group_name'];
            $remote_filename = $file_info['filename'];
 
            $i = fastdfs_get_file_info($group_name, $remote_filename);
            $storage_ip = $i['source_ip_addr'];
            //var_dump($file_info);
            return array($remote_filename, $group_name, $storage_ip, $real_name);
        }
        return false;
    }
 
    public function fdfs_down($group_name, $file_id){
        $file_content = fastdfs_storage_download_file_to_buff($group_name, $file_id);
        return $file_content;
    }
 
    public  function fdfs_del($group_name, $file_id){
        fastdfs_storage_delete_file($group_name, $file_id);
    }
}
 
?>

index.php

 
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
<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
define('S_ROOT',dirname(__FILE__).'/');
define('DOWNLOAD_URL','http://192.168.1.110:8080');
require_once(S_ROOT.'FastDFS.php');
$conn = mysql_connect('localhost','root','123456');
$link = mysql_select_db('fastdfs',$conn);
 
if(!empty($_FILES['upload1'])){
    $fdfs_obj = new FDFS();
    $res = $fdfs_obj->fdfs_upload("upload1");
    $sql = "INSERT INTO `fdfs` (`file_id`, `group_id`, `storage_ip`, `real_name`) VALUES ('".$res[0]."', '".$res[1]."', '".$res[2]."', '".$res[3]."')";
    echo $sql.'<hr>';
    $result = mysql_query($sql);var_dump($result);
 
}else{
 
    $sql = 'SELECT * FROM `fdfs`';
    $query = mysql_query($sql);
    while($row = mysql_fetch_assoc($query))
    {
        $data[] = $row;
    }
    if(!empty($data))
    {
        foreach($data as $v)
        {
             echo '<a href="'.DOWNLOAD_URL.'/'.$v['group_id'].'/'.$v['file_id'].'">'.$v['real_name'].'</a><br/>';
 
        }
    }
 
}
 
?>
 
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="myText" /><br />
<input type="file" name="upload1" /><br />
<input type="submit" />
</form>
</body>
</html>

 

sql:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
--
-- 数据库: `fastdfs`
--
 
-- --------------------------------------------------------
 
--
-- 表的结构 `fdfs`
--
 
CREATE TABLE IF NOT EXISTS `fdfs` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `file_id` varchar(200) NOT NULL DEFAULT '' COMMENT '文件ID',
  `group_id` varchar(50) NOT NULL DEFAULT '' COMMENT '组名',
  `real_name` varchar(200) NOT NULL COMMENT '真名',
  `storage_ip` varchar(16) NOT NULL COMMENT 'ip',
  PRIMARY KEY (`id`),
  KEY `i_file_id` (`file_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

也可以重新封装下fastdfs_client 方法

wget http://fastdfs.googlecode.com/files/fastdfs_client_php_v1.6.tar.gz

tar zxvf fastdfs_client_php_v1.6.tar.gz

cd fastdfs_client_php_v1.6.tar.gz

vim FastdfsClient.php

 

<?php

require_once 'fdfs_common.php';
require_once 'fdfs_tracker_client.php';
require_once 'fdfs_storage_client1.php';

class FastdfsClient {
private $_tracker_server = null;

public function __construct() {
  $this->_getTracker();
}

public function __destruct() {
  fdfs_quit($this->_tracker_server);
  tracker_close_all_connections();
}

public function getInstance() {
  static $instance;
  if (!isset($instance)) {
    $class = __CLASS__;
    $instance = new $class();
  }
  return $instance;
}

/**
* Upload file by filename to Fastdfs
*
* @param $local_filename local file name to upload
* @param $meta_list metadata assoc array (key value pair array)
* @param $group_name you can specify the group to upload file to
*
* @return fileid compose of $group_name and $remote_name, false for error
*/
public function saveFile($local_filename, $file_ext_name = '', $meta_list = array(), $group_name = '') {
  $this->_getTracker();
  $storage_server = null;
  $file_id = null;

  $result = storage_upload_by_filename1($this->_tracker_server, $storage_server,
  $local_filename, $meta_list, $file_id, $group_name, $file_ext_name);

  if ($result == 0) {
    $file_id = str_replace("M00/",IMAGESHOWURL,$file_id);
    return $file_id;
  } else {
    throw new Exception('fdfs_upload_by_filename_failed', $result);
    return false;
  }
}

/**
* Upload file by buff to the Fastdfs
*
* @param $file_buff the file content to upload
* @param $file_ext_name the file ext name (not including dot)
* @param $meta_list metadata assoc array (key value pair array)
* @param $group_name you can specify the group to upload file to
*
* @return fileid composed of $group_name and $remote_name, false for error
*/
public function saveFilebuff($file_buff, $file_ext_name, $meta_list = array(), $group_name = '') {
  $storage_server = null;
  $file_id = null;

  $file_size = strlen($file_buff);
  $result = storage_upload_by_filebuff1($this->_tracker_server, $storage_server,
  $file_buff, $file_size, $file_ext_name, $meta_list, $file_id, $group_name);
  if ($result == 0) {
    return $file_id;
  } else {
    throw new Exception('fdfs_upload_by_filebuff_failed', $result);
    return false;
  }
}

/**
* Copy file from Fastdfs to local
*
* @param $file_id
* @param $local_filename
* @return bool
*/
public function copyFile($file_id, $local_filename) {
  $storage_server = null;
  $file_size = null;

  $result = storage_download_file_to_file1($this->_tracker_server, $storage_server,
  $file_id, $local_filename, $file_size);

  if ($result == 0) {
    return true;
  } else {
    throw new Exception('fdfs_copy_file_failed', $result);
    return false;
  }
}

/**
* Get file content from Fastdfs
*
* @param $file_id
* @return file content
*/
public function getFilebuff($file_id) {
  $storage_server = null;
  $file_size = null;
  $file_buff = null;

  $result = storage_download_file_to_buff1($this->_tracker_server, $storage_server,$file_id, $file_buff, $file_size);
  if ($result == 0) {
    return $file_buff;
  } else {
    throw new Exception('fdfs_load_file_to_buff_failed', $result);
    return false;
  }
}

/**
* Delete file by fileid from the Fastdfs
*
* @param $file_id
* @return bool
*/
public function deleteFile($file_id) {
  if (empty($file_id)) return false;
  if (is_array($file_id)) {
    foreach ($file_id as $fid) {
      $this->deleteFile($fid);
    }
    return true;
  }

  $storage_server = null;
  $result = storage_delete_file1($this->_tracker_server, $storage_server, $file_id);

  if ($result == 0) {
    return true;
  } else {
    return false;
  }
}

/**
* Get a tracker server
*/
private function _getTracker() {
  $this->_tracker_server = null;
  $this->_tracker_server = tracker_get_connection();
  if ($this->_tracker_server == false) {
    throw new Exception('fdfs_get_tracker_server_failed');
    return false;
  }
}
} ?>

 

 

FastDFS架构介绍:

FastDFS是一款为互联网应用量身定做的类Google FS的开源应用级的分布式文件系统,它用纯C语言实现,支持Linux、FreeBSD、AIX等UNIX系统,其架构和设计理念有其独到之处,主要体现在轻量级、分组方式和对等结构三个方面:

轻量级:

  1. FastDFS只有两个角色:Tracker server和Storage server。Tracker
    server作为中心结点,其主要作用是负载均衡和调度。Tracker server在内存中记录分组和Storage
    server的状态等信息,不记录文件索引信息,占用的内存量很少。另外,客户端(应用)和Storage server访问Tracker
    server时,Tracker server扫描内存中的分组和Storage server信息,然后给出应答。由此可以看出Tracker
    server非常轻量化,不会成为系统瓶颈。
  2. FastDFS不对文件进行分块存储,客户端上传文件时,文件ID不是由客户端指定,而是由Storage
    server生成后返回给客户端的。文件ID中包含了组名、文件相对路径和文件名,Storage
    server可以根据文件ID直接定位到文件。因此FastDFS集群中根本不需要存储文件索引信息,这是FastDFS比较轻量级的一个例证。
  3. FastDFS轻量级的另外一个体现是代

抱歉!评论已关闭.