You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java双Quarkus微服务的简易API网关搭建建议咨询

基于Quarkus搭建简易API网关方案

针对你的需求,这里提供两种基于Quarkus实现简易API网关的方案,均能实现接收localhost:8000的请求后,调用两个微服务并聚合数据返回:

方案一:REST Client + 聚合接口(同步场景)

这种方案适合简单的同步数据聚合,实现成本低:

1. 创建Quarkus网关项目并配置端口

新建Quarkus项目后,修改src/main/resources/application.properties指定网关端口:

quarkus.http.port=8000

2. 添加REST Client依赖

在pom.xml中引入REST Client相关依赖,用于调用后端微服务:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-rest-client</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-rest-client-jackson</artifactId>
</dependency>

3. 定义微服务客户端接口

分别为两个微服务创建REST Client接口,指定基础地址和对应端点:

// User服务客户端
@RegisterRestClient(baseUri = "http://localhost:8080")
public interface UserServiceClient {
    @GET
    @Path("/users/{id}") // 替换为你的user服务实际端点
    User getUserById(@PathParam("id") Long id);
}

// Game服务客户端
@RegisterRestClient(baseUri = "http://localhost:8282")
public interface GameServiceClient {
    @GET
    @Path("/games/{userId}") // 替换为你的game服务实际端点
    List<Game> getGamesByUserId(@PathParam("userId") Long userId);
}

注:User和Game需与微服务返回的实体结构一致,可直接在网关项目中定义相同类。

4. 编写聚合接口

创建网关的API接口,注入两个客户端,调用微服务并聚合数据:

@Path("/")
public class GatewayResource {
    @Inject
    @RestClient
    UserServiceClient userClient;

    @Inject
    @RestClient
    GameServiceClient gameClient;

    @GET
    @Path("/user-with-games/{userId}")
    @Produces(MediaType.APPLICATION_JSON)
    public UserWithGames getUserWithGames(@PathParam("userId") Long userId) {
        // 调用两个微服务获取数据
        User user = userClient.getUserById(userId);
        List<Game> games = gameClient.getGamesByUserId(userId);
        // 聚合返回
        return new UserWithGames(user, games);
    }

    // 聚合用DTO
    public static class UserWithGames {
        public User user;
        public List<Game> games;

        public UserWithGames(User user, List<Game> games) {
            this.user = user;
            this.games = games;
        }
    }
}

方案二:Reactive Routes + 响应式聚合(高并发场景)

如果需要处理高并发请求,推荐使用响应式路由实现,性能更优:

1. 添加Reactive相关依赖

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-reactive-routes</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-rest-client-reactive</artifactId>
</dependency>

2. 编写响应式路由处理器

@ApplicationScoped
public class GatewayRoutes {
    @Inject
    @RestClient
    UserServiceClient userClient;

    @Inject
    @RestClient
    GameServiceClient gameClient;

    @Route(path = "/user-details/{userId}", methods = HttpMethod.GET)
    public Uni<Response> handleUserDetails(RoutingContext ctx) {
        Long userId = Long.parseLong(ctx.pathParam("userId"));
        // 并行调用两个微服务,提升效率
        return Uni.combine().all().unis(
                userClient.getUserById(userId),
                gameClient.getGamesByUserId(userId)
        ).asTuple()
         .map(tuple -> {
             UserWithGames result = new UserWithGames(tuple.getItem1(), tuple.getItem2());
             return Response.ok(result).build();
         });
    }
}

测试验证

启动网关和两个微服务后,通过Postman访问localhost:8000/user-with-games/{userId}(或响应式方案的/user-details/{userId}),即可获取聚合后的用户与游戏数据。

扩展建议

如果后续需要更复杂的网关能力(如负载均衡、认证、限流),可考虑集成Quarkus的quarkus-spring-cloud-gateway扩展,或引入服务网格工具,但当前方案已满足简易聚合需求。

内容的提问来源于stack exchange,提问作者Nikos Pougas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 08:15:44