免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6382 | 回复: 10
打印 上一主题 下一主题

[FastDFS] java客户端如何获取storage http端口进行文件下载 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-06-09 15:37 |只看该作者 |倒序浏览
本帖最后由 eighteencold 于 2013-06-09 15:41 编辑

两台同组的storage server:

第1台storage web server(nginx)  192.168.1.100 端口(storage.conf配置文件的http.server_port):80

第2台storage web server(nginx)  192.168.1.101 端口(storage.conf配置文件的http.server_port):82

假设我保存一个文件进了第1台storage,那么我的下载地址为http://192.168.1.100/remote_filename
假设我保存一个文件进了第2台storage,那么我的下载地址为http://192.168.1.101:82/remote_filename

问题是在java客户端中如何获取这个端口。

API有如下代码:
storageServers.getInetSocketAddress().getPort()
但这个端口获取的是storage.conf配置文件中的port端口值,并非http.server_port

论坛徽章:
0
2 [报告]
发表于 2013-06-09 17:10 |只看该作者
storage机器上直接搭个nginx,把鱼大提供的nginx module编进去,然后就可以直接提供文件下载了。

论坛徽章:
0
3 [报告]
发表于 2013-06-09 20:49 |只看该作者
回复 2# kyon_lonely

我的也能下载,但问题是URL怎么获取

论坛徽章:
0
4 [报告]
发表于 2013-06-13 08:02 |只看该作者
回复 3# eighteencold


    FastDFS不是给生成了么?前面再加上storage的ip就可以了。

论坛徽章:
0
5 [报告]
发表于 2013-06-13 10:12 |只看该作者
自带的test_upload中应该是有的,但在java客户端中应该是获取不到的,如果可以的话,麻烦给我发段获取代码,谢谢啦回复 4# kyon_lonely


   

论坛徽章:
0
6 [报告]
发表于 2013-06-13 10:22 |只看该作者
等不及了,准备自己通过配置文件维护了,通过获取storage的IP来映射它的下载路径的前半段如:

192.168.1.100=http://down.xxxx.com
192.168.1.101=http://192.168.1.100:82

论坛徽章:
0
7 [报告]
发表于 2013-06-13 11:22 |只看该作者
本帖最后由 eighteencold 于 2013-06-13 11:45 编辑

发下代码吧,有遇到相同问题的可以参考一下:

