FastDfs初学

FastDfs

  • 安装

    1. 首先要安装gcc,libevent、libent-devel

    2. 然后将下载好的fdfs的包传到服务器(fastdfs-5.11.zip libfastcommon-master.zip)

    3. 然后解压这两个压缩包,注意,这是需要编译的,解压后进入解压目录,首先执行 ./make.sh 然后继续执行 ./make.sh install

    4. 此时cd 到etc/fdfs下面会有四个文件,

    5. [root@node02 bin]# cd /etc/fdfs/
      [root@node02 fdfs]# ll
      总用量 24
      -rw-r--r--. 1 root root 1461 8月  12 22:02 client.conf.sample # 客户端配置  
      -rw-r--r--. 1 root root 7927 8月  12 22:02 storage.conf.sample  #存储配置
      -rw-r--r--. 1 root root  105 8月  12 22:02 storage_ids.conf.sample #存储id配置
      -rw-r--r--. 1 root root 7389 8月  12 22:02 tracker.conf.sample #跟踪服务配置
      
      
    6. 回到解压目录,将conf下面的两个文件拷贝到etc/fdfs/下面

    7. [root@node02 fastdfs-5.11]# cd conf/
      [root@node02 conf]# ll
      总用量 84
      -rw-r--r--. 1 root root 23981 6月   3 2017 anti-steal.jpg
      -rw-r--r--. 1 root root  1461 6月   3 2017 client.conf
      -rw-r--r--. 1 root root   955 6月   3 2017 http.conf
      -rw-r--r--. 1 root root 31172 6月   3 2017 mime.types
      -rw-r--r--. 1 root root  7927 6月   3 2017 storage.conf
      -rw-r--r--. 1 root root   105 6月   3 2017 storage_ids.conf
      -rw-r--r--. 1 root root  7389 6月   3 2017 tracker.conf
      [root@node02 conf]# cp http.conf mime.types  /etc/fdfs/
      
    8. 复制tracker.conf.sample 为tracker.conf

    9. 注意:以下需要自定义的文件路径都要自己手动创建,不然启动会报错

    10. 修改tracker.conf中 base_path=/opt/fadfs/tracker 的路径为自定义路径

    11. # is this config file disabled
      # false for enabled
      # true for disabled
      disabled=false
      
      # bind an address of this host
      # empty for bind all addresses of this host
      bind_addr=
      
      # the tracker server port
      port=22122
      
      # connect timeout in seconds
      # default value is 30s
      connect_timeout=30
      
      # network timeout in seconds
      # default value is 30s
      network_timeout=60
      
      # the base path to store data and log files
      base_path=/opt/fadfs/tracker  #修改这个路径
      ........
      
    12. 复制storage.conf.sample 为storage.conf

    13. 修改storage.conf

      1. base_path=/opt/fastdfs/storage:修改为自定义路径
      2. store_path0=/opt/fastdfs/storage/files 也是自定义,建议和我一样
      3. tracker_server=192.168.159.13:22122 修改为自己的服务器ip
    14. # is this config file disabled
      # false for enabled
      # true for disabled
      disabled=false
      
      # the name of the group this storage server belongs to
      #
      # comment or remove this item for fetching from tracker server,
      # in this case, use_storage_id must set to true in tracker.conf,
      # and storage_ids.conf must be configed correctly.
      group_name=group1
      
      # bind an address of this host
      # empty for bind all addresses of this host
      bind_addr=
      
      # if bind an address of this host when connect to other servers 
      # (this storage server as a client)
      # true for binding the address configed by above parameter: "bind_addr"
      # false for binding any address of this host
      client_bind=true
      
      # the storage server port
      port=23000
      
      # connect timeout in seconds
      # default value is 30s
      connect_timeout=30
      
      # network timeout in seconds
      # default value is 30s
      network_timeout=60
      
      # heart beat interval in seconds
      heart_beat_interval=30
      
      # disk usage report interval in seconds
      stat_report_interval=60
      
      # the base path to store data and log files
      base_path=/opt/fastdfs/storage  #自定义路径
      
      # max concurrent connections the server supported
      # default value is 256
      # more max_connections means more memory will be used
      max_connections=256
      
      # the buff size to recv / send data
      # this parameter must more than 8KB
      # default value is 64KB
      # since V2.00
      buff_size = 256KB
      
      # accept thread count
      # default value is 1
      # since V4.07
      accept_threads=1
      
      # work thread count, should <= max_connections
      # work thread deal network io
      # default value is 4
      # since V2.00
      work_threads=4
      
      # if disk read / write separated
      ##  false for mixed read and write
      ##  true for separated read and write
      # default value is true
      # since V2.00
      disk_rw_separated = true
      
      # disk reader thread count per store base path
      # for mixed read / write, this parameter can be 0
      # default value is 1
      # since V2.00
      disk_reader_threads = 1
      
      # disk writer thread count per store base path
      # for mixed read / write, this parameter can be 0
      # default value is 1
      # since V2.00
      disk_writer_threads = 1
      
      # when no entry to sync, try read binlog again after X milliseconds
      # must > 0, default value is 200ms
      sync_wait_msec=50
      
      # after sync a file, usleep milliseconds
      # 0 for sync successively (never call usleep)
      sync_interval=0
      
      # storage sync start time of a day, time format: Hour:Minute
      # Hour from 0 to 23, Minute from 0 to 59
      sync_start_time=00:00
      
      # storage sync end time of a day, time format: Hour:Minute
      # Hour from 0 to 23, Minute from 0 to 59
      sync_end_time=23:59
      
      # write to the mark file after sync N files
      # default value is 500
      write_mark_file_freq=500
      
      # path(disk or mount point) count, default value is 1
      store_path_count=1
      
      # store_path#, based 0, if store_path0 not exists, it's value is base_path
      # the paths must be exist
      store_path0=/opt/fastdfs/storage/files
      #store_path1=/home/yuqing/fastdfs2
      
      # subdir_count  * subdir_count directories will be auto created under each 
      # store_path (disk), value can be 1 to 256, default value is 256
      subdir_count_per_path=256
      
      # tracker_server can ocur more than once, and tracker_server format is
      #  "host:port", host can be hostname or ip address
      tracker_server=192.168.159.13:22122
      ........
      
    15. 启动trackerServer 和StorageServer

    16. fdfs_trackerd /etc/fdfs/tracker.conf start
      fdfs_storaged /etc/fdfs/storage.conf start
      
    17. 复制client.conf.simp* 为client.conf

    18. 修改client.conf

      1. base_path=/opt/fastdfs/client 修改自定义路径

      2. tracker_server=192.168.159.13:22122 修改为自己的服务器地址

      3. # connect timeout in seconds
        # default value is 30s
        connect_timeout=30
        
        # network timeout in seconds
        # default value is 30s
        network_timeout=60
        
        # the base path to store log files
        base_path=/opt/fastdfs/client  #修改的自定义路径
        
        # tracker_server can ocur more than once, and tracker_server format is
        #  "host:port", host can be hostname or ip address
        tracker_server=192.168.159.13:22122 #修改为自己的服务器地址
        ......
        
        
    19. 注意:以上自定义的文件夹都要自己手动创建,不然启动会报错

    20. 测试

      1. 回到家目录 cd ~

      2. 创建一个aa.txt并在里面写一段话

      3. 上传文件

        1. fdfs_test /etc/fdfs/client.conf upload
          
        2. 结果

        3. This is FastDFS client test program v5.11
          Copyright (C) 2008, Happy Fish / YuQing
          FastDFS may be copied only under the terms of the GNU General
          Public License V3, which may be found in the FastDFS source kit.
          Please visit the FastDFS Home Page http://www.csource.org/ 
          for more detail.
          
          [2020-08-12 22:42:15] DEBUG - base_path=/opt/fastdfs/client, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0
          
          tracker_query_storage_store_list_without_group: 
          	server 1. group_name=, ip_addr=192.168.159.13, port=23000
          
          group_name=group1, ip_addr=192.168.159.13, port=23000
          storage_upload_by_filename
          group_name=group1, remote_filename=M00/00/00/wKifDV8z_8eAFL_ZAAAAG0I0Pz4616.txt
          #代表了上传的文件在什么位置
          source ip address: 192.168.159.13
          file timestamp=2020-08-12 22:42:15
          file size=27
          file crc32=1110720318
          example file url: http://192.168.159.13/group1/M00/00/00/wKifDV8z_8eAFL_ZAAAAG0I0Pz4616.txt
          storage_upload_slave_by_filename
          group_name=group1, remote_filename=M00/00/00/wKifDV8z_8eAFL_ZAAAAG0I0Pz4616_big.txt
          source ip address: 192.168.159.13
          file timestamp=2020-08-12 22:42:15
          file size=27
          file crc32=1110720318
          
  • FastDFS的网络访问(必须整合nginx)

    1. 上传软件包: fastdfs-nginx-module-master

    2. 解压包后把包里面src中的 mod_fastdfs.conf复制到etc/fdfs/下面

    3. 修改mod_fasfs.conf

      1. 修改 base_path=/opt/fastdfs/nginx_mod 自定义路径 需要自己手动创建目录

      2. tracker_server=192.168.159.13:22122 修改服务器地址

      3. url_have_group_name = true 修改为true

      4. store_path0=/opt/fastdfs/storage/files 修改存盘路径 此目录上面已经创建过了

      5. # connect timeout in seconds
        # default value is 30s
        connect_timeout=2
        
        # network recv and send timeout in seconds
        # default value is 30s
        network_timeout=30
        
        # the base path to store log files
        base_path=/opt/fastdfs/nginx_mod
        
        # if load FastDFS parameters from tracker server
        # since V1.12
        # default value is false
        load_fdfs_parameters_from_tracker=true
        
        # storage sync file max delay seconds
        # same as tracker.conf
        # valid only when load_fdfs_parameters_from_tracker is false
        # since V1.12
        # default value is 86400 seconds (one day)
        storage_sync_file_max_delay = 86400
        
        # if use storage ID instead of IP address
        # same as tracker.conf
        # valid only when load_fdfs_parameters_from_tracker is false
        # default value is false
        # since V1.13
        use_storage_id = false
        
        # specify storage ids filename, can use relative or absolute path
        # same as tracker.conf
        # valid only when load_fdfs_parameters_from_tracker is false
        # since V1.13
        storage_ids_filename = storage_ids.conf
        
        # FastDFS tracker_server can ocur more than once, and tracker_server format is
        #  "host:port", host can be hostname or ip address
        # valid only when load_fdfs_parameters_from_tracker is true
        tracker_server=192.168.159.13:22122  #修改服务器地址
        
        # the port of the local storage server
        # the default value is 23000
        storage_server_port=23000
        
        # the group name of the local storage server
        group_name=group1
        
        # if the url / uri including the group name
        # set to false when uri like /M00/00/00/xxx
        # set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx
        # default value is false
        url_have_group_name = true  #修改这里
        
        # path(disk or mount point) count, default value is 1
        # must same as storage.conf
        store_path_count=1
        
        # store_path#, based 0, if store_path0 not exists, it's value is base_path
        # the paths must be exist
        # must same as storage.conf
        store_path0=/opt/fastdfs/storage/files  #存盘路径
        ...........
        
      6. 为nginx添加module(已经安装好nginx)https://blog.csdn.net/wangjia55/article/details/91961569

      7. 修改nginx.conf文件,添加如下配置

        1. location ~ /group[1-9]/M0[0-9]{  #正则表达式,用来指定文件存储位置
          		nginx_fastdfs_module;
          	}
          
      8. 启动nginx

      9. 打开浏览器测试 http://192.168.159.13/group1/M00/00/00/wKifDV8z_8eAFL_ZAAAAG0I0Pz4616.txt

  • java上传文件

    1. 新建项目,改pom.xml

    2. 
          <dependencies>
              <!--导入FastDfs的依赖包-->
              <dependency>
                  <groupId>cn.bestwu</groupId>
                  <artifactId>fastdfs-client-java</artifactId>
                  <version>1.27</version>
              </dependency>
          </dependencies>
      
    3. 在resource文件夹里新建fastdfs.conf文件

    4. tracker_server=192.168.159.13:22122  #服务器地址
      
    5. 新建文件上传类

    6. package com.xiefei.fastdfs;
      
      import org.csource.common.MyException;
      import org.csource.fastdfs.*;
      
      import java.io.IOException;
      
      public class FastDfsUtil {
          public static void main(String[] args) {
              upload();
          }
      
          /*文件上传*/
          private static void upload() {
              //读取配置文件,用于将所有trackerserver的地址读取到内存中
              try {
                  ClientGlobal.init("E:\\java项目\\fastdfsdemo\\src\\main\\resources\\fastdfs.conf");//相对路径的话会报错只能绝对路径
                  TrackerClient tc=new TrackerClient();
                  TrackerServer ts=tc.getConnection();
                  StorageServer ss=tc.getStoreStorage(ts);
                  /*定义storage的客户端对象,需要使用这个对象来完成具体的文件上传下载工作*/
                  StorageClient sc=new StorageClient(ts,ss);
                  /*
                  * 参数一:文件上传的绝对路径
                  * 参数二:需要上传的文件的扩展名
                  * 参数三:为文件属性文件,通常不上传
                  * 返回一个string类型数组,这个数据对我们很重要建议存入数据库
                  * */
                  String[] jpgs = sc.upload_file("C:/Users/Administrator/Pictures/4.jpg", "jpg", null);
                  for (String str: jpgs) {
                      System.out.println(str);
                  }
                  ss.close();
                  ts.close();
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (MyException e) {
                  e.printStackTrace();
              }
          }
      }
      
    7. 如果成功后会返回一个数组,数组的第一个是上传文件的组名,第二个参数是文件的存储路径

    8. group1
      M00/00/00/wKifDV801kCAPh7cAAGCxz6fcCI689.jpg
      
    9. 把上面控制台打印出来的数据拼在一起,前面在加上服务器地址即可在浏览器访问文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值