Linux(centos)系统下安装fastdfs安装部署步骤,问题复现 并在java中集成测试demo

试验环境,所以打算把跟踪器和存储器安装到同一台实例上

1、准备安装包上传到/usr/local/fastdfs

FastDFS_v5.05.tar.gz (FastDFS安装包)

libfastcommonV1.0.7.tar.gz (FastDFS依赖程序)

nginx-1.8.0.tar.gz (nginx安装包,用于做文件请求http代理服务器)

fastdfs-nginx-module_v1.16.tar.gz (nginx和fastdfs的桥梁插件模块)

安装包路径:

链接:https://pan.baidu.com/s/1sR1FbaFOg0WJ2GejsaqZBw
提取码:4dxf

2、安装C/C++ 编译环境

yum -y install gcc gcc-c++

yum -y install libevent

3、安装 libfastcommon

cd /usr/local/fastdfs
tar -zxf libfastcommonV1.0.7.tar.gz
cd libfastcommon-1.0.7
./make.sh
./make.sh install
cp /usr/lib64/libfastcommon.so /usr/lib

4、安装FastDFS

cd /usr/local/fastdfs

tar -zxf FastDFS_v5.05.tar.gz

cd FastDFS

./make.sh

./make.sh install

cd conf

cp * /etc/fdfs

5、编辑/etc/fdfs/tracker.conf

vi /etc/fdfs/tracker.conf
base_path=/usr/local/fastdfs/FastDFS/tracker

6、启动tracker

  /usr/bin/fdfs_trackerd /etc/fdfs/tracler.conf  

7、配置storage节点

vi /etc/fdfs/storage.conf

base_path=/usr/local/fastdfs/FastDFS/storage

store_path0=/usr/local/fastdfs/FastDFS/storage

tracker_server = 192.168.0.107:22122

8、启动storage节点

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

可能会出现的问题

[2019-06-18 15:51:26] ERROR - file: shared_func.c, line: 968, file /storage.conf not exist
[2019-06-18 15:51:26] ERROR - file: storage_func.c, line: 1079, load conf file "storage.conf" fail, ret code: 2
[2019-06-18 15:51:26] CRIT - exit abnormally!

最后把设置文件移到/etc/fdfs下,再执行就好了

执行 fdfs_storaged /etc/fdfs/storage.conf 

节点的启动 重启 关闭方式 点击  https://blog.csdn.net/weixin_40576010/article/details/88900810

9、配置测试

cd /usr/local/fastdfs//FastDFS/client/

cp libfastclient.so /usr/lib

10、修改测试配置文件

vi /etc/fdfs/client.conf

base_path=/usr/local/fastdfs/FastDFS/client

tracker_server=192.168.0.107:22122

11、测试上传

vim /root/hi.html

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /root/hi.html

可能会出现的错误

文件夹没有

在这里插入图片描述
该错误是 /etc/fdfs 的storage.conf配置文件出错。
在这里插入图片描述
修改storage.conf中的store_path0=xx ,xx为你自定义的文件存储位置,路径必须存在
store_path0=/home/fastdfs/storage
改 store_path0=你的路径(必须存在)

配置完需重启。

二 本地上传文件到FastDFS命令上传报错:ERROR - file: connection_pool.c, line: 130, connect to 47.96.88.***:22122 fail

报错的完整信息:ERROR - file: connection_pool.c, line: 130, connect to 47.96.88.***:22122 fail, errno: 110, error info: Connection timed out

问题分析:错误信息提示错误的原因为连接超时,连接超时有两种可能的原因,一种是云服务器拒绝我们的访问,另一种是我们服务器电脑开启了开机启动防火墙。
注:不管你进行什么操作,只要是报了类似连接失败错误代号,大部分都是由于两种原因造成


解决方法一
故先检查云服务是否对我们的23000端口(FastDFS的默认端口)进行了拦截,有的话创建安全组对该端口进行放行应用到实例上再次上传就能解决上传成功(lz由于阿里云配置较低,故放行所有端口)。

解决方法二:
由于出现该问题的原因是因为自己的防火墙没有开启,故开启防火墙即可解决问题,Linux上的指令如下:
1、关闭防火墙

