Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

## 作业提交说明
本项目为 AI 问答系统作业,请 review 代码实现。
709 changes: 709 additions & 0 deletions application.log

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions backend-services/api-gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 使用官方OpenJDK运行时作为基础镜像
FROM openjdk:17-jdk-slim

# 安装curl用于健康检查
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*

# 设置工作目录
WORKDIR /app

# 复制Maven构建产物
COPY target/*.jar app.jar

# 暴露端口
EXPOSE 8080

# 设置JVM参数
ENV JAVA_OPTS="-Xmx512m -Xms256m"

# 健康检查
# HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
# CMD curl -f http://localhost:8082/health || exit 1

# 启动应用
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]
9 changes: 4 additions & 5 deletions backend-services/api-gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- Nacos服务发现,用于动态路由 -->
<dependency>
<!-- <dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Expand All @@ -33,7 +33,7 @@
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependency> -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
Expand All @@ -60,11 +60,10 @@
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<!-- <dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<!-- 版本号会从你的 dependencyManagement 中继承 -->
</dependency>
</dependency> -->
</dependencies>

<!-- 3. 添加构建插件,用于打包成可执行jar -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package com.ai.qa.gateway.api.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/test")
public class TestConfigController {


@Value("${jwt.secret}")
private String jwtSecret;
// @Value("${jwt.secret}")
// private String jwtSecret;

@GetMapping("/config")
public String login() {
System.out.println("测试config");
return "测试JWT:"+jwtSecret;
// return "测试JWT:" + jwtSecret;
return "/api/test/config";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ public KeyResolver ipKeyResolver() {
public org.springframework.cloud.gateway.filter.ratelimit.RateLimiter<InMemoryRateLimiterConfig.RateLimiterConfig> inMemoryRateLimiter() {

// 定义默认的限流速率
final double defaultReplenishRate = 5.0; // 每秒生成的令牌数
final int defaultBurstCapacity = 100; // 令牌桶总容量
// final double defaultReplenishRate = 1000.0; // 每秒生成的令牌数
// final int defaultBurstCapacity = 10000; // 令牌桶总容量

final double defaultReplenishRate = 10000.0;
final int defaultBurstCapacity = 100000;

return new org.springframework.cloud.gateway.filter.ratelimit.RateLimiter<RateLimiterConfig>() {

Expand All @@ -41,6 +44,8 @@ public org.springframework.cloud.gateway.filter.ratelimit.RateLimiter<InMemoryRa

@Override
public Mono<Response> isAllowed(String routeId, String id) {


// routeId 是当前请求匹配的路由ID
// id 是 KeyResolver 解析出的 key (IP地址)
// 获取当前路由的配置,如果不存在则使用默认配置
Expand All @@ -57,6 +62,7 @@ public Mono<Response> isAllowed(String routeId, String id) {

// 尝试获取一个令牌
boolean allowed = limiter.tryAcquire();
log.info("Limiter: {} Allow: {}", id, allowed);

if (allowed) {
log.info("Request ALLOWED. Route: {}, Key: {}", routeId, id);
Expand Down
59 changes: 33 additions & 26 deletions backend-services/api-gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
server:
port: 8080 # 所有后端请求的入口
port: ${SERVER_PORT:8080}

logging:
file:
path: ./logs
name: application.log
level:
# 将 Gateway 的核心日志级别设置为 DEBUG
org.springframework.cloud.gateway: DEBUG
# (可选) 将 Reactor Netty 的日志也设为 DEBUG,可以看到更底层的网络交互
reactor.netty.http.client: DEBUG
com.alibaba.nacos.client: DEBUG

spring:
application:
name: api-gateway
config:
import:
# 引入共享配置
- "nacos:shared-config.yml"
cloud:
nacos:
server-addr: 54.219.180.170:8848
config:
# 明确告诉 Nacos Config 默认的文件扩展名是 yml
file-extension: yml
group: DEFAULT_GROUP
gateway:
globalcors:
cors-configurations:
"[/**]":
allowed-origins: "${ALLOWED_ORIGINS:http://localhost:3000,http://127.0.0.1:3000,http://16.170.233.101:3000}"
allowed-methods: "*"
allowed-headers: "*"
allow-credentials: true
max-age: 3600
discovery:
locator:
enabled: false # 开启基于服务发现的路由功能
lower-case-service-id: true # 将服务名转为小写路径,e.g., user-service -> /user-service/**
enabled: false
routes:
- id: user_service_route
uri: lb://user-service # lb:// 表示从Nacos负载均衡地选择一个user-service实例
# uri: http://192.168.31.186:8081
uri: ${USER_SERVICE_URL} # http://user-service:8081
predicates:
- Path=/api/user/** # 匹配所有/api/user/开头的请求
- Path=/api/user/**
filters:
- StripPrefix=2
- StripPrefix=0
- id: qa_service_route
uri: lb://qa-service
uri: ${QA_SERVICE_URL} # http://qa-service:8082
predicates:
- Path=/api/qa/**
default-filters:
# 配置默认的限流过滤器
- name: RequestRateLimiter
args:
key-resolver: '#{@ipKeyResolver}'
filters:
- StripPrefix=0
- name: RequestRateLimiter
args:
rate-limiter: "#{@inMemoryRateLimiter}"
key-resolver: "#{@ipKeyResolver}"
replenish-rate: 10000
burst-capacity: 100000
requested-tokens: 1

springdoc:
swagger-ui:
path: /swagger-ui.html
enabled: true
52 changes: 52 additions & 0 deletions backend-services/api-gateway/target/classes/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
server:
port: ${SERVER_PORT:8080}

logging:
file:
path: ./logs
name: application.log
level:
org.springframework.cloud.gateway: DEBUG
reactor.netty.http.client: DEBUG

spring:
application:
name: api-gateway
cloud:
gateway:
globalcors:
cors-configurations:
"[/**]":
allowed-origins: "${ALLOWED_ORIGINS:http://localhost:3000,http://127.0.0.1:3000,http://16.170.233.101:3000}"
allowed-methods: "*"
allowed-headers: "*"
allow-credentials: true
max-age: 3600
discovery:
locator:
enabled: false
routes:
- id: user_service_route
uri: ${USER_SERVICE_URL} # http://user-service:8081
predicates:
- Path=/api/user/**
filters:
- StripPrefix=0
- id: qa_service_route
uri: ${QA_SERVICE_URL} # http://qa-service:8082
predicates:
- Path=/api/qa/**
filters:
- StripPrefix=0
- name: RequestRateLimiter
args:
rate-limiter: "#{@inMemoryRateLimiter}"
key-resolver: "#{@ipKeyResolver}"
replenish-rate: 10000
burst-capacity: 100000
requested-tokens: 1

springdoc:
swagger-ui:
path: /swagger-ui.html
enabled: true
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
15 changes: 4 additions & 11 deletions backend-services/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version> <!-- 建议使用一个稳定版本 -->
<version>2.7.17</version> <!-- 建议使用一个稳定版本 -->
<relativePath/>
</parent>

Expand All @@ -25,21 +25,13 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-cloud.version>2022.0.5</spring-cloud.version>
<spring-boot.version>3.1.11</spring-boot.version>
<spring-cloud-alibaba.version>2022.0.0.0</spring-cloud-alibaba.version>
<spring-cloud.version>2021.0.8</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
</properties>

<!-- 统一管理所有子模块的依赖版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
Expand All @@ -54,6 +46,7 @@
<type>pom</type>
<scope>import</scope>
</dependency>

</dependencies>
</dependencyManagement>
</project>
24 changes: 24 additions & 0 deletions backend-services/qa-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 使用官方OpenJDK运行时作为基础镜像
FROM openjdk:17-jdk-slim

# 安装curl用于健康检查
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*

# 设置工作目录
WORKDIR /app

# 复制Maven构建产物
COPY target/*.jar app.jar

# 暴露端口
EXPOSE 8082

# 设置JVM参数
ENV JAVA_OPTS="-Xmx512m -Xms256m"

# 健康检查
# HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
# CMD curl -f http://localhost:8081/health || exit 1

# 启动应用
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]
Loading