preface

Distributed file storage, FastDFS is an option that is distributed and highly available.

The FastDFS distributed file System FastDFS distributed file System FastDFS distributed file System FastDFS distributed file System FastDFS

The outline

  • Install the FastDFS environment
    • preface
    • Install libfastcommon
    • Install FastDFS
    • Configure the FastDFS Tracker
    • Configuring FastDFS Storage
    • File upload Test
  • Cooperate with Nginx
    • The environment required to install Nginx
    • Install Nginx
    • Access to the file
  • FastDFS configures the Nginx module
    • Install and configure the Nginx module
  • Java projects operate FastDFS
    • Upload a file
    • The download file
    • Delete the file

Install the FastDFS environment

preface

The system uses centos7 64-bit, and the IP address is 192.168.211.131

Installing FastDFS requires 4 installation packages, which we uploaded to /usr/local/soft.

cd usr/local
mkdir soft
Copy the code

The installation package

  • Download address: Baidu net disk extraction code: VJPF

Dependencies required by the installation environment

For example, the C compiler: GCC

yum install make cmake gcc gcc-c++
Copy the code

Install libfastcommon

FastDFS relies on the libFastCommon public library, so install it first

Switch to /usr/local/soft and unzip libfastcommon

The tar - ZXVF libfastcommonV1.0.7. Tar. GzCopy the code

Go to the libfastcommon directory and compile and install

CD/usr/local/soft/libfastcommon - 1.0.7 / / / compile/make. Sh / / install/make. Sh installCopy the code

Install FastDFS

Decompress the FastDFS installation package

The tar - ZXVF FastDFS_v5.05. Tar. GzCopy the code

Change the directory to compile and install

CD /usr/local/sof/fastdfs-5.05 // compile./make. Sh // install./makeCopy the code

After installation, directories and files

The service script

In the /etc/init.d/ directory

/etc/init.d/fdfs_storaged
/etc/init.d/fdfs_tracker
Copy the code

The configuration file

The three configuration examples provided by the author are in the /etc/fdfs directory

/etc/fdfs/client.conf.sample
/etc/fdfs/storage.conf.sample
/etc/fdfs/tracker.conf.sample
Copy the code

The command tool

In the /usr/bin directory

fdfs_appender_test
fdfs_appender_test1
fdfs_append_file
fdfs_crc32
fdfs_delete_file
fdfs_download_file
fdfs_file_info
fdfs_monitor
fdfs_storaged
fdfs_test
fdfs_test1
fdfs_trackerd
fdfs_upload_appender
fdfs_upload_file
stop.sh
restart.sh
Copy the code

Configure the FastDFS Tracker

  • Enter the decompressionFastDFSUnzip the directory and enter it againconfDirectory, copyhttp.confandmine.types, the two files arriveetc/fdfsdirectory
CD /usr/local/sof/fastdfs-5.05 // Go to the conf directory CD conf // Copy two files to the etc/ FDFS directory cp http.conf /etc/fdfs cp mine.types /etc/fdfsCopy the code
  • copy/etc/fdfsModify and delete the two sample files in the directory on the desktop. The extension of sample isstorage.confandtracker.conf
/etc/fdfs/storage.conf.sample
/etc/fdfs/tracker.conf.sample
Copy the code

Modify thetracker.conf

Tracker Server, only need to configure 1 place

Change the base_path directory to /opt/ FastDFS /tracker. Log files are generated in this directory after FastDFS is started

Let’s change it to /opt/fastdfs/tracker

Note: This directory must be created manually. If it does not exist, an error will be reported at startup

#base_path=/home/yuqing/fastdfs #base_path=/ opt/fastdfs/trackerCopy the code

Configuring FastDFS Storage

Modify thestorage.conf

Storage Server: Requires three configurations

  1. Modify thebase_pathThe base path, where the log files are stored, is changed to:/opt/fastdfs/storage
