资料下载
大纲
FastDFS架构包括Tracker server 和 Storage server 。客户端请求Tracker server 进行文件上传、下载,通过Ttacker server 调度,最总由Storage server 完成文件上传和下载。
Tracher server 作用是负载均衡和调度,通过Tracker server 在文件上传时可以根据一些策略找到Storage server 提供的文件上传服务。可以将Tracker称为追踪服务器或调度服务器。
Storage server 的作用是文件存储,,客户端上传的文件最终存储在Storage服务器上,不过Storage server 没有实现自己的文件系统,而是利用操作系统的文件系统来管理文件。可以将Storage称为存储服务器。
6.在Nginx中安装FastDFS模块
代码语言:javascript复制1.上传fastdfs-nginx-module_v1.16.tar.gz到/usr/local目录
2.解压,进入fastdfs-nginx-module/src目录
3.修改config文件:把/usr/local改成/usr(共3处)
4.cp mod_fastdfs.conf /etc/fdfs/
5.修改/etc/fdfs/mod_fastdfs.conf内容
vim /etc/fdfs/mod_fastdfs.conf
base_path=/home/FastDFS
tracker_server=192.168.70.130:22122
# tracker_server=192.168.70.133:22122(多个tracker配置多行)
url_have_group_name=true #URL中包含group名称
store_path0=/home/FastDFS/fdfs_storage #指定文件存储路径
6.在Nginx的解压目录下重新生成MakeFile
./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=/usr/local/fastdfs-nginx-module/src
7.重新编译,还是执行make命令
8.用objs目录下的nginx文件替换/usr/local/nginx/sbin目录下的nginx文件
保险起见可以把被替换的/usr/local/nginx/sbin/nginx程序文件备份一下
如果看到Nginx文件忙无法替换的提示,则停止Nginx程序
/usr/local/nginx/sbin/nginx -s quit
9.在Nginx中加入如下配置
server {
listen 80;
server_name 192.168.70.130;
location /group1/M00/{
ngx_fastdfs_module;
}
}
10.启动Nginx
[root@rich conf]# /usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=10013
11.浏览器访问
代码语言:javascript复制#PRG=/usr/local/bin/fdfs_trackerd
PRG=/usr/bin/fdfs_trackerd
#if [ ! -f /usr/local/bin/stop.sh ]; then
# echo "file /usr/local/bin/stop.sh does not exist!"
# exit 2
#fi
if [ ! -f /usr/local/fdfs/stop.sh ]; then
echo "file /usr/local/fdfs/stop.sh does not exist!"
exit 2
fi
#if [ ! -f /usr/local/bin/restart.sh ]; then
# echo "file /usr/local/bin/restart.sh does not exist!"
# exit 2
#fi
if [ ! -f /usr/local/fdfs/restart.sh ]; then
echo "file /usr/local/fdfs/restart.sh does not exist!"
exit 2
fi
#stop() {
# /usr/local/bin/stop.sh $CMD
# RETVAL=$?
# return $RETVAL
#}
stop() {
/usr/local/fdfs/stop.sh $CMD
RETVAL=$?
return $RETVAL
}
#restart() {
# /usr/local/bin/restart.sh $CMD &
#}
restart() {
/usr/local/fdfs/restart.sh $CMD &
}
3.修改/etc/init.d/fdfs_storaged
代码语言:javascript复制#PRG=/usr/local/bin/fdfs_storaged
PRG=/usr/bin/fdfs_storaged
#if [ ! -f /usr/local/bin/stop.sh ]; then
# echo "file /usr/local/bin/stop.sh does not exist!"
# exit 2
#fi
if [ ! -f /usr/local/fdfs/stop.sh ]; then
echo "file /usr/local/fdfs/stop.sh does not exist!"
exit 2
fi
#if [ ! -f /usr/local/bin/restart.sh ]; then
# echo "file /usr/local/bin/restart.sh does not exist!"
# exit 2
#fi
if [ ! -f /usr/local/fdfs/restart.sh ]; then
echo "file /usr/local/fdfs/restart.sh does not exist!"
exit 2
fi
#stop() {
# /usr/local/bin/stop.sh $CMD
# RETVAL=$?
# return $RETVAL
#}
stop() {
/usr/local/fdfs/stop.sh $CMD
RETVAL=$?
return $RETVAL
}
#restart() {
# /usr/local/bin/restart.sh $CMD &
#}
restart() {
/usr/local/fdfs/restart.sh $CMD &
}
4.添加服务
chkconfig --add fdfs_trackerd chkconfig --add fdfs_storaged
## 5.设置Nginx开机自动启动 mkdir /usr/local/nginx/logs vim /usr/local/nginx/conf/nginx.conf
代码语言:javascript复制#pid logs/nginx.pid;
pid /usr/local/nginx/logs/nginx.pid;
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl enable nginx.service
7.示例代码
代码语言:javascript复制//1.声明一个字符串变量,用来存储tracker.conf配置文件的路径
//参照基准为类路径根目录
String configPath = "/tracker.conf";
//2.动态获取配置文件的绝对路径
String absolutePath = FastDFSClientTest.class.getResource(configPath).getPath();
//3.调用ClientGlobal类的静态方法执行初始化操作
ClientGlobal.init(absolutePath);
//4.创建TrackerClient对象
TrackerClient trackerClient = new TrackerClient();
//5.创建TrackerServer对象
TrackerServer trackerServer = trackerClient.getConnection();
//6.创建StorageServer对象,可以为null
StorageServer storageServer = null;
//7.创建StorageClient对象
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//8.上传文件
String[] resultArray = storageClient.upload_file("C:/Users/Lenovo/Desktop/girls/bbb.jpg", "jpg", null);
for (String result : resultArray) {
System.out.println(result);
}
八、SpringMVC环境下实现表单上传的文件上传到FastDFS
1.依赖
代码语言:javascript复制<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.准备tracker.conf
代码语言:javascript复制tracker_server=192.168.70.200:22122
3.准备SpringMVC环境
代码语言:javascript复制<mvc:annotation-driven/>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="com.atguigu.fdfs.controller"/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
</bean>
4.创建工厂类
代码语言:javascript复制private String confLocation;
public void setConfLocation(String confLocation) {
//原始:classpath:tracker.conf
//需要:/tracker.conf
int beginIndex = confLocation.indexOf(":") 1;
this.confLocation = "/" confLocation.substring(beginIndex);
}
@Override
public StorageClient getObject() throws Exception {
//1.初始化工作
//※需要把confPath从基于类路径根目录的相对路径转换为真实物理路径
String confAbsolutePath = FastDFSStorageClientFatory.class.getResource(confLocation).getPath();
System.err.println("☆☆☆" confAbsolutePath "☆☆☆");
ClientGlobal.init(confAbsolutePath);
//2.创建StorageClient
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
System.err.println("☆☆☆" storageClient.toString() "☆☆☆");
return storageClient;
}
@Override
public Class<?> getObjectType() {
// TODO Auto-generated method stub
return StorageClient.class;
}
@Override
public boolean isSingleton() {
// TODO Auto-generated method stub
return true;
}
5.在Spring配置文件中配置工厂的bean
代码语言:javascript复制<bean id="fastDFSStorageClientFatory" class="com.fdfs.factory.FastDFSStorageClientFatory">
<property name="confLocation" value="classpath:tracker.conf"/>
</bean>
6.在Controller中自动装配StorageClient
@Autowired private StorageClient storageClient;
项目应用下载