fastDFS项目调用详解
来源:互联网 发布:淘宝图片怎么加护盾 编辑:程序博客网 时间:2024/06/07 23:39
安装FastDFS
安装 fastDFS 之前需要依赖环境:
FastDFS 使用 c 语言开发,需要使用 gcc 编译,需要安装 gcc。
需要安装 fastdfs 依赖类库:libevent
安装FastDFS依赖环境
安装 gcc
2) 安装 libevent
a) 安装命令:yum –y install libevent libevent-devel
3) 安装 libfastcommon 依赖环境
a) 第一步:导入 libfastcommon 到 usr/local/Hadoop/libfastcommonV1.0.7.tar.gz
i. 使用 rz 命令导入
b) 第二步:解压
i. tar -zxvf libfastcommonV1.0.7.tar.gz
c) 第三步:编译
i. 命令:./make.sh
d) 第四步:安装
i. 命令:./make.sh install
e) 第五步:拷贝 usr/lib64 目录下:libfastcommon.so 文件到 usr/lib 下
i. [root@localhost libfastcommon-1.0.7]# find /usr/lib64 name “libfastcommon.so”
/usr/lib64/libfastcommon.so
[root@localhost libfastcommon-1.0.7]#
i. 拷贝命令:cp usr/lib64/ libfastcommon.so /usr/lib
注意:libfastcommon 安装好后会自动将库文件拷贝至/usr/lib64 下,由于 FastDFS 程序引用 usr/lib 目录所以需要将/usr/lib64 下的库文件拷贝至/usr/lib 下
安装FastDFS
1) 第一步:导入 fastDFS 安装包
a) 使用 rz 命令导入
2) 第二步:解压
a) tar -zxvf FastDFS_v5.05.tar.gz
3) 第三步:编译安装
a) 编译命令:./make.sh
b) 安装命令:./make.sh install
4) 第四步:拷贝 FastDFS/conf 下面所有配置文件到 etc/fdfs/
a) 拷贝命令:[root@localhost conf]# cp * /etc/fdfs/
[root@localhost fdfs]# ll total 104 -rw-r--r--. 1 root root 23981 May 13 18:34 anti-steal.jpg -rw-r--r--. 1 root root 1461 May 13 18:34 client.conf -rw-r--r--. 1 root root 858 May 13 18:34 http.conf -rw-r--r--. 1 root root 31172 May 13 18:34 mime.types -rw-r--r--. 1 root root 7829 May 13 18:34 storage.conf -rw-r--r--. 1 root root 7829 May 13 18:31 storage.conf.sample -rw-r--r--. 1 root root 105 May 13 18:34 storage_ids.conf -rw-r--r--. 1 root root 7102 May 13 18:34 tracker.conf -rw-r--r--. 1 root root 7102 May 13 18:31 tracker.conf.sample
安装tracker
安装 tracker,由于 tracker 运行程序就是 fasfdfs,fastDFS 安装成功,只需要修改
etc/fdfstracker.conf 配置文件即可。
1) 第一步:修改 base_path 存储基本路径
a) base_path=/home/fastdfs //fastdfs 需要手动创建,fastdfs 在 home 目录下
2) 第二步:修改存在组
a) store_group=group1
3) 第三步:在 home 目录下创建 fastdfs
a) 创建命令:mkdir fastdfs
4) 第三步:测试启动 tracker
a) 测试是否安装成功
b) 运行命令:/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
drwx——. 4 mine mine 4096 May 5 02:22 mine
[root@localhost home]# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
[root@localhost home]# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
waiting for pid [9077] exit …
starting
安装 storage
由于 storage 和 tracker 运行的都是 fastDFS 程序,每一台服务器都部署一台 fasfDFS,一台服务器是 tracker,一台是 storage。
此机器:tracker 和 fastDFS 都是使用同一个程序。只需要配置 storage 配置文件即可。
配置 storage.conf
1) 第一步:配置组名
a) group_name=group1 //必须和 tracker 的组名相同。
2) 第二步:配置基本存储路径 base_path
a) base_path=/home/fastdfs
3) 配置文件存储路径
a) store_path0=/home/fastdfs/storage_path //storage_path 需要手动创建 #store_path1=/home/yuqing/fastdfs2
如果有多个存储路径,就可以依次往下顺延。
4) 创建文件存储路径 storage_path
a) /home/fastdfs 此目录下面创建
b) 命令:mkdir storage_path
[root@localhost fastdfs]# mkdir storage_path
[root@localhost fastdfs]# ll
total 12
drwxr-xr-x. 2 root root 4096 May 13 18:45 data
drwxr-xr-x. 2 root root 4096 May 13 18:43 logs
drwxr-xr-x. 2 root root 4096 May 13 18:54 storage_path
5) 修改连接 tracker 服务器地址
a) tracker_server=192.168.66.66:22122
6) 启动 storage 测试
a) 启动命令:
[root@localhost fdfs]# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
[root@localhost fdfs]# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
waiting for pid [9132] exit …
starting …
整合 Nginx 和 FastDFS
FastDFS-nginx-module 整合 nginx 和 fastDFS。
安装 FastDFS-nginx-module
1) 第一步:导入 FastDFS-nginx-module 到 usr/local/Hadoop
a) fastdfs-nginx-module_v1.16.tar.gz
2) 第二步:解压 fastdfs-nginx-module_v1.16.tar.gz
a) 命令:tar –zxvf fastdfs-nginx-module_v1.16.tar.gz
3) 第三步:修改/usr/local/hadoop/fastdfs-nginx-module/src/Config
a) 修改路径
去掉错误路径,usr/local/include 改为 usr/include
第四步:拷贝 mod_fastdfs.conf 到 etc/fdfs
a) [root@localhost src]# cp mod_fastdfs.conf /etc/fdfs/
5) 第五步:修改 mod_fastdfs.conf
a) base_path=/home/fastdfs/storage_path //此处必须是存储路径
b) tracker_server=192.168.66.66:22122
c) group_name=group1 //和前面配置组名保持一致
d) store_path0=/home/fastdfs/storage_path //M00
e) url_have_group_name = true //允许使用组名
6) 第六步:拷贝 usr/lib64 目录下库文件 libfdfsclient.so
拷贝 usr/lib64/libfdfsclient.so 库文件 到 usr/lib 目录下。
拷贝命令:cp /usr/lib64/libfdfsclient.so /usr/lib
整合
需要把 module 添加 nginx 中。通过设置安装参数方式添加模块。
参数设置:
./configure \ --prefix=/usr/local/hadoop/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/hadoop/fastdfs-nginx-module/src
需要从新编译安装:
Make
Make install
修改 nginx 配置文件 nginx.conf 配置文件:
http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name 192.168.66.66; location /group1/M00/ { ngx_fastdfs_module; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
上传图片
使用 java 客户端上传图片,上传图片到 fastDFS 图片服务器上。
环境准备
1)需要 Client.conf 配置文件:
tracker_server=192.168.66.66:22122
2)配置上传视图解析器
<!--配置文件上传解析器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonMultipartResolver"> <property name="maxUploadSize" value="10240000" /> </bean>
3) 需要上传文件组件
<!-- 文件上传组件 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <dependency> <groupId>fastdfs_client</groupId> <artifactId>fastdfs_client</artifactId> <version>1.25</version> </dependency>
上传图片
分析上传步骤:
1) 指定图片绝对地址
2) 指定图片服务器地址;使用 Client.conf 配置文件
3) 加载图片服务器,连接图片服务器
4) 创建图片服务器 tracker 客户端
5) 从客户端获取 tracker 连接
6) 创建 storage 客户端,存储图片
上传图片,是把图片上传到 fastDFS 图片服务器,和 dao,Service 没有关系,直接使用 Controller 层就可以。
@Controller public class UploadController { @Value("${IMAGE_SERVER_URL}") private String IMAGE_SERVER_URL; @RequestMapping("/pic/upload") public @ResponseBody PicResult uploadPic(MultipartFile uploadFile){ try { //获取文件名称 String filename = uploadFile.getOriginalFilename(); //获取文件扩展名 String extName = filename.substring(filename.lastIndexOf(".")+1); //创建图片服务器上传工具类 FastDFSClient fClient = new FastDFSClient("classpath:client.conf"); String url = fClient.uploadFile(uploadFile.getBytes(), extName); //组合url图片服务器完整路径 url = IMAGE_SERVER_URL+url; //创建一个PicResult对象,设置返回值结果 PicResult pic = new PicResult(); pic.setError(0); pic.setUrl(url); return pic; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); //上传图片失败 //创建一个PicResult对象,设置返回值结果 PicResult pic = new PicResult(); pic.setError(1); pic.setMessage("上传失败"); return pic; } } }
- fastDFS项目调用详解
- [FastDFS] FastDFS 配置文件详解
- java调用fastDFS
- FastDFS 配置文件详解
- fastdfs+nginx安装详解
- FastDFS Storage配置详解
- FastDFS tracker 配置详解
- 分布式文件系统FastDFS详解
- FastDFS 配置文件详解
- fastdfs+nginx安装详解
- FastDFS Storage配置详解
- FastDFS 配置文件详解
- FastDFS Storage配置详解
- FastDFS配置文件详解
- fastDFS开发详解
- [FastDFS] FastDFS 配置文件详解(修订版1) 【转】
- FastDFS在项目中的应用
- FastDFS 安装及使用详解
- leetcode 257[easy]-----Binary Tree Paths
- 统计代码行数
- java.sql.SQLException: ORA-01790: expression must have same datatype as corresponding expression
- 数据结构实验之查找七:线性之哈希表
- ndk 开发
- fastDFS项目调用详解
- bigemap地图下载器功能与水经注相比较的优势有哪些?
- easyUI入门
- SpringBoot非官方教程 | 第十篇: 用spring Restdocs创建API文档
- git入门学习
- MYSQL 半同步复制环境搭建
- 数据约束
- UIImagePickerController在iOS11访问相册
- 电脑插上U盘双击打不开应用程序右键可以打开问题