【问题标题】:How do I call another micro-service from my micro-service?如何从我的微服务调用另一个微服务?
【发布时间】:2020-04-27 09:05:27
【问题描述】:

这听起来可能有点奇怪,但我面临的情况是,我有一个组装一些定价逻辑的微服务,但为此,它需要另一个微服务提供的一堆信息。

我相信我有两个选择:(1) 从数据库中获取我需要的所有数据并忽略在其他微服务中完成的 GraphQL 工作,或者 (2) 从我的内部以某种方式访问​​其他微服务当前服务并获取我需要的数据。

某人将如何完成 (2)?

我不知道如何在不造成混乱的情况下完成这项工作。

我认为将我的定价微服务转变为小型客户端可能会奏效,但我不确定这是否是不好的做法。

【问题讨论】:

    标签: apollo apollo-client apollo-server


    【解决方案1】:

    经过深思熟虑并阅读了我得到的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});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-04
      • 2018-12-27
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2021-05-24
      相关资源
      最近更新 更多