SpringBoot集成FastDFS+Nginx整合基於Token的防盜鏈

爲什麼要用SpringBoot?

SpringBoot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成爲領導者。

  • 創建獨立的Spring應用程序

  • 嵌入的Tomcat,無需部署WAR文件

  • 簡化Maven配置

  • 自動配置Spring

  • 提供生產就緒型功能,如指標,健康檢查和外部配置

  • 絕對沒有代碼生成並且對XML也沒有配置要求

爲什麼要用Nginx?

  • 概述

    Nginx(engine x)是一個開源的,支持高併發的www服務和代理服務軟件。Nginx是俄羅斯人Igor Sysoev開發的,最初被應用到俄羅斯的大型網站(www.rambler.ru)上。後來作者將源代碼以類BSD許可證的形式開源出來供全球使用。在功能應用方面,Nginx不僅是一個優秀的Web服務軟件,還具有反向代理負載均衡和緩存的功能。在反向代理負載均衡方面類似於LVS負載均衡及HAProxy等你專業代理軟件。Nginx部署起來更加方便簡單,在緩存服務功能方面,有類似於Squid等專業的緩存服務軟件。Nginx可以運行在UNIX、Linux、MS Windows Server、Mac OS X Server、Solaris等操作系統中。

  • Nginx的重要特性

    1. 可以針對靜態資源高速節點併發訪問及緩存。
    2. 可以使用反向代理加速,並且可以進行數據緩存。
    3. 具有簡單負載均衡,節點健康檢查和容錯功能。
    4. 支持遠程Fast CGI服務的緩存加速。
    5. 支持Fast CGI、Uwsgi、SCGI、Memcached Server的加速和緩存。
    6. 支持SSL、TLS、SNI。
    7. 具有模塊化的架構。
    8. 過濾器包括gzip壓縮、ranges支持、chunked響應、XSLT、SSL和圖像縮放等功能。
    9. 在SSL過濾器中,包含多個SSL頁面,如果經由Fast CGI或反向代理處理,可以並行處理。
  • Nginx所具備的WWW服務特性

    1. 支持基於域名、端口和IP的虛擬主機配置。
    2. 支持KeepAlived和piplined連接。
    3. 可進行簡單、方便、靈活的配置和管理。
    4. 支持修改Nginx配置,並且在代碼上線時,可平滑重啓,不中斷業務訪問。
    5. 可自定義訪問日誌格式,臨時緩衝寫日誌操作,快速日誌輪詢及通過rsyslog處理日誌。
    6. 可利用信號控制Nginx進程。
    7. 支持3xx-5xxHTTP狀態碼重定向。
    8. 支持rewrite模塊,支持URI重寫及正則表達式匹配。
    9. 支持基於客戶端IP地址和HTTP基本認證的訪問控制。
    10. 支持PUT、DELETE、MKCOL、COPY、MOVE等特殊的HTTP請求方法。
    11. 支持FLV流和MP4流技術產品應用。
    12. 支持HTTP響應速率限制。
    13. 支持同一IP地址的併發連接或請求限制。
    14. 支持郵件服務代理。
    15. 支持高併發,可以支持幾百萬併發連接。
    16. 資源消耗少,在3萬併發連接下,可以開啓10個nginx的線程消耗的內存不到200MB。
    17. 可以做HTTP反向代理及加速緩存,及負載均衡功能,內置對RS節點服務器健康檢查功能,折現但能夠與專業的HAProxy或LVS的功能。
    18. 具備Squid等專業緩存軟件等的緩存功能。
    19. 支持異步網絡I/O事件模型epoll(Linux2.6+)。
  • Nginx軟件主要企業應用

    1. 作爲Web服務軟件。
    2. 使用Nginx運行HTML、JS、CSS、小圖片等靜態數據(類似於Lighttpd)。
    3. 結合Fast CGI運行PHP等動態程序(例如使用fastcgi_pass方式)。
    4. Nginx結合Tomcat/Resin等支持Java動態程序(常用proxy_pass)。
    5. 反向代理或負載均衡服務(Nginx從1.9.0開始就開始支持TCP的代理了)。
    6. 前端業務數據緩存服務。
  • Web服務應用產品性能對比

    1. 靜態數據的訪問上:處理小文件(小於1MB)時,Nginx和Lighttpd比Apache更有優勢,Nginx處理小文件的優勢明顯,Lighttpd綜合最強。
    2. 動態數據的訪問上:三者差距不大,Apache更有優勢,因爲處理動態數據的能力在於PHP(Java)和後端數據庫的服務能力,也就是說瓶頸不在Web服務器上。
    3. 一般情況下普通PHP引擎支持的併發連接參考值300~1000。Java引擎和數據庫的併發連接參考值300~1500。
  • 爲什麼Nginx比Apache的性能高?

    1. Nginx使用最新版的eepoll(Linux 2.6內核)和kqueue(FreeBSD)異步網絡I/O模型,而Apache使用的是傳統的select模型。
    2. 目前Linux下能夠承受高併發訪問的Squid、Memcached軟件採用都是epoll模型。
    3. 處理大量的連接的讀寫時,Apache所採用的select網絡I/O模型比較低。
  • 如何正確採用Web服務器?

    1. 靜態業務:如果是高併發場景,儘量採用Nginx或Lighttpd,二者首選Nginx。
    2. 動態業務:理論上採用Nginx和Apache均可,建議使用Nginx,爲了避免相同業務服務的軟件多樣化,增加維護成本,動態業務可以使用Nginx兼做前端代理,再根據頁面的元素或目錄轉發到其他的服務器進行處理。
    3. 既有動態業務又有靜態業務,就用Nginx。