Base_path =/home/yuqing/fastdfs /opt/fastdfs/storageCopy the code
  1. Modify thestore_path0, where the real files are stored, change it to:/opt/fastdfs/storage/files
# # store_path0 = / home/yuqing prior to such modification/fastdfs # modified store_path0 = / opt/fastdfs/storage/filesCopy the code
  1. Modify thetracker_server, the value isTracker ServerChange to your Linux IP address
# tracker_server=192.168.209.121:22122 # tracker_server=192.168.211.131:22122Copy the code

Uploading a Configuration File

Conf and tracker.conf to the /etc/fdfs directory of Linux

Start the FastDFS

Create the required folders

As mentioned above, we need to manually create the configured directory, otherwise the startup will report an error

  • Create the Tracker Server directory
CD /opt/ / Base path mkdir /opt/fastdfs mkdir /opt/fastdfs/trackerCopy the code
  • Create a storage Server directory
Based path mkdir/CD/opt / / opt/fastdfs/storage / / file storage path mkdir/opt/fastdfs/storage/filesCopy the code

Start, close, and restart the Tracker Server

/ / syntax: Fdfs_trackerd configuration file path start | stop | restart, if not pass parameters, Conf start // Stop fdfs_trackerd /etc/fdfs/tracker.conf stop // Restart fdfs_trackerd /etc/fdfs/tracker fdfs_trackerd /etc/fdfs/tracker.conf restartCopy the code

Start, stop, or restart the Storage Server

// Syntax fdfs_storaged /etc/ffs/storage. conf start Conf start // Stop fdfs_storaged /etc/fff/storage. conf stop // Restart fdfs_storaged /etc/fff/storage. conf stop // Restart fdfs_storaged fdfs_storaged /etc/fdfs/storage.conf restartCopy the code

Do not run the kill -9 command to forcibly stop the process if it is not necessary. Otherwise, the process will be forcibly stopped during file uploading, which may cause file information synchronization

Check whether the startup is successful

  • Ps command view, can have such output on the right
ps -ef | grep fdfs

root      49467      1  0 11:40 ?        00:00:00 fdfs_trackerd /etc/fdfs/tracker.conf start
root      59332      1  9 11:50 ?        00:00:01 fdfs_storaged /etc/fdfs/storage.conf start
root      59652 113978  0 11:50 pts/0    00:00:00 grep --color=auto fdfs
Copy the code
  • If no, view the log information and start the system again
CD /opt/ CD fastdfs/ / Log file CD storage/ CD logs/ vim storage.logCopy the code

File storage directory

File storage directory: / opt/fastdfs/storage/files/data /, fastdfs will create 256 folders here, there are 256 in the first layer, a total of 2 layer, so a total of 256 * 256 folders, folder is 65536 (60000).

The goal is to sort the files and not concentrate on one folder, which would otherwise be slow to read.

File upload Test

Upload the client. Conf. sample file in /etc/fdfs/to the desktop and remove it

Need to change 2 places!

  1. Modify thebase_pathBase path, which is also created manually!
Base_path =/home/yuqing/fastdfs /opt/fastdfs/clientCopy the code
  1. Modify thetracker_serverTracer address, change to its own Linux IP address
# tracker_server=192.168.0.197:22122 # tracker_server=192.168.211.131:22122Copy the code
  1. Creating a folder
mkdir /opt/fastdfs/client
Copy the code

After modifying the client, pass the client. Conf file back to Linux

Create the file to upload

We create aa.txt in the home directory of the currently logged in user

This is FastDFS test File! This is FastDFS test File!Copy the code

upload

  • Upload command
Grammar: Upload, download, getMeta, setmeta, Delete and query_servers // Upload fdfs_test /etc/fdfs/client.conf upload The file path to be uploadedCopy the code
  • Upload success, output a large amount of information, let’s pick out some useful information
