fastDFS

fastDFS github

seata centos 安装_seata centos 安装

fastDFS介绍:

fastDFS 是以C语言开发的一项开源轻量级分布式文件系统,他对文件进行管理,主要功能有:文件存储,文件同步,
文件访问(文件上传/下载),特别适合以文件为载体的在线服务,如图片网站,视频网站等

安装gcc g++

  • yum -y install gcc
  • yum -y install gcc-c++ libstdc+±devel
  • yum -y install perl* #yum安装perl相关支持)
  • yum -y install cpan #perl需要的程序库,需要cpan的支持
  • yum -y install pcre pcre-devel
  • yum -y install zlib zlib-devel
  • yum -y install openssl openssl-devel
  • yum -y install libevent libevent-devel unzip net-tools wget
测试
  • gcc -v
  • g++ -v
遇到问题 为 repo ‘AppStream’ 下载元数据失败

ping -c 3 www.baidu.com 检查网络是否连通

seata centos 安装_git_02

如果不是网络问题,可执行下面代码

使用这两行代码解决
sudo sed -i -e "s|mirrorlist=|#mirrorlist=|g" /etc/yum.repos.d/CentOS-*
sudo sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g" /etc/yum.repos.d/CentOS-*
安装git
yum 安装 git
yum -y install git

查看 git 版本,查看 git 是否已安装
git --version 
查看git安装位置
which git

seata centos 安装_seata centos 安装_03

第一步安装 libfastcommon

seata centos 安装_dfs_04

下载安装 fastdfs

# step 1. download libfastcommon source codes and install it,
#   github address:  https://github.com/happyfish100/libfastcommon.git
#   gitee address:   https://gitee.com/fastdfs100/libfastcommon.git
# command lines as:

   git clone https://github.com/happyfish100/libfastcommon.git
   cd libfastcommon; git checkout V1.0.56
   ./make.sh clean && ./make.sh && ./make.sh install
   
# step 2. download fastdfs source codes and install it, 
#   github address:  https://github.com/happyfish100/fastdfs.git
#   gitee address:   https://gitee.com/fastdfs100/fastdfs.git
# command lines as:

   git clone https://github.com/happyfish100/fastdfs.git
   cd fastdfs; git checkout V6.08
   ./make.sh clean && ./make.sh && ./make.sh install
   
# step 3. setup the config files
#   the setup script does NOT overwrite existing config files,
#   please feel free to execute this script (take easy :)

./setup.sh /etc/fdfs

# step 4. edit or modify the config files of tracker, storage and client
such as:
 vi /etc/fdfs/tracker.conf
 vi /etc/fdfs/storage.conf
 vi /etc/fdfs/client.conf
 
 修改 base-path 为文件存储路径
 tracker
 base_path=/home/yuqing/fastdfs -> base_path=/var/data/fastdfs/tracker(自定义目录)
 
 创建 base_path 文件目录
 mkdir -p /var/data/fastdfs/tracker
 
 storage
 base_path=/home/yuqing/fastdfs -> base_path=/var/data/fastdfs/storage(自定义目录)
 store_path0=/home/yuqing/fastdfs -> store_path0=/var/data/fastdfs/storage(自定义目录)
 tracker_server=192.168.150.11:22122 -> tracker_server=tracker服务IP:22122
 
 创建 base_path store_path0 文件目录
 mkdir -p /var/data/fastdfs/storage
 mkdir -p /var/data/fastdfs/storage
 
 client
 base_path=/home/yuqing/fastdfs -> base_path=/var/data/fastdfs/client(自定义目录)
 tracker_server=192.168.150.11:22122 -> tracker_server=tracker服务IP:22122
 
 创建 base_path 文件目录
 mkdir -p /var/data/fastdfs/client


查看服务状态
ps -ef | grep fdfs
netstat -unltp |grep fdfs

seata centos 安装_java_05

