Spring Boot中配置并使用多个GraphQL Client端点
配置并使用多个GraphQL端点
针对你使用com.graphql-java-kickstart:graphql-webclient-spring-boot-starter:1.0.0的场景,要接入第二个GraphQL端点,需要手动创建多个GraphQLWebClient实例,具体步骤如下:
1. 修改配置文件
在application.properties中添加第二个端点的配置,用不同前缀区分两个客户端的配置:
# 第一个端点配置 graphql.client.foo1.url=https://api.foo.com/graphql-1 # 第二个端点配置 graphql.client.foo2.url=https://api.foo.com/graphql-2
2. 创建配置类,生成多个GraphQLWebClient实例
新建一个配置类,读取上述配置,分别构建两个客户端实例:
import com.graphql.spring.boot.autoconfigure.GraphQLWebClientAutoConfiguration; import com.graphql.spring.boot.autoconfigure.GraphQLWebClientProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import(GraphQLWebClientAutoConfiguration.class) public class MultipleGraphQLClientConfig { // 读取第一个端点的配置 @Bean @ConfigurationProperties(prefix = "graphql.client.foo1") public GraphQLWebClientProperties foo1ClientProperties() { return new GraphQLWebClientProperties(); } // 读取第二个端点的配置 @Bean @ConfigurationProperties(prefix = "graphql.client.foo2") public GraphQLWebClientProperties foo2ClientProperties() { return new GraphQLWebClientProperties(); } // 创建第一个GraphQLWebClient实例 @Bean public GraphQLWebClient foo1GraphQLWebClient(GraphQLWebClientAutoConfiguration.GraphQLWebClientBuilder builder) { return builder.build(foo1ClientProperties()); } // 创建第二个GraphQLWebClient实例 @Bean public GraphQLWebClient foo2GraphQLWebClient(GraphQLWebClientAutoConfiguration.GraphQLWebClientBuilder builder) { return builder.build(foo2ClientProperties()); } }
3. 在业务代码中使用不同的客户端
通过@Qualifier注解指定要注入的客户端实例,分别调用对应的端点:
import com.graphql.spring.boot.autoconfigure.GraphQLWebClient; import com.graphql.spring.boot.autoconfigure.request.GraphQLRequest; import com.graphql.spring.boot.autoconfigure.response.GraphQLResponse; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import java.util.Map; @Component public class FooService { private final GraphQLWebClient foo1GraphQLWebClient; private final GraphQLWebClient foo2GraphQLWebClient; // 构造方法注入,用@Qualifier区分不同实例 public FooService(@Qualifier("foo1GraphQLWebClient") GraphQLWebClient foo1GraphQLWebClient, @Qualifier("foo2GraphQLWebClient") GraphQLWebClient foo2GraphQLWebClient) { this.foo1GraphQLWebClient = foo1GraphQLWebClient; this.foo2GraphQLWebClient = foo2GraphQLWebClient; } // 调用第一个端点 public Foo getFooByIdFromFirstEndpoint(String id) throws Exception { final GraphQLRequest request = GraphQLRequest.builder() .query(orgByIdQuery) // 替换为你的查询语句 .variables(Map.of("id", id)) .build(); final GraphQLResponse response = foo1GraphQLWebClient.post(request).block(); response.validateNoErrors(); return response.get("Foo", Foo.class); } // 调用第二个端点 public Bar getBarByIdFromSecondEndpoint(String id) throws Exception { final GraphQLRequest request = GraphQLRequest.builder() .query(barByIdQuery) // 替换为第二个端点的查询语句 .variables(Map.of("id", id)) .build(); final GraphQLResponse response = foo2GraphQLWebClient.post(request).block(); response.validateNoErrors(); return response.get("Bar", Bar.class); } }
补充说明
- 配置类通过
@ConfigurationProperties绑定不同前缀的配置,确保每个客户端读取对应的URL - 使用
@Qualifier注解明确指定注入的客户端实例,避免依赖注入冲突 - 每个客户端可独立配置额外参数(如超时、请求头),只需在
application.properties对应前缀下添加即可,例如graphql.client.foo1.connect-timeout=5000
内容的提问来源于stack exchange,提问作者N N
相关产品推荐
相关产品推荐

