创建视频点播微服务工程
创建工程,注意父工程为 service
引入依赖
修改总父工程依赖
<properties>
<java.version>1.8</java.version>
<mybatis-plus.version>3.0.5</mybatis-plus.version>
<velocity.version>2.0</velocity.version>
<swagger.version>2.7.0</swagger.version>
<jodatime.version>2.10.1</jodatime.version>
<poi.version>3.17</poi.version>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
<commons-io.version>2.6</commons-io.version>
<httpclient.version>4.5.1</httpclient.version>
<jwt.version>0.7.0</jwt.version>
<aliyun-java-sdk-core.version>4.3.3</aliyun-java-sdk-core.version>
<aliyun-sdk-oss.version>3.10.2</aliyun-sdk-oss.version>
<aliyun-java-sdk-vod.version>2.15.2</aliyun-java-sdk-vod.version>
<aliyun-sdk-vod-upload.version>1.4.13</aliyun-sdk-vod-upload.version>
<fastjson.version>1.2.28</fastjson.version>
<gson.version>2.8.2</gson.version>
<json.version>20170516</json.version>
<commons-dbutils.version>1.7</commons-dbutils.version>
<canal.client.version>1.1.0</canal.client.version>
<docker.image.prefix>zx</docker.image.prefix>
<cloud-alibaba.version>0.2.2.RELEASE</cloud-alibaba.version>
</properties>
<!--锁定依赖的版本-->
<dependencyManagement>
<dependencies>
<!--Spring Cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--mybatis-plus 持久层-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!-- velocity 模板引擎, Mybatis Plus 代码生成器需要 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${velocity.version}</version>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<!--swagger ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<!--aliyunOSS-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>${aliyun-sdk-oss.version}</version>
</dependency>
<!--日期时间工具-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${jodatime.version}</version>
</dependency>
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<!--xlsx-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<!--文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<!--commons-io-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<!--httpclient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<!-- JWT -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jwt.version}</version>
</dependency>
<!--aliyun-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>${aliyun-java-sdk-core.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>${aliyun-java-sdk-vod.version}</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId>
<version>${aliyun-sdk-vod-upload.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json.version}</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>${commons-dbutils.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.otter</groupId>
<artifactId>canal.client</artifactId>
<version>${canal.client.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
添加本地依赖,添加方式如下
mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-sdk-vod-upload -Dversion=1.4.13 -Dpackaging=jar -Dfile=aliyun-java-vod-upload-1.4.13.jar
service_vod 模块依赖如下
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20170516</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId>
</dependency>
</dependencies>
在 resources
当中创建配置文件 applicaiton.yml
# application-dev.yml
server
port8003
logging
pattern
console"%clr(%5p) %clr(-){faint} %clr(%-80.80logger{79}){cyan} %clr(:) %m%n"
spring
application
name service-vod
profiles
active dev
servlet
multipart
max-file-size 1024MB
max-request-size 1024MB
# 定义oss相关属性
aliyun
keyid LTAI4GH19ep2P8caxW2E2N6A
keysecret NAmWiBmKXofw7hs4JRcx7SLhF1MN6P
如上配置文件当中的 keyid
和 keysecret
是我们之前做 OSS 配置文件当中的自行拷贝自己的即可如下图
在 service_vod 工程当中创建 top.it6666.service_vod
包在该包下创建启动类
/**
* @author BNTang
*/
(exclude = DataSourceAutoConfiguration.class)
(basePackages = {"top.it6666"})
public class VodApplication {
public static void main(String[] args) {
SpringApplication.run(VodApplication.class, args);
}
}
创建微服务点播工具类,在 utils 包中创建配置文件读取常量工具类 VodConstant
/**
* @author BNTang
* @version 1.0
* @project video_parent
* @description 配置文件读取常量工具类
* @since Created in 2021/4/17 22:01
**/
public class VodConstant implements InitializingBean {
("${aliyun.keyid}")
private String keyId;
("${aliyun.keysecret}")
private String keySecret;
public static String ACCESS_KEY_ID;
public static String ACCESS_KEY_SECRET;
public void afterPropertiesSet() {
ACCESS_KEY_ID = keyId;
ACCESS_KEY_SECRET = keySecret;
}
}
在 utils 包中创建阿里云点播初始化工具类
/**
* @author BNTang
* @version 1.0
* @project video_parent
* @description 阿里云点播初始化工具类
* @since Created in 2021/4/17 22:03
**/
public class ALiYunVodSdkUtils {
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
// 点播服务接入区域 不能修改
String regionId = "cn-shanghai";
DefaultProfile profile = DefaultProfile.getProfile(
regionId,
accessKeyId,
accessKeySecret);
return new DefaultAcsClient(profile);
}
}
请求接口与业务
在 Controller 包下创建 VodController
/**
* @author BNTang
* @version 1.0
* @project video_parent
* @description 视频点播与上传请求接口与业务
* @since Created in 2021/4/17 22:06
**/
("/service_vod/vod")
(tags = "视频组")
public class VodController {
private VodService vodService;
/**
* <b>
* 视频上传
* </b>
*/
(value = "视频上传")
("/upload")
public ResponseResult uploadVideo( ("file") MultipartFile file) {
String videoId = vodService.uploadVideo(file);
return ResponseResult.ok().message("视频上传成功!").data("videoId", videoId);
}
/**
* <b>
* 删除视频
* </b>
*/
(value = "删除视频")
("/delete-vod/{videoId}")
public ResponseResult removeVideo( String videoId) {
vodService.deleteVideo(videoId);
return ResponseResult.ok().message("视频删除成功!");
}
}
上传业务与删除业务,在 service 包下创建 VodService
/**
* @author BNTang
* @version 1.0
* @project video_parent
* @description 视频组业务
* @since Created in 2021/4/17 22:10
**/
public interface VodService {
/**
* <p>
* 上传视频
* </p>
*
* @param file 视频文件
* @return 上传结果
*/
String uploadVideo(MultipartFile file);
/**
* <p>
* 根据 videoId 删除对应的视频
* </p>
*
* @param videoId 删除结果
*/
void deleteVideo(String videoId);
}
紧接着在 service 包当中创建 impl 包在创建对应的 VodServiceImpl
实现类如下
/**
* @author BNTang
* @version 1.0
* @project video_parent
* @description 视频组业务实现类
* @since Created in 2021/4/17 22:13
**/
public class VodServiceImpl implements VodService {
public String uploadVideo(MultipartFile file) {
try {
// 获取文件输入流
InputStream inputStream = file.getInputStream();
// 获取文件名
String originalFilename = Optional.ofNullable(file.getOriginalFilename()).orElse("");
//截取标题
String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
// 获取上传请求
UploadStreamRequest request = new UploadStreamRequest(
VodConstant.ACCESS_KEY_ID,
VodConstant.ACCESS_KEY_SECRET,
title, originalFilename, inputStream);
// 开始上传
UploadVideoImpl uploader = new UploadVideoImpl();
// 上传完毕后,服务器给于响应
UploadStreamResponse response = uploader.uploadStream(request);
// 如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。
// 其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
String videoId = response.getVideoId();
if (!response.isSuccess()) {
String errorMessage =
"阿里云上传错误:" + "code:" + response.getCode() + ", message:" + response.getMessage();
if (StringUtils.isEmpty(videoId)) {
throw new BnTangException(20001, errorMessage);
}
}
return videoId;
} catch (IOException e) {
throw new BnTangException(20001, "vod 服务上传失败");
}
}
public void deleteVideo(String videoId) {
try {
// 获取客户端请求
DefaultAcsClient client = ALiYunVodSdkUtils.initVodClient(
VodConstant.ACCESS_KEY_ID,
VodConstant.ACCESS_KEY_SECRET);
// 创建删除请求
DeleteVideoRequest request = new DeleteVideoRequest();
// 设置要删除的视频ID
request.setVideoIds(videoId);
// 删除请求后服务器给予的响应
client.getAcsResponse(request);
} catch (ClientException e) {
throw new BnTangException(20001, "视频删除失败");
}
}
}