使用SmallRye GraphQL时,如何开启Federation支持并暴露@key字段?
解读
国内金融、运营商、政务云等项目在JBoss EAP 7.4/8.0上落地微服务时,GraphQL Federation 是“拆单体→服务编织”的标配能力。面试官问“怎么开 Federation、怎么把 @key 字段暴露出来”,表面看是配置题,实则考察四层能力:
- 对 WildFly/EAP 子系统(extension/subsystem)加载顺序的熟悉度;
- 对 SmallRye GraphQL 与 Federation 依赖版本匹配关系的敏感度;
- 对 schema-first 与 annotation-first 两种模式在 Federation 场景下差异的理解;
- 对运行时类隔离(JBoss Modules)与部署包结构(WAR/jboss-deployment-structure.xml)的掌控力。
答得太浅(“加依赖就行”)会被追问性能、热部署、类冲突;答得太深(“改 WildFly 内核”)会被质疑稳定性。必须给出“官方支持 + 红帽 KB 验证 + 生产可回滚”的闭环方案。
知识点
- JBoss EAP 8.0 开始通过
org.wildfly.extension.microprofile.graphql子系统内置 SmallRye GraphQL 1.8.x,已自带smallrye-graphql-federation运行时,但默认不启用。 - Federation 需要两个 JAR 在部署类路径可见:
graphql-java-federation与smallrye-graphql-federation-runtime;EAP 采用模块隔离,需通过jboss-deployment-structure.xml显式引入org.eclipse.microprofile.graphql与io.smallrye.smallrye-graphql-federation模块。 @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。
- 域模式(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-smallryesubsystem 里加<federation enabled="true"/>。 - 热部署时若出现
ClassNotFoundException: graphql/schema/idl/SchemaParser,说明graphql-java多版本冲突;EAP 8 自带 19.x,需排除业务 war 里低版本,或使用jboss-deployment-structure.xml的<exclusions>。 - 开启 Federation 后,SmallRye 会自动暴露
_service { sdl }与_entities(representations: [_Any!]!)两个入口;性能压测需关注BlockingQueue在FederationEntityFetcher的线程池大小,可通过-Dsmallrye.graphql.federation.fetcher-thread-pool-size=200调整。 - 红帽官方 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 字段暴露成功。
步骤七:生产加固
- 在
standalone.conf加-Dsmallrye.graphql.federation.fetcher-thread-pool-size=200防止_entities查询打爆线程; - 通过
microprofile-metrics暴露vendor_federation_entity_fetch_seconds指标接入 Prometheus; - 使用 EAP 的 RBAC 子系统给
/management/microprofile-graphql路径加管理员角色,防止恶意下载完整 SDL。
拓展思考
- 如果将来需要支持复合主键
@key(fields = {"tenant" "id"}),SmallRye 1.9+ 才原生支持,而 EAP 8.0 自带 1.8,必须等待红帽官方 CP;临时方案可在 Java 侧拼成tenantId虚拟字段并自定义TypeResolver,但会失去官方支持,面试时可作为“权衡案例”讲述。 - 在 OpenShift 上,EAP Operator 会自动把
smallrye.graphql.federation.enabled=true注入到ConfigMap,若同 Pod 内还有 Sidecar 做 Schema Registry,需把_service端点排除在 Istio 的 JWT 验证之外,否则 Gateway 无法拉取 SDL;这体现了“云原生集成”深度。 - 当 Federation 服务数量 >50 时,EAP 的内存占用会因 SDL 缓存线性上涨,可开启
-Dsmallrye.graphql.schema.cache.enabled=false并外接 Redis Schema Registry,面试中可展示对“WildFly 子系统扩展 + 自定义缓存” 的掌握。