關於部署,就不在重複了,如果需要請移步《Java高級架構之FastDFS分佈式文件集羣》https://blog.51cto.com/xvjunjie/2377669

使用IDEA場景啓動器創建工程

  • 創建Maven工程,修改POM.xml文件添加如下依賴:
<dependencies>
    <!-- SpringBoot的自動配置相關依賴 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>1.5.20.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <version>1.5.20.RELEASE</version>
    </dependency>
    <!-- 日誌相關的依賴 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
        <version>1.5.20.RELEASE</version>
    </dependency>
    <!-- 對象池相關的依賴 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.6.0</version>
    </dependency>

</dependencies>

創建必要的包

  • annotation:存放相關的註解

  • autoconfiguation: 存儲自動配置類

  • factory: 存放工廠類

  • properties: 存放配置參數類

  • service: 存放服務類

一般情況下,SpringBoot都會提供相應的@EnableXxx註解標註在應用的主啓動類上開啓某個功能:

// EnableFastdfsClient.java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(FastdfsAutoConfiguration.class)
@Documented
public @interface EnableFastdfsClient {
}

下面是相關的自動配置類:

// FastdfsAutoConfiguration.java
@Configuration
@EnableConfigurationProperties(FastdfsProperties.class)
public class FastdfsAutoConfiguration {
    @Autowired
    private FastdfsProperties fastdfsProperties;

    @Bean
    @ConditionalOnMissingBean(FastdfsClientService.class)
    public FastdfsClientService fastdfsClientService() throws Exception {
        return new FastdfsClientService(fastdfsProperties);
    }
}

創建相關的工廠類:

// StorageClientFactory.java
// 用於創建連接對象的工廠類
public class StorageClientFactory implements PooledObjectFactory<StorageClient> {

    @Override
    public PooledObject<StorageClient> makeObject() throws Exception {
        TrackerClient client = new TrackerClient();
        TrackerServer server = client.getConnection();
        return new DefaultPooledObject<>(new StorageClient(server, null));
    }

    @Override
    public void destroyObject(PooledObject<StorageClient> p) throws Exception {
        p.getObject().getTrackerServer().close();
    }

    @Override
    public boolean validateObject(PooledObject<StorageClient> p) {
        return false;
    }

    @Override
    public void activateObject(PooledObject<StorageClient> p) throws Exception {

    }

    @Override
    public void passivateObject(PooledObject<StorageClient> p) throws Exception {

    }
}

Properties類用來映射application.properties或者application.yml配置文件:

// FastdfsProperties.java
@ConfigurationProperties(prefix = "fastdfs")
public class FastdfsProperties {
    // 連接超時時間
    // 網絡超時時間
    // 字符集編碼
    // 是否使用Token
    // Token加密密鑰
    // 跟蹤器IP地址,多個使用分號隔開
    // 連接池的連接對象最大個數
    // 連接池的最大空閒對象個數
    // 連接池的最小空閒對象個數
    // Nginx服務器IP,多個使用分號分割
    // 獲取連接對象時可忍受的等待時長(毫秒)
    private String connectTimeout = "5";
    private String networkTimeout = "30";
    private String charset = "UTF-8";
    private String httpAntiStealToken = "false";
    private String httpSecretKey = "";
    private String httpTrackerHttpPort = "";
    private String trackerServers = "";
    private String connectionPoolMaxTotal = "18";
    private String connectionPoolMaxIdle = "18";
    private String connectionPoolMinIdle = "2";
    private String nginxServers = "";

    // 需要創建相關的Setter和Getter方法
}

在Service類中封裝方法, 下面僅展示3個常用的方法:

// FastdfsClientSerivce.java
public class FastdfsClientService {
    // SpringBoot加載的配置文件
    // 連接池配置項
    // 轉換後的配置條目
    // 連接池
    // Nginx服務器地址
    private FastdfsProperties fdfsProp;
    private GenericObjectPoolConfig config;
    private Properties prop;
    private GenericObjectPool<StorageClient> pool;
    private String[] nginxServers;
    private Logger logger;

    public FastdfsClientService(FastdfsProperties fdfsProp) throws Exception {
        this.fdfsProp = fdfsProp;
        this.logger = LoggerFactory.getLogger(getClass());
        init();
        create();
        info();
    }

    /**
     * 初始化全局客戶端
     */
    private void init() throws Exception {
        this.prop = new Properties();
        this.logger.info("FastDFS: reading config file...");
        this.logger.info("FastDFS: fastdfs.connect_timeout_in_seconds=" + this.fdfsProp.getConnectTimeout());
        this.logger.info("FastDFS: fastdfs.network_timeout_in_seconds=" + this.fdfsProp.getNetworkTimeout());
        this.logger.info("FastDFS: fastdfs.charset=" + this.fdfsProp.getCharset());
        this.logger.info("FastDFS: fastdfs.http_anti_steal_token=" + this.fdfsProp.getHttpAntiStealToken());
        this.logger.info("FastDFS: fastdfs.http_secret_key=" + this.fdfsProp.getHttpSecretKey());
        this.logger.info("FastDFS: fastdfs.http_tracker_http_port=" + this.fdfsProp.getHttpTrackerHttpPort());
        this.logger.info("FastDFS: fastdfs.tracker_servers=" + this.fdfsProp.getTrackerServers());
        this.logger.info("FastDFS: fastdfs.connection_pool_max_total=" + this.fdfsProp.getConnectionPoolMaxTotal());
        this.logger.info("FastDFS: fastdfs.connection_pool_max_idle=" + this.fdfsProp.getConnectionPoolMaxIdle());
        this.logger.info("FastDFS: fastdfs.connection_pool_min_idle=" + this.fdfsProp.getConnectionPoolMinIdle());
        this.logger.info("FastDFS: fastdfs.nginx_servers=" + this.fdfsProp.getNginxServers());

        this.prop.put("fastdfs.connect_timeout_in_seconds", this.fdfsProp.getConnectTimeout());
        this.prop.put("fastdfs.network_timeout_in_seconds", this.fdfsProp.getNetworkTimeout());
        this.prop.put("fastdfs.charset", this.fdfsProp.getCharset());
        this.prop.put("fastdfs.http_anti_steal_token", this.fdfsProp.getHttpAntiStealToken());
        this.prop.put("fastdfs.http_secret_key", this.fdfsProp.getHttpSecretKey());
        this.prop.put("fastdfs.http_tracker_http_port", this.fdfsProp.getHttpTrackerHttpPort());
        this.prop.put("fastdfs.tracker_servers", this.fdfsProp.getTrackerServers());
        ClientGlobal.initByProperties(this.prop);
    }
    /**
     * 顯示初始化信息
     */
    private void info() {
        this.logger.info("FastDFS parameter: ConnectionPoolMaxTotal ==> " + this.pool.getMaxTotal());
        this.logger.info("FastDFS parameter: ConnectionPoolMaxIdle ==> " + this.pool.getMaxIdle());
        this.logger.info("FastDFS parameter: ConnectionPoolMinIdle ==> " + this.pool.getMinIdle());
        this.logger.info("FastDFS parameter: NginxServer ==> " + Arrays.toString(this.nginxServers));
        this.logger.info(ClientGlobal.configInfo());
    }