[2020-08-31 12:32:26] DEBUG - base_path=/opt/fastdfs/client, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0 tracker_query_storage_store_list_without_group: Group_name =group1, ip_addr=192.168.211.131, port=23000 group_name=group1, ip_addr=192.168.211.131, port=23000 storage_upload_by_filename group_name=group1, remote_filename=M00/00/00/wKjTg19MfVqAA3YFAAAAGw0ZxeU707.txt source ip address: 192.168.211.131 File TIMESTAMP =2020-08-31 12:32:26 File size=27 file crc32=219792869 Example file URL http://192.168.211.131/group1/M00/00/00/wKjTg19MfVqAA3YFAAAAGw0ZxeU707.txt storage_upload_slave_by_filename group_name=group1, remote_filename=M00/00/00/wKjTg19MfVqAA3YFAAAAGw0ZxeU707_big.txt source ip address: 192.168.211.131 File TIMESTAMP =2020-08-31 12:32:26 File size=27 file crc32=219792869 Example file URL http://192.168.211.131/group1/M00/00/00/wKjTg19MfVqAA3YFAAAAGw0ZxeU707_big.txtCopy the code
  1. Group_name, the name of the group to which the file is uploaded, determines the machine to which the file is uploaded
  2. Remote_filename, the name of the remote file that determines the disk directory to which the file is stored
  • Looking back,/etc/fdfs/In the directoryclient.infofile

There are two attributes to note: store_path_count and store_path

  1. Store_path_count, which is how many disks to store, usually we only have one disk, so it’s 1, unless you have multiple disks, for example, 2, change it to 2
  2. Store_path: indicates the location where files are stored on local disks. It corresponds to the number of disksstore_path. For example,store_path0Is the storage path of the first disk,store_path1Is the storage path of the second disk
  • Take a look at the output

Decomposition: remote_filename = M00/00/00 / wKjTg19MfVqAA3YFAAAAGw0ZxeU707. TXT

  1. M00 indicates the number of disks. The values start from 0. For example, if there is only one disk, the values are M00. If you have two pieces, it could be M00 or M01
  2. 00/00, that is, the 60,000 directories, only the second directory is the storage of files
  3. WKjTg19MfVqAA3YFAAAAGw0ZxeU707. TXT, upload the file name

Note that FastDFS will add the data/ directory to our configured storage path and then store it.

With the above information, M00/00/00 / wKjTg19MfVqAA3YFAAAAGw0ZxeU707. TXT, The corresponding path to/opt/fastdfs/storage/files/data / 00/00 / wKjTg19MfVqAA3YFAAAAGw0ZxeU707. Text

We went to the directory, looked at the files, and found four files

  1. WKjTg19MfVqAA3YFAAAAGw0ZxeU707_big. TXT, because we are testing commands, plus _big suffix is backup files, and the followingwKjTg19MfVqAA3YFAAAAGw0ZxeU707.txt, the content is exactly the same, official use, will not have the file!
  2. WKjTg19MfVqAA3YFAAAAGw0ZxeU707_big. TXT -m, meta data of the file information, recorded the extension of some documents, such as file size information
  3. WKjTg19MfVqAA3YFAAAAGw0ZxeU707. TXT, upload files, real we uploaded files is aa. TXT, upload to come will be FastDFS renaming, directory is to prevent multiple users to upload the same file name, lead to conflict
  4. WKjTg19MfVqAA3YFAAAAGw0ZxeU707. TXT -m, file attributes, record file extension, the information such as size, height, width, because it is a test command, to generate the file. Normally this information is stored in a database, not in this file
  • The file to view

The information above, there is a Url: example file Url: http://192.168.211.131/group1/M00/00/00/wKjTg19MfVqAA3YFAAAAGw0ZxeU707.txt

By default this file is not directly accessible, we need to configure later to access!

File download and delete

download

