当前位置:   article > 正文

二. fastDFS:springboot 整合fastDFS_springboot fastdfs war

springboot fastdfs war

一.引入官方客户端依赖

  1. <!--FastDFS-->
  2. <dependency>
  3. <groupId>net.oschina.zcx7878</groupId>
  4. <artifactId>fastdfs-client-java</artifactId>
  5. <version>1.27.0.0</version>
  6. </dependency>
  1. <!--pool-->
  2. <dependency>
  3. <groupId>org.apache.commons</groupId>
  4. <artifactId>commons-pool2</artifactId>
  5. <version>2.4.2</version>
  6. </dependency>

-- 以上只列举相关的依赖,其它依赖自行添加

二 . 配置文件

  1. #######################FastDFS#######################
  2. #文件服务器地址
  3. file_server_addr=192.168.17.128:80
  4. # 最大连接数 并发量较大的话可加大该连接数
  5. max_storage_connection=10
  6. #超时配置
  7. fastdfs.connect_timeout_in_seconds=100000
  8. fastdfs.network_timeout_in_seconds=300000
  9. fastdfs.charset=UTF-8
  10. #token 防盗链功能
  11. fastdfs.http_anti_steal_token=true
  12. #密钥,默认为FASTDFS1234567890
  13. fastdfs.http_secret_key=FASTDFS1234567890
  14. # TrackerServer port
  15. fastdfs.http_tracker_http_port=6666
  16. ## Tracker Server, if more than one, separate with ","
  17. fastdfs.tracker_servers=192.168.17.128:22122
  18. #######################FastDFS#######################

-- FastDFS的配置文件只支持properties类型的,可以整合放入application.properties里面,但是不能放入yml类型的里面

三.服务端代码编写

 0.启动类配置

SpringbootFastdfsApplication.class
  1. @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
  2. @EnableAutoConfiguration
  3. @MapperScan(value = "com.vesus.springbootfastdfs.mapper")
  4. @Configuration
  5. @PropertySource(value = "classpath:fastdfs.properties",encoding = "utf-8")
  6. public class SpringbootFastdfsApplication extends SpringBootServletInitializer {
  7. public static void main(String[] args) {
  8. SpringApplication.run(SpringbootFastdfsApplication.class, args);
  9. }
  10. /**
  11. * war 包部署
  12. * @param application
  13. * @return
  14. */
  15. @Override
  16. protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
  17. {
  18. return application.sources(SpringbootFastdfsApplication.class);
  19. }
  20. }

 -- 

 1.编写调度tracker池

  1. TrackerServerPool.class
  2. public class TrackerServerPool {
  3. private static Logger logger= LoggerFactory.getLogger(TrackerServerPool.class);
  4. /***
  5. * 配置文件路径
  6. */
  7. private static final String FASTDFS_CONFIG_PATH = "fastdfs.properties";
  8. /***
  9. * 最大连接数
  10. */
  11. @Value("${max_storage_connection}")
  12. private static int maxStorageConnection ;
  13. /***
  14. * TrackerServer 对象池
  15. */
  16. private static GenericObjectPool<TrackerServer> pool ;
  17. /***
  18. * 无参构造函数
  19. */
  20. private TrackerServerPool(){}
  21. /***
  22. * 获取TrackerServer连接池
  23. * @return
  24. * @throws Exception
  25. */
  26. public static synchronized GenericObjectPool<TrackerServer> getObjectPool() throws Exception{
  27. if (pool ==null){
  28. //加载配置文件
  29. ClientGlobal.initByProperties(FASTDFS_CONFIG_PATH);
  30. //pool配置,设置最大值和最小值
  31. GenericObjectPoolConfig config = new GenericObjectPoolConfig();
  32. config.setMinIdle(2);
  33. if (maxStorageConnection>0){
  34. config.setMaxIdle(maxStorageConnection);
  35. }
  36. pool = new GenericObjectPool<>(new TrackerServerFactory(),config) ;
  37. }
  38. return pool ;
  39. }
  40. /***
  41. * 获取TrackerServer
  42. * @return
  43. * @throws Exception
  44. */
  45. public static TrackerServer borrowObject()throws Exception{
  46. TrackerServer trackerServer = null;
  47. trackerServer = getObjectPool().borrowObject() ;
  48. return trackerServer ;
  49. }
  50. /***
  51. * 回收 TrackerServer
  52. * @param server
  53. * @throws Exception
  54. */
  55. public static void recycleObject(TrackerServer server) throws Exception{
  56. getObjectPool().returnObject(server);
  57. }
  58. }

2.fastdfs调度Tracker工厂

TrackerServerFactory.class
  1. public class TrackerServerFactory extends BasePooledObjectFactory<TrackerServer> {
  2. public TrackerServer create() throws Exception {
  3. //实例化TrackerClient
  4. TrackerClient client = new TrackerClient();
  5. //获取TrackerServer
  6. TrackerServer trackerServer = client.getConnection();
  7. return trackerServer;
  8. }
  9. public PooledObject<TrackerServer> wrap(TrackerServer server) {
  10. return new DefaultPooledObject<TrackerServer>(server);
  11. }
  12. }

3.编写fastdfs操作客户端

FastDFSClient.class
  1. @Component
  2. public class FastDFSClient {
  3. /**
  4. * 路径分隔符
  5. */
  6. public static final String SEPARATOR = "/";
  7. /**
  8. * Point
  9. */
  10. public static final String POINT = ".";
  11. /**
  12. *
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/891108
推荐阅读
相关标签
  

闽ICP备14008679号