# (optional) in Linux, you can start fdfs_trackerd and fdfs_storaged as a service:
fdfs_trackerd 先启动,才能启动 fdfs_storaged

service fdfs_trackerd start
service fdfs_storaged start

service fdfs_trackerd restart
service fdfs_storaged restart

service fdfs_trackerd stop
service fdfs_storaged stop

使用客户但上传功能

cd /usr/bin

seata centos 安装_dfs_06


/usr/local/bin/fdfs_upload_file /etc/fdfs/client.conf /要上传的文件

/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /etc/fdfs/t.txt

seata centos 安装_dfs_07

返回结果:group1/M00/00/00/wKgIgGK3Na2AQEcOAAAAAAAAAAA814.txt
  • 虚拟磁盘路径:M00
  • 真实文件路径:00/00
  • 真实文件名:wKgIgGK3Na2AQEcOAAAAAAAAAAA814.txt
  • 组名: group1
  • 对外暴露查找文件名:M00/00/00/wKgIgGK3Na2AQEcOAAAAAAAAAAA814.txt

删除文件

/usr/bin/fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKgIgGK3Na2AQEcOAAAAAAAAAAA814.txt

seata centos 安装_seata centos 安装_08

打开防火墙端口,外部应该调用
firewall-cmd --zone=public --add-port=22122/tcp --permanent
firewall-cmd --zone=public --add-port=23000/tcp --permanent
firewall-cmd --reload

java 使用

fastdfs-client-java 下载源码打包jar包,项目引用jar包
Node +FastDFS 实现文件的上传下载

jar包下载入口

项目代码结构

seata centos 安装_centos_09


引用本地目录 lib 依赖

seata centos 安装_git_10

seata centos 安装_centos_11

resources 目录下 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 = 192.168.8.128:22122
#  ,10.0.11.202:22122,10.0.11.203:22122

fastdfs.connection_pool.enabled = true
fastdfs.connection_pool.max_count_per_entry = 500
fastdfs.connection_pool.max_idle_time = 3600
fastdfs.connection_pool.max_wait_time_in_ms = 1000


#注1:properties 配置文件中属性名跟 conf 配置文件不尽相同,并且统一加前缀"fastdfs.",便于整合到用户项目配置文件
#注2:fastdfs.tracker_servers 配置项不能重复属性名,多个 tracker_server 用逗号","隔开
#注3:除了fastdfs.tracker_servers,其它配置项都是可选的
FDFSUtil 工具类
public class FDFSUtil {

    /**
     * 客户端配置文件
     */
    private static final String txt_filename = "fastdfs-client.properties";

    /**
     * 检查初始化配置信息是否成功
     */
    private static final boolean connFlag;

    /**
     * 加载配置文件
     * @throws MyException
     * @throws IOException
     */
    private static void loadProperties() throws MyException, IOException {
        ClientGlobal.initByProperties(txt_filename);
        System.out.println("loadProperties(): " + ClientGlobal.configInfo());


    }

