eggjs / egg

🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
https://eggjs.org
MIT License
18.88k stars 1.81k forks source link

sofa-node rpc 一个client怎么连接多个服务,这个问题绊了我 #4340

Open 345316222 opened 4 years ago

345316222 commented 4 years ago
const rpc = {
    client: {
        serverHost: '127.0.0.1:12202;127.0.0.1:12204',
        responseTimeout: 3000, // responseTimeout(可选): RPC 的超时时长,默认为 3 秒
    },
};

这是我的配置文件。 同时有端口号12202,12204两个服务,向第二个服务发送请求,第一个服务会报错,报不能再第一个服务上找到方法,说明我只链接了第一个服务,第二个服务是没有被链接上的。怎么解决一个客户端连接多个服务端的问题呢?

这是第一个服务报的错,实际上这个服务是第二个服务 2020-06-03 05:59:29,199 ERROR 1089 nodejs.Error: not found service: com.alipay.sofa.rpc.protobuf.TestService:1.0

gxcsoccer commented 4 years ago

可以按照服务的提供的 app 维度来配置,app1 和 app2 分别对应 config/proxy.js 里 appName

module.exports = () => {
  return {
    `${app1}.rpc.service.url`: '127.0.0.1:12202',
    `${app2}.rpc.service.url`: '127.0.0.1:12204',
  };
};

config/proxy.js

module.exports = {
  version: '1.0.0',
  services: [{
    appName: 'app1',
    api: {
      DemoService: {
        interfaceName: 'eggjs.demo.DemoService',
      },
    },
  }, {
    appName: 'app2',
    api: {
      DemoService2: {
        interfaceName: 'eggjs.demo.DemoService2',
      },
    },
  }],
};
345316222 commented 3 years ago
module.exports = () => {
  return {
    `${app1}.rpc.service.url`: '127.0.0.1:12202',
    `${app2}.rpc.service.url`: '127.0.0.1:12204',
  };
};

这个是写在哪个文件上的? config文件上吗?

const rpc = {
    client: {
        serverHost: {
            'book_rpc.rpc.service.url': '127.0.0.1:12201', // book服务
            'typeset_rpc.rpc.service.url': '127.0.0.1:12202',
        },
        responseTimeout: 3000, // responseTimeout(可选): RPC 的超时时长,默认为 3 秒
    },
};

这样写的不对,还请指教