Commit ee329005 authored by liuyan's avatar liuyan

fix:提交代码

parent 5b1541a6
No preview for this file type
......@@ -17,6 +17,8 @@
<dependencies>
<!-- spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
......@@ -30,23 +32,13 @@
<artifactId>springfox-boot-starter</artifactId>
</dependency>
<!-- 防止进入swagger页面报类型转换错误,排除3.0.0中的引用,手动增加1.6.2版本 -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.6.2</version>
</dependency>
<!-- Mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>net.devh</groupId>-->
<!-- <artifactId>grpc-spring-boot-starter</artifactId>-->
<!-- <version>2.15.0.RELEASE</version>-->
<!-- </dependency>-->
<!-- 核心模块-->
<dependency>
......
package com.ruoyi;
import com.ruoyi.client.server.GrpcFileServer;
import com.ruoyi.grpc.server.GrpcFileServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
......
......@@ -3,12 +3,14 @@ package com.ruoyi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
/**
* 启动程序
*
* @author ruoyi
*/
@EnableConfigurationProperties
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class RuoYiApplication
{
......
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: file.proto
package com.ruoyi.grpc.file;
public final class File {
private File() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistryLite registry) {
}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
registerAllExtensions(
(com.google.protobuf.ExtensionRegistryLite) registry);
}
static final com.google.protobuf.Descriptors.Descriptor
internal_static_FileUploadRequest_descriptor;
static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_FileUploadRequest_fieldAccessorTable;
static final com.google.protobuf.Descriptors.Descriptor
internal_static_FileUploadResponse_descriptor;
static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_FileUploadResponse_fieldAccessorTable;
static final com.google.protobuf.Descriptors.Descriptor
internal_static_FileDownloadRequest_descriptor;
static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_FileDownloadRequest_fieldAccessorTable;
static final com.google.protobuf.Descriptors.Descriptor
internal_static_FileDownloadResponse_descriptor;
static final
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
internal_static_FileDownloadResponse_fieldAccessorTable;
public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
static {
String[] descriptorData = {
"\n\nfile.proto\"@\n\021FileUploadRequest\022\022\n\010fil" +
"ename\030\001 \001(\tH\000\022\017\n\005chunk\030\002 \001(\014H\000B\006\n\004data\"2" +
"\n\022FileUploadResponse\022\016\n\006status\030\001 \001(\t\022\014\n\004" +
"size\030\002 \001(\003\"\'\n\023FileDownloadRequest\022\020\n\010fil" +
"ename\030\001 \001(\t\"%\n\024FileDownloadResponse\022\r\n\005c" +
"hunk\030\001 \001(\0142\205\001\n\013FileService\0227\n\nUploadFile" +
"\022\022.FileUploadRequest\032\023.FileUploadRespons" +
"e(\001\022=\n\014DownloadFile\022\024.FileDownloadReques" +
"t\032\025.FileDownloadResponse0\001B\035\n\023com.ruoyi." +
"grpc.fileP\001Z\004./pbb\006proto3"
};
descriptor = com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
});
internal_static_FileUploadRequest_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_FileUploadRequest_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_FileUploadRequest_descriptor,
new String[] { "Filename", "Chunk", "Data", });
internal_static_FileUploadResponse_descriptor =
getDescriptor().getMessageTypes().get(1);
internal_static_FileUploadResponse_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_FileUploadResponse_descriptor,
new String[] { "Status", "Size", });
internal_static_FileDownloadRequest_descriptor =
getDescriptor().getMessageTypes().get(2);
internal_static_FileDownloadRequest_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_FileDownloadRequest_descriptor,
new String[] { "Filename", });
internal_static_FileDownloadResponse_descriptor =
getDescriptor().getMessageTypes().get(3);
internal_static_FileDownloadResponse_fieldAccessorTable = new
com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
internal_static_FileDownloadResponse_descriptor,
new String[] { "Chunk", });
}
// @@protoc_insertion_point(outer_class_scope)
}
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: file.proto
package com.ruoyi.grpc.file;
public interface FileDownloadRequestOrBuilder extends
// @@protoc_insertion_point(interface_extends:FileDownloadRequest)
com.google.protobuf.MessageOrBuilder {
/**
* <code>string filename = 1;</code>
* @return The filename.
*/
String getFilename();
/**
* <code>string filename = 1;</code>
* @return The bytes for filename.
*/
com.google.protobuf.ByteString
getFilenameBytes();
}
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: file.proto
package com.ruoyi.grpc.file;
public interface FileDownloadResponseOrBuilder extends
// @@protoc_insertion_point(interface_extends:FileDownloadResponse)
com.google.protobuf.MessageOrBuilder {
/**
* <code>bytes chunk = 1;</code>
* @return The chunk.
*/
com.google.protobuf.ByteString getChunk();
}
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: file.proto
package com.ruoyi.grpc.file;
public interface FileUploadRequestOrBuilder extends
// @@protoc_insertion_point(interface_extends:FileUploadRequest)
com.google.protobuf.MessageOrBuilder {
/**
* <code>string filename = 1;</code>
* @return Whether the filename field is set.
*/
boolean hasFilename();
/**
* <code>string filename = 1;</code>
* @return The filename.
*/
String getFilename();
/**
* <code>string filename = 1;</code>
* @return The bytes for filename.
*/
com.google.protobuf.ByteString
getFilenameBytes();
/**
* <code>bytes chunk = 2;</code>
* @return Whether the chunk field is set.
*/
boolean hasChunk();
/**
* <code>bytes chunk = 2;</code>
* @return The chunk.
*/
com.google.protobuf.ByteString getChunk();
public FileUploadRequest.DataCase getDataCase();
}
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: file.proto
package com.ruoyi.grpc.file;
public interface FileUploadResponseOrBuilder extends
// @@protoc_insertion_point(interface_extends:FileUploadResponse)
com.google.protobuf.MessageOrBuilder {
/**
* <code>string status = 1;</code>
* @return The status.
*/
String getStatus();
/**
* <code>string status = 1;</code>
* @return The bytes for status.
*/
com.google.protobuf.ByteString
getStatusBytes();
/**
* <code>int64 size = 2;</code>
* @return The size.
*/
long getSize();
}
package com.ruoyi.client.server;
package com.ruoyi.grpc.server;
import com.ruoyi.client.service.FileServiceImpl;
import com.ruoyi.grpc.service.FileServiceImpl;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -11,15 +11,15 @@ import org.springframework.stereotype.Component;
public class GrpcFileServer {
@Autowired
private FileServiceImpl fileServiceImpl;
private FileServiceImpl fileService;
public void initServer(){
try {
// 创建服务实例
Server server = ServerBuilder.forPort(50051)
.addService(fileServiceImpl) // 关键控制器注册
.addService(fileService) // 关键控制器注册
.build();
server.start();
System.out.println("Server started on port 50051");
......
package com.ruoyi.client.service;
package com.ruoyi.grpc.service;
import com.google.protobuf.ByteString;
import com.ruoyi.grpc.file.*;
import com.ruoyi.user.dto.ClientUserOnline;
import com.ruoyi.user.service.TokenApiService;
import com.ruoyi.watch.service.INmyUserWatchSubscribeService;
import com.ruoyi.websocket.dto.WebSocketMsgDto;
import com.ruoyi.websocket.service.WebSocketService;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class FileServiceImpl extends FileServiceGrpc.FileServiceImplBase {
private static final Logger log = LoggerFactory.getLogger(FileServiceImpl.class);
@Value("${server.upload.base_path}")
String uploadBasePath;
@Autowired
private TokenApiService tokenApiService;
@Autowired
private INmyUserWatchSubscribeService myUserWatchSubscribeService;
@Autowired
private WebSocketService webSocketService;
@Override
public StreamObserver<FileUploadRequest> uploadFile(StreamObserver<FileUploadResponse> responseObserver) {
return new StreamObserver<FileUploadRequest>() {
// 定义成员变量保存上下文信息
private String filename;
private String token;
private Long userId;
private Long deviceId;
private String changeBasePath;
private FileOutputStream fos;
private long totalSize = 0;
@Override
public void onNext(FileUploadRequest request) {
try {
if (request.hasFilename()) {
filename = request.getFilename();
// 保存首个请求块中的元数据
this.filename = request.getFilename();
this.token = request.getToken();
this.changeBasePath = request.getBasePath();
// 解析用户信息
ClientUserOnline loginUser = tokenApiService.getLoginUser();
if (loginUser == null) {
throw new RuntimeException("无效Token: " + token);
}
this.userId = loginUser.getUserId();
this.deviceId = loginUser.getDeviceId();
// 创建文件对象
java.io.File ioFile = new java.io.File(uploadBasePath + filename);
// 创建文件所在的目录
......@@ -44,6 +82,7 @@ public class FileServiceImpl extends FileServiceGrpc.FileServiceImplBase {
}
}catch (Exception e){
e.printStackTrace();
cleanupResources();
// 返回 INVALID_ARGUMENT 错误
responseObserver.onError(Status.INVALID_ARGUMENT
.withDescription("Upload failed:" + e.getMessage())
......@@ -54,6 +93,7 @@ public class FileServiceImpl extends FileServiceGrpc.FileServiceImplBase {
@Override
public void onError(Throwable t) {
cleanupResources();
System.err.println("客户端中断上传: " + t.getMessage());
}
......@@ -66,6 +106,13 @@ public class FileServiceImpl extends FileServiceGrpc.FileServiceImplBase {
.setSize(totalSize)
.build());
responseObserver.onCompleted();
//发消息让该同步的客户端进行同步的消息
List<WebSocketMsgDto> webSocketSendMsg = myUserWatchSubscribeService.getWebSocketSendMsg(userId, deviceId, changeBasePath);
//TODO
webSocketService.sendMsg(new ArrayList<>(),"add");
} catch (IOException e) {
e.printStackTrace();
// 最终错误处理
......@@ -74,6 +121,17 @@ public class FileServiceImpl extends FileServiceGrpc.FileServiceImplBase {
.asRuntimeException());
}
}
private void cleanupResources() {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
log.warn("文件流关闭异常", e);
}
}
};
}
......
......@@ -2,7 +2,6 @@ syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.ruoyi.grpc.file";
option go_package = "./pb";
message FileUploadRequest {
//表示这两个字段只能有一个被设置值,要么是文件名称要么是文件块
......@@ -10,6 +9,9 @@ message FileUploadRequest {
string filename = 1;
bytes chunk = 2;
}
//监控的资源需要上传,服务端要判断是哪个资源变化了,更新对应目录下面的文件,并同步给需要的客户端
string basePath = 3;
string token = 4;
}
message FileUploadResponse {
......
......@@ -47,15 +47,12 @@ user:
# Spring配置
spring:
# 引入项目自定义文件
config:
import: "optional:classpath:server-config.yml"
# 资源信息
messages:
# 国际化资源文件路径
basename: i18n/messages
profiles:
active: druid
active: druid,dev
# 文件上传
servlet:
multipart:
......@@ -132,3 +129,9 @@ xss:
urlPatterns: /system/*,/monitor/*,/tool/*
# token配置给客户端
tokenapi: # 令牌自定义标识
header: Authorization
# 令牌密钥
secret: abcdefghijklmnopqrstuvwxyz
server:
upload:
base_path : E:\server_file\ #文件上传至服务器的路径
......@@ -16,6 +16,16 @@
</description>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 防止进入swagger页面报类型转换错误,排除3.0.0中的引用,手动增加1.6.2版本 -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.6.2</version>
</dependency>
<!-- Spring框架基本的核心工具 -->
<dependency>
......@@ -121,4 +131,4 @@
</dependencies>
</project>
\ No newline at end of file
</project>
......@@ -114,7 +114,7 @@ public class SecurityConfig
requests.antMatchers("/login", "/register", "/captchaImage").permitAll()
// 静态资源,可匿名访问
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**","/ws/**").permitAll()
// 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated();
})
......
......@@ -16,13 +16,27 @@
</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<!-- 通用工具-->
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common</artifactId>
</dependency>
<!-- websocket -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
</project>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment