集群:同一个业务部署在多台服务器上,提高系统的高可用
分布式:不同的业务模块部署在不同的服务器,解决高并发的问题
group1/M00/00/00/rBAABGCBbIqAZDiqAAHy7AgNjgQ467.jpg
组名:一台服务器就是一个组。
虚拟磁盘路径(M00):store_path0 ----> /home/fastdfs/fdfs_storage/data
数据两级目录(00):两级数据目录
文件名:包含:storage服务器的ip,创建时 间戳,大小,后缀名等信息
<dependency>
<groupId>net.oschina.zcx7878groupId>
<artifactId>fastdfs-client-javaartifactId>
<version>1.27.0.0version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-ioartifactId>
<version>1.3.2version>
dependency>
##fastdfs-client.properties
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 80
fastdfs.tracker_servers = IP:22122
@Before
public void test0() throws Exception {
//加载配置文件
ClientGlobal.initByProperties("config/fastdfs-client.properties");
//创建tracker客户端
TrackerClient trackerClient = new TrackerClient();
//获取tracker的连接服务
TrackerServer trackerServer = trackerClient.getConnection();
//声明storage服务
StorageServer storageServer = null;
//定义storage客户端
client =
new StorageClient1(trackerServer, storageServer);
}
@Test() //上传 client.upload_file1
public void test1() throws Exception {
//定义文件元信息
NameValuePair[] list = new NameValuePair[1];
list[0] = new NameValuePair("fileName", "1.jpg");
//上传 返回fileId
String fileId = client.upload_file1("C:\\Users\\HUSKY\\Pictures\\Saved Pictures\\1.jpg", "jpg", list);
System.out.println(fileId);
}
@Test //查询 client.query_file_info1
public void test2() throws IOException, MyException {
FileInfo fileInfo = client.query_file_info1("group1/M00/00/00/rBAABGCBFEOAf_IuABcmmIOasaQ784.png");
System.out.println(fileInfo);
}
@Test//下载 client.download_file1
public void test3() throws Exception{
byte[] bytes = client.download_file1("group1/M00/00/00/rBAABGCBFEOAf_IuABcmmIOasaQ784.png");
//定义空文件 将字节流写入
FileOutputStream outputStream = new FileOutputStream("C:\\test\\1.png");
outputStream.write(bytes);
outputStream.close();
}
@RestController
public class test {
@RequestMapping("upload")
public FileSystem upload(MultipartHttpServletRequest request) throws Exception {
FileSystem fileSystem = new FileSystem();
//上传到本地服务器
//获取表单提交项
MultipartFile file = request.getFile("fname");
//获取文件名1.jpg
String oldFileName = file.getOriginalFilename();
//获取文件后缀 jpg
String suffix = oldFileName.substring(oldFileName.lastIndexOf(".") + 1);
//新文件名
String newFileName = UUID.randomUUID().toString() + "." + suffix;
//定义文件
File windowsFile = new File("C:/test/" + newFileName);
//写入文件
file.transferTo(windowsFile);
//获取图片的绝对路径
String absolutePath = windowsFile.getAbsolutePath();
//上传到FastDFS
ClientGlobal.initByProperties("config/fastdfs-client.properties");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient1 client = new StorageClient1(trackerServer,
storageServer);
NameValuePair[] list = new NameValuePair[1];
list[0] = new NameValuePair("fileName", oldFileName);
String fileId = client.upload_file1(absolutePath, suffix, list);
trackerServer.close();
// 封装fileSystem数据对象
fileSystem.setFileId(fileId);
fileSystem.setFileName(oldFileName);
fileSystem.setFilePath(fileId);
return fileSystem;
}
}
<beans>
<context:component-scan base-package="controller"/>
<mvc:annotation-driven/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="2048000000"/>
bean>
beans>
#1 安装gcc(编译时需要)
yum install -y gcc gcc-c++
#2 安装libevent(运行时需求)
yum -y install libevent
#3 安装 libfastcommon(FastDFS运行基础库)
安装解压zip包的命令: yum install -y unzip
解压包: unzip libfastcommon.zip
进入目录: cd libfastcommon-master
# 编译
chmod 777 make.sh
./make.sh
# 安装
./make.sh install
# 拷贝库文件
cd /usr/lib64
cp libfastcommon.so /usr/lib
#4 安装Tracker
tar -zxvf FastDFS_v5.05.tar.gz
cd FastDFS
./make.sh
./make.sh install
# 安装成功将安装目录下的conf下的文件拷贝到/etc/fdfs/
cp /opt/FastDFS/conf/* /etc/fdfs/
# Tracker配置
vim /etc/fdfs/tracker.conf
#端口号
port=22122
#基础目录(Tracker运行时会向此目录存储storage的管理数据)(基础目录不存在的话,需要自行创建mkdir /home/fastdfs)
base_path=/home/fastdfs
# Storage配置
vim /etc/fdfs/storage.conf
#配置组名
group_name=group1
#端口
port=23000
#向tracker心跳间隔(秒)
heart_beat_interval=30
#storage基础目录
#目录不存在,需要自行创建
base_path=/home/fastdfs
#store存放文件的位置(store_path)
#可以理解一个磁盘一个path,多个磁盘,多个store_path
#fdfs_storage目录不存在,需要自行创建
#mkdir /home/fastdfs/fdfs_storage
store_path0=/home/fastdfs/fdfs_storage
#如果有多个挂载磁盘则定义多个store_path,如下
#store_path1=..... (M01)
#store_path2=..... (M02)
#配置tracker服务器:IP
tracker_server=10.1.220.247:22122
#如果有多个则配置多个tracker
#tracker_server=10.1.220.x:22122
# 启动tracker
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
# 启动storage
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
# 启动 nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 解压nginx模块
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
# 修改 config 文件,将文件中的 /usr/local/ 路径改为 /usr/
cd /opt/fastdfs-nginx-module/src
vim config
# 将 fastdfs-nginx-module/src下的 mod_fastdfs.conf 拷贝至 /etc/fdfs 下
cp mod_fastdfs.conf /etc/fdfs/
# 修改 /etc/fdfs/mod_fastdfs.conf
vim /etc/fdfs/mod_fastdfs.conf
base_path=/home/fastdfs
tracker_server=IP:22122
#(n个tracker配置n行)
#tracker_server=10.1.220.x:22122
#url中包含group名称
url_have_group_name=true
#指定文件存储路径(上面配置的store路径)
store_path0=/home/fastdfs/fdfs_storage
# 将 libfdfsclient.so 拷贝至 /usr/lib 下
cp /usr/lib64/libfdfsclient.so /usr/lib/
# 创建nginx/client目录
mkdir -p /var/temp/nginx/client
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
yum install openssl
yum install openssl-devel
./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=/opt/fastdfs-nginx-module/src
在 /var 下创建 temp 及 nginx 目录:mkdir /var/temp/nginx
编译:make
安装:make install
拷贝配置文件
cd /opt/FastDFS/conf
cp http.conf mime.types /etc/fdfs/
是否覆盖:yes
修改nginx配置文件
cd /usr/local/nginx/conf/
vim nginx.conf
server {
listen 80;
server_name 10.1.220.247;
#charset koi8-r;
#access_log logs/host.access.log main;
location /group1/M00 {
root /home/fastdfs/fdfs_storage/data;
ngx_fastdfs_module;
}
pkill -9 nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
cd /usr/local/nginx/conf/
vim nginx.conf
server {
listen 80;
server_name 10.1.220.247;
#charset koi8-r;
#access_log logs/host.access.log main;
location /group1/M00 {
root /home/fastdfs/fdfs_storage/data;
ngx_fastdfs_module;
}
pkill -9 nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf