图片服务器的搭建
配置到虚拟机上,一个Tracker,一个Storage服务,配置nginx访问图片。
搭建步骤:
- 1.把fastDFS的所需压缩包上传到linux系统中,
FastDFS_v5.05.tar.gz
fastdfs-nginx-module_v1.16.tar.gz
libfastcommonV1.0.7.tar.gz
-
2.安装FastDFS之前,先安装libevent工具包:
yum -y install libevent -
3.安装libfastcommonV1.0.7 工具包。
安装编译环境:
yum -y groupinstall 'Development Tools' -
4.解压缩:[root@localhost ~]# tar -zxvf libfastcommonV1.0.7.tar.gz
-
5 ./make.sh
-
6 ./make.sh install
-
7 把/usr/lib64/libfastcommon.so 文件向/usr/lib/下复制一份:
[root@localhost lib64]# cp libfastcommon.so ../lib -
8 安装Tracker服务
1、 解压缩 [root@localhost ~]# tar -zxvf FastDFS_v5.05.tar.gz
2、 [root@localhost libfastcommon-1.0.7]#./make.sh
3、 [root@localhost libfastcommon-1.0.7]#./make.sh install
安装后在/usr/bin/目录下有以 fdfs 开头的文件都是编译出来的。
配置文件都放到/etc/fdfs 文件夹
4、 把/root/FastDFS/conf 目录下的所有的配置文件都复制到/etc/fdfs 下。
[root@localhost conf]# cp * /etc/fdfs
5、 配置 tracker 服务。修改/etc/fdfs/tracker.conf 文件。
-
9 将base_path=..改成自己的路径:
base_path 日志路径, 创建目录 /home/fastdfs/tracker:
[root@localhost home]# mkdir -p fastdfs/tracker -
10 启动 tracker。 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
重启使用命令: /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart -
11 安装storage服务:
1、 如果是在不同的服务器安装,第四步的 1~4 需要重新执行。
2、 配置 storage 服务。修改/etc/fdfs/storage.conf 文件
[root@localhost fdfs]# vi storage.conf
创建 storage 服务日志路径: /home/fastdfs/storage
3、修改 storage 服务日志路径为: /home/fastdfs/storage
4、修改图片保存路径为: /home/fastdfs/storage
5、修改 trancker 服务器的 ip 及端口
-
12 ,启动 storage 服务。
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
会在/home/fastdfs/storage/data 目录下创建两层目录 -
上传图片进行测试:通过java Client API
-
1,将 fastdfs_client 工程导入到 Eclipse 中,并通过 install 添加到本地仓库
-
2, 将坐标(GAV)添加到 shop-manager-common 工程中, 因为图片上传是个通用的功能
-
3,这是所需要的依赖代码:
fastdfs_client
fastdfs_client
1.25
-
3.在 shop-manager-web 工程中,定义测试方法
- 加载配置文件,配置文件中的内容就是 tracker 服务的地址。
- 配置文件内容: tracker_server=192.168.1.158:22122
- 创建一个 TrackerClient 对象。直接 new 一个。
- 使用 TrackerClient 对象创建连接,获得一个 TrackerServer 对象。
- 创建一个 StorageServer 的引用,值为 null
- 创建一个 StorageClient 对象,需要两个参数 TrackerServer 对象、 StorageServer 的引用
- 使用 StorageClient 对象上传图片。
- 返回数组。包含组名和图片的路径。
public class FastDfsTest {
@Test
public void testUpload() throws Exception {
//创建一个配置文件。文件名任意。内容就是tracker服务器的地址。
//使用全局对象加载配置文件。
ClientGlobal.init("E:\Users\admin\workspace\shop-manager-web
src\main\resources\conf\client.conf");
//创建一个TrackerClient对象
TrackerClient trackerClient = new TrackerClient();
//通过TrackClient获得一个TrackerServer对象
TrackerServer trackerServer = trackerClient.getConnection();
//创建一个StrorageServer的引用,可以是null
StorageServer storageServer = null;
//创建一个StorageClient,参数需要TrackerServer和StrorageServer
StorageClient storageClient = new StorageClient(trackerServer,
storageServer);
//使用StorageClient上传文件。
// 第一个参数:文件路径 第二个参数:文件扩展名(不包含.) 第三个
参数:元数据
String[] strings =
storageClient.upload_file("C:\Users\admin\Desktop\timg.jpg",
"jpg", null);
for (String string : strings) {
System.out.println(string);
}
}
}
-
4.shop-manager-web 模 块 中 的 resources 目 录 下 定 义 confclient.conf , 内 容 为tracker_server 的服务器地址。
tracker_server=192.168.1.158:22122 -
5,运行结果:
group1
M00/00/00/图片名称.jpg -
6,访问:
http://192.168.1.158/group1/M00/00/00/wKgBnln18niABXj-AADZa5WaCng833.jpg
发现无法访问,因为没有提供 http 服务,那么我们可以通过 nginx 来搭建 http 服务。 -
7,搭建nginx提供http服务:
可以使用官方提供的 nginx 插件。要使用 nginx 插件需要重新编译。
fastdfs-nginx-module_v1.16.tar.gz -
7.1 ,解压插件压缩包
[root@localhost ~]# tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -
7.2,修改/root/fastdfs-nginx-module/src/config 文件,把其中的 local 去掉。
-
7.3,对nginx重新config
./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=/root/fastdfs-nginx-module/src
- 7.4, make
- 7.5, make install
- 7.6, 把/root/fastdfs-nginx-module/src/mod_fastdfs.conf 文件复制到/etc/fdfs 目录下。
[root@localhost nginx]# cp /root/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
编辑:
日志存放路径,可以使用默认也可以自己更改
修改实际 tracker 服务的 ip:端口
将 URL 是否包含组名修改为 true,默认是 false:
url_have_group_name=true
修改图片保存的路径: store_path0=/home/fastdfs/storage - nginx 的配置:
在 nginx 的配置文件中添加一个 Server:
server {
listen 80;
server_name 192.168.1.158;
location /group1/M00/{
-
root /home/FastDFS/fdfs_storage/data;
-
插件的名称
ngx_fastdfs_module;
}
}