    static {
        try {
            loadProperties();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 检查 connection 是否成功,尝试2次
        Connection conn = null;
        int retry = 0;
        boolean flag = false;

        while(retry++ < 2) {
            System.out.println("check retry = " + retry);
            try {
                conn = getClient().getTrackerServer().getConnection();
                flag = true;
                break;
            } catch (Exception e) {
            }
        }

        System.out.println("check flag = " + flag);

        connFlag = flag;
        if(connFlag){
            System.out.println("load properties success ... ");
            if (conn != null) {
                try {
                    conn.release();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 上传
     * @param uploadName 上传文件名
     * @param ext_name 扩展名
     */
    public static void upload(String uploadName, String ext_name) {
        Assert.assertTrue(FDFSUtil.connFlag);
        try {
            // 文件描述信息
            NameValuePair nvp[] = new NameValuePair[]{
                    new NameValuePair("age", "18"),
                    new NameValuePair("sex", "male")
            };
            String fileIds[] = getClient().upload_file(uploadName, ext_name, nvp);

            System.out.println(Arrays.toString(fileIds));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

    /**
     * 上传
     * @param bs 上传字节内容
     * @param ext_name 扩展名
     */
    public static void upload(byte[] bs, String ext_name) {
        Assert.assertTrue(FDFSUtil.connFlag);
        try {
            // 文件描述信息
            NameValuePair nvp[] = new NameValuePair[]{
                    new NameValuePair("age", "18"),
                    new NameValuePair("sex", "male")
            };
            String fileIds[] = getClient().upload_file(bs, ext_name, nvp);

            System.out.println(Arrays.toString(fileIds));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

    /**
     * 下载
     * @param group 组
     * @param fdsName fds名
     * @param fileName 指定文件名
     */
    public static void download(String group, String fdsName, String fileName) {
        Assert.assertTrue(FDFSUtil.connFlag);
        try {
            byte[] b = getClient().download_file(group, fdsName);
            System.out.println(b);
            IOUtils.write(b, new FileOutputStream(fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 下载
     * @param group 组
     * @param fdsName fds名
     * @return
     */
    public static byte[] download(String group, String fdsName) {
        Assert.assertTrue(FDFSUtil.connFlag);
        try {
            return getClient().download_file(group, fdsName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取文件信息
     * @param group 组
     * @param fdsName fds名
     */
    public static void getFileInfo(String group, String fdsName) {
        Assert.assertTrue(FDFSUtil.connFlag);
        try {

            FileInfo fi = getClient().get_file_info(group, fdsName);
            System.out.println(fi.getSourceIpAddr());
            System.out.println(fi.getFileSize());
            System.out.println(fi.getCreateTimestamp());
            System.out.println(fi.getCrc32());
            System.out.println(fi.getFetchFromServer());
            System.out.println(fi.getFileType());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取文件 Mate 信息
     * @param group 组
     * @param fdsName fds名
     */
    public static void getFileMate(String group, String fdsName) {
        Assert.assertTrue(FDFSUtil.connFlag);
        try {
            NameValuePair nvps[] = getClient().get_metadata(group, fdsName);
            for (NameValuePair nvp : nvps) {
                System.out.println(nvp.getName() + ":" + nvp.getValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除指定文件
     * @param group
     * @param fdsName
     */
    public static void delete(String group, String fdsName) {
        Assert.assertTrue(FDFSUtil.connFlag);
        try {
            int i = getClient().delete_file(group, fdsName);
            System.out.println(i == 0 ? "删除成功" : "删除失败:" + i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取客户端
     * @return
     * @throws IOException
     */
    private static StorageClient getClient() throws IOException {
        TrackerClient tracker = new TrackerClient();
        TrackerServer trackerServer = tracker.getTrackerServer();

        return new StorageClient(trackerServer, null);
    }
test代码 TestFDFSUtil
public class TestFDFSUtil {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testUpload() {
        FDFSUtil.upload("C:/fastdfs/v2-5228257f988dc59400461a9969833fbc_720w.jpg", "png");
    }

    @Test
    public void testDownload() {
        FDFSUtil.download("group1", "M00/00/00/wKgIgGK1kKmAAMvBAAAAAAAAAAA711.txt",
                "C:/fastdfs/" + UUID.randomUUID() + ".txt");
    }


    @Test
    public void testGetFileInfo() {
        FDFSUtil.getFileInfo("group1", "M00/00/00/wKgIgGK1kKmAAMvBAAAAAAAAAAA711.txt");
    }

    @Test
    public void testGetFileMate() {

        FDFSUtil.getFileMate("group1", "M00/00/00/wKgIgGK3DtKAYJo5AAB4k9qr9Ag951.png");
    }

    @Test
    public void testDelete() {
        FDFSUtil.delete("group1", "M00/00/00/wKgIgGK3Ha6ARe76AAB4k9qr9Ag205.png");
    }

}