sudo systemctl stop firewalld.service

2、关闭开机启动

sudo systemctl disable firewalld.service
原文https://blog.csdn.net/qq_42910468/article/details/88702643

12、安装nginx

yum install -y gcc-c++   (已安装)

yum install -y pcre pcre-devel

yum install -y zlib zlib-devel

yum install -y openssl openssl-devel

13、解压fastdfs-nginx-module_v1.16.tar.gz

cd /usr/local/fastdfs/

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

14、修改fastdfs-nginx-module/src/config配置文件

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

​ 修改为:CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

​ 把CORE_LIBS="$CORE_LIBS -L/usr/local/lib -lfastcommon -lfdfsclient"

​ 修改为:CORE_LIBS="$CORE_LIBS -L/usr/lib -lfastcommon -lfdfsclient"

实际就是把路径中的local这一层删除掉

15、复制fastdfs-nginx-module/src/mod_fastdfs.conf 到/etc/fdfs目录下

并编辑

cp mod_fastdfs.conf /etc/fdfs/

tracker_server=192.168.0.107:22122

url_have_group_name = true

storage_server_port=23000

group_name=group1

store_path0=/usr/local/fastdfs/FastDFS/storage

16、安装nginx

cd /usr/local/fastdfs

tar -zxf nginx-1.8.0.tar.gz

cd nginx-1.8.0
./configure --add-module=/usr/local/fastdfs/fastdfs-nginx-module/src

make

make install

17、配置nginx

cd /usr/local/nginx/conf​

vim nginx.conf

server{
listen 80;
server_name localhost;
location /group1/M00/{
ngx_fastdfs_module;
}
}

18、重启nginx

/usr/local/nginx/sbin/nginx

配置步骤原文链接 https://www.cnblogs.com/qiaolizhi/p/12461901.html

java集成fastDFS

java代码部分

 

2.1 引入相关依赖包

 

    <dependency>
      <groupId>com.github.tobato</groupId>
      <artifactId>fastdfs-client</artifactId>
      <version>1.26.5</version>
    </dependency>

2.2 FDFS 配置类

@Configuration@Import(FdfsClientConfig.class// 导入FastDFS-Client组件
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) 
// 解决jmx重复注册bean的问题
public class FdfsConfiguration {}

2.3 配置文件

# 分布式文件系统fastdfs配置
fdfs: 
# socket连接超时时长
 soTimeout: 1500 
# 连接tracker服务器超时时长 
connectTimeout: 600 
# nginx 访问的地址和端口 
reqHost: 114.55.164.189 
reqPort: 80 
pool:   
# 从池中借出的对象的最大数目   
max-total: 153   
# 获取连接时的最大等待毫秒数100   
max-wait-millis: 102 
# 缩略图生成参数,可选 
thumbImage:   
 150   
height: 150 
# 跟踪服务器tracker_server请求地址,支持多个,这里只有一个,如果有多个在下方加- x.x.x.x:port 
trackerList:   - 114.55.164.189:22122 
# # 存储服务器storage_server访问地址
 web-server-url: http://114.55.164.189:80/

配置文件看不明白可以直接点击原文 出处https://juejin.im/post/6869982744529600519#heading-7

2.4 读取配置文件的实体类

@Data@ConfigurationProperties(prefix = "fdfs",ignoreInvalidFields = true)@Slf4jpublic class FastdfsProperties implements InitializingBean {    private String reqHost;    private String reqPort;    private String webServerUrl;    private String soTimeout;    @Override    public void afterPropertiesSet() throws Exception {        log.debug("fdfs.reqHost is {},fdfs.reqPort is {},fdfs.webServerUrl is {}",getReqHost(),getReqPort(),getWebServerUrl());        if (StringUtils.isBlank(getReqHost())) {            throw new IllegalStateException("Property "fdfs.reqHost" cannot  be blank");        }        if (StringUtils.isBlank(getReqPort())) {            throw new IllegalStateException("Property "fdfs.reqPort" cannot  be blank");        }        if (StringUtils.isBlank(getWebServerUrl())) {            throw new IllegalStateException("Property "fdfs.webServerUrl" cannot  be blank");        }        if (StringUtils.isBlank(getSoTimeout())) {            throw new IllegalStateException("Property "fdfs.soTimeout" cannot  be blank");        }    }}

