centos7安装fastdfs经过nginx代理以及java客户端

  • 1、下载安装依赖
    • 1.1 下载依赖、解压
    • 1.2 安装libfastcommon
    • 1.3 安装fastdfs
    • 1.4 编辑fastdfs配置
    • 1.5 安装 nginx-module
    • 1.6 配置nginx
  • 2、Java客户端上传文件
    • 2.1 引入依赖
    • 2.2 编写客户端上传单元测试
  • 3、FastDFS原理

1、下载安装依赖

1.1 下载依赖、解压

下载libfastcommonfastdfsfastdfs-nginx-modulenginxjava

wget https://github.com/happyfish100/libfastcommon/archive/V1.0.39.tar.gz -SO libfastcommon.tar.gz
wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz -SO fastdfs.tar.gz
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz -SO fastdfs-nginx-module.tar.gz
wget http://nginx.org/download/nginx-1.10.3.tar.gz
复制代码

解压资源nginx

tar -zxvf xxx.tar.gz
复制代码

1.2 安装libfastcommon

## 进入libfastcommon解压目录
./make.sh
./make.sh install
复制代码

1.3 安装 fastdfs

## 进入fastdfs解压目录
./make.sh
./make.sh install
复制代码

安装好后,程序是在/usr/bin目录下git

## 查看fdfs_trackerd命令位置
which fdfs_trackerd
复制代码

配置文件是在/etc/fdfs目录下,可是都是模板,咱们拷贝下fastdfs解压目录下conf下的配置github