    /**
     * 創建連接池
     */
    private void create() {
        this.config = new GenericObjectPoolConfig();
        this.logger.info("FastDFS Client: Creating connection pool...");
        this.config.setMaxTotal(Integer.parseInt(this.fdfsProp.getConnectionPoolMaxTotal()));
        this.config.setMaxIdle(Integer.parseInt(this.fdfsProp.getConnectionPoolMaxIdle()));
        this.config.setMinIdle(Integer.parseInt(this.fdfsProp.getConnectionPoolMinIdle()));
        StorageClientFactory factory = new StorageClientFactory();
        this.pool = new GenericObjectPool<StorageClient>(factory, this.config);
        this.nginxServers = this.fdfsProp.getNginxServers().split(",");
    }

    /**
     * Nginx服務器負載均衡算法
     *
     * @param servers 服務器地址
     * @param address 客戶端IP地址
     * @return 可用的服務器地址
     */
    private String getNginxServer(String[] servers, String address) {
        int size = servers.length;
        int i = address.hashCode();
        int index = abs(i % size);
        return servers[index];
    }

    /**
     * 帶有防盜鏈的下載
     *
     * @param fileGroup       文件組名
     * @param remoteFileName  遠程文件名稱
     * @param clientIpAddress 客戶端IP地址
     * @return 完整的URL地址
     */
    public String autoDownloadWithToken(String fileGroup, String remoteFileName, String clientIpAddress) throws Exception {
        int ts = (int) (System.currentTimeMillis() / 1000);
        String token = ProtoCommon.getToken(remoteFileName, ts, ClientGlobal.getG_secret_key());
        String nginx = this.getNginxServer(this.nginxServers, clientIpAddress);
        return "http://" + nginx + "/" + fileGroup + "/" + remoteFileName + "?token=" + token + "&ts=" + ts;
    }

    /**
     * 上傳文件,適合上傳圖片
     *
     * @param buffer 字節數組
     * @param ext    擴展名
     * @return 文件組名和ID
     */
    public String[] autoUpload(byte[] buffer, String ext) throws Exception {
        String[] upload = this.upload(buffer, ext, null);
        return upload;
    }

    /**
     * 不帶防盜鏈的下載,如果開啓防盜鏈會導致該方法拋出異常
     *
     * @param fileGroup       文件組名
     * @param remoteFileName  遠程文件ID
     * @param clientIpAddress 客戶端IP地址,根據客戶端IP來分配Nginx服務器
     * @return 完整的URL地址
     */
    public String autoDownloadWithoutToken(String fileGroup, String remoteFileName, String clientIpAddress) throws Exception {
        if (ClientGlobal.getG_anti_steal_token()) {
            this.logger.error("FastDFS Client: You've turned on Token authentication.");
            throw new Exception("You've turned on Token authentication.");
        }
        String nginx = this.getNginxServer(this.nginxServers, clientIpAddress);
        return "http://" + nginx + fileGroup + "/" + remoteFileName;
    }

    // 後面還有好多方法,就不一一展示了
}

爲了在IDEA中使用便捷的配置提示功能,我們需要創建元數據文件(resources/spring-configuration-metadata.json):

