graalvm 23.1.0 开始 js docker 镜像&简单试用

发布时间 2023-09-20 11:11:56作者: 荣锋亮

graaljs docker 镜像

很简单就是下载官方包,集成下,然后进行一些简单的配置

  • Dockerfile
FROM debian:bullseye-backports
LABEL author="rongfengliang"
LABEL email="1141591465@qq.com"
WORKDIR /opt/
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo 'Asia/Shanghai' >/etc/timezone
COPY graalnodejs-23.1.0-linux-amd64/ /opt/nodejs/
ENV PATH=/opt/nodejs/bin:$PATH

参考项目

  • package.json
{
  "name": "graaljs-learning",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "ncc build index.js -o dist"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "fastify": "^4.23.2",
    "hashids": "^2.3.0"
  },
  "devDependencies": {
    "@vercel/ncc": "^0.38.0"
  }
}
  • index.js
const hashids = require('hashids')
 
const myid = new hashids('this is my salt', 10, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890')
 
const Fastify = require('fastify')
 
const fastify = Fastify()
 
fastify.get('/', async (request, reply) => {
    console.log(myid.encode(1, 2, 3))
    reply.send({ hello: myid.encode(1, 2, 3) })
})
 
fastify.listen({
    port: 3000,
    host: "0.0.0.0",
    backlog: 511
}, (err, address) => {
    if (err) {
        console.error(err)
        process.exit(1)
    }
    console.log(`Server listening at ${address}`)
})
  • 构建
yarn build
  • docker-compose 文件
version: '3'
services:
   app:
     image: dalongrong/graaljs:23.1.0
     command: node --jvm --polyglot  /app/index.js
     ports:
       - "3000:3000"
     volumes:
     - ./dist:/app/
  • 效果

 

说明

docker 镜像我已经push dockerhub了 dalongrong/graaljs:23.1.0 目前来说独立的graaljs 的确会简化node 与jvm 项目的集成,当然从实际测试来说graaljs 暂时还是不如nodejs 项目启动快速,但是对于业务有node 与java 互调用的场景是一个特别好的选择,比基于node-addon 的模式很很多

参考资料

https://github.com/oracle/graaljs/releases/