「注意启动类需要添加自动加载配置类注解」 @EnableConfigurationProperties({ApplicationProperties.class,FastdfsProperties.class})

2.5 工具类

@Component
public class FastDFSClient {

    private  Logger log = LoggerFactory.getLogger(FastDFSClient.class);

    @Autowired
    FastdfsProperties fastdfsProperties;


    @Autowired
    private  FastFileStorageClient storageClient;

    @Autowired
    private  ThumbImageConfig imageConfig;   //创建缩略图的

    @Autowired
    private  TrackerClient trackerClient;


    /**
     * 简单文件上传
     * @param file
     * @return
     */
    public  String upload (MultipartFile file){
        String fileName  = file.getOriginalFilename();
        try {
            InputStream inputStream= file.getInputStream();

            long size = file.getSize();

            StorePath path = storageClient.uploadFile(inputStream,size,fileName.substring(fileName.lastIndexOf(".")+ 1), null);

            return getResAccessUrl(path);
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.toString());
            return null;
        }
    }

    /**
     * 删除指定文件
     * @param url
     */
    public  void delFile (String url){
        storageClient.deleteFile(url);
    }


    /**
     * 文件下载
     * @param fileUrl
     * @return
     * @throws IOException
     */
    public byte[] downloadFile(String fileUrl) throws IOException {
        fileUrl = fileUrl.replaceAll(fastdfsProperties.getWebServerUrl(),"");
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        DownloadByteArray downloadByteArray = new DownloadByteArray();
        byte[] bytes = storageClient.downloadFile(group,path, downloadByteArray);
        return bytes;
    }
    /**
     * 获取文件路径
     * @param path
     * @return
     */
    private  String getResAccessUrl(StorePath path) {
        String fileUrl = "http://" +  fastdfsProperties.getReqHost() + ":" +  fastdfsProperties.getReqPort() +  "/" + path.getFullPath();
        return fileUrl;
    }
}

2.6 测试

@RestController
@RequestMapping("/file")
@Api(tags = "文件管理")
public class UploadController {

    @Autowired
    private FastDFSClient fastDFSClient;


    @ApiOperation(value = "文件上传")
    @RequestMapping(value = "/upload" ,method = RequestMethod.POST)
    public Result upload(
            @RequestParam("file")  MultipartFile file
    ){

        if(file == null || file.isEmpty()){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),null);
        }
        String path = fastDFSClient.upload(file);
        return new Result(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),path);

    }

    @ApiOperation(value = "删除文件")
    @RequestMapping(value = "/del" ,method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件全路径" ,name = "url" ,required = true,dataType = "String",paramType = "query")
    })
    public Result delFile(String url){
        if(url == null || url.equals("")){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),null);
        }
        try {
            fastDFSClient.delFile(url);
            return new Result(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),null);
        } catch (Exception e){
            return new Result(RetCode.PARAM_VALUE_ERROR.getCode(),RetCode.PARAM_VALUE_ERROR.getMsg(),null);
        }


    }

    @ApiOperation(value = "文件下载")
    @GetMapping("/downLoad")
    public ResponseEntity<byte []> downLoadFile(
            @ApiParam(value = "文件路径",required = true) @RequestParam(value = "url" ,required = true) String url
    ) {
        try {
            byte[] bytes = fastDFSClient.downloadFile(url);

            String fileName =url.substring(url.lastIndexOf("/" +1));
            fileName = new String(fileName.getBytes("utf-8"),"ISO_8859_1");

            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            httpHeaders.setContentDispositionFormData("attachment", fileName);

            return new ResponseEntity<byte[]>(bytes,httpHeaders,HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}

2.6 文件下载(返回的是字节码数组,可以直接在前端下载文件)

前端文件下载参考  https://juejin.im/post/6847902216230010894
我们不生产技术 我们只是技术的搬运工 感谢以上大神链接的分享 本次只是整合完成的 fastDFS 上传demo 有不明白的欢迎留言讨论
离开电脑之后整个人都是清醒的
原文地址:https://www.cnblogs.com/C1own/p/13936071.html