cp ${base_path}/fastdfs-5.11/conf/* /etc/fdfs
复制代码

而后再把/fastdfs-nginx-module/src/下的mod_fastdfs.conf配置文件也拷过去
(这个是重点,若是没拷过去可能致使nginx启动没有worker进程)web

cp ${base_path}/fastdfs-nginx-module-1.20/src/mod_fastdfs.conf /etc/fdfs/mod_fastdfs.conf
复制代码

1.4 编辑fastdfs配置

编辑跟踪器配置vim /etc/fdfs/tracker.confspring

# 跟踪器运行端口,默认就行,不用改
port=22122
# fastdfs存储日志和文件的目录
base_path=/home/www/fdfs

# 跟踪器向外暴露的端口,一下子nginx要代理这个端口
http.server_port=9270
复制代码

编辑存储器配置vim /etc/fdfs/storage.confvim

# 储存器所属的组
group_name=group1

# 储存器运行端口
port=23000

# 跟上面那个base_path一致便可
base_path=/home/www/fdfs

# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist 意思是这个目录必须存在,不存在就得建立,否则启动不了
store_path0=/home/www/fdfs
#store_path1=/home/www/fdfs2

# tracker服务器,虽然是同一台机器上,可是不能写127.0.0.1。这项配置能够出现一次或屡次,写公网ip
tracker_server=132.232.48.200:22122

# the port of the web server on this storage server,这个不知道干吗的,先不改
http.server_port=8888
复制代码

编辑客户端配置 vim /etc/fdfs/client.confsegmentfault

# 也跟上面同样吧,这个配置看状况配就好了,我这个是简单点配置一下
base_path=/home/www/fdfs
# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address,
# 同样的配置跟踪器的地址
tracker_server=132.232.48.200:22122
#HTTP settings
#这个配跟踪器的刚刚那个http端口
http.tracker_server_port=9270
复制代码

编辑 mod_fastdfs.conf vim /etc/fdfs/mod_fastdfs.conf 暂时还不知道干什么用的,应该跟nginx-module有关系,不配就报错, 通常nginx启动没有worker进程可能就是由于这个配置有问题安全

# the base path to store log files
# 不知道干啥的,不改就是
base_path=/tmp

# 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=132.232.48.200:22122
# tracker_server=132.232.48.201:22122 这个若是有多个跟踪器,就复制一下这个

# url是否能够包含group这个关键字,这个要改为true,否则会404
url_have_group_name=true  

# the port of the local storage server
# the default value is 23000
# 配的存储器的端口
storage_server_port=23000

# the group name of the local storage server
# 组名,我猜是nginx转发须要用的。。。
group_name=group1

# 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=/home/www/fdfs
#store_path1=/home/yuqing/fastdfs1
复制代码

如今咱们再来建立一下刚刚没有建立的base_pathbash

mkdir -p /home/www/fdfs
复制代码

启动一下

# 启动
fdfs_trackerd /etc/fdfs/tracker.conf start
fdfs_storaged /etc/fdfs/storage.conf start
复制代码

查看一下是否是启动成功 netstat -anp | grep fdfs

有这两个说明基本ok

测试上传一下 fdfs_test /etc/fdfs/client.conf upload /home/www/dximg/test.jpg
语法是 fdfs_test {client.conf客户端配置路径} upload {file_path文件路线}

此时能够看到已经有地址返回了,不过这个时候还访问不了。再继续配置

1.5 安装 nginx-module

这一步最坑爹,不过仍是得继续
首先咱们进入nginx的源码目录的src下面
若是已经装了nginxnginx -V查看配置

--prefix这后面一段都复制一下,由于我已经安装了 fdfs-nginx-module
因此已经有了 --add-module=/home/www/fastdfs/fastdfs-nginx-module-1.20/src
这个是咱们一下子要添加进去的 语法是 --add-module={模块解压的src目录下}
如今复制刚刚那一段而后执行

./configure --prefix=/usr/local/nginx --pid-path=/usr/local/nginx/run --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_image_filter_module --with-debug --add-module=/home/www/fastdfs/fastdfs-nginx-module-1.20/src
复制代码

这句话看我的的配置执行,而后再继续

make
# 报错
Fatal error: can't create objs/addon/src/ngx_http_fastdfs_module.o: Permission denied In file included from /home/xxxx/caibh/fdfs-package/fastdfs-nginx-module-1.20/src/common.c:26:0, from /home/xxxx/caibh/fdfs-package/fastdfs-nginx-module-1.20/src/ngx_http_fastdfs_module.c:6: /usr/include/fastdfs/fdfs_define.h:15:27: fatal error: common_define.h: No such file or directory # 解决 # 修改fastdfs-nginx-module的src/config文件 $ cd {源码下载目录}/fastdfs-nginx-module-1.20/src $ vim config # 修改一下两项: ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/" CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/" # 从新运行执行一下 ./configure --prefix=/usr/local/nginx --pid-path=/usr/local/nginx/run --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_image_filter_module --with-debug --add-module=/home/www/fastdfs/fastdfs-nginx-module-1.20/src # 再继续 make # 网上说不能执行会覆盖如今的,我执行发现覆盖没啥问题, # 他会把你以前的配置命令什么的拷贝一下在同级目录下生成一些 nginx.old这样的备份 make install 复制代码

启动一下nginx /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
而后看看是否是启动成功了ps -ef|grep nginx,若是没有worker多半是配置有问题

1.6 配置nginx

server
     {
         listen 9270;

        location ~/group([0-9])/ {
            ngx_fastdfs_module;
        }
    }

复制代码

重启几个服务一下

fdfs_trackerd /etc/fdfs/tracker.conf restart
fdfs_storaged /etc/fdfs/storage.conf restart
nginx -s stop
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 
复制代码

这个时候再访问刚刚上传返回的地址应该是能够的了(前提是刚刚的22122, 23000, 9270, 8888几个端口都开放一下,什么安全组,iptables)

2、Java客户端上传文件

2.1 引入依赖

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

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        
复制代码

2.2 编写客户端上传单元测试

# ===================================================================
# 分布式文件系统FDFS配置
# ===================================================================
fdfs:
  so-timeout: 1501
  connect-timeout: 601
  thumb-image:             #缩略图生成参数
    width: 150
    height: 150
  tracker-list:            #TrackerList参数,支持多个
    - 132.232.48.200:22122
  pool:
    #从池中借出的对象的最大数目(配置为-1表示不限制)
    max-total: -1
    #获取链接时的最大等待毫秒数(默认配置为5秒)
    max-wait-millis: 5000
    #每一个key最大链接数
    max-total-per-key: 50
    #每一个key对应的链接池最大空闲链接数
    max-idle-per-key: 10
    #每一个key对应的链接池最小空闲链接数
    max_idle_per_key: 5

复制代码
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;

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

}
复制代码
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.upload.FastFile;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashSet;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class FastdfsClientApplicationTests {

    @Autowired
    private FastFileStorageClient client;

    @Test
    public void contextLoads() throws FileNotFoundException {
        File file = new File("C:\\Users\\Administrator\\Desktop\\666.jpg");

        StorePath storePath = client.uploadFile(new FastFile(
                new FileInputStream(file), file.length(), file.getName().substring(file.getName().lastIndexOf(".") + 1), new HashSet<>()
        ));
        String fullPath = storePath.getFullPath();
        log.info("path --> " + fullPath);
    }

}

复制代码

运行返回地址了就ok

3、原理

先挂个别人的 juejin.im/entry/5b7c1…

搭建参考:segmentfault.com/a/119000001…
Java客户端:github.com/tobato/Fast… FastDFS架构 github.com/tobato/Fast…

相关文章
相关标签/搜索