/ / command: Fdfs_test Configuration file Path Download Group name Remote file name // For example, So we just uploaded files fdfs_test/etc/FDFS/client. The conf download group1 M00/00/00 / wKjTg19MfVqAA3YFAAAAGw0ZxeU707. TXTCopy the code

If the following information is displayed, the download is successful

download file success, file size=27, file save to wKjTg19MfVqAA3YFAAAAGw0ZxeU707.txt
Copy the code

In the current folder, just one more wKjTg19MfVqAA3YFAAAAGw0ZxeU707. TXT file

Delete the file

/ / command: Fdfs_test Configuration file path delete Group name Remote file name // For example, delete, We have just uploaded files fdfs_test/etc/FDFS/client. The conf delete group1 M00/00/00 / wKjTg19MfVqAA3YFAAAAGw0ZxeU707. TXTCopy the code

If the following information is deleted, the deletion is successful

delete file success
Copy the code

At this time, we’ll check the file upload directory, / opt/fastdfs/storage/files/data / 00/00, you will find file is deleted

Cooperate with Nginx

If we want to access the address, we need to cooperate with Nginx. In addition to installing Nginx, you also need to install an Nginx extension module provided by FastDFS.

Upload the Nginx and Nginx extension modules to the usr/local/soft directory.

//nginx nginx-1.12.1.tar.gz // Fastdfs nginx extension module fastdfS-nginx-module_v1.16.tar. gzCopy the code

Decompress the two installation packages

Gz // Decompress the extension module tar -zxvf fastdfs-nginx-module_v1.16.tar.gzCopy the code

Go to the fastdfs-nginx-module directory and then go to the SRC directory inside

cd fastdfs-nginx-module/
cd src/
Copy the code

Download the mod_fastdfs.conf configuration file to your desktop and copy the full SRC path for nginx configuration.

// Obtain the current directory path PWD // Run /usr/local/sof/fastdfs-nginx-module/SRCCopy the code

Install nginx

The above decompression of nginx, have done, will not repeat

Configure nginx

CD nginx-1.12.1 // Configure the nginx installation path. --prefix indicates the nginx installation path. --add-module indicates the path to add a module to nginx. /usr/local/soft/fastdfs-nginx-module/ SRC // If your module path is changed --add-module, /configure --prefix=/usr/local/nginx_fdfs --add-module=/usr/local/soft/fastdfs-nginx-module/ SRC // make // install make installCopy the code

Note: If you appear make: *** in make there is no rule to create the target “build” required by “default”

Pcre-devel, zlib-devel, openSSL-devel may also be missing

yum install pcre-devel zlib zlib-devel openssl openssl-devel
Copy the code

Note: if you get a fatal error while making: fdfs_define. H: there is no file or directory, then you need to create a soft link for 3 files and then execute make

ln -sv /usr/include/fastcommon /usr/local/include/fastcommon 
ln -sv /usr/include/fastdfs /usr/local/include/fastdfs 
ln -sv /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
Copy the code

Configure the nginx extension module

Go to the fastdfs-nginx-module directory and then go to the SRC directory inside

cd fastdfs-nginx-module/
cd src/
Copy the code

Download the mod_fastdfs.conf configuration file to your desktop.

  1. Modify the base path, this directory, we also have to manually create!
# change base_path=/opt/fastdfs/nginx_modCopy the code
  1. Specify the address of the tracker server and change it to your address
# 修改前
tracker_server=tracker:22122

# 修改后
tracker_server=192.168.211.131:22122
Copy the code
  1. The request to modify the URL must contain the group name, which must be changed to true for use in the regular expression judged later by nginx interception
# change url_have_group_name = false # change url_have_group_name = trueCopy the code
  1. Specify how many disk storage paths, you have several write several, generally only 1
store_path_count=1
Copy the code
  1. Specify the file storage path
