云服务器搭建分布式服务器fastdfs
阿里云服务器搭建分布式服务器fastdfs
一、准备环境
#1.使用wget命令来下载压缩包,先安装wget命令
yum install wget
#下载fastdfs
wget -c "https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz" \
-O fastdfs-6.06.tar.gz
#下载libfastcommon
wget -c "https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz" \
-O libfastcommon-1.0.43.tar.gz
#下载fastdfs-nginx-module
wget -c "https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz" \
-O fastdfs-nginx-module-1.22.tar.gz
#下载nginx
wget -c http://nginx.org/download/nginx-1.17.7.tar.gz
#安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
#关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
#查看防火墙状态
firewall-cmd --state
二、解压压缩包并安装
1.找到上一步下载到的安装包,我的是在root文件夹中
tar -zxvf fastdfs-6.06.tar.gz
tar -zxvf fastdfs-nginx-module-1.22.tar.gz
tar -zxvf libfastcommon-1.0.43.tar.gz
tar -zxvf nginx-1.17.7.tar.gz
2.编译安装
#安装libfastcommon
cd /root/libfastcommon-1.0.43
./make.sh && ./make.sh install
#安装fastdfs
cd /root/fastdfs-6.06
./make.sh && ./make.sh install
#启用配置文件
cd /etc/fdfs/
cp storage.conf.sample storage.conf
cp client.conf.sample client.conf
cp tracker.conf.sample tracker.conf
mkdir -p /fastdfs/tracker
#修改 tracker.conf 文件
vi /etc/fdfs/tracker.conf
#将tracker.conf文件中的base_path的值设置位 /fastdfs/tracker
#也就是base_path = /fastdfs/tracker
#vim编辑器 / 进入搜索模式 i 进入编辑模式 esc退出编辑模式 :wq 保存并退出编辑器
#启动tracker服务
/etc/init.d/fdfs_trackerd start
#修改 storage.conf 文件
#base_path = /fastdfs/storage
#store_path0 = /fastdfs/storage
#tracker_server = 公网ip:22122
#http.server_port = 80
#启动 storage 服务
/etc/init.d/fdfs_storaged start
#设置开机启动
vi /etc/rc.d/rc.local
#加入配置 /etc/init.d/fdfs_trackerd start
#编辑 client.conf 文件
vi /etc/fdfs/client.conf
#base_path=/fastdfs/tracker
#tracker_server=192.168.0.154:22122
#安装nginx
#先安装pcre
yum -y install pcre pcre-devel
#进入 /root/fastdfs-nginx-module-1.22/src/
cd /root/fastdfs-nginx-module-1.22/src/
#编辑配置文件
vi config
#将config文件中的/usr/local替换成/usr,直接在命令模式中输入:%s+/usr/local+/usr 回车即可,然后:wq保存退出
#进入 nginx 解压目录
cd /root/nginx-1.17.7
./configure --prefix=/usr/local/nginx --with-http_stub_status_module
./configure --add-module=/root/download/fastdfs-nginx-module-1.22/src/
make && make install
cp /root/download/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
#修改配置
vi /etc/fdfs/mod_fastdfs.conf
#connect_timeout=10
#tracker_server=公网ip:22122
#url_have_group_name = true
#store_path0=/fastdfs/storage
#进入fastdfd源码conf目录
cd /root/download/fastdfs-6.06/conf/
cp http.conf mime.types /etc/fdfs/
#创建软连接,如果创建失败,看看文件夹是否存在
ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
#修改nginx配置
vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name 公网ip;
location ~/group([0-9])/M00 {
root /fastdfs/storage/data;
ngx_fastdfs_module;
}
}
三、测试
1.创建maven项目,pom.xml文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
2.创建springboot应用程序类
@Configuration
@SpringBootApplication()
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FileClientApplication {
public static void main(String[] args) {
SpringApplication.run(FileClientApplication.class,args);
}
}
3.创建FastDFSClient类
@Component
public class FastDFSClient {
private static Logger log =LoggerFactory.getLogger(FastDFSClient.class);
private static FastFileStorageClient fastFileStorageClient;
private static FdfsWebServer fdfsWebServer;
@Autowired
public void setFastDFSClient(FastFileStorageClient fastFileStorageClient, FdfsWebServer fdfsWebServer) {
FastDFSClient.fastFileStorageClient = fastFileStorageClient;
FastDFSClient.fdfsWebServer = fdfsWebServer;
}
/**
* @param multipartFile 文件对象
* @return 返回文件地址
* @author qbanxiaoli
* @description 上传文件
*/
public static String uploadFile(MultipartFile multipartFile) {
try {
StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
return storePath.getFullPath();
} catch (IOException e) {
log.error(e.getMessage());
return null;
}
}
/**
* @param multipartFile 图片对象
* @return 返回图片地址
* @author qbanxiaoli
* @description 上传缩略图
*/
public static String uploadImageAndCrtThumbImage(MultipartFile multipartFile) {
try {
StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
return storePath.getFullPath();
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
/**
* @param file 文件对象
* @return 返回文件地址
* @author qbanxiaoli
* @description 上传文件
*/
public static String uploadFile(File file) {
try {
FileInputStream inputStream = new FileInputStream(file);
StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
return storePath.getFullPath();
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
/**
* @param file 图片对象
* @return 返回图片地址
* @author qbanxiaoli
* @description 上传缩略图
*/
public static String uploadImageAndCrtThumbImage(File file) throws IOException {
FileInputStream inputStream = new FileInputStream(file);
try {
StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
return storePath.getFullPath();
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
finally {
inputStream.close();
}
}
/**
* @param bytes byte数组
* @param fileExtension 文件扩展名
* @return 返回文件地址
* @author qbanxiaoli
* @description 将byte数组生成一个文件上传
*/
public static String uploadFile(byte[] bytes, String fileExtension) {
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
StorePath storePath = fastFileStorageClient.uploadFile(stream, bytes.length, fileExtension, null);
return storePath.getFullPath();
}
/**
* @param fileUrl 文件访问地址
* @param file 文件保存路径
* @author qbanxiaoli
* @description 下载文件
*/
public static boolean downloadFile(String fileUrl, File file) throws IOException {
FileOutputStream stream = new FileOutputStream(file);
try {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
stream.write(bytes);
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
finally {
stream.close();
}
return true;
}
/**
* @param fileUrl 文件访问地址
* @author qbanxiaoli
* @description 删除文件
*/
public static boolean deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return false;
}
try {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
return true;
}
// 封装文件完整URL地址
public static String getResAccessUrl(String path) {
String url = fdfsWebServer.getWebServerUrl() + path;
log.info("上传文件地址为:\n" + url);
return url;
}
}
4.修改application.properties配置文件
fdfs.connect-timeout=600
fdfs.pool.max-total=153
fdfs.pool.max-wait-millis=102
fdfs.so-timeout=1500
fdfs.thumb-image.height=150
fdfs.thumb-image.width=150
fdfs.tracker-list=公网ip:22122
fdfs.web-server-url=http://公网ip/
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
5.测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class FileClientApplicationTests {
@Test
public void Upload() {
String fileUrl = "图片路径";
System.out.println(fileUrl);
File file = new File(fileUrl);
String str = FastDFSClient.uploadFile(file);
FastDFSClient.getResAccessUrl(str);
}
@Test
public void Delete() {
FastDFSClient.deleteFile("");
}
}
源代码:https://github.com/zeliCouer/fastdfs/tree/main
参考文章:

浙公网安备 33010602011771号