Distributed File Storage System] [Fastdfs environment to build, using java client calls FastDFS

Here Insert Picture Description
Documentation is detailed, very detailed every step

The following link:
link: https: //pan.baidu.com/s/1TOqQJwLmcRvKKONTVN54hA
extraction code: 26p5

FastDfs installation documentation

Here Insert Picture Description

Installation Order

  1. libfastcommon
  2. fdfs_tracker
    dependence: gcc, libevent, perl
  3. fdfs_storage
  4. FastDFS-nginx-module
  5. nginx
    依赖:pcre-devel、zlib-devel

Function file directory
opt / fastdfs data storage directory
usr / local / fdfs startup file directory
etc / fdfs profile directory
usr / bin / fdfs_trackerd boot configuration
etc / init.d / fdfs_trackerd start the service script

FastDFS-tracker is mounted
tutorial Centos7.4 used as installation environments.

  1. Installation gcc environment-dependent yum install gcc -y
  2. yum -y install libevent
  3. If there is no perl library, use yum install perl * command to install it
  4. Installation libfastcommon
  5. Upload Package file libfastcommonV1.0.7.tar.gz to under / usr / local directory, and unzip it.
  6. tar -zxvf libfastcommonV1.0.7.tar.gz
  7. After extracting the files into a folder
  8. Compile ./make.sh
  9. If the compiler does not recognize the perl running this command appears below
  10. yum -y install zlib zlib-devel pcre pcre-devel gcc gcc-c++ openssl openssl-devel libevent libevent-devel perl unzip net-tools wget
  11. /make.sh install
  12. Note: After libfastcommon installed will automatically copy files to the library under / usr / lib64, due FastDFS program references usr / lib directory so you need to copy the library files in / usr / lib64 to under / usr / lib.
  13. cp /usr/lib64/libfastcommon.so /usr/lib/

Installation tracker

  1. Upload FastDFS_v5.05.tar.gz to under / usr / local directory
  2. Compile and install unzip
  3. tar -zxvf FastDFS_v5.05.tar.gz
  4. CD FastDFS
  5. ./make.sh
  6. ./make.sh install
  7. After successful installation, the installation files in the conf directory copied to / etc / fdfs / down.
  8. cd conf
  9. * cp / etc / FDF /
  10. Modify the configuration file
  11. vim /etc/fdfs/tracker.conf
    Here Insert Picture Description
  12. Fastdfs create folders
    mkdir / opt / fastdfs

Set Startup Items

  1. mkdir /usr/local/fdfs

  2. And copy the install directory stop.sh restart.sh the / usr / local / FDFS /
    CP restart.sh / usr / local / FDFS /
    CP stop.sh / usr / local / FDFS /

  3. Modify the startup script
    vim /etc/init.d/fdfs_trackerd
    Here Insert Picture Description
    Here Insert Picture Description

把启动脚本中的路径按照上图修改
修改完毕后
4. 注册服务
chkconfig --add fdfs_trackerd
然后可以用service fdfs_trackerd start 启动测试 如下图
Here Insert Picture Description

FastDFS—storage配置

  1. 修改配置文件
    vim /etc/fdfs/storage.conf
    Here Insert Picture Description
    Here Insert Picture Description

  2. 创建 fdfs_storage文件夹
    mkdir /opt/fastdfs/fdfs_storage

  3. 设置服务
    vim /etc/init.d/fdfs_storaged
    Here Insert Picture Description
    Here Insert Picture Description

  4. chkconfig - -add fdfs_storaged

  5. 启动服务
    service fdfs_storaged start
    Here Insert Picture Description

上传图片测试

  1. 修改 /etc/fdfs/client.conf文件
    vim /etc/fdfs/client.conf
base_path=/opt/fastdfs
tracker_server=192.168.67.163:22122

测试上传:
执行命令:/usr/bin/fdfs_test /etc/fdfs/client.conf upload 图片路径/文件名
Here Insert Picture Description

FastDFS整合nginx

  1. 安装nginx整合插件fastdfs-nginx-module
    上传fastdfs-nginx-module_v1.16.tar.gz上传到 /usr/local
    tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
  2. 编辑配置文件:修改config文件将/usr/local/路径改为/usr/
    (这里只修改两个路径,第二行不用改,图是错的)
    vim fastdfs-nginx-module/src/config
    Here Insert Picture Description
  3. 将FastDFS-nginx-module/src下的mod_fastdfs.conf拷贝至/etc/fdfs/下
    cp mod_fastdfs.conf /etc/fdfs/
    并修改mod_fastdfs.conf的内容:
    vim /etc/fdfs/mod_fastdfs.conf
    Here Insert Picture Description
    Here Insert Picture Description
    Continue to modify
    Here Insert Picture Description
  4. 将libfdfsclient.so拷贝至/usr/lib下
    cp /usr/lib64/libfdfsclient.so /usr/lib/

安装nginx

  1. 创建nginx/client目录
    mkdir -p /var/temp/nginx/client
  2. 安装环境:
    安装pcre库
    yum -y install pcre-devel
    安装zlib库
    yum install -y zlib-devel
  3. 上传nginx
    上传nginx压缩包到/usr/local目录下,解压
    tar -zxvf nginx-1.12.2.tar.gz
  4. 添加fastdfs-nginx-module模块
    cd nginx-1.12.2到nginx目录下,执行如下命令
    ./configure
    –prefix=/usr/local/nginx
    –pid-path=/var/run/nginx/nginx.pid
    –lock-path=/var/lock/nginx.lock
    –error-log-path=/var/log/nginx/error.log
    –http-log-path=/var/log/nginx/access.log
    –with-http_gzip_static_module
    –http-client-body-temp-path=/var/temp/nginx/client
    –http-proxy-temp-path=/var/temp/nginx/proxy
    –http-fastcgi-temp-path=/var/temp/nginx/fastcgi
    –http-uwsgi-temp-path=/var/temp/nginx/uwsgi
    –http-scgi-temp-path=/var/temp/nginx/scgi
    –add-module=/usr/local/fastdfs-nginx-module/src

注意:特别注意最后一行,请按照自己的实际目录填写

执行结果如下,就说明成功了
Here Insert Picture Description
5. 编译
[root@localhost nginx-1.12.2]# make
6. 安装
[root@localhost nginx-1.12.2]# make install
7.编辑 nginx.conf
vim /usr/local/nginx/conf/nginx.conf
Here Insert Picture Description
注意:是location / 不是/group1/M00/
图片内容有错误

启动nginx

  1. /usr/local/nginx/sbin/nginx

  2. 设置开机启动
    vim /etc/rc.d/rc.local

  3. 需要关闭防火墙
    systemctl disable firewalld

  4. 测试

  5. /usr/bin/fdfs_test /etc/fdfs/client.conf upload /root/ty.jpg

Here Insert Picture Description
6. 这时候可以直接访问了
Here Insert Picture Description

关于集群,就是在不同机器上配置多个storage,或者配置多个tracker
Here Insert Picture Description
Here Insert Picture Description

利用Java客户端调用FastDFS

服务器安装完毕后,咱们通过Java调用fastdfs
加载Maven依赖
fastdfs 没有在中心仓库中提供获取的依赖坐标。
只能自己通过源码方式编译,打好jar 包,安装到本地仓库。
官方仓库地址:
https://github.com/happyfish100/fastdfs-client-java
它其实就是一个maven工程,下载完成后
直接用idea 直接把这个源码作为模块导入工程
Here Insert Picture Description
别的不用改,只把pom.xml中的版本改成1.27。
Here Insert Picture Description
然后右边 执行install 就好了
Here Insert Picture Description
安装好了 ,别的模块就可以直接使用这个坐标了

<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27</version>

而这个fastdfs-client-java模块可以从idea 中删除。
加入tracker.conf文件
Here Insert Picture Description

tracker_server=172.16.31.67:22122

# 连接超时时间,针对socket套接字函数connect,默认为30秒
connect_timeout=30000

# 网络通讯超时时间,默认是60秒
network_timeout=60000
然后可以进行一下上传的测试
@RunWith(SpringRunner.class)
@SpringBootTest

@Test
public void textFileUpload() throws IOException, MyException {
    String file = this.getClass().getResource("/tracker.conf").getFile();
    ClientGlobal.init(file);
    TrackerClient trackerClient=new TrackerClient();
    TrackerServer trackerServer=trackerClient.getConnection();
    StorageClient storageClient=new StorageClient(trackerServer,null);
    String orginalFilename="e://victor.jpg";
    String[] upload_file = storageClient.upload_file(orginalFilename, "jpg", null);
    for (int i = 0; i < upload_file.length; i++) {
        String s = upload_file[i];
        System.out.println("s = " + s);
    }

}

打印结果
Here Insert Picture Description
这个打印结果实际上就是我们访问的路径,加上服务器地址我们可以拼接成一个字符串
可以在浏览器直接访问

http://172.16.31.67/group1/M00/00/00/wKhDo1qjU2qAWKQmAAATla901AQ534.jpg

Here Insert Picture Description
上传成功!

对接到业务模块中
在修改Controller的方法
利用@Value 标签可以引用application.properties中的值
fileServer.url=http://172.16.31.67

@Value("${fileServer.url}")
String fileUrl;

@RequestMapping(value = "fileUpload",method = RequestMethod.POST)
public String fileUpload(@RequestParam("file") MultipartFile file) throws IOException, MyException {
    String imgUrl=fileUrl;
    if(file!=null){
        System.out.println("multipartFile = " + file.getName()+"|"+file.getSize());
        String configFile = this.getClass().getResource("/tracker.conf").getFile();
        ClientGlobal.init(configFile);
        TrackerClient trackerClient=new TrackerClient();
        TrackerServer trackerServer=trackerClient.getConnection();
        StorageClient storageClient=new StorageClient(trackerServer,null);
        String filename=    file.getOriginalFilename();
        String extName = StringUtils.substringAfterLast(filename, ".");

        String[] upload_file = storageClient.upload_file(file.getBytes(), extName, null);
        imgUrl=fileUrl ;
        for (int i = 0; i < upload_file.length; i++) {
            String path = upload_file[i];
            imgUrl+="/"+path;
        }
    }
    return imgUrl;
}

至此我们解决了文件上传的功能。
建议:初级开发和高级开发的差别就是在环境搭建和功能设计和代码设计,架构设计的差别(个人认为),所以应该把上传文件的功能直接写成一个工具类,controller层直接进行注入,调用

Published 21 original articles · won praise 1 · views 379

Guess you like

Origin blog.csdn.net/qq_44909430/article/details/103901672