Before # modified store_path0 = / home/yuqing/fastdfs # modified store_path0 = / opt/fastdfs/storage/filesCopy the code
  1. (Major) Upload the mod_fastdfs.conf file to /etc/fdfs.conf

  2. (Important) Create the /opt/fastdfs/nginx_mod directory, otherwise nginx will not start

mkdir /opt/fastdfs/nginx_mod
Copy the code

Configure nginx

Switch to the Nginx directory of FDFS and then go to the conf configuration directory

CD /usr/local/nginx_fdfs/ / Go to the configuration directory CD conf/Copy the code

Conf file, nginx configuration file, upload to the desktop for editing, we configure a regular expression forwarding rule under the server node

server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } # {ngx_fastdfs_module;} # {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; }}Copy the code

Finally, upload this file to the /usr/local/nginx_fdfs/conf directory

Start the nginx

/ / -t parameters, Check whether the syntax of the configuration file is incorrect. /usr/local/nginx_fdfs/sbin/nginx -c /usr/local/nginx_fdfs/conf/nginx.conf -t // The configuration file is started /usr/local/nginx_fdfs/sbin/nginx -c /usr/local/nginx_fdfs/conf/nginx.confCopy the code

If the following information is displayed, the system succeeds

ngx_http_fastdfs_set pid=115915
nginx: the configuration file /usr/local/nginx_fdfs/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx_fdfs/conf/nginx.conf test is successful
Copy the code

Check whether the process is started successfully. The output has two processes, one master and one worker, so it is successful. If the worker process is not started, check the log

Ps - ef | grep nginx/root/output 117133 1 0 14:45? 00:00:00 nginx: master process /usr/local/nginx_fdfs/sbin/nginx -c /usr/local/nginx_fdfs/conf/nginx.conf nobody 117134 117133 0 14:45 ? 00:00:00 nginx: worker process root 120415 113978 0 14:48 pts/0 00:00:00 grep --color=auto nginxCopy the code

Go to the nginx directory, where there is a log directory, where log files are stored

CD /usr/local/nginx_fdfs // Go to the log directory CD logs/ /ls and run access.log error.log nginx.pidCopy the code

If no file exists in the directory, go to /opt/fastdfs/ and go to the nginx_mod directory to check whether the log file exists

CD /opt/fastdfs/ / Go to the extension plug-in directory CD nginx_mod/ // and check whether the file ll existsCopy the code

It usually doesn’t work for two reasons

  1. mod_fastdfs.confFile, not in/etc/fdfsGo to the catalog!
  2. If I do, butmod_fastdfs.confThere’s an error in the file. A common error isbase_pathThe base path of this directory is not created, must create their own mkdir manually.

Java projects operate FastDFS

The FastDFS Java client does not upload Maven’s central repository, so it can only be downloaded from Github and packaged into a local repository.

To download it, I chose V1.27.

Pack to local storage

After the download is complete, decompress, go to the decompression directory, and execute the cleaning and packaging commands (install Maven environment first, if not, you can directly open in Idea, and then package).

mvn clean install
Copy the code

Java Construction Project

Create a normal Maven-managed Java project with the following dependencies

<dependencies> <! -- Introducing the fast DFS Java client --> <! This dependency is not in the Maven central library, so you need to compile the source code yourself. --> <dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> The < version > 1.27 - the SNAPSHOT < / version > < / dependency > < / dependencies >Copy the code

In the resources directory, create a fastdfs.conf file as the fastdfs configuration file

And configure your Tracker server address

// Set the tracker_server to 192.168.211.131:22122Copy the code

Create a New FastDFS utility class that encapsulates the upload, download, and delete functionality for some files

