标签:junit ror 状态 localhost conf spring sample sbin 关闭
FastDFS是C语言编写的一个开源的轻量级分布式文件系统,它由淘宝资深架构师余庆编写并开源。FasdDFS专为互联网量身定制,充分考虑到了冗余备份、负载均衡、线程扩容等机制。并注重高可用、高性能等指标,使用FastDFS很容易扩建一套高性能的文件服务器集群提供文件的上传、下载等服务。
FastDFS技术架构图:
FastDFS服务端有三个角色:客户端(client)、跟踪服务器(tracker server)、存储服务器(storage server)
client:客户端,作为业务请求的发起方,通过专有接口,使用TCP/IP协议与跟踪器服务器或存储节点进行数据交互。
tracker server:跟踪服务器,主要做调度工作,起负载均衡的作用。在内存中记录集群中所有存储组和存储服务器的状态信息,是客户端和数据服务器交互的枢纽。相比GFS中的master更为精简,不记录文件索引信息,占用的内存量很少。
storage server:存储服务器(又称:存储节点或数据服务器),文件和文件属性(meta data)都保存到存储服务器上。Storage server直接利用OS的文件系统调用管理文件。
客户端上传文件后存储服务器将文件返回给客户端,此文件ID用于以后该文件的索引信息。文件索引信息包括:组名、虚拟磁盘路径、数据两级目录、文件名
tracker根据请求的文件路径即文件ID来快速定义文件。
比如请求下边的文件:
1、通过组名tracker能够快速定位到客户端需要访问的存储服务器组是group1,并选择合适的存储服务器根据客户端访问。
2、存储服务器根据"文件存储虚拟磁盘路径"和"数据文件两级目录"可以很快定位到文件所在目录,并根据文件名找到客户端需要访问的文件
软件名称 | 版本 | 说明 | 下载地址 |
---|---|---|---|
CentOS7 | 7.0+ | 系统,IP为192.168.73.160 | |
FastDFS | 5.11 | 文件系统 | https://github.com/happyfish100/fastdfs/releases |
libfastcommon | 1.0.39 | FastDFS运行所需要的一些基础库 | https://github.com/happyfish100/libfastcommon/releases |
fastdfs-nginx-module | 1.20 | 集成模块 | https://github.com/happyfish100/fastdfs-nginx-module/releases |
nginx | 1.8 | nginx模块 | http://nginx.org/en/download.html |
FastDFS是基于C语言开发的一款文件系统,所以我们需要先安装C语言的环境
[root@localhost ~]# yum -y install gcc-c++
检查gcc-c++是否已经安装
[root@localhost ~]# whereis gcc gcc: /usr/bin/gcc /usr/lib/gcc /usr/libexec/gcc /usr/share/man/man1/gcc.1.gz /usr/share/man/man1/gcc.1.gz
FastDFS依赖libevent库,需要安装:
[root@localhost ~]# yum -y install libevent
libfastcommon是FastDFS官方提供的,libfastcommon包含了FastDFS运行所需要的一些基础库。
[root@localhost ~]# cd /usr/local
[root@localhost local]# tar -zxvf libfastcommon-1.0.39.tar.gz
[root@localhost local]# cd libfastcommon-1.0.7/
[root@localhost libfastcommon-1.0.7]# ./make.sh && ./make.sh install
拷贝文件
[root@localhost libfastcommon-1.0.7]# cd /usr/lib64/
[root@localhost lib64]# cp libfastcommon.so /usr/lib
[root@localhost lib64]# cd /usr/lib
[root@localhost lib]# ls
libfastcommon.so
[root@localhost ~]# cd /usr/local/
[root@localhost local]# tar -zxvf FastDFS_v5.05.tar.gz
[root@localhost local]# cd FastDFS
[root@localhost FastDFS]# ./make.sh && ./make.sh install
将安装目录下的conf文件拷贝到/etc/fdsf/下
[root@localhost conf]# cp /usr/local/FastDFS/conf/* /etc/fdfs/
[root@localhost conf]# cd /etc/fdfs/
[root@localhost fdfs]# ll
总用量 104
-rw-r--r--. 1 root root 23981 1月 9 12:14 anti-steal.jpg
-rw-r--r--. 1 root root 1461 1月 9 12:14 client.conf
-rw-r--r--. 1 root root 1461 1月 9 12:12 client.conf.sample
-rw-r--r--. 1 root root 858 1月 9 12:14 http.conf
-rw-r--r--. 1 root root 31172 1月 9 12:14 mime.types
-rw-r--r--. 1 root root 7829 1月 9 12:14 storage.conf
-rw-r--r--. 1 root root 7829 1月 9 12:12 storage.conf.sample
-rw-r--r--. 1 root root 105 1月 9 12:14 storage_ids.conf
-rw-r--r--. 1 root root 7102 1月 9 12:14 tracker.conf
-rw-r--r--. 1 root root 7102 1月 9 12:12 tracker.conf.sample
1、编辑tracker.conf
[root@localhost ~]# cd /etc/fdfs/
[root@localhost fdfs]# vim tracker.conf
# Tracker 数据和日志目录地址(根目录必须存在,子目录会自动创建)
base_path=/home/FastDFS
# HTTP 服务端口 默认8080 ,建议修改 防止冲突
http.server_port=80
如果提示vim命令无效,请使用
yum -y install vim*
进行安装
2、创建tracker基础数据目录,也是base_path目录
[root@localhost fdfs]# mkdir -p /home/FastDFS
3、防火墙打开跟踪端口22122和80
[root@localhost fdfs]# firewall-cmd --zone=public --add-port=22122/tcp --permanent
success
[root@localhost fdfs]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@localhost fdfs]# firewall-cmd --reload
success
4、启动tracker
[root@localhost fdfs]# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
1、编辑 storage.conf
[root@localhost fdfs]# cd /etc/fdfs/
[root@localhost fdfs]# vim storage.conf
# Storage 数据和日志目录地址(根目录必须存在,子目录会自动生成) (注 :这里不是上传的文件存放的地址,之前版本是的,在某个版本后更改了)
base_path=/home/FastDFS
# 逐一配置 store_path_count 个路径,索引号基于 0。
# 如果不配置 store_path0,那它就和 base_path 对应的路径一样。
store_path0=/home/FastDFS/fdfs_storage
# tracker_server 的列表 ,会主动连接 tracker_server
# 有多个 tracker server 时,每个 tracker server 写一行
tracker_server=192.168.73.160:22122
# tracker_server=192.168.73.161:22122
# 访问端口
http.server_port=80
3、创建Storage基础数据目录,对应base_path目录
# 对应store_path0
[root@localhost fdfs]# mkdir -p /home/FastDFS/fdfs_storage
# 这是配置的store_path0路径,有多个要创建多个
[root@localhost fdfs]# mkdir -p /fastdfs/storage/
4、开通Storage(23000)
[root@localhost fdfs]# firewall-cmd --zone=public --add-port=23000/tcp --permanent
success
[root@localhost fdfs]# firewall-cmd --reload
success
5、启动Storage
[root@localhost fdfs]# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
6、查询monitor来查看storage是否成功绑定
[root@localhost ~]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf
Group 1:
group name = group1
disk total space = 16370 MB
disk free space = 14120 MB
trunk free space = 0 MB
storage server count = 1
active server count = 1
storage server port = 23000
storage HTTP port = 80
store path count = 1
subdir count per path = 256
current write server index = 0
current trunk file id = 0
Storage 1:
id = 192.168.73.160
ip_addr = 192.168.73.160 (localhost.localdomain) ACTIVE
7、查询端口使用情况
[root@localhost sbin]# netstat -apn|grep fdfs
tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 8631/fdfs_trackerd
tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 27679/fdfs_storaged
tcp 0 0 192.168.73.160:49998 192.168.73.160:22122 ESTABLISHED 27679/fdfs_storaged
tcp 0 0 192.168.73.160:22122 192.168.73.160:49998 ESTABLISHED 8631/fdfs_trackerd
如果提示vim命令无效,请使用
yum install net-tools*
进行安装
1、修改Tracker 服务器中的客户端配置文件
[root@localhost fdfs]# vim client.conf
2、修改如下配置即可,其它默认
# Client 的数据和日志目录
base_path=/home/FastDFS
# Tracker端口
tracker_server=192.168.73.160:22122
3、上传测试
在linux内部执行如下命令上传 namei.jpeg 图片
[root@localhost fdfs]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/anti-steal.jpg
group1/M00/00/00/wKhJoF_5MTCAXymNAABdrZgsqUU145.jpg
[root@localhost fdfs]# cd /usr/local
[root@localhost local]# tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
[root@localhost local]# cd fastdfs-nginx-module/src/
修改config文件中的/usr/local/路径为/usr/
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
CORE_LIBS="$CORE_LIBS -L/usr/lib -lfastcommon -lfdfsclient"
将 FastDFS-nginx-module/src 下的 mod_FastDFS.conf 拷贝至/etc/fdfs/下
[root@localhost src]# cp mod_fastdfs.conf /etc/fdfs/
并编辑文件
[root@localhost src]# cd /etc/fdfs/
[root@localhost fdfs]# vim mod_fastdfs.conf
base_path=/home/FastDFS
url_have_group_name = true
store_path0=/home/FastDFS/fdfs_storage
将 libfdfsclient.so 拷贝至/usr/lib 下
[root@localhost fdfs]# cp /usr/lib64/libfdfsclient.so /usr/lib/
创建 nginx/client 目录
[root@localhost fdfs]# mkdir -p /var/temp/nginx/client
[root@localhost fdfs]# cd /usr/local/
[root@localhost local]# tar -zxvf nginx-1.8.1.tar.gz
[root@localhost local]# cd nginx-1.8.1
[root@localhost local]# yum -y install pcre-devel zlib zlib-devel openssl openssl-devel
[root@localhost nginx-1.8.1]# ./configure --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs-nginx-module/src
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@localhost nginx-1.8.1]# make
[root@localhost nginx-1.8.1]# make install
[root@localhost ~]# /usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
server {
listen 80;
server_name 192.168.73.160;
#charset koi8-r;
#access_log logs/host.access.log main;
location /group1/M00/ {
root /home/FastDFS/fdfs_storage/data;
ngx_fastdfs_module;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost ~]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx
http://192.168.73.160/group1/M00/00/00/wKhJoF_5MTCAXymNAABdrZgsqUU145.jpg
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.8.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs-nginx-module/src
[root@localhost sbin]# ./nginx -s stop
[root@localhost sbin]# ./nginx -t
ngx_http_fastdfs_set pid=97921
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
1、Maven坐标文件POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>xc-framework-parent</artifactId>
<groupId>com.xuecheng</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../xc-framework-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-fastdfs</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java -->
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>
2、创建配置文件:fastdfs-client.properties
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.tracker_servers = 192.168.73.160:22122
3、测试
package com.xuecheng.test.fastdfs;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Administrator
* @version 1.0
**/
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestFastDFS {
// 上传文件
@Test
public void updateFile() throws IOException, MyException {
//加载fastdfs-client.properties配置文件
ClientGlobal.initByProperties("config/fastdfs-client.properties");
//定义TrackerClient,用于请求TrackerServer
TrackerClient trackerClient = new TrackerClient();
//连接tracker
TrackerServer trackerServer = trackerClient.getConnection();
//获取Stroage
StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
//创建stroageClient
StorageClient1 storageClient1 = new StorageClient1(trackerServer,storeStorage);
//向stroage服务器上传文件
//本地文件的路径
String filePath = "D://timg.jpg";
//上传成功后拿到文件Id
String fileId = storageClient1.upload_file1(filePath, "jpg", null);
System.out.println(fileId);
//group1/M00/00/00/wKhJoF_5Vb2AWz8NAABUBpueJFU072.jpg
}
// 下载文件
@Test
public void downFile() throws IOException, MyException {
//加载fastdfs-client.properties配置文件
ClientGlobal.initByProperties("config/fastdfs-client.properties");
//定义TrackerClient,用于请求TrackerServer
TrackerClient trackerClient = new TrackerClient();
//连接tracker
TrackerServer trackerServer = trackerClient.getConnection();
//获取Stroage
StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
//创建stroageClient
StorageClient1 storageClient1 = new StorageClient1(trackerServer,storeStorage);
//向stroage服务器下载文件
String fileId = "group1/M00/00/00/wKhJoF_5Vb2AWz8NAABUBpueJFU072.jpg";
byte[] bytes = storageClient1.download_file1(fileId);
// 输出流保存文件
FileOutputStream fileOutputStream = new FileOutputStream(new File("E:/logo.jpg"));
fileOutputStream.write(bytes);
}
// 文件查询
@Test
public void queryFile() throws IOException, MyException {
//加载fastdfs-client.properties配置文件
ClientGlobal.initByProperties("config/fastdfs-client.properties");
//定义TrackerClient,用于请求TrackerServer
TrackerClient trackerClient = new TrackerClient();
//连接tracker
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
FileInfo fileInfo = storageClient.query_file_info("group1", "M00/00/00/wKhJoF_5Vb2AWz8NAABUBpueJFU072.jpg");
System.out.println("文件大小" + fileInfo.getFileSize());//文件大小21510
System.out.println("文件所在IP" + fileInfo.getSourceIpAddr());//文件所在IP192.168.73.160
System.out.println("上传时间" + fileInfo.getCreateTimestamp());//上传时间Sat Jan 09 15:05:33 CST 2021
}
}
标签:junit ror 状态 localhost conf spring sample sbin 关闭
原文地址:https://www.cnblogs.com/xianbeier/p/14255591.html