使用SmallRye GraphQL时,如何开启Federation支持并暴露@key字段?

解读

国内金融、运营商、政务云等项目在JBoss EAP 7.4/8.0上落地微服务时,GraphQL Federation 是“拆单体→服务编织”的标配能力。面试官问“怎么开 Federation、怎么把 @key 字段暴露出来”,表面看是配置题,实则考察四层能力:

  1. 对 WildFly/EAP 子系统(extension/subsystem)加载顺序的熟悉度;
  2. 对 SmallRye GraphQL 与 Federation 依赖版本匹配关系的敏感度;
  3. 对 schema-first 与 annotation-first 两种模式在 Federation 场景下差异的理解;
  4. 对运行时类隔离(JBoss Modules)与部署包结构(WAR/jboss-deployment-structure.xml)的掌控力。
    答得太浅(“加依赖就行”)会被追问性能、热部署、类冲突;答得太深(“改 WildFly 内核”)会被质疑稳定性。必须给出“官方支持 + 红帽 KB 验证 + 生产可回滚”的闭环方案。

知识点

  1. JBoss EAP 8.0 开始通过 org.wildfly.extension.microprofile.graphql 子系统内置 SmallRye GraphQL 1.8.x,已自带 smallrye-graphql-federation 运行时,但默认不启用。
  2. Federation 需要两个 JAR 在部署类路径可见:graphql-java-federation 与 smallrye-graphql-federation-runtime;EAP 采用模块隔离,需通过 jboss-deployment-structure.xml 显式引入 org.eclipse.microprofile.graphql 与 io.smallrye.smallrye-graphql-federation 模块。
  3. @key 字段必须同时满足:
    • 是 GraphQL ObjectType 的字段;
    • 在实体类上标注 @io.smallrye.graphql.api.federation.Key(fields = {"id"});
    • 该字段必须被 @GraphQLQuery 或 @Source 暴露,且返回类型可序列化;
    • 若采用 schema-first,则需在 sdl 里写 type User @key(fields: "id"),并在 Java 侧用 @FederationService 把 sdl 注册到 SchemaBuilder。
  4. 域模式(domain mode)下,Federation 开关是 server-group 级属性:
    /server-group=main-server-group/system-property=smallrye.graphql.federation.enabled:add(value=true,boot-time=true)
    
    单机模式可直接在 standalone.xml 的 microprofile-graphql-smallrye subsystem 里加 <federation enabled="true"/>。
  5. 热部署时若出现 ClassNotFoundException: graphql/schema/idl/SchemaParser,说明 graphql-java 多版本冲突;EAP 8 自带 19.x,需排除业务 war 里低版本,或使用 jboss-deployment-structure.xml 的 <exclusions>。
  6. 开启 Federation 后,SmallRye 会自动暴露 _service { sdl } 与 _entities(representations: [_Any!]!) 两个入口;性能压测需关注 BlockingQueue 在 FederationEntityFetcher 的线程池大小,可通过 -Dsmallrye.graphql.federation.fetcher-thread-pool-size=200 调整。
  7. 红帽官方 TAM 给出的生命周期承诺:EAP 8 内置的 SmallRye GraphQL 版本会跟随 EAP CP(累积补丁)升级,向后兼容 10 年,因此生产环境禁止自行升级 graphql-java 主版本,否则失去商业支持。

答案

步骤一:确认 EAP 版本与补丁

$ $EAP_HOME/bin/jboss-cli.sh --connect --command="version"
Red Hat JBoss Enterprise Application Platform - Version 8.0.0.GA-redhat-00001

低于 8.0 需先升到 8.0 并打到最新 CP,否则 Federation 模块不存在。

步骤二:在单机或域模式开启 Federation 开关
单机:

<subsystem xmlns="urn:wildfly:microprofile-graphql-smallrye:1.0">
    <federation enabled="true"/>
</subsystem>

域模式:

/server-group=main-server-group/system-property=smallrye.graphql.federation.enabled:add(value=true,boot-time=true)
reload --server-group=main-server-group

步骤三:在 Maven 项目里 provided 依赖(由 EAP 模块提供,不打包)

<dependency>
    <groupId>io.smallrye</groupId>
    <artifactId>smallrye-graphql-federation-api</artifactId>
    <scope>provided</scope>
</dependency>

步骤四:在实体类上声明 @Key

@Key(fields = {"id"})
public class User {
    private String id;
    @GraphQLQuery
    public String getId() { return id; }
}

若 schema-first,则在 schema.graphqls 写

type User @key(fields: "id") {
    id: String!
    name: String
}

并在 @FederationService 实现里返回 SDL 字符串。

步骤五:配置 jboss-deployment-structure.xml 解决模块可见性

<jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="io.smallrye.smallrye-graphql-federation" export="true"/>
            <module name="org.eclipse.microprofile.graphql" export="true"/>
        </dependencies>
    </deployment>
</jboss-deployment-structure>

步骤六:部署并验证

curl -H "Content-Type: application/json" \
http://localhost:8080/your-app/graphql \
-d '{"query":"{ _service { sdl } }"}'

返回内容里出现 type User @key(fields: "id") 即表示 Federation 已生效,@key 字段暴露成功。

步骤七:生产加固

  1. 在 standalone.conf 加 -Dsmallrye.graphql.federation.fetcher-thread-pool-size=200 防止 _entities 查询打爆线程;
  2. 通过 microprofile-metrics 暴露 vendor_federation_entity_fetch_seconds 指标接入 Prometheus;
  3. 使用 EAP 的 RBAC 子系统给 /management/microprofile-graphql 路径加管理员角色,防止恶意下载完整 SDL。

拓展思考

  1. 如果将来需要支持复合主键 @key(fields = {"tenant" "id"}),SmallRye 1.9+ 才原生支持,而 EAP 8.0 自带 1.8,必须等待红帽官方 CP;临时方案可在 Java 侧拼成 tenantId 虚拟字段并自定义 TypeResolver,但会失去官方支持,面试时可作为“权衡案例”讲述。
  2. 在 OpenShift 上,EAP Operator 会自动把 smallrye.graphql.federation.enabled=true 注入到 ConfigMap,若同 Pod 内还有 Sidecar 做 Schema Registry,需把 _service 端点排除在 Istio 的 JWT 验证之外,否则 Gateway 无法拉取 SDL;这体现了“云原生集成”深度。
  3. 当 Federation 服务数量 >50 时,EAP 的内存占用会因 SDL 缓存线性上涨,可开启 -Dsmallrye.graphql.schema.cache.enabled=false 并外接 Redis Schema Registry,面试中可展示对“WildFly 子系统扩展 + 自定义缓存” 的掌握。