如何从我的微服务中调用另一个微服务?
How do I call another micro-service from my micro-service?
这听起来可能有点奇怪,但我面临这样一种情况:我有一个微服务,它组装了一些定价逻辑,但为此,它需要另一个微服务提供的一堆信息。
我相信我有两个选择:(1) 从数据库中获取我需要的所有数据并忽略在这个其他微服务中完成的 GraphQL 工作,或者 (2) 以某种方式从在我当前的服务中获取我需要的数据。
某人将如何完成 (2)?
我不知道如何在不造成混乱的情况下完成这项工作。
我认为将我的定价微服务转变为小型客户端可能行得通,但我不确定这是否是一种不好的做法。
经过深思熟虑并阅读了我得到的答案here,我决定使用 apollo-client 将我的微服务变成一个迷你客户端。
简而言之,我有这样的东西:
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { HttpLink } from 'apollo-link-http';
// Instantiate required constructor fields
const cache = new InMemoryCache();
const link = new HttpLink({
uri: 'http://localhost:3000/graphql',
});
const client = new ApolloClient({
// Provide required constructor fields
cache: cache,
link: link,
});
export default client;
HttpLink 是联合架构,因此我可以从我的解析器或其他任何地方调用它,如下所示:
const query = gql`
query {
something(uid: "${uid}") {
field1
field2
field3
anotherThing {
field1
field2
}
}
}
`;
const response = await dataSources.client.query({query});
这听起来可能有点奇怪,但我面临这样一种情况:我有一个微服务,它组装了一些定价逻辑,但为此,它需要另一个微服务提供的一堆信息。
我相信我有两个选择:(1) 从数据库中获取我需要的所有数据并忽略在这个其他微服务中完成的 GraphQL 工作,或者 (2) 以某种方式从在我当前的服务中获取我需要的数据。
某人将如何完成 (2)?
我不知道如何在不造成混乱的情况下完成这项工作。
我认为将我的定价微服务转变为小型客户端可能行得通,但我不确定这是否是一种不好的做法。
经过深思熟虑并阅读了我得到的答案here,我决定使用 apollo-client 将我的微服务变成一个迷你客户端。
简而言之,我有这样的东西:
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { HttpLink } from 'apollo-link-http';
// Instantiate required constructor fields
const cache = new InMemoryCache();
const link = new HttpLink({
uri: 'http://localhost:3000/graphql',
});
const client = new ApolloClient({
// Provide required constructor fields
cache: cache,
link: link,
});
export default client;
HttpLink 是联合架构,因此我可以从我的解析器或其他任何地方调用它,如下所示:
const query = gql`
query {
something(uid: "${uid}") {
field1
field2
field3
anotherThing {
field1
field2
}
}
}
`;
const response = await dataSources.client.query({query});