GraphQL Federation Nodemon 在重启时中断网关
GraphQL Federation Nodemon breaks Gateway on Restart
我正在努力设置我的 GraphQL Federation 开发环境。我有一些服务,我希望服务器在文件更改时自动重启。我使用的是 Typescript,所以 Typescript 必须先转译到 dist 文件夹,然后 nodemon 应该重新启动发生更改的服务。
我在 package.json 文件中创建了以下设置:
"dev": "concurrently -k \"tsc -w\" \"npm:dev:*\"",
"dev:accounts": "nodemon -r dotenv/config -r esm --watch ./dist/services/accounts ./dist/services/accounts/index.js",
"dev:profiles": "nodemon -r dotenv/config -r esm --watch ./dist/services/profiles ./dist/services/profiles/index.js",
"dev:gateway": "wait-on tcp:4001 tcp:4002 && nodemon -r dotenv/config -r esm ./dist/index.js"
这在我 运行 npm run dev
开始时有效 - 帐户和配置文件服务启动,一旦它们 运行ning,网关就会启动。
但是,一旦 Typescript 完成转译,nodemon 就会重新启动所有服务和网关,因为 dist 文件夹中的文件已被更改。
问题来了。 Nodemon独立重启服务和网关,网关重启更快,也就是说网关先于服务运行ning,导致如下错误:
[dev:gateway] Error checking for changes to service definitions: Couldn't load service definitions for "profiles" at http://localhost:4002: request to http://localhost:4002/ failed, reason: connect ECONNREFUSED 127.0.0.1:4002
[dev:gateway] This data graph is missing a valid configuration. Couldn't load service definitions for "profiles" at http://localhost:4002: request to http://localhost:4002/ failed, reason: connect ECONNREFUSED 127.0.0.1:4002
每当 nodemon 执行重启时,我不知道如何告诉 nodemon 等到所有服务都重启!
因为现在等待只在 npm run dev
的第一次初始执行时起作用。此后,对于每次更改,nodemon 负责重新启动并且不关心等待。
这是整个火车残骸:
$ npm 运行 开发
> my-app@1.0.0 dev
> concurrently -k "tsc -w" "npm:dev:*"
17:08:48 - Starting compilation in watch mode...
[dev:accounts] > my-app@1.0.0 dev:accounts
[dev:accounts] > nodemon -r dotenv/config -r esm --watch ./dist/services/accounts ./dist/services/accounts/index.js
[dev:gateway] > my-app@1.0.0 dev:gateway
[dev:gateway] > wait-on tcp:4001 tcp:4002 && nodemon -r dotenv/config -r esm ./dist/index.js
[dev:profiles] > my-app@1.0.0 dev:profiles
[dev:profiles] > nodemon -r dotenv/config -r esm --watch ./dist/services/profiles ./dist/services/profiles/index.js
[dev:accounts] [nodemon] 2.0.7
[dev:accounts] [nodemon] to restart at any time, enter `rs`
[dev:accounts] [nodemon] watching path(s): dist/services/accounts/**/*
[dev:accounts] [nodemon] watching extensions: js,mjs,json
[dev:accounts] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/accounts/index.js`
[dev:profiles] [nodemon] 2.0.7
[dev:profiles] [nodemon] to restart at any time, enter `rs`
[dev:profiles] [nodemon] watching path(s): dist/services/profiles/**/*
[dev:profiles] [nodemon] watching extensions: js,mjs,json
[dev:profiles] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/profiles/index.js`
[dev:accounts] Accounts service ready at http://localhost:4001/
[dev:profiles] Profiles service ready at http://localhost:4002/
[dev:gateway] [nodemon] 2.0.7
[dev:gateway] [nodemon] to restart at any time, enter `rs`
[dev:gateway] [nodemon] watching path(s): *.*
[dev:gateway] [nodemon] watching extensions: js,mjs,json
[dev:gateway] [nodemon] starting `node -r dotenv/config -r esm ./dist/index.js`
[dev:gateway] Server started at http://localhost:4000/graphql
[dev:accounts] > my-app@1.0.0 dev:accounts
[dev:accounts] > nodemon -r dotenv/config -r esm --watch ./dist/services/accounts ./dist/services/accounts/index.js
[dev:gateway] > my-app@1.0.0 dev:gateway
[dev:gateway] > wait-on tcp:4001 tcp:4002 && nodemon -r dotenv/config -r esm ./dist/index.js
[dev:profiles] > my-app@1.0.0 dev:profiles
[dev:profiles] > nodemon -r dotenv/config -r esm --watch ./dist/services/profiles ./dist/services/profiles/index.js
[dev:accounts] [nodemon] 2.0.7
[dev:accounts] [nodemon] to restart at any time, enter `rs`
[dev:accounts] [nodemon] watching path(s): dist/services/accounts/**/*
[dev:accounts] [nodemon] watching extensions: js,mjs,json
[dev:accounts] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/accounts/index.js`
[dev:profiles] [nodemon] 2.0.7
[dev:profiles] [nodemon] to restart at any time, enter `rs`
[dev:profiles] [nodemon] watching path(s): dist/services/profiles/**/*
[dev:profiles] [nodemon] watching extensions: js,mjs,json
[dev:profiles] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/profiles/index.js`
[dev:accounts] Accounts service ready at http://localhost:4001/
[dev:profiles] Profiles service ready at http://localhost:4002/
[dev:gateway] [nodemon] 2.0.7
[dev:gateway] [nodemon] to restart at any time, enter `rs`
[dev:gateway] [nodemon] watching path(s): *.*
[dev:gateway] [nodemon] watching extensions: js,mjs,json
[dev:gateway] [nodemon] starting `node -r dotenv/config -r esm ./dist/index.js`
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
Typescript doing stuff...
[0] 17:08:57 - Found 226 errors. Watching for file changes.
[dev:profiles] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] starting `node -r dotenv/config -r esm ./dist/index.js`
[dev:profiles] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/accounts/index.js`
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/profiles/index.js`
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/profiles/index.js`
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] starting `node -r dotenv/config -r esm ./dist/index.js`
[dev:accounts] Accounts service ready at http://localhost:4001/
[dev:gateway] Server started at http://localhost:4000/graphql
[dev:gateway] Error checking for changes to service definitions: Couldn't load service definitions for "profiles" at http://localhost:4002: request to http://localhost:4002/ failed, reason: connect ECONNREFUSED 127.0.0.1:4002
[dev:gateway] This data graph is missing a valid configuration. Couldn't load service definitions for "profiles" at http://localhost:4002: request to http://localhost:4002/ failed, reason: connect ECONNREFUSED 127.0.0.1:4002
[dev:profiles] Profiles service ready at http://localhost:4002/
我希望这是可以理解的,我期待着关于如何解决这个问题的任何建议。
谢谢,马库斯
尝试在您的代码中而不是在您的 package.json 脚本中使用等待。确保用等待代码包装您的网关服务器。
示例:
import waitOn from 'wait-on'
import app from './config/app'
import server from './config/apollo'
const port = process.env.PORT
const options = {
resources: ['tcp:4001'],
}
waitOn(options)
.then(() => {
server.applyMiddleware({ app })
app.listen({ port }, () => {
console.log(
`Server ready at http://localhost:${port}${server.graphqlPath}`
)
})
})
.catch((err) => {
console.error('ERR:', err)
})
我正在努力设置我的 GraphQL Federation 开发环境。我有一些服务,我希望服务器在文件更改时自动重启。我使用的是 Typescript,所以 Typescript 必须先转译到 dist 文件夹,然后 nodemon 应该重新启动发生更改的服务。
我在 package.json 文件中创建了以下设置:
"dev": "concurrently -k \"tsc -w\" \"npm:dev:*\"",
"dev:accounts": "nodemon -r dotenv/config -r esm --watch ./dist/services/accounts ./dist/services/accounts/index.js",
"dev:profiles": "nodemon -r dotenv/config -r esm --watch ./dist/services/profiles ./dist/services/profiles/index.js",
"dev:gateway": "wait-on tcp:4001 tcp:4002 && nodemon -r dotenv/config -r esm ./dist/index.js"
这在我 运行 npm run dev
开始时有效 - 帐户和配置文件服务启动,一旦它们 运行ning,网关就会启动。
但是,一旦 Typescript 完成转译,nodemon 就会重新启动所有服务和网关,因为 dist 文件夹中的文件已被更改。
问题来了。 Nodemon独立重启服务和网关,网关重启更快,也就是说网关先于服务运行ning,导致如下错误:
[dev:gateway] Error checking for changes to service definitions: Couldn't load service definitions for "profiles" at http://localhost:4002: request to http://localhost:4002/ failed, reason: connect ECONNREFUSED 127.0.0.1:4002
[dev:gateway] This data graph is missing a valid configuration. Couldn't load service definitions for "profiles" at http://localhost:4002: request to http://localhost:4002/ failed, reason: connect ECONNREFUSED 127.0.0.1:4002
每当 nodemon 执行重启时,我不知道如何告诉 nodemon 等到所有服务都重启!
因为现在等待只在 npm run dev
的第一次初始执行时起作用。此后,对于每次更改,nodemon 负责重新启动并且不关心等待。
这是整个火车残骸:
$ npm 运行 开发
> my-app@1.0.0 dev
> concurrently -k "tsc -w" "npm:dev:*"
17:08:48 - Starting compilation in watch mode...
[dev:accounts] > my-app@1.0.0 dev:accounts
[dev:accounts] > nodemon -r dotenv/config -r esm --watch ./dist/services/accounts ./dist/services/accounts/index.js
[dev:gateway] > my-app@1.0.0 dev:gateway
[dev:gateway] > wait-on tcp:4001 tcp:4002 && nodemon -r dotenv/config -r esm ./dist/index.js
[dev:profiles] > my-app@1.0.0 dev:profiles
[dev:profiles] > nodemon -r dotenv/config -r esm --watch ./dist/services/profiles ./dist/services/profiles/index.js
[dev:accounts] [nodemon] 2.0.7
[dev:accounts] [nodemon] to restart at any time, enter `rs`
[dev:accounts] [nodemon] watching path(s): dist/services/accounts/**/*
[dev:accounts] [nodemon] watching extensions: js,mjs,json
[dev:accounts] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/accounts/index.js`
[dev:profiles] [nodemon] 2.0.7
[dev:profiles] [nodemon] to restart at any time, enter `rs`
[dev:profiles] [nodemon] watching path(s): dist/services/profiles/**/*
[dev:profiles] [nodemon] watching extensions: js,mjs,json
[dev:profiles] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/profiles/index.js`
[dev:accounts] Accounts service ready at http://localhost:4001/
[dev:profiles] Profiles service ready at http://localhost:4002/
[dev:gateway] [nodemon] 2.0.7
[dev:gateway] [nodemon] to restart at any time, enter `rs`
[dev:gateway] [nodemon] watching path(s): *.*
[dev:gateway] [nodemon] watching extensions: js,mjs,json
[dev:gateway] [nodemon] starting `node -r dotenv/config -r esm ./dist/index.js`
[dev:gateway] Server started at http://localhost:4000/graphql
[dev:accounts] > my-app@1.0.0 dev:accounts
[dev:accounts] > nodemon -r dotenv/config -r esm --watch ./dist/services/accounts ./dist/services/accounts/index.js
[dev:gateway] > my-app@1.0.0 dev:gateway
[dev:gateway] > wait-on tcp:4001 tcp:4002 && nodemon -r dotenv/config -r esm ./dist/index.js
[dev:profiles] > my-app@1.0.0 dev:profiles
[dev:profiles] > nodemon -r dotenv/config -r esm --watch ./dist/services/profiles ./dist/services/profiles/index.js
[dev:accounts] [nodemon] 2.0.7
[dev:accounts] [nodemon] to restart at any time, enter `rs`
[dev:accounts] [nodemon] watching path(s): dist/services/accounts/**/*
[dev:accounts] [nodemon] watching extensions: js,mjs,json
[dev:accounts] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/accounts/index.js`
[dev:profiles] [nodemon] 2.0.7
[dev:profiles] [nodemon] to restart at any time, enter `rs`
[dev:profiles] [nodemon] watching path(s): dist/services/profiles/**/*
[dev:profiles] [nodemon] watching extensions: js,mjs,json
[dev:profiles] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/profiles/index.js`
[dev:accounts] Accounts service ready at http://localhost:4001/
[dev:profiles] Profiles service ready at http://localhost:4002/
[dev:gateway] [nodemon] 2.0.7
[dev:gateway] [nodemon] to restart at any time, enter `rs`
[dev:gateway] [nodemon] watching path(s): *.*
[dev:gateway] [nodemon] watching extensions: js,mjs,json
[dev:gateway] [nodemon] starting `node -r dotenv/config -r esm ./dist/index.js`
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
Typescript doing stuff...
[0] 17:08:57 - Found 226 errors. Watching for file changes.
[dev:profiles] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] starting `node -r dotenv/config -r esm ./dist/index.js`
[dev:profiles] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] restarting due to changes...
[dev:accounts] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/accounts/index.js`
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/profiles/index.js`
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:profiles] [nodemon] starting `node -r dotenv/config -r esm ./dist/services/profiles/index.js`
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] restarting due to changes...
[dev:gateway] [nodemon] starting `node -r dotenv/config -r esm ./dist/index.js`
[dev:accounts] Accounts service ready at http://localhost:4001/
[dev:gateway] Server started at http://localhost:4000/graphql
[dev:gateway] Error checking for changes to service definitions: Couldn't load service definitions for "profiles" at http://localhost:4002: request to http://localhost:4002/ failed, reason: connect ECONNREFUSED 127.0.0.1:4002
[dev:gateway] This data graph is missing a valid configuration. Couldn't load service definitions for "profiles" at http://localhost:4002: request to http://localhost:4002/ failed, reason: connect ECONNREFUSED 127.0.0.1:4002
[dev:profiles] Profiles service ready at http://localhost:4002/
我希望这是可以理解的,我期待着关于如何解决这个问题的任何建议。
谢谢,马库斯
尝试在您的代码中而不是在您的 package.json 脚本中使用等待。确保用等待代码包装您的网关服务器。 示例:
import waitOn from 'wait-on'
import app from './config/app'
import server from './config/apollo'
const port = process.env.PORT
const options = {
resources: ['tcp:4001'],
}
waitOn(options)
.then(() => {
server.applyMiddleware({ app })
app.listen({ port }, () => {
console.log(
`Server ready at http://localhost:${port}${server.graphqlPath}`
)
})
})
.catch((err) => {
console.error('ERR:', err)
})