{
  "groups": [
    {
      "name": "fastdfs",
      "type": "com.bluemiaomiao.properties.FastdfsProperties",
      "sourceType": "com.bluemiaomiao.properties.FastdfsProperties"
    }
  ],
  "properties": [
    {
      "name": "connectTimeout",
      "type": "java.lang.String",
      "sourceType": "com.bluemiaomiao.properties.FastdfsProperties",
      "defaultValue": "5"
    },
    {
      "name": "networkTimeout",
      "type": "java.lang.String",
      "sourceType": "com.bluemiaomiao.properties.FastdfsProperties",
      "defaultValue": "30"
    },
    {
      "name": "charset",
      "type": "java.lang.String",
      "defaultValue": "UTF-8"
    },
    {
      "name": "httpAntiStealToken",
      "type": "java.lang.String",
      "sourceType": "com.bluemiaomiao.properties.FastdfsProperties",
      "defaultValue": "false"
    },
    {
      "name": "httpSecretKey",
      "type": "java.lang.String",
      "sourceType": "com.bluemiaomiao.properties.FastdfsProperties"
    },
    {
      "name": "httpTrackerHttpPort",
      "type": "java.lang.Integer",
      "sourceType": "com.bluemiaomiao.properties.FastdfsProperties"
    },
    {
      "name": "trackerServers",
      "type": "java.lang.String",
      "sourceType": "com.bluemiaomiao.properties.FastdfsProperties"
    },
    {
      "name": "connectionPoolMaxTotal",
      "type": "java.lang.Integer",
      "sourceType": "com.bluemiaomiao.properties.FastdfsProperties",
      "defaultValue": "18"
    },
    {
      "name": "connectionPoolMaxIdle",
      "type": "java.lang.Integer",
      "sourceType": "com.bluemiaomiao.properties.FastdfsProperties",
      "defaultValue": "18"
    },
    {
      "name": "connectionPoolMinIdle",
      "type": "java.lang.Integer",
      "sourceType": "com.bluemiaomiao.properties.FastdfsProperties",
      "defaultValue": "2"
    },
    {
      "name": "nginxServers",
      "type": "java.lang.String",
      "sourceType": "com.bluemiaomiao.properties.FastdfsProperties"
    }
  ],
  "hints": [
    {
      "name": "http_anti_steal_token",
      "values": [
        {
          "value": "false"
        },
        {
          "value": "true"
        }
      ]
    }
  ]
}

將自定義starter添加到項目

  • 創建SpringBoot項目,勾選Web選項,版本選擇1.5.20

  • 進入場景啓動器的項目目錄執行mvn clean install 將其安裝到本地

  • 在POM.xml文件中添加依賴:
<dependency>
    <groupId>com.bluemiaomiao</groupId>
    <artifactId>fastdfs-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

記得開啓IDEA的自動導入功能

  • 創建配置文件application.properties
fastdfs.nginx-servers=192.168.80.2:8000,192.168.80.3:8000,192.168.80.4:8000
fastdfs.tracker-servers=192.168.80.2:22122,192.168.80.3:22122,192.168.80.4:22122
fastdfs.http-secret-key=2scPwMPctXhbLVOYB0jyuyQzytOofmFCBIYe65n56PPYVWrntxzLIDbPdvDDLJM8QHhKxSGWTcr+9VdG3yptkw
fastdfs.http-anti-steal-token=true
fastdfs.http-tracker-http-port=8080
fastdfs.network-timeout=30
fastdfs.connect-timeout=5
fastdfs.connection-pool-max-idle=18
fastdfs.connection-pool-min-idle=2
fastdfs.connection-pool-max-total=18
fastdfs.charset=UTF-8

或者使用application.yml

fastdfs:
  charset: UTF-8
  connect-timeout: 5
  http-secret-key: 2scPwMPctXhbLVOYB0jyuyQzytOofmFCBIYe65n56PPYVWrntxzLIDbPdvDDLJM8QHhKxSGWTcr+9VdG3yptkw
  network-timeout: 30
  http-anti-steal-token: true
  http-tracker-http-port: 8080
  connection-pool-max-idle: 20
  connection-pool-max-total: 20
  connection-pool-min-idle: 2
  nginx-servers: 192.168.80.2:8000,192.168.80.3:8000,192.168.80.4:8000
  tracker-servers: 192.168.80.2:22122,192.168.80.3:22122,192.168.80.4:22122
  • 創建控制器類測試方法
// controllers.DownloadController.java
@Controller
@RequestMapping(value = "/download")
public class DownloadController {

    @Autowired
    private FastdfsClientService service;

    @ResponseBody
    @RequestMapping(value = "/image")
    public String image() throws Exception {
        // 之前上傳過的數據,實際應用場景應該使用SQL數據庫來存儲
        return service.autoDownloadWithToken("group1", "M00/00/00/wKhQA1ysjSGAPjXbAAVFOL7FJU4.tar.gz", "192.168.80.1");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章