首先在写这篇文章之前,百度了不少资料基本上都是
1.iptables
2.用--net=host主机网络
3.替换java fast-client.jar自己做mapping映射。
方法一:iptables麻烦,应该是要在调用者那台机器通过防火墙转发到真实机器,维护起来容易让后面的人掉坑里。
方法二:没有尝试因为host主机网络的话,镜像内部的一些端口会和主机冲突,不得不做一些处理甚至要改镜像内部端口更麻烦,因为我的镜像除了fastdfs的端口还有内置的一个nginx
方法三:替换jar似乎可以行得通,自己在代码里面搞定了,但是工具包不好升级,你这个Client包相当于定制的,其它地方不一定找得到。
最后翻看了一下源码,我的版本是:
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</exclusion>
</exclusions>
</dependency>
获取Storage节点信息的类在DefaultTrackerClient,默认是@Service注入到Spring容器中,其它地方是通过@Autowired注入的,因此我自己写了一个类继承DefaultTrackerClient,
用@Primary顶替掉默认的,然后再关键部位实现自定义的IP映射,思路是这样,测试了一下上传下载都没有问题。
@Primary @Service public class AeyeTrackerClient extends DefaultTrackerClient implements TrackerClient { @Autowired private TrackerConnectionManager trackerConnectionManager; @Autowired private AeyeTrackerConfig aeyeTrackerConfig; /** * 获取存储节点 */ @Override public StorageNode getStoreStorage() { TrackerGetStoreStorageCommand command = new TrackerGetStoreStorageCommand(); StorageNode node = trackerConnectionManager.executeFdfsTrackerCmd(command); mappingIp(node); return node; } /** * 按组获取存储节点 */ @Override public StorageNode getStoreStorage(String groupName) { TrackerGetStoreStorageCommand command; if (StringUtils.isBlank(groupName)) { command = new TrackerGetStoreStorageCommand(); } else { command = new TrackerGetStoreStorageCommand(groupName); } StorageNode node = trackerConnectionManager.executeFdfsTrackerCmd(command); mappingIp(node); return node; } @Override public StorageNodeInfo getFetchStorage(String groupName, String filename) { TrackerGetFetchStorageCommand command = new TrackerGetFetchStorageCommand(groupName, filename, false); StorageNodeInfo nodeInfo = trackerConnectionManager.executeFdfsTrackerCmd(command); String mappingIp = aeyeTrackerConfig.getStorageIpMapping().get(nodeInfo.getIp()+":"+nodeInfo.getPort()); if(AeyeStringUtils.isNotBlank(mappingIp)){ nodeInfo.setIp(mappingIp.split(":")[0]); nodeInfo.setPort(Integer.parseInt(mappingIp.split(":")[1])); } return nodeInfo; } private void mappingIp(StorageNode node){ String mappingIp = aeyeTrackerConfig.getStorageIpMapping().get(node.getIp()+":"+node.getPort()); if(AeyeStringUtils.isNotBlank(mappingIp)){ node.setIp(mappingIp.split(":")[0]); node.setPort(Integer.parseInt(mappingIp.split(":")[1])); } }
@Data @Component @ConfigurationProperties(prefix = FdfsClientConstants.ROOT_CONFIG_PREFIX) public class AeyeTrackerConfig{ /** * storage地址映射列表 */ private Map<String, String> storageIpMapping = new HashMap<>(16); }
yml增加配置项
fdfs: storage-ip-mapping: "[172.19.0.1:23000]": "192.168.x.xxx:23001"