|
|
il y a 1 mois | |
|---|---|---|
| .. | ||
| src | il y a 1 mois | |
| publish.yaml | il y a 1 mois | |
| readme.md | il y a 1 mois | |
快速部署一个 JAVA 11 的 Event 类型的读写 Mysql 数据库函数到阿里云函数计算。在本案例中提供公网方式连接到 RDS MySQL数据库。
使用该项目,推荐您拥有以下的产品权限 / 策略:
| 服务/业务 | 函数计算 |
| --- | --- |
| 权限/策略 | AliyunFCFullAccess |
使用该项目,您需要提前准备好 MySQL 数据库并执行以下 SQL 语句创建表:
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`age` tinyint(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
MySQL 数据库既可以选择阿里云 MySQL 数据库也可以选择其它 MySQL 数据库。在本案例中我们使用公网方式连接到阿里云 MySQL 数据库,若使用vpc方式连接阿里云 MySQL 数据库,请参考文档配置网络配置VPC网络。
s init rds-mysql-fc-event-java11 -d rds-mysql-fc-event-java11cd rds-mysql-fc-event-java11 && s deploy -y| 参数名称 | 参数类型 | 是否必填 | 例子 | 参数含义 |
| --- | --- | --- | --- | --- |
| MYSQL_ENDPOINT | String | 必填 | rm-uf6rrswxxxxxxxxxxxx.mysql.rds.aliyuncs.com | 数据库网址,用于连接数据库 |
| MYSQL_PORT | String | 必填 | 3306 | 数据库端口 |
| MYSQL_DBNAME | String | 必填 | test | 数据库名称 |
| MYSQL_USER | String | 必填 | fc | 数据库用户名 |
| MYSQL_PASSWORD | String | 必填 | xxxxxxxx | 数据库密码 |
使用 Serverless Devs Cli 调试
s invoke 进行远程调试调用函数时收到的响应如下所示:
========= FC invoke Logs begin =========
FC Initialize Start RequestId: 9fe60072-7fe2-4f62-9d13-xxxxxxx
[Name] Register [com.aliyun.serverless.runtime.classloader.FunctionClassLoader@58372a00] as [com.aliyun.serverless.runtime.classloader.FunctionClassLoader@com.aliyun.serverless.runtime.classloader.FunctionClassLoader@/code/HelloFCJava-1.0-SNAPSHOT.jar/code/original-HelloFCJava-1.0-SNAPSHOT.jar]: hash [8bbd2e0] (normal mode)
2022-07-19 04:21:54.577 [INFO] [9fe60072-7fe2-4f62-9d13-62fc04156f77] database connection time cost: 397ms
FC Initialize End RequestId: 9fe60072-7fe2-4f62-9d13-xxxxxxx
FC Invoke Start RequestId: 7581b7c4-bb40-4690-90d6-xxxxxxx
2022-07-19 04:21:59.539 [INFO] [7581b7c4-bb40-4690-90d6-xxxxxxx] Success - 1 rows affected.
2022-07-19 04:21:59.613 [INFO] [7581b7c4-bb40-4690-90d6-xxxxxxx] get user: {name=wanger, id=3, age=5}
FC Invoke End RequestId: 7581b7c4-bb40-4690-90d6-xxxxxxx
Duration: 108.03 ms, Billed Duration: 109 ms, Memory Size: 128 MB, Max Memory Used: 109.13 MB
========= FC invoke Logs end =========
FC Invoke instanceId: c-62d63161-a82af772bxxxxxxx
FC Invoke Result:
{name=wanger, id=3, age=5}
End of method: invoke
端对端测试
控制台返回结果如下所示:
{name=wanger, id=3, age=5}
数据库访问限制
本应用仅作为学习和参考使用,您可以基于本项目进行二次开发和完善,实现自己的业务逻辑
未设置白名单,MySQL 网址或端口设置错误
"errorMessage": "Communications link failure\n\nThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server."
MySQL 用户名、密码错误
"errorMessage": "Access denied for user 'fc'@'120.76.207.131' (using password: YES)"
MySQL 数据库名称错误
"errorMessage": "Unknown database 'users1'"
您如果有关于错误的反馈或者未来的期待,您可以在 Serverless Devs repo Issues 中进行反馈和交流。如果您想要加入我们的讨论组或者了解 FC 组件的最新动态,您可以通过以下渠道进行:
|
|
|
|
|--- | --- | --- |
| 微信公众号:`serverless` | 微信小助手:`xiaojiangwh` | 钉钉交流群:`33947367` |