访问数据库是指在函数计算中通过函数调用数据库应用编程接口,对数据库执行数据插入、查询等操作。通常函数计算中不同的执行环境实例之间的状态是不共享的,通过数据库可以将结构化的数据持久化以实现状态共享。本文以Python
3为例,介绍如何使用Serverless Devs部署函数来访问Lindorm数据库。
配置数据库访问IP地址白名单
请使用IP地址白名单来实现安全访问,请勿使用安全组。否则,可能会导致函数连接不稳定的情况,影响业务正常运行。
- 登录Lindorm管理控制台。
- 在页面左上角,选择实例所属的地域。
- 在实例列表页,单击目标实例ID或者目标实例所在行操作列的管理。
- 在左侧导航栏,单击访问控制。
- 单击修改分组白名单。
- 在修改分组白名单对话框,输入允许访问的IP地址段,单击确认。
注意 此处填写的IP地址,需为在函数计算服务的网络配置中添加的vSwitch的IP地址段。
部署并调用函数
- 执行以下命令初始化项目。
s init start-fc-lindorm-python -d start-fc-lindorm-python
- 执行以下命令进入项目。
cd start-fc-lindorm-python
- 编辑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*** # 数据库用户密码。
- 执行以下命令构建项目。
- 执行以下命令部署项目。
- 执行以下命令调用函数。
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