学位数字档案馆系统:从零搭建到实战部署全指南

一、系统架构与核心技术选型

学位数字档案馆系统的核心需求是安全存储、高效检索和长期保存。我们采用微服务架构,确保系统可扩展性和维护性。

1.1 技术栈确定

后端使用Java 17 + Spring Boot 3.1.2,数据库使用PostgreSQL 15,全文检索使用Elasticsearch 8.11.0,文件存储使用MinIO,缓存使用Redis 7.0。

1.2 开发环境准备

安装以下软件,版本必须完全匹配:

  • JDK 17.0.8
  • Maven 3.9.4
  • PostgreSQL 15.3
  • Elasticsearch 8.11.0

二、数据库设计与初始化

2.1 创建数据库和用户

登录PostgreSQL执行以下命令:

``` CREATE DATABASE degree_archive; CREATE USER archive_admin WITH PASSWORD 'Archive@2024'; GRANT ALL PRIVILEGES ON DATABASE degree_archive TO archive_admin; ```

2.2 核心表结构

创建学位论文主表:

``` CREATE TABLE thesis ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), student_id VARCHAR(50) NOT NULL, student_name VARCHAR(100) NOT NULL, title VARCHAR(500) NOT NULL, abstract TEXT, keywords VARCHAR(500), degree_type VARCHAR(20), department VARCHAR(100), defense_date DATE, approval_status VARCHAR(20) DEFAULT 'PENDING', file_path VARCHAR(500), metadata JSONB, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX idx_thesis_student_id ON thesis(student_id); CREATE INDEX idx_thesis_title ON thesis(title); CREATE INDEX idx_thesis_keywords ON thesis USING GIN(keywords gin_trgm_ops); ```

三、Spring Boot项目搭建

3.1 项目初始化

使用Spring Initializr创建项目,pom.xml关键依赖:

``` org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa org.postgresql postgresql 42.6.0 org.springframework.boot spring-boot-starter-data-elasticsearch io.minio minio 8.5.4 ```

3.2 配置文件

application.yml完整配置:

``` spring: datasource: url: jdbc:postgresql://localhost:5432/degree_archive username: archive_admin password: Archive@2024 driver-class-name: org.postgresql.Driver jpa: hibernate: ddl-auto: update show-sql: true properties: hibernate: dialect: org.hibernate.dialect.PostgreSQLDialect format_sql: true elasticsearch: uris: http://localhost:9200 username: elastic password: your_password_here minio: endpoint: http://localhost:9000 accessKey: minioadmin secretKey: minioadmin bucket: degree-theses ```

四、文件存储服务实现

4.1 MinIO安装与配置

使用Docker快速部署MinIO:

``` docker run -p 9000:9000 -p 9001:9001 \ -e "MINIO_ROOT_USER=minioadmin" \ -e "MINIO_ROOT_PASSWORD=minioadmin" \ -v /mnt/data:/data \ minio/minio server /data --console-address ":9001" ```

4.2 文件上传服务

创建FileStorageService:

``` @Service public class FileStorageService { @Value("${minio.endpoint}") private String endpoint; @Value("${minio.accessKey}") private String accessKey; @Value("${minio.secretKey}") private String secretKey; @Value("${minio.bucket}") private String bucketName; private MinioClient minioClient; @PostConstruct public void init() { minioClient = MinioClient.builder() .endpoint(endpoint) .credentials(accessKey, secretKey) .build(); } public String uploadFile(MultipartFile file, String thesisId) throws Exception { String objectName = thesisId + "/" + file.getOriginalFilename(); minioClient.putObject( PutObjectArgs.builder() .bucket(bucketName) .object(objectName) .stream(file.getInputStream(), file.getSize(), -1) .contentType(file.getContentType()) .build() ); return objectName; } } ```

五、全文检索服务集成

5.1 Elasticsearch索引配置

创建Elasticsearch索引映射:

``` PUT /thesis_index { "mappings": { "properties": { "id": {"type": "keyword"}, "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "abstract": { "type": "text", "analyzer": "ik_max_word" }, "keywords": { "type": "text", "analyzer": "ik_max_word" }, "studentName": {"type": "keyword"}, "department": {"type": "keyword"}, "defenseDate": {"type": "date"} } } } ```

5.2 搜索服务实现

ThesisSearchService关键代码:

``` @Service public class ThesisSearchService { private final ElasticsearchOperations elasticsearchOperations; public Page search(String keyword, int page, int size) { NativeSearchQuery query = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.multiMatchQuery(keyword, "title", "abstract", "keywords")) .withPageable(PageRequest.of(page, size)) .build(); SearchHits searchHits = elasticsearchOperations.search(query, ThesisDocument.class); List theses = searchHits.get().map(SearchHit::getContent).collect(Collectors.toList()); return new PageImpl<>(theses, PageRequest.of(page, size), searchHits.getTotalHits()); } } ```

