访问数据库是指在函数计算中通过函数调用数据库应用编程接口,对数据库执行数据插入、查询等操作。通常函数计算中不同的执行环境实例之间的状态是不共享的,通过数据库可以将结构化的数据持久化以实现状态共享。本文以Python 3为例,介绍如何使用Serverless Devs部署函数来访问Lindorm数据库。

前提条件

  • 创建Lindorm实例
    注意
    • 请确保您所创建的数据库实例与需要访问该数据库实例的函数在同一地域。

    • 建议您在函数计算支持的可用区创建数据库实例。

      如果您的资源所在的可用区不在以下列表中,可以通过在您的VPC环境中创建一个与函数计算相同可用区的vSwitch,并在函数计算的服务的VPC配置中设置此vSwitch ID。由于同一VPC内不同vSwitch之间私网互通,因此函数计算可以通过该vSwitch访问在其他可用区的VPC内的资源。具体步骤,请参见遇到vSwitch is in unsupported zone的错误怎么办?

      地域 地域ID 函数计算支持的可用区
      华东1(杭州) cn-hangzhou cn-hangzhou-f、cn-hangzhou-g、cn-hangzhou-h
      华东2(上海) cn-shanghai cn-shanghai-b、cn-shanghai-e、cn-shanghai-g、cn-shanghai-f
      华北1(青岛) cn-qingdao cn-qingdao-c
      华北2(北京) cn-beijing cn-beijing-h、cn-beijing-c、cn-beijing-e、cn-beijing-f
      华北3(张家口) cn-zhangjiakou cn-zhangjiakou-b、cn-zhangjiakou-a
      华北5(呼和浩特) cn-huhehaote cn-huhehaote-a、cn-huhehaote-b
      华南1(深圳) cn-shenzhen cn-shenzhen-e、cn-shenzhen-d
      西南1(成都) cn-chengdu cn-chengdu-a、 cn-chengdu-b
      中国香港 cn-hongkong cn-hongkong-c
      新加坡 ap-southeast-1 ap-southeast-1a、ap-southeast-1b
      澳大利亚(悉尼) ap-southeast-2 ap-southeast-2a、ap-southeast-2b
      马来西亚(吉隆坡) ap-southeast-3 ap-southeast-3a
      印度尼西亚(雅加达) ap-southeast-5 ap-southeast-5a、ap-southeast-5b
      日本(东京) ap-northeast-1 ap-northeast-1b、ap-northeast-1a
      英国(伦敦) eu-west-1 eu-west-1a
      德国(法兰克福) eu-central-1 u-central-a、eu-central-1a、eu-central-1b
      美国(硅谷) us-west-1 us-west-1a、us-west-1b
      美国(弗吉尼亚) us-east-1 us-east-1b、us-east-1a
      印度(孟买) ap-south-1 ap-south-1a、ap-south-1b
  • 安装Serverless Devs和Docker
  • 配置Serverless Devs
  • 创建安全组

    在数据库部署的VPC内创建一个默认的安全组。如果使用已有的安全组,请确保该安全组的出口方向已放行数据库的端口和私网IP段(一般来说都是默认允许所有访问)。

配置数据库访问IP地址白名单

请使用IP地址白名单来实现安全访问,请勿使用安全组。否则,可能会导致函数连接不稳定的情况,影响业务正常运行。

  1. 登录Lindorm管理控制台
  2. 在页面左上角,选择实例所属的地域。
  3. 实例列表页,单击目标实例ID或者目标实例所在行操作列的管理
  4. 在左侧导航栏,单击访问控制
  5. 单击修改分组白名单
  6. 修改分组白名单对话框,输入允许访问的IP地址段,单击确认
    注意 此处填写的IP地址,需为在函数计算服务的网络配置中添加的vSwitch的IP地址段。

部署并调用函数

  1. 执行以下命令初始化项目。
    s init start-fc-lindorm-python -d start-fc-lindorm-python
  2. 执行以下命令进入项目。
    cd start-fc-lindorm-python
  3. 编辑s.yaml文件。

    示例如下:

    edition: 1.0.0          
    name: fcDeployApp       
    access: "default"  
    
    services:
      fc-db-lindorm-python: 
        component: devsapp/fc  
        props:
          region: cn-hangzhou
          service:
            name: fc-db-demo
            description: 'demo for fc visit db'
            internetAccess: true
            vpcConfig:
              vpcId: vpc-bp1hcg467ekqsv0zr**** # 数据库实例所在的VPC ID。
              securityGroupId: sg-bp1gg2tyyzuvz8x0**** # 安全组ID。
              vswitchIds:
                - vsw-bp1sciqyju9qxocys**** # 请确保该vSwitch对应的网段已配置到数据库实例访问白名单中。
          function:
            name: lindorm
            description: visit lindorm
            runtime: python3
            codeUri: ./code
            handler: index.handler
            memorySize: 256
            timeout: 30
            environmentVariables:
              DATABASE_ADDR: ld-bp13c5s05n1mu****-proxy-lindorm.lindorm.rds.aliyuncs.com:30** # 数据库接入点。
              USER: r*** # 数据库用户。
              PASSWORD: r*** # 数据库用户密码。
  4. 执行以下命令构建项目。
    s build --use-docker
  5. 执行以下命令部署项目。
    s deploy -y
  6. 执行以下命令调用函数。
    s invoke -e "{}"

    预期输出如下:

    [2021-09-22T22:54:19.851] [INFO ] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: a41a7647-cf8b-466d-a63f-59635043453e
    2021-09-22T14:54:20.289Z a41a7647-cf8b-466d-a63f-59635043453e [INFO] start to test Lindorm 
    2021-09-22T14:54:20.291Z a41a7647-cf8b-466d-a63f-59635043453e [WARNING] Cluster.__init__ called with contact_points specified, but no load_balancing_policy. In the next major version, this will raise an error; please specify a load-balancing policy. (contact_points = ['ld-bp13c5s05n1mu****-proxy-lindorm.lindorm.rds.aliyuncs.com'], lbp = None)
    2021-09-22T14:54:20.301Z a41a7647-cf8b-466d-a63f-59635043453e [WARNING] Downgrading core protocol version from 66 to 65 for 192.168.**.**:90**. To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version
    2021-09-22T14:54:20.309Z a41a7647-cf8b-466d-a63f-59635043453e [WARNING] Downgrading core protocol version from 65 to 5 for 192.168.**.**:90**. To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version
    2021-09-22T14:54:20.317Z a41a7647-cf8b-466d-a63f-59635043453e [ERROR] Closing connection <LibevConnection(13962401490****) 192.168.**.**:90**> due to protocol error: Error from server: code=000a [Protocol error] message="Beta version of the protocol used (5/v5-beta), but USE_BETA flag is unset"
    2021-09-22T14:54:20.318Z a41a7647-cf8b-466d-a63f-59635043453e [WARNING] Downgrading core protocol version from 5 to 4 for 192.168.**.**:90**. To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version
    2021-09-22T14:54:20.364Z a41a7647-cf8b-466d-a63f-59635043453e [INFO] Using datacenter 'datacenter1' for DCAwareRoundRobinPolicy (via host '192.168.**.**:90**'); if incorrect, please specify a local_dc to the constructor, or limit contact points to local cluster nodes
    2021-09-22T14:54:20.550Z a41a7647-cf8b-466d-a63f-59635043453e [INFO] # row: Row(id=1, address='hangzhou', age=11, name='testname')
    FC Invoke End RequestId: a41a7647-cf8b-466d-a63f-59635043453e
    
    Duration: 267.05 ms, Billed Duration: 268 ms, Memory Size: 256 MB, Max Memory Used: 87.87 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    null
    
    
    End of method: invoke

更多信息

函数计算访问数据库案例集