版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cdnight/article/details/86619559
前言
作为文件服务器,当然要有文件存储服务。这里选用的是fastdfs作为存储服务器,下面进行相关内容配置。
配置
添加第三方依赖
在根项目gradle下面:
首先定义版本:
globalFastDfsClientVersion = "1.27"
然后,定义引用地址:
ref4FastdfsClient=["cn.bestwu:fastdfs-client-java:$globalFastDfsClientVersion"]
在FileServerWebApp的构建文件引用:
//fastdfs client
compile ref4FastdfsClient
配置中心配置
在配置中心执行以下脚本:
create or replace function "initFdfsConfig"(
in envName varchar,
in appname varchar,
)
returns varchar
as $BODY$
declare _defaultValues varchar;
declare _envName varchar;
declare _appname varchar;
declare _prefix varchar;
declare strArrays varchar[];
declare arrItemLv1 varchar;
declare tempArrSubItem varchar;
declare valArrs varchar[];
declare item_attr varchar;
declare item_title varchar;
declare item_val varchar;
begin
if envName <> 'test' and envName<> 'ppe' and envName<> 'product' then
raise notice '环境变量异常,只能为test、ppe以及product其中一个。';
return '环境变量异常,只能为test、ppe以及product其中一个。';
end if;
_appname:=appanme;
_prefix:=concat(_appname,'.fdfs.','');
_defaultValues:=
'connect_timeout_in_seconds->连接fdfs服务器超时时间(单位:秒)->5$$' ||
'network_timeout_in_seconds->网络连接超时时间(单位:秒)->30$$' ||
'charset->字符集->UTF-8$$' ||
'anti_steal_token->token 防盗链功能->false$$' ||
'secret_key->注意,只有在防盗链开启以后这个密钥才有用->FastDFS1234567890$$' ||
'tracker_http_port->tracker服务器的http端口【默认为80】->80$$' ||
'tracker_servers->tracker服务器列表【多个用英文逗号分隔】->localhost:22122$$' ||
'visit_url->外网访问图片文件的url地址->http://localhost/$$'
;
strArrays:=string_to_array(_defaultValues,'$$');
_envName:=envName;
-- fastdfs.connect_timeout_in_seconds = 5
-- fastdfs.network_timeout_in_seconds = 30
-- fastdfs.charset = UTF-8
-- fastdfs.http_anti_steal_token = false
-- fastdfs.http_secret_key = FastDFS1234567890
-- fastdfs.http_tracker_http_port = 80
-- #fastdfs.tracker_servers = tw-server:22122,10.0.11.202:22122,10.0.11.203:22122
-- fastdfs.tracker_servers = localhost:22122
-- fastdfs.visit_url = http://localhost/
-- env varchar(100) not null,
-- key varchar(200) not null,
-- appname varchar(100) not null,
-- title varchar(100) not null,
-- value varchar(2000) default NULL::character varying,
<<loop4BigArray>>
foreach arrItemLv1 in array strArrays
loop
if char_length(arrItemLv1) < 1 then
raise notice '空字符串无须处理';
continue ;
end if;
valArrs:=string_to_array(arrItemLv1,'->');
item_attr:=valArrs[1];
item_title:=valArrs[2];
item_val:=valArrs[3];
raise notice '属性名称:%,描述:%,当前值:%',item_attr,item_title,item_val;
raise notice '开始添加记录';
insert into xxl_conf_node("env","key","appname","title","value")
values (_envName,concat(_prefix,item_attr),_appname,item_title,item_val)
on conflict ("env","key") do nothing ;
end loop loop4BigArray;
return 'ok';
end;
$BODY$ language plpgsql volatile ;
-- 记住执行下面方法分别添加三个环境下的默认数据。
-- select "initFdfsConfig"('test','file-server');
-- select "initFdfsConfig"('ppe','file-server');
-- select "initFdfsConfig"('product','file-server');
然后打开配置中心后台,可以看到:
已经将fdfs的配置都导入配置数据库了。
项目配置
新添加一个配置类:
代码如下:
package net.w2p.local.plugins.config;
import com.xxl.conf.core.annotation.XxlConf;
/***
*
'connect_timeout_in_seconds->连接fdfs服务器超时时间(单位:秒)->5$$' ||
'network_timeout_in_seconds->网络连接超时时间(单位:秒)->30$$' ||
'charset->字符集->UTF-8$$' ||
'anti_steal_token->token 防盗链功能->false$$' ||
'secret_key->注意,只有在防盗链开启以后这个密钥才有用->FastDFS1234567890$$' ||
'tracker_http_port->tracker服务器的http端口【默认为80】->80$$' ||
'tracker_servers->tracker服务器列表【多个用英文逗号分隔】->localhost:22122$$' ||
'visit_url->外网访问图片文件的url地址->http://localhost/$$'
;
*
* ***/
public class FdfsConf {
private static final String conf_prefix ="file-server.fdfs.";
@XxlConf(conf_prefix +"connect_timeout_in_seconds")
public Integer connect_timeout_in_seconds = 5;
@XxlConf(conf_prefix +"network_timeout_in_seconds")
public Integer network_timeout_in_seconds = 5;
@XxlConf(conf_prefix +"charset")
public String charset = "";
@XxlConf(conf_prefix +"anti_steal_token")
public Boolean anti_steal_token = false;
@XxlConf(conf_prefix +"secret_key")
public String secret_key = "";
@XxlConf(conf_prefix +"tracker_http_port")
public Integer tracker_http_port = 80;
@XxlConf(conf_prefix +"tracker_servers")
public String tracker_servers = "";
@XxlConf(conf_prefix +"visit_url")
public String visit_url = "";
public Integer getConnect_timeout_in_seconds() {
return connect_timeout_in_seconds;
}
public void setConnect_timeout_in_seconds(Integer connect_timeout_in_seconds) {
this.connect_timeout_in_seconds = connect_timeout_in_seconds;
}
public Integer getNetwork_timeout_in_seconds() {
return network_timeout_in_seconds;
}
public void setNetwork_timeout_in_seconds(Integer network_timeout_in_seconds) {
this.network_timeout_in_seconds = network_timeout_in_seconds;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public Boolean getAnti_steal_token() {
return anti_steal_token;
}
public void setAnti_steal_token(Boolean anti_steal_token) {
this.anti_steal_token = anti_steal_token;
}
public String getSecret_key() {
return secret_key;
}
public void setSecret_key(String secret_key) {
this.secret_key = secret_key;
}
public Integer getTracker_http_port() {
return tracker_http_port;
}
public void setTracker_http_port(Integer tracker_http_port) {
this.tracker_http_port = tracker_http_port;
}
public String getTracker_servers() {
return tracker_servers;
}
public void setTracker_servers(String tracker_servers) {
this.tracker_servers = tracker_servers;
}
public String getVisit_url() {
return visit_url;
}
public void setVisit_url(String visit_url) {
this.visit_url = visit_url;
}
}
然后,在applicationContext-XxlConf.xml中注册这个bean:
<!-- ********************************* fastdfs 配置 ********************************* -->
<bean id="fastdfsConf" class="net.w2p.local.plugins.config.FdfsConf">
</bean>
接下来,写fastdfs的工具类:
其中,FastDFSFile如下:
package net.w2p.local.plugins.vo;
import java.io.Serializable;
import java.util.Arrays;
/**
* @ClassName FastDFSFile
* @Description FastDFS上传文件业务对象
* @author zhangkai
* @date 2017年7月18日
*/
public class FastDFSFile implements Serializable{
private static final long serialVersionUID = 2637755431406080379L;
/**
* 文件二进制
*/
private byte[] content;
/**
* 文件名称
*/
private String name;
/**
* 文件长度
*/
private Long size;
public FastDFSFile(){
}
public FastDFSFile(byte[] content, String name, Long size){
this.content = content;
this.name = name;
this.size = size;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getSize() {
return size;
}
public void setSize(Long size) {
this.size = size;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
FastDFSUtils代码如下:
package net.w2p.local.plugins.utils;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.URL;
import java.util.Properties;
import net.w2p.local.plugins.BeanConfiguration.FastDfsConfig;
import net.w2p.local.plugins.config.FdfsConf;
import net.w2p.local.plugins.vo.FastDFSFile;
import org.apache.commons.io.FilenameUtils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
//import org.springframework.core.io.ClassPathResource;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//import org.springframework.http.ResponseEntity;
/**
* @ClassName FastDFSUtils
* @Description FastDFS工具类
* @author zhangkai
* @date 2017年7月18日
*/
public class FastDFSUtils implements Serializable {
/**
*
*/
public final long serialVersionUID = -4462272673174266738L;
private TrackerClient trackerClient;
private TrackerServer trackerServer;
private StorageClient1 storageClient1;
private Properties config;
private FdfsConf setting = null;
public FastDFSUtils(FdfsConf setting) {
this.setting = setting;
config = new Properties();
config.setProperty("fastdfs.connect_timeout_in_seconds", this.setting.connect_timeout_in_seconds + "");
config.setProperty("fastdfs.network_timeout_in_seconds", this.setting.network_timeout_in_seconds + "");
config.setProperty("fastdfs.charset", this.setting.charset);
config.setProperty("fastdfs.http_anti_steal_token", this.setting.anti_steal_token + "");
config.setProperty("fastdfs.http_secret_key", this.setting.secret_key + "");
config.setProperty("fastdfs.http_tracker_http_port", this.setting.tracker_http_port + "");
config.setProperty("fastdfs.tracker_servers", this.setting.tracker_servers + "");
config.setProperty("fastdfs.visit_url", this.setting.visit_url + "");
try {
ClientGlobal.initByProperties(config);
//trackerclient
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
//storageclient
storageClient1 = new StorageClient1(trackerServer, null);
} catch (Exception e) {
e.printStackTrace();
}
}
/****
*
* 获取文件信息
*
* ****/
public FileInfo getFileInfo(String group,String name) throws Exception {
return storageClient1.query_file_info(group,name);
}
public FileInfo getFileInfo1(String fileid) throws Exception {
return storageClient1.query_file_info1(fileid);
}
/**
* fastDFS文件上传
*
* @param file 上传的文件 FastDFSFile
* @return String 返回文件的绝对路径
*/
public String uploadFile(FastDFSFile file) {
String path = null;
try {
//文件扩展名
String ext = FilenameUtils.getExtension(file.getName());
//mata list是表文件的描述
NameValuePair[] mata_list = new NameValuePair[3];
mata_list[0] = new NameValuePair("fileName", file.getName());
mata_list[1] = new NameValuePair("fileExt", ext);
mata_list[2] = new NameValuePair("fileSize", String.valueOf(file.getSize()));
path = storageClient1.upload_file1(file.getContent(), ext, mata_list);
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
/**
* fastDFS文件下载
*
* @param groupName 组名
* @param remoteFileName 文件名
* @return ResponseEntity<byte [ ]>
*/
public byte[] downloadFile(String groupName, String remoteFileName) {
byte[] content = null;
try {
content = storageClient1.download_file(groupName, remoteFileName);
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
/**
* 根据fastDFS返回的path得到文件的组名
*
* @param path fastDFS返回的path
* @return
*/
public String getGroupFormFilePath(String path) {
return path.split("/")[0];
}
/**
* 根据fastDFS返回的path得到文件名
*
* @param path fastDFS返回的path
* @return
*/
public String getFileNameFormFilePath(String path) {
return path.substring(path.indexOf("/") + 1);
}
public String getVisitUrl() {
return this.setting.visit_url;
}
}
在代码中设置bean:
代码如下:
package net.w2p.local.plugins.BeanConfiguration;
import com.alibaba.druid.pool.DruidDataSource;
import net.w2p.local.plugins.config.DbConf;
import net.w2p.local.plugins.config.FdfsConf;
import net.w2p.local.plugins.utils.FastDFSUtils;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;
/**
* Druid监控web配置
*
* @author jinxiaoxin
*
*/
/*******/
@DependsOn(value = "fdfsConf")
@Configuration
public class FastDfsConfig {
@Autowired
@Bean(name = "fastDFSUtils")
public FastDFSUtils fastDFSUtils(@Qualifier("fdfsConf") FdfsConf fdfsConf) {
return new FastDFSUtils(fdfsConf);
}
}
测试
写测试代码:
@Autowired
FastDFSUtils fastDFSUtils;
@Test
public void testFdfs(){
try {
// FileInfo fileInfo = fastDFSUtils.getFileInfo1("x", "xxx");
FastDFSFile ffile=new FastDFSFile();
ffile.setSize(1L);
ffile.setName("test");
ffile.setContent(new byte[]{});
String str=fastDFSUtils.uploadFile(ffile);
System.out.println(str);
}
catch (Exception ed){
ed.printStackTrace();
}
}
测试结果:
测试成功。
结论
这些配置虽然可以用,但是结构,代码层次并非最优,还有优化的空间。
下一节就来配置zookeeper了。