重写StorageClient的query_file_info方法,在storageServer 置空之前, 获取它的InetSocketAddress拿到storageServer 的ip;新增getHttpURI
这们我就能在调用get_file_info方法之后再调用getHttpURI获取httpURL。
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.net.Socket;
  5. import java.util.Arrays;
  6. import java.util.Properties;

  7. import org.csource.common.MyException;
  8. import org.csource.fastdfs.ClientGlobal;
  9. import org.csource.fastdfs.FileInfo;
  10. import org.csource.fastdfs.ProtoCommon;
  11. import org.csource.fastdfs.StorageServer;
  12. import org.csource.fastdfs.TrackerServer;

  13. public class StorageClient extends org.csource.fastdfs.StorageClient {

  14.         public final static String confPath = "FileServer_client.conf";
  15.        
  16.         private static Properties prop = new Properties();
  17.        
  18.         private static boolean isInit=false;
  19.        
  20.         protected String uri;

  21.         public StorageClient(TrackerServer trackerServer, StorageServer storageServer) {
  22.                 super(trackerServer, storageServer);
  23.                 init();
  24.         }

  25.         public StorageClient() {
  26.                 super();
  27.                 init();
  28.         }

  29.         public void init(){
  30.                 if(isInit){
  31.                         return;
  32.                 }
  33.                 ClassLoader cl = Thread.currentThread().getContextClassLoader();
  34.                 InputStream in = cl.getResourceAsStream(confPath);
  35.                 try {
  36.                         prop.load(in);
  37.                         in.close();
  38.                         isInit=true;
  39.                 } catch (IOException e) {
  40.                         e.printStackTrace();
  41.                 }

  42.         }
  43.        
  44.         public String getHttpURI() {
  45.                 return uri;
  46.         }

  47.         @Override
  48.         public FileInfo query_file_info(String group_name, String remote_filename)
  49.                         throws IOException, MyException {
  50.                 boolean bNewConnection = this.newUpdatableStorageConnection(group_name,
  51.                                 remote_filename);
  52.                 Socket storageSocket = this.storageServer.getSocket();

  53.                 try {
  54.                         byte[] header;
  55.                         byte[] groupBytes;
  56.                         byte[] filenameBytes;
  57.                         byte[] bs;
  58.                         int groupLen;
  59.                         ProtoCommon.RecvPackageInfo pkgInfo;

  60.                         filenameBytes = remote_filename.getBytes(ClientGlobal.g_charset);
  61.                         groupBytes = new byte[ProtoCommon.FDFS_GROUP_NAME_MAX_LEN];
  62.                         bs = group_name.getBytes(ClientGlobal.g_charset);

  63.                         Arrays.fill(groupBytes, (byte) 0);
  64.                         if (bs.length <= groupBytes.length) {
  65.                                 groupLen = bs.length;
  66.                         } else {
  67.                                 groupLen = groupBytes.length;
  68.                         }
  69.                         System.arraycopy(bs, 0, groupBytes, 0, groupLen);

  70.                         header = ProtoCommon.packHeader(
  71.                                         ProtoCommon.STORAGE_PROTO_CMD_QUERY_FILE_INFO,
  72.                                         +groupBytes.length + filenameBytes.length, (byte) 0);
  73.                         OutputStream out = storageSocket.getOutputStream();
  74.                         byte[] wholePkg = new byte[header.length + groupBytes.length
  75.                                         + filenameBytes.length];
  76.                         System.arraycopy(header, 0, wholePkg, 0, header.length);
  77.                         System.arraycopy(groupBytes, 0, wholePkg, header.length,
  78.                                         groupBytes.length);
  79.                         System.arraycopy(filenameBytes, 0, wholePkg, header.length
  80.                                         + groupBytes.length, filenameBytes.length);
  81.                         out.write(wholePkg);

  82.                         pkgInfo = ProtoCommon.recvPackage(storageSocket.getInputStream(),
  83.                                         ProtoCommon.STORAGE_PROTO_CMD_RESP, 3
  84.                                                         * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE
  85.                                                         + ProtoCommon.FDFS_IPADDR_SIZE);

  86.                         this.errno = pkgInfo.errno;
  87.                         if (pkgInfo.errno != 0) {
  88.                                 return null;
  89.                         }

  90.                         long file_size = ProtoCommon.buff2long(pkgInfo.body, 0);
  91.                         int create_timestamp = (int) ProtoCommon.buff2long(pkgInfo.body,
  92.                                         ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
  93.                         int crc32 = (int) ProtoCommon.buff2long(pkgInfo.body,
  94.                                         2 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE);
  95.                         String source_ip_addr = (new String(pkgInfo.body,
  96.                                         3 * ProtoCommon.FDFS_PROTO_PKG_LEN_SIZE,
  97.                                         ProtoCommon.FDFS_IPADDR_SIZE)).trim();
  98.                         return new FileInfo(file_size, create_timestamp, crc32,
  99.                                         source_ip_addr);
  100.                 } catch (IOException ex) {
  101.                         if (!bNewConnection) {
  102.                                 try {
  103.                                         this.storageServer.close();
  104.                                 } catch (IOException ex1) {
  105.                                         ex1.printStackTrace();
  106.                                 } finally {
  107.                                         this.storageServer = null;
  108.                                 }
  109.                         }

  110.                         throw ex;
  111.                 } finally {
  112.                         if (bNewConnection) {
  113.                                 try {
  114.                                         this.storageServer.close();
  115.                                 } catch (IOException ex1) {
  116.                                         ex1.printStackTrace();
  117.                                 } finally {
  118.                                         String ip = super.storageServer.getInetSocketAddress().getHostName();
  119.                                         uri=prop.getProperty("httpURI."+ip);
  120.                                         if(!uri.endsWith("/")){
  121.                                                 uri=uri+"/";
  122.                                         }
  123.                                         this.storageServer = null;
  124.                                 }
  125.                         }
  126.                 }
  127.         }
  128. }
复制代码
配置文件:
  1. connect_timeout = 2
  2. network_timeout = 30
  3. charset = UTF-8
  4. http.tracker_http_port = 80
  5. http.anti_steal_token = no
  6. http.secret_key = FastDFS1234567890

  7. tracker_server = 192.168.15.201:22122

  8. httpURI.192.168.15.201=http://192.168.15.201
复制代码

论坛徽章:
0
8 [报告]
发表于 2013-06-13 11:51 |只看该作者
回复 5# eighteencold


看一下FastDFS如何与nginx进行整合吧。楼主没必要整那么复杂,拿到FastDFS生成的文件名之后,加上前缀域名。storage机器配置好nginx rewrite,下载文件时通过域名解析到具体机器,剩下的就是nginx自己处理了,不需要自己写代码。

http://blog.sina.com.cn/s/blog_6c9683650101e00g.html

论坛徽章:
0
9 [报告]
发表于 2013-06-13 12:05 |只看该作者
刚开始想复杂了,就想着怎么获取ip了,其实ip已经在get_file_info返回的FileInfo中已经存在了(source_ip_addr)

故修改代码为提供工具类获取如下:
  1. public class FileServerUtil {

  2.         private IFileService fileService;

  3.         private static FileServerUtil instance;
  4.        
  5.         public final static String confPath = "FileServer_client.conf";
  6.        
  7.         private static Properties prop = new Properties();

  8.         public void setFileService(IFileService fileService) {
  9.                 this.fileService = fileService;
  10.         }

  11.         public static IFileService getFileService() {
  12.                 return instance.fileService;
  13.         }

  14.         public void init(){
  15.                 instance=this;
  16.                 instance.setFileService(fileService);
  17.                 initProp();
  18.         }

  19.         public static void initProp(){
  20.                 ClassLoader cl = Thread.currentThread().getContextClassLoader();
  21.                 InputStream in = cl.getResourceAsStream(confPath);
  22.                 try {
  23.                         prop.load(in);
  24.                         in.close();
  25.                 } catch (IOException e) {
  26.                         e.printStackTrace();
  27.                 }
  28.         }
  29.        
  30.         public static String getHttpURI(String ip) {
  31.                 String uri=prop.getProperty("httpURI."+ip);
  32.         if(!uri.endsWith("/")){
  33.                 uri=uri+"/";
  34.         }
  35.                 return uri;
  36.         }
  37. }
复制代码

论坛徽章:
0
10 [报告]
发表于 2013-06-13 12:06 |只看该作者
回复 8# kyon_lonely


应该我这里说的问题跟你说的不是同一个问题,你再好好看看
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP