fastdfs

GitHub地址:https://github.com/happyfish100/fastdfs

安装步骤:https://github.com/happyfish100/fastdfs/blob/master/INSTALL

在/etc/fdfs/下没有tracker.conf文件,将tracker.conf.sample复制过来即可

启动服务

service fdfs_trackerd start

查看状态

systemctl status fdfs_trackerd.service

 继续修改storage.conf

这里需要修改track_server=192.168.31.146:22122

启动服务

service fdfs_storaged start

检查状态

service fdfs_storaged status

 

 接下来编辑/etc/fdfs/client.conf,同样配置tracker_server

运行监控

/usr/bin/fdfs_monitor /etc/fdfs/client.conf

会出现对应的监控信息

 

从这里可以看到用到了这几个端口22122,23000,8888,在防火墙中打开这几个端口

firewall-cmd --zone=public --add-port=22122/tcp --permanent
firewall-cmd --zone=public --add-port=23000/tcp --permanent
firewall-cmd --zone=public --add-port=8888/tcp --permanent

 

新建spring-boot项目,添加pom引用

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.25.4-RELEASE</version>
        </dependency>

这里用到了:https://github.com/tobato/FastDFS_Client

添加配置类ComponetImport

import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;

@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class ComponetImport {
}
View Code

添加FastdfsService

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

@Service
public class FastdfsService {
    @Autowired
    private FastFileStorageClient storageClient;

    public String uploadFile(String fileName) throws FileNotFoundException {
        String fileType= FilenameUtils.getExtension(fileName).toLowerCase();
        File file = new File(fileName);
        InputStream inputStream=new FileInputStream(file);

        StorePath path=null;
        path=storageClient.uploadFile(inputStream,file.length(),fileType,null);
        if(path!=null)
            return path.getFullPath();
        return null;
    }

    public void deleteFile(String fullPath){
        storageClient.deleteFile(fullPath);
    }
}
View Code

添加FileController

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;

@RestController
public class FileController {
    @Autowired
    private FastdfsService fastdfsService;
    @RequestMapping(value = "/upload")
    public String upload() throws FileNotFoundException {
        String path="C:\\Users\\mythsky\\Desktop\\dubai\\dsc_2290.jpg";
        return fastdfsService.uploadFile(path);
    }
    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable String id){
        try{
            fastdfsService.deleteFile(id);
            return "success";
        }catch (Exception e){
            return "failure";
        }
    }
}
View Code

运行程序,打开:http://localhost:8080/upload,可以看到已经保存了文件

 

在服务端查看监控,可以看到已经成功上传了

 

接下来配置nginx,参考:

FastDFS【二】:FastDFS配置Nginx

下载nginx到/opt 目录

wget http://nginx.org/download/nginx-1.13.8.tar.gz

解压

tar xzf nginx-1.13.8.tar.gz

 在编译nginx前需要增加编译环境

yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel

然后执行

./configure --prefix=/usr/local/nginx

之后执行

make
make install

启动

/usr/local/nginx/sbin/nginx

查看

netstat -anp|grep 80

 此时如果在虚拟机内访问:curl http://localhost

可以看到

但在虚拟机外使用IP是无法访问的,需要修改配置

vi /usr/local/nginx/conf/nginx.conf

将监听的server中的localhost改为虚拟机的IP地址

接下来在虚拟机外部就可以访问了

 

下载fastdfs-nginx-module:https://github.com/happyfish100/fastdfs-nginx-module

下载后解压

tar xzf fastdfs-nginx-module_v1.16.tar.gz

这里都放在/opt 目录下

进入

cd fastdfs-nginx-module/src/

修改config

修改如下:不修改后面编译nginx会报错

找到下面一行包含有 local 字眼去掉

CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"

改为如下:

CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

 

进入fastdfs-nginx-module/src目录下,复制mod_fastdfs.conf文件到 /etc/fdfs目录,进入/etc/fdfs目录,修改mod_fastdfs.conf配置文件

修改如下三处:

tracker_server=192.168.31.146:22122 # tracker服务IP和端口
url_have_group_name=true # 访问链接前缀加上组名
store_path0=/data/fastdfs/storage # 文件存储路径

进入/opt/nginx 目录,执行

./configure --add-module=/opt/fastdfs-nginx-module/src/ --prefix=/usr/local/nginx/

之后进行make  make install

修改nginx配置

vi /usr/local/nginx/conf/nginx.conf

修改如下

 

启动nginx

/usr/local/nginx/sbin/nginx

查看

netstat -anp|grep 8888

 

在另外一台服务器上同样配置,打开http://192.168.150.130/group1/M00/00/00/wKiWglpazuOABzC8ADSKr9JGGfc820.jpg

发现报404错误,打开日志发现是目录的问题

创建两个链接

ln -s /home/yuqing/fastdfs/data  /home/yuqing/fastdfs/data/group1
ln -s /home/yuqing/fastdfs/data  /home/yuqing/fastdfs/data/group1/M00

再次刷新,就能看到图片了。

 

posted @ 2018-01-13 10:42  uptothesky  阅读(208)  评论(0编辑  收藏  举报