1:添加spring-data-redis1.7.1相关jar及测试代码
2:添加fastdfs的相关jar及测试代码 3:修改某些系统文件中项目名称为maat_service(有些系统配置文件里面的项目名称没有改过来)
This commit is contained in:
28
src/main/java/com/nis/datasource/DynamicJedisDataBase.java
Normal file
28
src/main/java/com/nis/datasource/DynamicJedisDataBase.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.nis.datasource;
|
||||
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
import com.nis.web.service.SpringContextHolder;
|
||||
|
||||
/**
|
||||
*
|
||||
* <p>Title: DynamicJedisDataBase</p>
|
||||
* <p>Description: 设置使用redis的哪个数据库</p>
|
||||
* <p>Company: IIE</p>
|
||||
* @author rkg
|
||||
* @date 2018年5月17日
|
||||
*
|
||||
*/
|
||||
public class DynamicJedisDataBase {
|
||||
/**
|
||||
* 设置redisTemplate使用哪个数据库,默认使用的是数据库0
|
||||
* @param index
|
||||
* @param redisTemplate
|
||||
*/
|
||||
public static void setRedisDataBase(int index, RedisTemplate redisTemplate) {
|
||||
JedisConnectionFactory connectionFactory = SpringContextHolder.getBean("connectionFactory");
|
||||
connectionFactory.setDatabase(index);
|
||||
redisTemplate.setConnectionFactory(connectionFactory);
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import com.nis.util.StringUtils;
|
||||
import com.nis.web.controller.BaseRestController;
|
||||
import com.nis.web.service.SaveRequestLogThread;
|
||||
import com.nis.web.service.ServicesRequestLogService;
|
||||
import com.nis.web.service.restful.TestRedisService;
|
||||
import com.nis.web.service.restful.ConfigSourcesService;
|
||||
import com.wordnik.swagger.annotations.Api;
|
||||
import com.wordnik.swagger.annotations.ApiOperation;
|
||||
@@ -48,6 +49,22 @@ public class ConfigSourcesController extends BaseRestController {
|
||||
@Autowired
|
||||
protected ServicesRequestLogService servicesRequestLogService;
|
||||
|
||||
@Autowired
|
||||
TestRedisService testRedisServiceimpl;
|
||||
|
||||
@RequestMapping(value = "/save", method = RequestMethod.GET)
|
||||
@ApiOperation(value = "test redis", httpMethod = "GET", response = Map.class, notes = "测试redis事务的crontroller")
|
||||
@ApiParam(value = "test redis", name = "测试redis事务的crontroller", required = true)
|
||||
public String testRedis() {
|
||||
try {
|
||||
testRedisServiceimpl.saveConfigCompile();
|
||||
return "ok";
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "false";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/cfg/v1/configSources", method = RequestMethod.POST)
|
||||
@ApiOperation(value = "业务配置存储", httpMethod = "POST", response = Map.class, notes = "对有效的配置(封堵|监测|白名单)存储")
|
||||
@ApiParam(value = "业务配置数据源", name = "configSource", required = true)
|
||||
@@ -123,14 +140,16 @@ public class ConfigSourcesController extends BaseRestController {
|
||||
checkOpAction(thread, System.currentTimeMillis() - start, opAction, 2);
|
||||
String msg = "";
|
||||
ConfigCompile compile = configSource.getConfigCompileList().get(0);
|
||||
if(StringUtil.isEmpty(compile.getGroupRelationList())&&StringUtil.isEmpty(compile.getIpRegionList())&&StringUtil.isEmpty(compile.getStrRegionList())&&StringUtil.isEmpty(compile.getNumRegionList())){
|
||||
if (StringUtil.isEmpty(compile.getGroupRelationList()) && StringUtil.isEmpty(compile.getIpRegionList())
|
||||
&& StringUtil.isEmpty(compile.getStrRegionList())
|
||||
&& StringUtil.isEmpty(compile.getNumRegionList())) {
|
||||
msg = configSourcesService.updateByJDBCThread3(thread, start, configSource.getConfigCompileList(),
|
||||
configSource.getOpTime(), sb);
|
||||
}else{
|
||||
configSource.getOpTime(), sb);
|
||||
} else {
|
||||
msg = configSourcesService.updateByJDBCThread12(thread, start, configSource.getConfigCompileList(),
|
||||
configSource.getOpTime(), sb);
|
||||
configSource.getOpTime(), sb);
|
||||
}
|
||||
|
||||
|
||||
if (msg.equals("error")) {
|
||||
String errorCode = "";
|
||||
Exception exception = ConfigSourcesService.getMsgList().get(0);
|
||||
@@ -240,7 +259,8 @@ public class ConfigSourcesController extends BaseRestController {
|
||||
|
||||
}
|
||||
}
|
||||
Map<Integer, String> throwExceptionInfo = OracleErrorCodeUtil.throwExceptionInfo(errorCode);
|
||||
Map<Integer, String> throwExceptionInfo = OracleErrorCodeUtil
|
||||
.throwExceptionInfo(errorCode);
|
||||
for (int errorNum : throwExceptionInfo.keySet()) {
|
||||
if (errorNum == 998) {
|
||||
thread.setExceptionInfo(e.toString());
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package com.nis.web.controller.restful;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.csource.common.MyException;
|
||||
import org.csource.common.NameValuePair;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.nis.web.service.fdfs.FastDFSFile;
|
||||
import com.nis.web.service.fdfs.FileManager;
|
||||
|
||||
/**
|
||||
* <p>Title: FastDFSController.java</p>
|
||||
* <p>Description: fastdfs测试controller</p>
|
||||
* <p>Company: IIE</p>
|
||||
* @author rkg
|
||||
* @date 2018年5月16日
|
||||
*
|
||||
*/
|
||||
@Controller
|
||||
public class FastDFSController {
|
||||
public static void main(String[] args) {
|
||||
String path = new FastDFSController().testAdd();
|
||||
//String path = "M00/00/01/CgAGwFr786aAegOQAADz3NN2rlY283.jpg";
|
||||
new FastDFSController().testDelete(path);
|
||||
|
||||
}
|
||||
|
||||
public void testDelete(String filePath) {
|
||||
filePath = filePath.substring(filePath.indexOf("group"));
|
||||
String groupName=filePath.substring(0,filePath.indexOf("/"));
|
||||
filePath=filePath.substring(filePath.indexOf("/")+1);
|
||||
Integer a = FileManager.delete_file(groupName, filePath);
|
||||
System.out.println(a);
|
||||
}
|
||||
|
||||
public String testAdd() {
|
||||
String filePath = "d:/1.jpg";
|
||||
File file = new File(filePath);
|
||||
String fileName = file.getName();
|
||||
String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
NameValuePair[] meta_list = new NameValuePair[4];
|
||||
meta_list[0] = new NameValuePair("fileName", fileName);
|
||||
meta_list[1] = new NameValuePair("fileLength", String.valueOf(file.length()));
|
||||
meta_list[2] = new NameValuePair("fileExt", ext);
|
||||
meta_list[3] = new NameValuePair("fileAuthor", "rkg");
|
||||
String successFilePath = FileManager.upload(new FastDFSFile(getByte(file), ext), null);
|
||||
System.out.println("上传成功返回的路径是" + successFilePath);
|
||||
return successFilePath;
|
||||
}
|
||||
|
||||
private byte[] getByte(File file) {
|
||||
byte[] bf = null;
|
||||
try {
|
||||
InputStream fis = new FileInputStream(file);
|
||||
bf = new byte[fis.available()];
|
||||
fis.read(bf);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return bf;
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping(value = "/add", method = RequestMethod.POST)
|
||||
// public String add(@Validated User user, MultipartFile attach,
|
||||
// HttpServletRequest request)
|
||||
public String add(MultipartFile attach, HttpServletRequest request) throws IOException, MyException {
|
||||
// 获取文件后缀名
|
||||
String ext = attach.getOriginalFilename().substring(attach.getOriginalFilename().lastIndexOf(".") + 1);
|
||||
FastDFSFile file = new FastDFSFile(attach.getBytes(), ext);
|
||||
NameValuePair[] meta_list = new NameValuePair[4];
|
||||
meta_list[0] = new NameValuePair("fileName", attach.getOriginalFilename());
|
||||
meta_list[1] = new NameValuePair("fileLength", String.valueOf(attach.getSize()));
|
||||
meta_list[2] = new NameValuePair("fileExt", ext);
|
||||
meta_list[3] = new NameValuePair("fileAuthor", "rkg");
|
||||
String filePath = FileManager.upload(file, meta_list);
|
||||
// user.setFilePath(filePath);
|
||||
// users.put(user.getUsername(), user);
|
||||
return filePath;
|
||||
}
|
||||
|
||||
// public ResponseEntity<byte[]> download(@PathVariable String filePath,
|
||||
// HttpServletResponse response)
|
||||
@RequestMapping(value = "/download", method = RequestMethod.GET)
|
||||
public ResponseEntity<byte[]> download(@RequestParam("filePath") String filePath, HttpServletResponse response)
|
||||
throws IOException, MyException {
|
||||
// User u = users.get(username);
|
||||
// String filePath = u.getFilePath();
|
||||
// String filePath = "";
|
||||
String substr = filePath.substring(filePath.indexOf("group"));
|
||||
String group = substr.split("/")[0];
|
||||
String remoteFileName = substr.substring(substr.indexOf("/") + 1);
|
||||
// 重命名文件
|
||||
String specFileName = "rkg" + substr.substring(substr.indexOf("."));
|
||||
return FileManager.download(group, remoteFileName, specFileName);
|
||||
}
|
||||
}
|
||||
13
src/main/java/com/nis/web/dao/impl/BaseRedisDao.java
Normal file
13
src/main/java/com/nis/web/dao/impl/BaseRedisDao.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.nis.web.dao.impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public abstract class BaseRedisDao<K, V> {
|
||||
|
||||
@Autowired
|
||||
protected RedisTemplate<K, V> redisTemplate;
|
||||
|
||||
}
|
||||
80
src/main/java/com/nis/web/service/fdfs/FastDFSFile.java
Normal file
80
src/main/java/com/nis/web/service/fdfs/FastDFSFile.java
Normal file
@@ -0,0 +1,80 @@
|
||||
package com.nis.web.service.fdfs;
|
||||
|
||||
/**
|
||||
* <p>Title: FastDFSFile.java</p>
|
||||
* <p>Description: </p>
|
||||
* <p>Company: IIE</p>
|
||||
* @author rkg
|
||||
* @date 2018年5月16日
|
||||
*
|
||||
*/
|
||||
|
||||
public class FastDFSFile extends FileManagerConfig {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private byte[] content;
|
||||
private String name;
|
||||
private String ext;
|
||||
private String length;
|
||||
private String author = FILE_DEFAULT_AUTHOR;
|
||||
|
||||
public FastDFSFile(byte[] content, String ext) {
|
||||
this.content = content;
|
||||
this.ext = ext;
|
||||
}
|
||||
|
||||
public FastDFSFile(byte[] content, String name, String ext) {
|
||||
this.content = content;
|
||||
this.name = name;
|
||||
this.ext = ext;
|
||||
}
|
||||
|
||||
public FastDFSFile(byte[] content, String name, String ext, String length, String author) {
|
||||
this.content = content;
|
||||
this.name = name;
|
||||
this.ext = ext;
|
||||
this.length = length;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
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 String getExt() {
|
||||
return ext;
|
||||
}
|
||||
|
||||
public void setExt(String ext) {
|
||||
this.ext = ext;
|
||||
}
|
||||
|
||||
public String getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public void setLength(String length) {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
}
|
||||
108
src/main/java/com/nis/web/service/fdfs/FileManager.java
Normal file
108
src/main/java/com/nis/web/service/fdfs/FileManager.java
Normal file
@@ -0,0 +1,108 @@
|
||||
package com.nis.web.service.fdfs;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.csource.common.NameValuePair;
|
||||
import org.csource.fastdfs.ClientGlobal;
|
||||
import org.csource.fastdfs.StorageClient;
|
||||
import org.csource.fastdfs.StorageServer;
|
||||
import org.csource.fastdfs.TrackerClient;
|
||||
import org.csource.fastdfs.TrackerServer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
/**
|
||||
* <p>Title: FileManager.java</p>
|
||||
* <p>Description: </p>
|
||||
* <p>Company: IIE</p>
|
||||
* @author rkg
|
||||
* @date 2018年5月16日
|
||||
*
|
||||
*/
|
||||
|
||||
public class FileManager extends FileManagerConfig {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static TrackerClient trackerClient;
|
||||
private static TrackerServer trackerServer;
|
||||
private static StorageServer storageServer;
|
||||
private static StorageClient storageClient;
|
||||
|
||||
static {
|
||||
try {
|
||||
String classPath = new File(FileManager.class.getResource("/").getFile()).getCanonicalPath();
|
||||
String fdfsClientConfigFilePath = classPath + File.separator + CLIENT_CONFIG_FILE;
|
||||
ClientGlobal.init(fdfsClientConfigFilePath);
|
||||
trackerClient = new TrackerClient();
|
||||
trackerServer = trackerClient.getConnection();
|
||||
storageClient = new StorageClient(trackerServer, storageServer);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向fastdfs上传文件
|
||||
* @param file
|
||||
* @param valuePairs
|
||||
* @return
|
||||
*/
|
||||
public static String upload(FastDFSFile file, NameValuePair[] valuePairs) {
|
||||
String[] uploadResults = null;
|
||||
try {
|
||||
// uploadResults = storageClient.upload_file(file.getContent(), file.getExt(),
|
||||
// valuePairs);
|
||||
uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), valuePairs);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String groupName = uploadResults[0];
|
||||
String remoteFileName = uploadResults[1];
|
||||
|
||||
String fileAbsolutePath = PROTOCOL + TRACKER_NGNIX_ADDR
|
||||
// + trackerServer.getInetSocketAddress().getHostName()
|
||||
// + SEPARATOR + TRACKER_NGNIX_PORT
|
||||
+ SEPARATOR + groupName + SEPARATOR + remoteFileName;
|
||||
return fileAbsolutePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从fastdfs下载文件
|
||||
* @param groupName
|
||||
* @param remoteFileName
|
||||
* @param specFileName
|
||||
* @return
|
||||
*/
|
||||
public static ResponseEntity<byte[]> download(String groupName, String remoteFileName, String specFileName) {
|
||||
byte[] content = null;
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
try {
|
||||
content = storageClient.download_file(groupName, remoteFileName);
|
||||
headers.setContentDispositionFormData("attachment",
|
||||
new String(specFileName.getBytes("UTF-8"), "iso-8859-1"));
|
||||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new ResponseEntity<byte[]>(content, headers, HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据组和文件名删除文件
|
||||
* @param group 文件所属组
|
||||
* @param filePath 文件存放路径,不包含group,例如文件url是http://10.0.6.192/group1/M00/00/01/CgAGwFr786aAegOQAADz3NN2rlY283.jpg,那么filePath=/M00/00/01/CgAGwFr786aAegOQAADz3NN2rlY283.jpg
|
||||
* @return 返回0代表成功,其他则失败
|
||||
*/
|
||||
public static Integer delete_file(String group, String filePath) {
|
||||
Integer result = -1;
|
||||
try {
|
||||
result = storageClient.delete_file(group, filePath);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.nis.web.service.fdfs;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* <p>Title: FileManagerConfig.java</p>
|
||||
* <p>Description: </p>
|
||||
* <p>Company: IIE</p>
|
||||
* @author rkg
|
||||
* @date 2018年5月16日
|
||||
*
|
||||
*/
|
||||
|
||||
public class FileManagerConfig implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final String FILE_DEFAULT_AUTHOR = "renkaige";
|
||||
|
||||
public static final String PROTOCOL = "http://";
|
||||
|
||||
public static final String SEPARATOR = "/";
|
||||
|
||||
public static final String TRACKER_NGNIX_ADDR = "10.0.6.192";
|
||||
|
||||
public static final String TRACKER_NGNIX_PORT = "";
|
||||
|
||||
public static final String CLIENT_CONFIG_FILE = "fdfs_client.conf";
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.nis.web.service.restful;
|
||||
|
||||
/**
|
||||
*
|
||||
* <p>Title: TestRedisService</p>
|
||||
* <p>Description: 测试redis事务</p>
|
||||
* <p>Company: IIE</p>
|
||||
* @author rkg
|
||||
* @date 2018年5月17日
|
||||
*
|
||||
*/
|
||||
public interface TestRedisService {
|
||||
public void saveConfigCompile();
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.nis.web.service.restful;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.nis.datasource.DynamicJedisDataBase;
|
||||
import com.nis.web.dao.impl.BaseRedisDao;
|
||||
|
||||
@Service("testRedisServiceimpl")
|
||||
public class TestRedisServiceimpl extends BaseRedisDao<String, String> implements TestRedisService {
|
||||
@Transactional
|
||||
public void saveConfigCompile() {
|
||||
DynamicJedisDataBase.setRedisDataBase(2, redisTemplate);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
redisTemplate.boundZSetOps("1").add("a" + i, i);
|
||||
if (i == 5) {
|
||||
// int a = 1 / 0;
|
||||
}
|
||||
}
|
||||
// 切换redis数据库
|
||||
DynamicJedisDataBase.setRedisDataBase(3, redisTemplate);
|
||||
for (int i = 10; i < 20; i++) {
|
||||
redisTemplate.boundZSetOps("1").add("a" + i, i);
|
||||
if (i == 5) {
|
||||
// int a = 1 / 0;
|
||||
}
|
||||
}
|
||||
DynamicJedisDataBase.setRedisDataBase(4, redisTemplate);
|
||||
for (int i = 20; i < 30; i++) {
|
||||
redisTemplate.boundZSetOps("1").add("a" + i, i);
|
||||
if (i == 25) {
|
||||
int a = 1 / 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user