我在 NGINX UNIT 上创建了一个 GraphQL MSA 服务器并进行了测试

尽管nginx在网络服务中被广泛使用且非常多才多艺,但是nginx-unit似乎并不为人所熟知。

据看到Nginx Unit的官网,它似乎是为了微服务架构而诞生的。

看到这个形象的话,应该是一种像 MSA(微软亚洲研究院)那样的结构吧!

使用 Docker Nginx Unit 作为基准,Nginx Unit 相比于 Nginx 能够更轻松地进行配置,并且可以动态地更改配置文件。据说性能也可以负载高达10000个 HTTP 连接。只需调整 config.json 文件即可进行配置(这是我最喜欢的部分)。

我将描述将使用Node Express创建的GraphQL服务器转变为微服务架构的过程。

我参考了GraphQL官方网站上的express服务器代码,详细请参阅https://graphql.org/graphql-js/running-an-express-graphql-server/。与网站上的不同之处是文件名不是index.js,而是app.js。

//app.js
var express = require('express');
var { graphqlHTTP } = require('express-graphql');
var { buildSchema } = require('graphql');

// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
  type Query {
    hello: String
    amount: Int
  }
`);

// The root provides a resolver function for each API endpoint
var root = {
  hello: () => 'Hello, Hello, Hello',
  amount:() => 34500
};

var app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at http://localhost:4000/graphql');

这个服务将使用4000端口进行/graphql服务。

接下来我们将准备config.json。下面是用于express的config.js。

{
    "listeners": {
        "*:4000": {
            "pass": "applications/express"
        }
    },

    "applications": {
        "express": {
            "type": "external",
            "working_directory": "/www/",
            "executable": "/usr/bin/env",
            "arguments": [
                "node",
                "--loader",
                "unit-http/loader.mjs",
                "--require",
                "unit-http/loader",
                "app.js"
            ]
        }
    }
}

当在端口4000上进行更改,并查看applications时,listeners会发现working directory是/www。我们将app.js复制到该目录中。

使用Dockerfile通过docker build命令创建Docker镜像。Dockerfile的构成如下所示。

# Using base image provided by nginx unit

# nginx unit node16のバージョンimageを選択(1.26.1-node16)
FROM nginx/unit:1.26.1-node16

# docker imageの中/docker-entrypoint.d/で存在るconfig.jsonを作業したconfig.jsonで切り替え。
COPY config.json /docker-entrypoint.d/config.json

# expressのapp.jsをdocker imageの中/www/にコピー。もし他の必要なライブラリがあれば一緒にコピー。
COPY app.js /www/

# docker imageの中でgraphQLのためにexpress, express-graphql, graphqlインストール
RUN cd /www && npm install express express-graphql graphql --save

# docker imageの中でExpressのためにunit-httpインストール
RUN cd /www && npm install -g --unsafe-perm unit-http
RUN cd /www && npm link unit-http

# 使用するポートexpose
EXPOSE 4000

如果这里只有安装了GraphQL,但如果还需要其他必要的内容,我们会运行`cd /www && npm install`并添加所需的内容。

目前,使用Docker构建的目录结构如下。

.
├── Dockerfile
├── app.js
├── config.json
├── node_modules ⇦ フォルダです。
├── package-lock.json
└── package.json

我会在这个目录下进行docker build。

> docker build --tag=expressql .

当Docker构建完成后,您可以使用docker images命令来确认结果。

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
expressql    latest    541b254c0f04   3 hours ago    929MB ⇦ これです。
jenkins      1.0       35ec6519bffc   4 months ago   1.18GB

因为我提前三小时完成了任务,所以它显示为“3小时前”。

好吧,让我们启动集装箱。

docker run -d -p 4000:4000 --name eql expressql

当以 http://localhost:4000/graphql 进行连接时,若出现以下画面则表示成功!。

所以我用nginx-unit创建了一个简单的MSA应用程序来测试。

bannerAds