<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
业务描述
基于Spring Cloud Alibaba解决方案实现文件上传,例如
初始架构设计
本次项目实践,整体上基于前后端分离架构,服务设计上基于spring cloud alibaba解决方案进行实现,例如:
工程创建及初始化
工程结构
参考如下工程结构,进行项目创建
创建父工程
创建项目父工程用来管理项目依赖.
创建客户端服务工程
创建一个客户端工程,在此工程中定义一些静态页面,例如文件上传页面.
父工程初始化
打开父工程的pom.xml文件,添加如下依赖:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
文件资源服务实现
添加项目依赖
在sca-resource工程中添加如下依赖:
<!--Spring Boot Web (服务-内置tomcat)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Nacos Discovery (服务注册发现)-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--Nacos Config (配置中心)-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--Sentinel (流量防卫兵-限流和熔断)-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--Spring Boot 监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
服务初始化配置
在项目的resources目录下创建bootstrap.yml配置文件(假如后续配置信息要写到配置中心配置文件名必须为bootstrap.yml),并添加如下内容:
server:
port: 8881 #配置访问端口
spring:
application:
name: sca-resource
resources:
static-locations: file:d:/uploads #静态资源路径
#(原先储存到/resources/static目标下的资源可以存储到此目录中)
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848
jt:
resource:
path: D:/uploads #设计上传文件存储的根目录(后续要写到配置文件)
host: http://localhost:8881/ #定义上传文件对应的访问服务器
构建项目启动类
package com.jt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FileApplication {
public static void main(String[] args) {
SpringApplication.run(FileApplication.class, args);
}
}
Controller逻辑实现
@RefreshScope //动态刷新,如果配置中心的数据变化了,那么属性值也需要变化
@Service
@Slf4j
public class ResourceServersImpl implements ResourceServers{
/**
* jt:
resource:
path: D:/uploads #设计上传文件存储的根目录(后续要写到配置文件)
host: http://localhost:8881/ #定义上传文件对应的访问服务器
*/
@Value("${jt.resource.path:d:/upload}")
private String resourcePath;
@Value("${jt.resource.host:http://localhost:8881/}")
private String resourceHost;
@Override
public String upload(MultipartFile uploadFile) throws IOException {
//1.创建文件的储存目录
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
//String dateStr2 = sdf.format(new Date());
// log.debug(dateStr2);
String dateStr =
DateTimeFormatter.ofPattern("yyyy/MM/dd").format(LocalDate.now());
log.debug(dateStr);
//合成总路径
File uploadDir = new File(resourcePath,dateStr );
//如果目标路径不存在
if(!uploadDir.exists()){
uploadDir.mkdirs();
}
//获取原始名称
String fileName = uploadFile.getOriginalFilename();
//获取后缀名
String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
//打印文件后缀
log.debug(fileSuffix);
//根据文件后缀判断文件是否是图片
if(!fileSuffix.matches("^(.jpg|.png|.gif|.jpeg)$")){
log.debug("上传文件类型错误");
return null;
}
//判断是不是恶意文件
InputStream inputStream = uploadFile.getInputStream();
BufferedImage bufferedImage = ImageIO.read(inputStream);
if(bufferedImage.getHeight()==0 || bufferedImage.getWidth()==0){
log.debug("上传文件错误");
return null;
}
//重构文件名
fileName = UUID.randomUUID().toString().replace("-", "")
.concat(fileSuffix);
log.debug(fileName);
//3上传至指定位置
//transferTo底层,文件复制
//基于inputStream读取uploadFile内容
//基于OutputStream写出文件
uploadFile.transferTo(new File(uploadDir,fileName));
//返回访问到图片的网页路径
String fileRealPath = resourceHost+dateStr+"/"+fileName;
log.debug(fileRealPath);
return fileRealPath;
}
}
设置跨域配置
我们在通过客户端工程,访问文件上传服务时,需要进行跨域配置,在服务端的跨域配置中有多种方案,最常见是在过滤器的层面进行跨域设计,
//方案二基于过滤器层面实现跨域配置
@Configuration
public class CorsFilterConfig {
@Bean
public FilterRegistrationBean<CorsFilter> filterFilterRegistrationBean(){
//创建基于Url的核心配置服务
UrlBasedCorsConfigurationSource configService =
new UrlBasedCorsConfigurationSource();
//设置核心配置
CorsConfiguration config = new CorsConfiguration();
//允许哪种请求头跨域
config.addAllowedHeader("*");
//允许哪种方法类型跨域 get post delete put
config.addAllowedMethod("*");
// 允许哪些请求源(ip:port)跨域
config.addAllowedOrigin("*");
//是否允许携带cookie跨域
config.setAllowCredentials(true);
//2.注册过滤器并设置其优先级
//配置服务.配置核心服务(配置路径,配置内容)
configService.registerCorsConfiguration("/****", config);
//过滤器Bean注册
FilterRegistrationBean fBean = new FilterRegistrationBean(
new CorsFilter(configService)
);
//设置其最高优先级
fBean.setOrder(Ordered.LOWEST_PRECEDENCE);
//返回一个过滤器
return fBean;
}
}
客户端工程逻辑实现
本次项目我们的客户端工程基于springboot工程进行设计,项目上线时可以将其静态资源直接放到一个静态资源目录中.
添加依赖
在sca-resource-ui工程的pom文件中添加web依赖,例如:
构建项目启动类
package com.jt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication .class, args);
}
}
创建文件上传页面
在工程的resources目录下创建static目录(假如这个目录已经存在则无需创建),然后在此目录创建fileupload.html静态页面,例如:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上载演示</title>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<form id="fileForm" method="post" enctype="multipart/form-data" onsubmit="return doUpload()">
<div>
<label>上传文件
<input id="uploadFile" type="file" name="uploadFile">
</label>
</div>
<button type="submit">上传文件</button>
</form>
</body>
<script>
//jquery代码的表单提交事件
function doUpload(){
//获得用户选中的所有图片(获得数组)
let files=document.getElementById("uploadFile").files;
if(files.length>0){
//获得用户选中的唯一图片(从数组中取出)
let file=files[0];
//开始上传这个图片
//由于上传代码比较多,不想和这里其它代码干扰,所以定义一个方法调用
upload(file);
}
//阻止表单提交效果
return false;
};
// 将file上传到服务器的方法
function upload(file){
//定义一个表单
let form=new FormData();
//将文件添加到表单中
form.append("uploadFile",file);
//异步提交
let url="http://localhost:8881/resource/upload/";
axios.post(url,form)
.then(function (response){
alert("upload ok")
console.log(response.data);
})
.catch(function (e){//失败时执行catch代码块
console.log(e);
})
}
</script>
</html>
启动服务访问测试
第一步:启动nacos服务(在这里做服务的注册和配置管理)
第二步:启动sca-resource服务,此服务提供文件上传功能
第三步:启动sca-resource-ui服务,此服务为客户端工程,提供静态资源的访问.所有页面放到此工程中.
第四步:打开浏览器,访问sca-resource-ui工程下的文件上传页面,例如:
API网关(Gateway)工程实践
概述
API 网关是外部资源对服务内部资源访问的入口,所以文件上传请求应该首先请求的是网关服务,然后由网关服务转发到具体的资源服务上。
服务调用架构
工程项目结构设计
创建网关工程及初始化
第一步:创建sca-resource-gateway工程,添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
第二步:创建配置文件bootstrap.xml
server:
port: 9000
spring:
application:
name: sca-resource-gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848
file-extension: yml
gateway:
discovery:
locator:
enabled: true
routes:
- id: router01
uri: lb://sca-resource
predicates:
- Path=/sca/resource/upload/**
filters:
- StripPrefix=1
第三步:构建项目启动类,并进行服务启动
package com.jt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ResourceApplication {
public static void main(String[] args) {
SpringApplication.run(ResourceApplication.class,args);
}
}
网关跨域配置
当我们基于Ajax技术访问网关时,需要在网关层面进行跨域设计,例如:
package com.jt.config;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
//@Configuration
public class CorsFilterConfig {
@Bean
public CorsWebFilter corsWebFilter(){
//1.构建基于url方式的跨域配置
UrlBasedCorsConfigurationSource source= new UrlBasedCorsConfigurationSource();
//2.进行跨域配置
CorsConfiguration config=new CorsConfiguration();
//2.1允许所有ip:port进行跨域
config.addAllowedOrigin("*");
//2.2允许所有请求头跨域
config.addAllowedHeader("*");
//2.3允许所有请求方式跨域:get,post,..
config.addAllowedMethod("*");
//2.4允许携带有效cookie进行跨域
config.setAllowCredentials(true);
source.registerCorsConfiguration("/**",config);
return new CorsWebFilter(source);
}
}
Spring Gateway工程中的跨域设计,除了可以在网关项目中以java代码方式进行跨域过滤器配置,还可以直接在配置文件进行跨域配置,例如:
spring:
cloud:
gateway:
globalcors: #跨域配置
corsConfigurations:
'[/**]':
allowedOrigins: "*"
allowedHeaders: "*"
allowedMethods: "*"
allowCredentials: true
启动工程进行服务访问
首先打开网关(Gateway),资源服务器(Resource),客户端工程服务(UI),然后修改fileupload.html文件中访问资源服务端的url
let url="http://localhost:9000/sca/resource/upload/";
AOP方式操作日志记录
页面描述
在实现文件上传业务时,添加记录日志的操作.
添加项目依赖
在sca-resource工程中添加AOP依赖,例如:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
创建切入点注解
我们项目要为目标业务实现功能增强,锦上添花,但系统要指定谁是目标业务,这里我们定义一个注解,后续用此注解描述目标业务。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RequiredLog {
String value() default "";
}
定义切入点方法
通过上面定义的注解RequiredLog,对sca-resources工程中的ResourceController文件上传方法进行描述,例如:
@RequiredLog("upload file")
@PostMapping("/upload/")
public String uploadFile(MultipartFile uploadFile) throws IOException {...}
说明:通过@RequiredLog注解描述的方法可以认为锦上添花的“锦”,后续添花的行为可以放在切面的通知方法中。
@Slf4j
@Aspect
@Component
public class LogAspect {
//定义切入点
@Pointcut("@annotation(com.jt.resource.annotation.RequiredLog)")
public void doLog(){}//锦上添花的锦(注解描述的方法)
//定义扩展业务逻辑
@Around("doLog()")
//@Around("@annotation(com.jt.resource.annotation.RequiredLog)")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
log.debug("Before {}",System.currentTimeMillis());
Object result=joinPoint.proceed();//执行执行链(其它切面,目标方法-锦)
log.debug("After {}",System.currentTimeMillis());
return result;//目标方法(切入点方法)的执行结果
}
}
AOP 方式日志记录原理分析
我们在基于AOP方式记录用户操作日志时,其底层工作流程如下:
说明:当我们在项目中定义了AOP切面以后,系统启动时,会对有@Aspect注解描述的类进行加载分析,基于切入点的描述为目标类型对象,创建代理对象,并在代理对象内部创建一个执行链,这个执行链中包含拦截器(封装了切入点信息),通知(Around,…),目标对象等,我们请求目标对象资源时,会直接按执行链的顺序对资源进行调用。
总结(Summary)
本章节已经文件上传为例回顾和加强微服务基础知识的掌握和实践。