六、元数据提取与处理

6.1 PDF元数据提取

学位数字档案馆系统:从零搭建到实战部署全指南

使用Apache PDFBox提取PDF信息:

``` org.apache.pdfbox pdfbox 2.0.29 ```

元数据提取实现:

``` public class PdfMetadataExtractor { public Map extractMetadata(File pdfFile) throws IOException { PDDocument document = PDDocument.load(pdfFile); PDDocumentInformation info = document.getDocumentInformation(); Map metadata = new HashMap<>(); metadata.put("title", info.getTitle()); metadata.put("author", info.getAuthor()); metadata.put("subject", info.getSubject()); metadata.put("keywords", info.getKeywords()); metadata.put("pageCount", document.getNumberOfPages()); metadata.put("fileSize", pdfFile.length()); document.close(); return metadata; } } ```

七、系统安全与权限控制

7.1 JWT认证实现

添加Spring Security依赖:

``` org.springframework.boot spring-boot-starter-security io.jsonwebtoken jjwt-api 0.11.5 ```

7.2 安全配置

SecurityConfig配置类:

``` @Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .csrf().disable() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .authorizeHttpRequests(authz -> authz .requestMatchers("/api/auth/").permitAll() .requestMatchers("/api/theses/download/").permitAll() .requestMatchers("/api/admin/").hasRole("ADMIN") .anyRequest().authenticated() ) .addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); return http.build(); } } ```

八、系统部署与监控

8.1 Docker容器化部署

创建docker-compose.yml:

``` version: '3.8' services: postgres: image: postgres:15.3 environment: POSTGRES_DB: degree_archive POSTGRES_USER: archive_admin POSTGRES_PASSWORD: Archive@2024 volumes: - postgres_data:/var/lib/postgresql/data ports: - "5432:5432" elasticsearch: image: elasticsearch:8.11.0 environment: - discovery.type=single-node - xpack.security.enabled=true - ELASTIC_PASSWORD=your_password_here volumes: - es_data:/usr/share/elasticsearch/data ports: - "9200:9200" minio: image: minio/minio command: server /data --console-address ":9001" environment: MINIO_ROOT_USER: minioadmin MINIO_ROOT_PASSWORD: minioadmin volumes: - minio_data:/data ports: - "9000:9000" - "9001:9001" app: build: . depends_on: - postgres - elasticsearch - minio environment: SPRING_PROFILES_ACTIVE: prod ports: - "8080:8080" volumes: postgres_data: es_data: minio_data: ```

8.2 应用Dockerfile

创建Dockerfile:

``` FROM openjdk:17-jdk-slim WORKDIR /app COPY target/degree-archive-system-0.0.1-SNAPSHOT.jar app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "app.jar"] ```

8.3 启动系统

执行以下命令启动所有服务:

``` docker-compose up -d ```

验证服务状态:

``` curl http://localhost:8080/actuator/health ```

九、数据备份与恢复

9.1 数据库备份脚本

创建备份脚本backup.sh:

``` !/bin/bash BACKUP_DIR="/backups/degree_archive" DATE=$(date +%Y%m%d_%H%M%S) 备份PostgreSQL pg_dump -U archive_admin -h localhost degree_archive > $BACKUP_DIR/db_backup_$DATE.sql 备份Elasticsearch索引 curl -X GET "localhost:9200/_snapshot/backup_repository/snapshot_$DATE?wait_for_completion=true" 备份MinIO数据 mc mirror --overwrite minio/degree-theses $BACKUP_DIR/minio_backup_$DATE ```

9.2 设置定时备份

添加到crontab:

``` 0 2 /opt/degree-archive/backup.sh ```

十、系统测试与验证

10.1 API接口测试

使用curl测试论文上传接口:

``` curl -X POST http://localhost:8080/api/theses/upload \ -H "Authorization: Bearer YOUR_JWT_TOKEN" \ -H "Content-Type: multipart/form-data" \ -F "file=@/path/to/thesis.pdf" \ -F "studentId=2024001" \ -F "studentName=张三" \ -F "title=基于深度学习的图像识别研究" ```

10.2 搜索功能测试

测试全文检索:

``` curl -X GET "http://localhost:8080/api/theses/search?keyword=深度学习&page=0&size=10" ```
AI咨询
热线电话

028-85154420

15388110056

全国售前咨询电话

扫码咨询
安答联动微信公众号二维码

微信扫码关注安答联动

申请试用
热线电话
申请试用

安答联动档案管理系统