Ubantu/centos安装、配置fastdfs以及fastdfs使用方法

2019-10-14
#######################################**

ubantu安装

**#####################################################
1.ubantu不需要安装gcc,直接把jar包FastDFS_v5.05.tar.gz和libfastcommonV1.0.7.tar.gz上传到服务器
指定文件夹(我放在:/home/p-s-13/fastdfs)
2.解压libfastcommonV1.0.7.tar.gz,命令为:tar -zxvf libfastcommonV1.0.7.tar.gz
3.安装libfastcommon依赖,进入libfastcommon目录,依次执行脚本:
./make.sh 和 ./make.sh install
4.查看服务器是多少位:uname -m
结果:x86_64,说明是64位
5.设置环境变量和软链接,在32位ubuntu中,libfastcommon会安装在/usr/lib 中,
64位系统则安装在 /usr/lib64 中。依次执行以下命令:(根据自己的操作系统选择路径)
32位执行:
export LD_LIBRARY_PATH=/usr/lib/
ln -s /usr/lib/libfastcommon.so /usr/local/lib/libfastcommon.so
64位执行:
export LD_LIBRARY_PATH=/usr/lib64/
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
6.解压并安装FastDFS:在放压缩包的目录(我的在/home/p-s-13/fastdfs的)执行:

tar -zxvf FastDFS_v5.05.tar.gz

7.安装:进入解压后的目录,依次执行:
./make.sh

./make.sh install
8.修改配置文件,在默认安装路径 /etc/fdfs 下,有三个示例配置文件
client.conf/storage.conf/tracker.conf三个,如果没有就复制对应的xxx.conf.sample去掉sample就是xxx.conf了
9.修改:
#base_path:工作文件夹,日志存在此
#http.tracker_server_port=8080 HTTP访问设置

tracker.conf需要修改:
文件夹路径根据自己需要修改(先要看看/home/p-s-13/fastdfs/log是否存在,不存在用mkdir创建)
base_path=/home/p-s-13/fastdfs/log(这是我的路径,你们根据自己需要修改)
store_group=group1
http.server_port=8090

#note:
#只有http.disabled=false时才生效
#http.server_port=8090
(下同)

storage.conf 文件修改:
文件夹路径根据自己需要修改(先要看看/home/p-s-13/fastdfs/storage是否存在,不存在用mkdir创建)
group_name=group1(与tracker.conf保持一致)
store_path0=/home/p-s-13/fastdfs/storage(文件的实际存储路径)
base_path=/home/p-s-13/fastdfs/storage(日志文件)
tracker_server=10.1.0.38:22122(自己的服务器ip)
http.server_port=8090
修改 client.conf 文件:
文件夹路径根据自己需要修改(先要看看/home/p-s-13/fastdfs/client是否存在,不存在用mkdir创建)
base_path=/home/p-s-13/fastdfs/client
tracker_server=10.1.0.38:22122
http.tracker_server_port=8090

10.启动服务
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf

11.查看tracker和storage是不是在通信:
fdfs_monitor /etc/fdfs/storage.conf
如下提示,出现ACTIVE,表示二者均正常启动

################################### **

centos安装

**#########################################

1.准备两个压缩包(与ubantu一样)
FastDFS_v5.05.tar.gz
libfastcommonV1.0.7.tar.gz
我的已下载到百度网盘
2.安装gcc(与ubantu不一样)
检查linux系统是否有安装GCC
gcc –version
提示未安装就去安装。
安装GCC
yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
yum -y install wget httpd-tools vim
就把gcc当成c语言编译器, g++当成c++语言编译器用就是了.(知乎
3.安装libfastcommon
进入libfastcommon压缩包目录解压,编译,安装

tar -zxvf libfastcommonV1.0.7.tar.gz
进入解压的libfastcommon-1.0.7目录,编译
./make.sh
安装

./make.sh install
最后,libfastcommon.so文件到usr/lib下 ,
请按照自己的实际文件路径修改被复制路径(/data/fastDFS/software需要查看自己的服务器路径,不要复制)

cp /data/fastDFS/software/libfastcommon-1.0.7/src/libfastcommon.so /usr/lib

4.安装FastDFS
解压FastDFS_v5.05.tar.gz,然后编译安装FastDFS

tar -zxvf FastDFS_v5.05.tar.gz
./make.sh
./make.sh install
安装成功后将目录conf内的文件拷贝到/etc/fdfs目录下:
(/data/fastDFS/software需要查看自己的服务器路径,不要复制)
cp /data/fastDFS/software/FastDFS/conf/* /etc/fdfs/

5.安装 tracker
/home/logs/fastdfs目录如果不存在,就创建一个,可用在home目录mkdir logs在进入logs 执行mkdir fastdfs 。

进入/etc/fdfs目录,修改tracker.conf文件。如果不存在,就拷贝tracker.conf.sample文件为tracker.conf,然后再修改:

把base_path=/home/yuqing/fastdfs 修改为: base_path=/home/logs/fastdfs (注解:日志文件)

http.server_port=8080 修改为: http.server_port=8090 (注解:端口,80是方便默认,只有http.disabled=false时才生效)

store_group=group1 (注解:组名)

之后,就启动tracker,并查看是启动成功(出现如下提示,表示启动成功):restart重启

fdfs_trackerd /etc/fdfs/tracker.conf start
netstat -unltp | grep tracker

6.安装storage
如果/home/logs/storage 和/home/data/storage 不存在,就创建该目录mkdir ………………
由于storage和tracker运行的都是fastDFS程序,每一台服务器都部署一台fasfDFS,一台服务器是tracker,一台是storage

/etc/fdfs,修改storage.conf文件。如果不存在,就拷贝storage.conf.sample文件为storage.conf,然后再修改:

复制代码
base_path=/home/yuqing/fastdfs 修改为: base_path=/home/logs/storage (日志路径)

store_path0=/home/yuqing/fastdfs 修改为: store_path0=/home/data/storage (实际储存文件路径,可以配置多个)

tracker_server=192.168.209.121:22122 修改为: tracker_server=192.168.61.147:22122 (连接tracker服务器地址)
注:192.168.61.147:22122是连接tracker服务器地址,也就是tracker.conf所在服务器的host,我这里是装在一台机器上的。

group_name=group1 (必须和tracker的组名相同)

http.server_port=8090 (这个端口也要改)
复制代码
其中,

然后,就启动storage,并查看是否成功(出现如下提示,表示启动成功):

fdfs_storaged /etc/fdfs/storage.conf start
netstat -unltp | grep storage

7.查看tracker和storage是不是在通信:

fdfs_monitor /etc/fdfs/storage.conf
如下提示,出现 ACTIVE,表示二者均正常启动,至此就可以进行上传文件测试了。

########################### ***

fastdfs的使用


#########################################
1.导包(也可以下载fastdfs-client-java-master-master,使用mvn clean install 打一个本地jar包)这里去掉了<>、/,读者自己加上
dependency
groupId cn.bestwu groupId
artifactId fastdfs-client-java artifactId
version 1.27 version
dependency
2.配置(把这段配置到application.yml)
fastdfs:
tracker_servers: 10.1.0.38:22122
connect_timeout_in_seconds: 2
network_timeout_in_seconds: 30
charset: UTF-8
http_anti_steal_token: no
http_secret_key: FastDFS1234567890
http_tracker_http_port: 22122
3.写配置类FastDfsConfig
@Component
public class FastDfsConfig {
private static String connectTime;
private static String netWorkTime;
private static String trackerServers;
private static String charset;
private static String token;
private static String secretKey;
private static String port;
@Value("${fastdfs.connect_timeout_in_seconds}")
public void setConnectTime(String connectTimes) {
connectTime = connectTimes;
}

@Value("${fastdfs.network_timeout_in_seconds}")
public void setNetWorkTime(String netWorkTimes) {
    netWorkTime = netWorkTimes;
}

@Value("${fastdfs.tracker_servers}")
public void setTrackerServers(String trackerServerss) {
    trackerServers = trackerServerss;
}

@Value("${fastdfs.charset}")
public void setCharset(String charsets) {
    charset = charsets;
}

@Value("${fastdfs.http_anti_steal_token}")
public void setToken(String tokens) {
    token = tokens;
}

@Value("${fastdfs.http_secret_key}")
public void setSecretKey(String secretKeys) {
    secretKey = secretKeys;
}

@Value("${fastdfs.http_tracker_http_port}")
public void setPort(String ports) {
    port = ports;
}

public static String getConnectTime() {
    return connectTime;
}

public static String getNetWorkTime() {
    return netWorkTime;
}

public static String getTrackerServers() {
    return trackerServers;
}

public static String getCharset() {
    return charset;
}

public static String getToken() {
    return token;
}

public static String getSecretKey() {
    return secretKey;
}

public static String getPort() {
    return port;
}
}

4.写工具类FileManager
public class FileManager {

private static ConcurrentLinkedQueue<StorageClient> queue = new ConcurrentLinkedQueue();
static {
    try {
        Properties properties = new Properties();
        properties.setProperty("fastdfs.tracker_servers", FastDfsConfig.getTrackerServers());
        properties.setProperty("fastdfs.connect_timeout_in_seconds", FastDfsConfig.getConnectTime());
        properties.setProperty("fastdfs.network_timeout_in_seconds", FastDfsConfig.getNetWorkTime());
        properties.setProperty("fastdfs.charset", FastDfsConfig.getCharset());
        properties.setProperty("fastdfs.http_anti_steal_token", FastDfsConfig.getToken());
        properties.setProperty("fastdfs.http_secret_key", FastDfsConfig.getSecretKey());
        properties.setProperty("fastdfs.http_tracker_http_port", FastDfsConfig.getPort());

        ClientGlobal.initByProperties(properties);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static StorageClient getStorageClient(){
    StorageClient storageClient = queue.poll();
    return storageClient == null ? new StorageClient() : storageClient;
}

private static void release(StorageClient storageClient){
    queue.offer(storageClient);
}


public static String upload(byte[] content) {
    String[] uploadResults = null;
    StorageClient storageClient = getStorageClient();
    try {
        uploadResults =  storageClient.upload_file("group1",content,null,null);
        release(storageClient);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (MyException e) {
        e.printStackTrace();
    }
    return uploadResults[1];
}


public static byte[] download(String remoteFilename){
    byte[] group1s = null;
    StorageClient storageClient = getStorageClient();
    try {
        group1s = storageClient.download_file("group1", remoteFilename);
        release(storageClient);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return group1s;
}

public static void delete(String remoteFilename){
    StorageClient storageClient = getStorageClient();
    try {
        int group1s = storageClient.delete_file("group1", remoteFilename);
        release(storageClient);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}
5.使用:这里就是可以调用增删查了
/**
* 上传
*
* @param file
*/

   @RequestMapping("/upload")
    public void uploadFile(@RequestParam MultipartFile file) {
        log.info("……uploadFile………start………");
        String upload = null;
        try {
            upload = FileManager.upload(file.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(upload);
    }
/**
 * 下载
 */
   @RequestMapping("/download")
    public void downloadFile(String filePath, HttpServletResponse response) {
        log.info("…downloadFile………start……");
        InputStream ins = null;
        OutputStream bos = null;
        try {
            byte[] fileByte = FileManager.download(filePath);
            ins = new ByteArrayInputStream(fileByte);
            byte[] buffer = new byte[ins.available()];
            ins.read(buffer);
            ins.close();

            response.reset();

            response.setCharacterEncoding("utf-8");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment; filename=");

            bos = new BufferedOutputStream(response.getOutputStream());
            bos.write(buffer);

            bos.flush();
            bos.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 /**
 * 删除
 */

@RequestMapping("/deleteFile")
public void deleteFile(String filePath){
log.info("……deleteFile………start………");
try {
FileManager.delete(filePath);
}catch (Exception e){
e.printStackTrace();
}
System.out.println(“删除成功!”);

}

判断远程服务器上的图片是否存在?
在执行删除的时候,总先要确定图片存在远程服务器再删除,否则会报错!
(注意:File file = new File(url);
boolean isexist = file.exist();
这个方法只能判断本地图片是否存在
)
方法一:

  String remotePicture = "http://"+FastDfsConfig.getTrackerServers().split(":")[0]+"/group1/"+oldLogoPath;

            URL serverUrl = new URL(remotePicture);

        String message = urlcon.getHeaderField(0);
        if (StringUtils.hasText(message) && message.startsWith("HTTP/1.1 404")) {
            System.out.println("不存在");
        }else{
            System.out.println("存在"+message);
        }
            
        方法二:
   String remotePicture = "http://"+FastDfsConfig.getTrackerServers().split(":")[0]+"/group1/"+oldLogoPath;

            URL serverUrl = new URL(remotePicture);
        HttpURLConnection urlcon2 = (HttpURLConnection) url.openConnection();
        Long TotalSize=Long.parseLong(urlcon2.getHeaderField("Content-Length"));  
        if (TotalSize>0){
            System.out.println("存在");
            
        }else{
            System.out.println("不存在");
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神雕大侠mu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值