Skip to content

Commit e5b28c2

Browse files
authored
add gateway (#187)
* feature: add gateway
1 parent 46f4a8c commit e5b28c2

File tree

41 files changed

+706
-291
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+706
-291
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Gateway Docker Image CI
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
paths:
7+
- 'backend/**'
8+
- 'scripts/images/gateway/**'
9+
- '.github/workflows/docker-image-gateway.yml'
10+
- '.github/workflows/docker-images-reusable.yml'
11+
pull_request:
12+
branches: [ "main" ]
13+
paths:
14+
- 'backend/**'
15+
- 'scripts/images/gateway/**'
16+
- '.github/workflows/docker-image-gateway.yml'
17+
- '.github/workflows/docker-images-reusable.yml'
18+
workflow_dispatch:
19+
workflow_call:
20+
21+
jobs:
22+
call-docker-build:
23+
name: Build and Push Gateway Docker Image
24+
uses: ./.github/workflows/docker-images-reusable.yml
25+
permissions:
26+
contents: read
27+
packages: write
28+
with:
29+
service_name: gateway
30+
build_dir: .

.github/workflows/docker-images-release.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ jobs:
1414
service_name: frontend
1515
build_dir: .
1616

17+
gateway:
18+
name: Gateway Image
19+
uses: ./.github/workflows/docker-images-reusable.yml
20+
with:
21+
service_name: gateway
22+
build_dir: .
23+
1724
backend:
1825
name: Backend Image
1926
uses: ./.github/workflows/docker-images-reusable.yml

.github/workflows/docker-push-to-huaweicloud.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ jobs:
1818
secrets:
1919
HUAWEI_CLOUD_SWR_LOGIN_PWD: ${{ secrets.HUAWEI_CLOUD_SWR_LOGIN_PWD}}
2020

21+
gateway:
22+
name: Push Gateway Image
23+
uses: ./.github/workflows/docker-push-to-huaweicloud-reusable.yml
24+
with:
25+
service_name: datamate-gateway
26+
version: ${{ inputs.version}}
27+
secrets:
28+
HUAWEI_CLOUD_SWR_LOGIN_PWD: ${{ secrets.HUAWEI_CLOUD_SWR_LOGIN_PWD}}
29+
2130
backend:
2231
name: Push Backend Image
2332
uses: ./.github/workflows/docker-push-to-huaweicloud-reusable.yml

.github/workflows/enterprise-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jobs:
6161
run: |
6262
mkdir -p images/datamate
6363
LOWERCASE_REPO=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
64-
services=("runtime" "backend" "frontend" "database" "backend-python")
64+
services=("runtime" "backend" "frontend" "database" "backend-python" "gateway")
6565
for service in "${services[@]}"; do
6666
docker pull ghcr.io/$LOWERCASE_REPO/datamate-$service:latest --platform arm64
6767
docker tag ghcr.io/$LOWERCASE_REPO/datamate-$service:latest datamate-$service:latest

.github/workflows/package.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,20 @@ jobs:
3434
with:
3535
service_name: backend-python
3636

37+
gateway-docker-build:
38+
name: Build and Push Frontend Docker Image
39+
uses: ./.github/workflows/docker-image-save.yml
40+
with:
41+
service_name: gateway
42+
3743
package-all:
3844
needs:
3945
- backend-docker-build
4046
- frontend-docker-build
4147
- database-docker-build
4248
- backend-python-docker-build
4349
- runtime-docker-build
50+
- gateway-docker-build
4451
runs-on: ubuntu-latest
4552
steps:
4653
- name: Checkout

backend/api-gateway/pom.xml

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,38 @@
1313
</parent>
1414

1515
<artifactId>api-gateway</artifactId>
16+
<packaging>jar</packaging>
1617
<name>API Gateway</name>
1718
<description>API网关服务</description>
1819

20+
<properties>
21+
<spring-boot.version>3.5.6</spring-boot.version>
22+
<spring-cloud.version>2025.0.0</spring-cloud.version>
23+
</properties>
24+
1925
<dependencies>
2026
<dependency>
2127
<groupId>org.springframework.cloud</groupId>
2228
<artifactId>spring-cloud-starter-gateway</artifactId>
2329
</dependency>
2430
<dependency>
25-
<groupId>org.springframework.boot</groupId>
26-
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
27-
</dependency>
28-
<dependency>
29-
<groupId>org.springframework.boot</groupId>
30-
<artifactId>spring-boot-starter-security</artifactId>
31-
</dependency>
32-
<dependency>
33-
<groupId>org.springframework.boot</groupId>
34-
<artifactId>spring-boot-starter-actuator</artifactId>
35-
</dependency>
36-
<dependency>
37-
<groupId>org.springframework.cloud</groupId>
38-
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
31+
<groupId>com.terrabase</groupId>
32+
<artifactId>enterprise-impl-commercial</artifactId>
33+
<version>1.0.0</version>
34+
<exclusions>
35+
<exclusion>
36+
<artifactId>spring-web</artifactId>
37+
<groupId>org.springframework</groupId>
38+
</exclusion>
39+
<exclusion>
40+
<artifactId>spring-boot-starter-web</artifactId>
41+
<groupId>org.springframework.boot</groupId>
42+
</exclusion>
43+
</exclusions>
3944
</dependency>
4045
<dependency>
41-
<groupId>org.springframework.boot</groupId>
42-
<artifactId>spring-boot-starter-test</artifactId>
43-
<scope>test</scope>
46+
<groupId>com.alibaba.fastjson2</groupId>
47+
<artifactId>fastjson2</artifactId>
4448
</dependency>
4549
</dependencies>
4650

@@ -49,6 +53,18 @@
4953
<plugin>
5054
<groupId>org.springframework.boot</groupId>
5155
<artifactId>spring-boot-maven-plugin</artifactId>
56+
<version>${spring-boot.version}</version>
57+
<configuration>
58+
<finalName>gateway</finalName>
59+
<mainClass>com.datamate.gateway.ApiGatewayApplication</mainClass>
60+
</configuration>
61+
<executions>
62+
<execution>
63+
<goals>
64+
<goal>repackage</goal>
65+
</goals>
66+
</execution>
67+
</executions>
5268
</plugin>
5369
</plugins>
5470
</build>

backend/api-gateway/src/main/java/com/datamate/gateway/ApiGatewayApplication.java

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@
55
import org.springframework.cloud.gateway.route.RouteLocator;
66
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
77
import org.springframework.context.annotation.Bean;
8-
import org.springframework.context.annotation.ComponentScan;
98

109
/**
1110
* API Gateway & Auth Service Application
1211
* 统一的API网关和认证授权微服务
1312
* 提供路由、鉴权、限流等功能
1413
*/
1514
@SpringBootApplication
16-
@ComponentScan(basePackages = {
17-
"com.datamate.gateway",
18-
"com.datamate.shared"
19-
})
2015
public class ApiGatewayApplication {
2116

2217
public static void main(String[] args) {
@@ -26,51 +21,21 @@ public static void main(String[] args) {
2621
@Bean
2722
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
2823
return builder.routes()
29-
// 数据归集服务路由
30-
.route("data-collection", r -> r.path("/api/data-collection/**")
31-
.uri("lb://data-collection-service"))
32-
33-
// 数据管理服务路由
34-
.route("data-management", r -> r.path("/api/data-management/**")
35-
.uri("lb://data-management-service"))
36-
37-
// 算子市场服务路由
38-
.route("operator-market", r -> r.path("/api/operators/**")
39-
.uri("lb://operator-market-service"))
40-
41-
// 数据清洗服务路由
42-
.route("data-cleaning", r -> r.path("/api/cleaning/**")
43-
.uri("lb://data-cleaning-service"))
44-
4524
// 数据合成服务路由
4625
.route("data-synthesis", r -> r.path("/api/synthesis/**")
47-
.uri("lb://data-synthesis-service"))
26+
.uri("http://datamate-backend-python:18000"))
4827

4928
// 数据标注服务路由
5029
.route("data-annotation", r -> r.path("/api/annotation/**")
51-
.uri("lb://data-annotation-service"))
30+
.uri("http://datamate-backend-python:18000"))
5231

5332
// 数据评估服务路由
5433
.route("data-evaluation", r -> r.path("/api/evaluation/**")
55-
.uri("lb://data-evaluation-service"))
56-
57-
// 流程编排服务路由
58-
.route("pipeline-orchestration", r -> r.path("/api/pipelines/**")
59-
.uri("lb://pipeline-orchestration-service"))
60-
61-
// 执行引擎服务路由
62-
.route("execution-engine", r -> r.path("/api/execution/**")
63-
.uri("lb://execution-engine-service"))
64-
65-
// 认证服务路由
66-
.route("auth-service", r -> r.path("/api/auth/**")
67-
.uri("lb://auth-service"))
34+
.uri("http://datamate-backend-python:18000"))
6835

69-
// RAG服务路由
70-
.route("rag-indexer", r -> r.path("/api/rag/indexer/**")
71-
.uri("lb://rag-indexer-service"))
72-
.route("rag-query", r -> r.path("/api/rag/query/**")
73-
.uri("lb://rag-query-service"))
36+
// 其他后端服务
37+
.route("default", r -> r.path("/api/**")
38+
.uri("http://datamate-backend:8080"))
7439

7540
.build();
7641
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.datamate.gateway.filter;
2+
3+
import com.terrabase.enterprise.api.dto.LoginUserDto;
4+
import com.terrabase.enterprise.api.sdk.TerrabaseSDK;
5+
import com.terrabase.enterprise.api.sdk.TerrabaseSDKConfig;
6+
import jakarta.annotation.PostConstruct;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
10+
import org.springframework.cloud.gateway.filter.GlobalFilter;
11+
import org.springframework.stereotype.Component;
12+
import org.springframework.web.server.ServerWebExchange;
13+
import reactor.core.publisher.Mono;
14+
15+
/**
16+
* 用户信息过滤器
17+
*
18+
* @since 2025/12/22
19+
*/
20+
@Slf4j
21+
@Component
22+
public class UserContextFilter implements GlobalFilter {
23+
@Value("${terrabase.jar.path:/opt/terrabase}")
24+
private String jarPath;
25+
26+
@Value("${commercial.switch:false}")
27+
private boolean isCommercial;
28+
29+
private TerrabaseSDK terrabaseSDK;
30+
31+
@PostConstruct
32+
public void init() {
33+
TerrabaseSDKConfig sdkConfig = TerrabaseSDKConfig.createDefault();
34+
sdkConfig.setJarPath(jarPath);
35+
terrabaseSDK = TerrabaseSDK.init(sdkConfig);
36+
}
37+
38+
@Override
39+
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
40+
if (!isCommercial) {
41+
return chain.filter(exchange);
42+
}
43+
try {
44+
LoginUserDto loginUserDto = terrabaseSDK.userManagement().getCurrentUserInfo().getData().getFirst();
45+
} catch (Exception e) {
46+
log.error("get current user info error", e);
47+
return chain.filter(exchange);
48+
}
49+
return chain.filter(exchange);
50+
}
51+
}

0 commit comments

Comments
 (0)