FastDFS is a lightweight, high-performance open source distributed file system developed by Mr. Yu Qing from Taobao. Developed in pure C language, with rich functions:
File storage
File synchronization
File access (upload, download)
Access load balancing
Online expansion
Architecture diagram
Upload and download process
Installation process:
1. Realize image upload
1.1. Build the project (rebuild the ly-upload microservice)
1.1.1. Create module
1.1.2. Dependency
We need EurekaClient and web dependencies:
1.1.3. Write configuration
1.1.4. Startup class
1.2. Write upload function
1.2.1.controller
There are 4 things you need to know to write a controller:
Request method: upload must be POST
Request path: /upload/image
Request parameter: file, the parameter name is file, SpringMVC will be encapsulated as an interface: MultipleFile
Return result: the url path of the file obtained after the upload is successful
1.2.2.service
In the process of uploading files, we need to verify the uploaded content:
Verify file size
Verify the media type of the file
Verify the contents of the file
The file size is set in the Spring configuration file, so it will already be verified, so we don't care.
There is a question here: Why does the image address need to use another url? ,
Pictures cannot be saved inside the server, which will cause additional loading burden on the server
Generally static resources should use independent domain names, so that unnecessary cookies will not be carried when accessing static resources, reducing the amount of requested data
1.2.3. Test upload
1.2.4. Bypass the gateway
Image upload is the transmission of files. If it also passes through the proxy of the Zuul gateway, the files will be transmitted through the network multiple times, causing unnecessary network burdens. In high concurrency, it may cause network congestion and the Zuul gateway is unavailable. In this way, our entire system is paralyzed.
Therefore, our request to upload files is not processed by the gateway.
1.2.4.1. Zuul's route filtering
Zuul provides an ignored-patterns attribute to ignore URL paths that you don’t want to be routed. Example:
1.2.4.2. Nginx rewrite instruction
1.2.5. Cross-domain issues
But fortunately, this error is not the first time I have seen it, a cross-domain issue.
We can add a CorsFilter to upload-service:
1.2.6. Defects uploaded before
Think about it first, is there any problem with the upload function before?
There is no problem with the upload itself. The problem lies in the way the file is saved. We save the file on the server machine, and there will be the following problems:
Single machine storage, limited storage capacity
Unable to expand horizontally, because the files of multiple machines cannot be shared, and there will be situations where they cannot be accessed
Data is not backed up, there is a single point of failure risk
Poor concurrency
At this time, it is best to use distributed file storage instead of local file storage.