Distributed file server entry to the actual FastDFS

What is FastDFS

FastDFS is written in c language an open source distributed file system. FastDFS tailor-made for the Internet, full account of redundancy, load balancing, and other mechanisms linear expansion, and focus on high availability, high performance and other indicators, it is easy to use FastDFS to build a high-performance file server clusters to provide file upload and download and other services.

FastDFS architecture includes Tracker server and Storage server. Tracker server client requests for file upload, download, upload and download files finalized by the Storage server by Tracker server scheduling.

Tracker server role is load balancing and scheduling can be found Storage server to provide file upload service to Tracker server when the file is uploaded, according to some policies. tracker track server or may be referred to dispatch server.

Storage server role is a file stored on Storage server, Storageserver does not implement its own file system client upload the final file storage but to use the operating system's file system to manage files. Storage can be referred to as a storage server.
Here Insert Picture Description
Two server roles:

  1. Tracker: Management cluster, tracker can also be achieved clusters. Each tracker node equal footing. Collecting state Storage cluster.
  2. Storage: Storage actually saving the file
    into a plurality of groups, each group of stored files between different. Within each group can have more than one member, save a member of the internal group content is the same, the status of group members is the same, there is no concept of master and slave.

File upload and download process

File upload process

Here Insert Picture Description
After the client upload a file server to store the file ID is returned to the client, this ID file for later access index information for the file. File index information includes: group name, virtual disk paths, two data directory, file name.
Here Insert Picture Description

  • Group Name: storage group after the name of the file upload, file upload after the successful return has storage server, the client needs to save itself.
  • Virtual disk path: storage configured virtual path, and disk options store_path * correspondence. If you configure store_path0 is
    M00, if you configure store_path1 is M01, and so on.
  • Data two directories: storage server to create two directories in each virtual disk paths for storing data files.
  • Different upload and file: the file name. Is based on specific information generated by the storage server, the file name contains: source storage server IP
    address, file creation time stamp, file size, the random number and filename extension and other information.

File download process

Here Insert Picture Description

The simplest architecture FastDFS

Here Insert Picture Description

Getting small FastDFS Demo

(1) create a Maven project fastDFSdemo
due FastDFS client jar package and not in a central repository, so need to use the following command to manually install jar package to the local Maven repository (the jar package into the d drive setup directory).

mvn install:install-file -DgroupId=org.csource.fastdfs -DartifactId=fastdfs  -Dversion=1.2 -Dpackaging=jar -Dfile=d:\setup\fastdfs_client_v1.20.jar

pom.xml introduced

  	<dependency>
	    <groupId>org.csource.fastdfs</groupId>
	    <artifactId>fastdfs</artifactId>
	    <version>1.2</version>
	</dependency>

(2) add a profile fdfs_client.conf, wherein the server address to 192.168.25.133

# connect timeout in seconds
# default value is 30s
connect_timeout=30

# network timeout in seconds
# default value is 30s
network_timeout=60

# the base path to store log files
base_path=/home/fastdfs

# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=192.168.25.133:22122

#standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info

# if use connection pool
# default value is false
# since V4.05
use_connection_pool = false

# connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
# since V4.05
connection_pool_max_idle_time = 3600

# if load FastDFS parameters from tracker server
# since V4.05
# default value is false
load_fdfs_parameters_from_tracker=false

# if use storage ID instead of IP address
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# default value is false
# since V4.05
use_storage_id = false

# specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V4.05
storage_ids_filename = storage_ids.conf


#HTTP settings
http.tracker_server_port=80

#use "#include" directive to include HTTP other settiongs
##include http.conf


(3) Create java class, main method code is as follows:

   // 1、加载配置文件,配置文件中的内容就是 tracker 服务的地址。
		ClientGlobal.init("D:/maven_work/fastDFS-demo/src/fdfs_client.conf");
		// 2、创建一个 TrackerClient 对象。直接 new 一个。
		TrackerClient trackerClient = new TrackerClient();
		// 3、使用 TrackerClient 对象创建连接,获得一个 TrackerServer 对象。
		TrackerServer trackerServer = trackerClient.getConnection();
		// 4、创建一个 StorageServer 的引用,值为 null
		StorageServer storageServer = null;
		// 5、创建一个 StorageClient 对象,需要两个参数 TrackerServer 对象、StorageServer 的引用
		StorageClient storageClient = new StorageClient(trackerServer, storageServer);
		// 6、使用 StorageClient 对象上传图片。
		//扩展名不带“.”
		String[] strings = storageClient.upload_file("D:/pic/benchi.jpg", "jpg",
				null);
		// 7、返回数组。包含组名和图片的路径。
		for (String string : strings) {
			System.out.println(string);
		}

Console output the following results:

group1
M00/00/00/wKgZhV4r9uSAZvkZAAaZOWtmGtU020.jpg

In the browser input:
http://192.168.25.133/group1/M00/00/00/wKgZhV4r9uSAZvkZAAaZOWtmGtU020.jpg
Here Insert Picture Description

Published 49 original articles · won praise 32 · views 2149

Guess you like

Origin blog.csdn.net/qq_43255017/article/details/104083981