且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

函数运行环境系统动态链接库版本太低?函数计算 fun 分忧解难

更新时间:2022-06-17 08:44:15

场景介绍

用户问题

简要描述一下用户当时遇到的问题:

用户使用函数计算的 nodejs8 runtime,在本地自己的开发环境使用 npm install couchbase 安装了 couchbase 这个第三方库。couchbase 封装了 C 库,依赖系统底层动态链接库 libstdc++.so.6。因为用户自己的开发环境的操作系统内核比较新,所以本地安装、编译和调试都比较顺利。所以,最后按照函数计算的打包方式成功创建了 Function,但是执行 InvokeFunction 时,遇到了这样的错误:

"errorMessage": "/usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by /code/node_modules/couchbase/build/Release/couchbase_impl.node)",
    "errorType": "Error",
    "stackTrace": [
        "Error: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by /code/node_modules/couchbase/build/Release/couchbase_impl.node)",
...

错误发生的原因如堆栈描述,即没有 CXXABI_1.3.9 这个版本,couchbase 2.6.5 版本已经修复了该问题,即使环境中没有CXXABI_1.3.9 这个版本也是可以使用的,可以直接使用 fun 工具直接安装该模块,但是需要确保 package.json 中 couchbase 版本在 2.6.5 以上即可。

下面我们就详细介绍一下利用 fun 工具安装 couchbase 模块的步骤。

代码项目搭建

前提:先按照 fun 的安装步骤安装 fun工具,并进行 fun config 配置。

在本地很快搭建了一个项目目录:

- test_code/
  - index.js
  - template.yml
  - Funfile
  

其中 index.js , template.yml 和 Funfile 的 内容分别为

# index.js
const couchbase = require('couchbase');

module.exports.handler = function(event, context, callback) {
    var cluster = new couchbase.Cluster('couchbase://127.0.0.1', {
        username: 'username',
        password: 'password',
      });
    var bucket = cluster.bucket('default');
    var coll = bucket.defaultCollection();
    coll.upsert('testdoc', {name:'Frank'}, (err, res) => {
        if (err) throw err;
       
        coll.get('testdoc', (err, res) => {
          if (err) throw err;
       
          console.log(res.value);
          // {name: Frank}
        });
      });
    callback(null, {
        hello: 'world'
    })
}


#template.yml 
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
  fc: # service name
    Type: 'Aliyun::Serverless::Service'
    Properties:
      Description: 'fc test'
    helloworld: # function name
      Type: 'Aliyun::Serverless::Function'
      Properties:
        Handler: index.handler
        Runtime: nodejs8
        CodeUri: './'
        Timeout: 60
 
       
#Funfile
RUNTIME nodejs8
RUN npm install couchbase         

执行 fun install 命令

$ fun install
using template: template.yml
start installing function dependencies without docker

building fc/helloworld
Funfile exist, Fun will use container to build forcely
Step 1/2 : FROM registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-nodejs8:build-1.9.1
 ---> fff7345ce8a3
Step 2/2 : RUN npm install couchbase
 ---> Using cache
 ---> cdd1bba7c23e
sha256:cdd1bba7c23e162042da0df8c433674f1e7bfec8d49e133478dc06c9939bb891
Successfully built cdd1bba7c23e
Successfully tagged fun-cache-84cac7f3-5391-49a6-b108-b1ac7269ccf7:latest
copying function artifact to /Users/txd123/Desktop/nodejs

build function using image: fun-cache-84cac7f3-5391-49a6-b108-b1ac7269ccf7
running task flow NpmTaskFlow
running task: CopySource
running task: NpmInstall

Install Success


Tips for next step
======================
* Invoke Event Function: fun local invoke
* Invoke Http Function: fun local start
* Build Http Function: fun build
* Deploy Resources: fun deploy

发布上线

使用 fun deploy 发布上线,然后到控制台执行一下线上实际的运行效果:
函数运行环境系统动态链接库版本太低?函数计算 fun 分忧解难

总结

fun install 功能能够将代码和依赖文件分离开,独立安装系统依赖文件,而且 fun local 和 fun deply 都能够自动帮你设置第三方库的依赖引用路径,让您无需关心环境变量问题。