1. 机构类型的CRUD
2. 使用DTO的方式封装数据
使用场景:前台发送数据封装对象的时候,一些字段不能满足需求,可能是其他类中的,因此可以使用dto(data to object)的方式封装数据!
原则:
①:扁平化(接收几个字段,就定义几个字段)
②:使用对象组合的方式
example:
public class EnteringDTO {
private Employee employee;
private Tenant tenant;
private Long mealId;
...
}
3. course使用Hystrix熔断功能返回托底数据
①:配置文件中开启Hystrix
feign:
hystrix:
enabled: true
②:创建一个类实现接口,使用匿名内部类的方式重写托底返回的数据
public class CourseFeignFallbackFactory implements FallbackFactory<RedisFeignClient> {
@Override
public RedisFeignClient create(Throwable throwable) {
return new RedisFeignClient() {
@Override
public AjaxResult set(String key, String value) {
return null;
}
@Override
public AjaxResult get(String key) {
return AjaxResult.me().setSuccess(false)
.setMessage("发生了一点小问题:["+throwable.getMessage()+"]");
}
};
}
}
③:在feign客户端中指定托底类 fallback:不能拿到异常信息,fallbackFactory:可以拿到异常信息
@FeignClient(value = "redis-server",fallbackFactory = CourseFeignFallbackFactory.class)
public interface RedisFeignClient {
@PostMapping("/redis/set")
AjaxResult set(@RequestParam("key") String key, @RequestParam("value")String value);
@GetMapping("/redis/get/{key}")
AjaxResult get(@PathVariable("key") String key);
}
4. 分布式文件处理
4.1 linux安装FastDFS
参考上篇文章
4.2 FastDFS服务搭建
①:集成到注册中心
②:集成到配置中心
③:zuul配置网关地址
④:swagger集成
⑤:zuul中swagger整合
4.3 FastDFS实现文件上传
①:导入FastDFS依赖
在此网站https://github.com/happyfish100/fastdfs-client-java 下下载压缩包,解压,在当前目录执行cmd
执行命令 mvn -clean install 打成jar包,安装到本地仓库!然后在idea中导包!
②:在resources下创建配置文件fdfs_client.conf 指向tracker_server虚拟机的ip
tracker_server=118.25.154.214:22122
②:导入工具类
public class FastDfsApiOpr {
public static String CONF_FILENAME = FastDfsApiOpr.class.getClassLoader()
.getResource("fdfs_client.conf").getFile();
/**
* 上传文件
* @param file
* @param extName
* @return
*/
public static String upload(byte[] file,String extName) {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getTrackerServer();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String fileIds[] = storageClient.upload_file(file,extName,null);
System.out.println(fileIds.length);
System.out.println("组名:" + fileIds[0]);
System.out.println("路径: " + fileIds[1]);
return "/"+fileIds[0]+"/"+fileIds[1];
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 上传文件
* @param extName
* @return
*/
public static String upload(String path,String extName) {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getTrackerServer();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String fileIds[] = storageClient.upload_file(path, extName,null);
System.out.println(fileIds.length);
System.out.println("组名:" + fileIds[0]);
System.out.println("路径: " + fileIds[1]);
return "/"+fileIds[0]+"/"+fileIds[1];
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 下载文件
* @param groupName
* @param fileName
* @return
*/
public static byte[] download(String groupName,String fileName) {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getTrackerServer();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
byte[] b = storageClient.download_file(groupName, fileName);
return b;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 删除文件
* @param groupName
* @param fileName
*/
public static void delete(String groupName,String fileName){
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getTrackerServer();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer,
storageServer);
int i = storageClient.delete_file(groupName,fileName);
System.out.println( i==0 ? "删除成功" : "删除失败:"+i);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("删除异常,"+e.getMessage());
}
}
}
④:在controller中执行文件上传,可以使用io工具包
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
@RestController
@RequestMapping("/fastdfs")
public class FastDFSController {
@PostMapping("/upload")
public AjaxResult upload(MultipartFile file){
String fileExtensionName = FilenameUtils.getExtension(file.getOriginalFilename());
try {
String filePath = FastDfsApiOpr.upload(file.getBytes(), fileExtensionName);
return AjaxResult.me().setSuccess(true).setMessage("上传成功!").setResultObj(filePath);
} catch (IOException e) {
e.printStackTrace();
return AjaxResult.me().setSuccess(false).setMessage("上传失败!");
}
}
}