Public class FastDFSUtil {/** * file upload */ public static String[] upload(String localFileName) trackerServer = null; StorageServer storageServer = null; Clientglobal.init ("fastdfs.conf"); TrackerClient trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageServer = trackerClient.getStoreStorage(trackerServer); // Define the client object of the Storage, This object is used to upload, download, and delete files. StorageClient StorageClient = New StorageClient(trackerServer, storageServer); // Upload, parameter 1: the absolute path of the file to be uploaded, parameter 2: the extension of the file to be uploaded, parameter 3: the property of the file file, usually not uploaded // Return a String array, this data is very useful for us, must be taken care of, it is recommended to save to the database // return the result array: Return storageclient.upload_file (localFileName, "PNG ", null); return storageclient.upload_file (localFileName," PNG ", null). } catch (IOException | MyException e) { e.printStackTrace(); } finally {// Close the resource try {if (storageServer! = null) { storageServer.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (trackerServer ! = null) { trackerServer.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; } /** * download ** @param groupName groupName * @param remoteFileName name of the remote file * @param saveFileName name of the saved file * @return returns whether the file is downloaded successfully */ public static boolean download(String groupName, String remoteFileName, String saveFileName) { TrackerServer trackerServer = null; StorageServer storageServer = null; Clientglobal.init ("fastdfs.conf"); TrackerClient trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageServer = trackerClient.getStoreStorage(trackerServer); // Define the client object of the Storage, This object is used to upload, download, and delete files. StorageClient StorageClient = New StorageClient(trackerServer, storageServer); // File download, parameter 1: group name of file, parameter 2: remote file name, parameter 3: Int code = storageclient. download_file(groupName, remoteFileName, saveFileName); // If 0 is returned, the file is downloaded successfully. return code == 0; } catch (IOException | MyException e) { e.printStackTrace(); } finally {// Close the resource try {if (storageServer! = null) { storageServer.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (trackerServer ! = null) { trackerServer.close(); } } catch (IOException e) { e.printStackTrace(); } } return false; } public static Boolean delete(String groupName, String groupName, String groupName, String groupName, String groupName, String groupName, String groupName) String remoteFileName) { TrackerServer trackerServer = null; StorageServer storageServer = null; Clientglobal.init ("fastdfs.conf"); TrackerClient trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageServer = trackerClient.getStoreStorage(trackerServer); // Define the client object of the Storage, This object is used to upload, download, and delete files. StorageClient StorageClient = New StorageClient(trackerServer, storageServer); Int code = storageclient.delete_file (groupName, remoteFileName); // Delete a file. Parameter 1: name of the file group to be deleted. Parameter 2: remote name of the file. return code == 0; } catch (IOException | MyException e) { e.printStackTrace(); } finally {// Close the resource try {if (storageServer! = null) { storageServer.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (trackerServer ! = null) { trackerServer.close(); } } catch (IOException e) { e.printStackTrace(); } } return false; }}Copy the code

Provide a class that executes the main() method by calling the utility class method test

public class Main { public static void main(String[] args) { //upload(); //download(); delete(); } /** * upload file */ private static void upload() {String[] results = FastDFSUtil. Upload ("/Users/wally/Desktop/Code/Java/fastdfs - Java/SRC/main/resources/fastdfs named parsing. PNG "); if (results ! = null && results.length == 2) { String group = results[0]; String remoteFileName = results[1]; System. The out. Println (" group: "+ group); System.out.println("remoteFileName: "+ remoteFileName); System.out.println(" link address: http://192.168.211.131/" + group + "/" + remoteFileName); }} /** * download files */ private static void download() {result = fastdfsutil. download("group1", "M00/00/00/wKjTg19Ms0iAFnpDAAAmdb_zhmw098.png", "a.png"); If (result) {system.out.println (" File downloaded successfully!" ); } else {system.out.println (" File download failed!" ); Public static void delete() {Boolean result = fastdfsutil. delete("group1", "M00/00/00/wKjTg19Ms0iAFnpDAAAmdb_zhmw098.png"); If (result) {system.out.println (" File deleted successfully!" ); } else {system.out.println (" File deletion failed!" ); }}}Copy the code

reference

Build file management system with FastDFS (with video and execute commands)

FastDFS file server on Linux