Skip to content

Commit

Permalink
redis 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
txd12 authored and vangie committed Feb 12, 2020
1 parent bf6591c commit f0c4c32
Show file tree
Hide file tree
Showing 24 changed files with 831 additions and 14 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ node_modules
.classpath
.project
.vscode
[Bb]in/
[Oo]bj/
[Oo]ut/
*/dotnetcore/[Bb]in/
*/dotnetcore/[Oo]bj/
*/dotnetcore/[Oo]ut/
4 changes: 1 addition & 3 deletions rds-mysql/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

OSS_BUCKET ?= fun-pkg
STACK_NAME ?= mysql-test

start_mysql:
Expand All @@ -13,7 +11,7 @@ build:


deploy:
fun package --oss-bucket $(OSS_BUCKET)
fun package
fun deploy --use-ros --stack-name $(STACK_NAME)

invoke_java:
Expand Down
13 changes: 6 additions & 7 deletions rds-mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ Windows 和 Linux 用户安装请参考:

安装好后,记得先执行 `fun config` 初始化一下配置。

**注意**, 如果你已经安装过了 funcraft,确保 funcraft 的版本在 3.5.1 以上。
**注意**, 如果你已经安装过了 funcraft,确保 funcraft 的版本在 3.5.3 以上。

```bash
$ fun --version
3.5.1
3.5.3
```

## 初始化
Expand All @@ -37,8 +37,6 @@ git clone http://github.com/vangie/fc-db
cd fc-db/rds-mysql
```



## 构建

```bash
Expand All @@ -47,10 +45,8 @@ make build

## 部署

新建或者找一个已有的 OSS Bucket ,其所属的 region 与 fun config 时指定的 region 相同,用于存放 fun package 打包的代码包。将新建的 Bucket 名称替换下面命令的 `xxx` 部分。

```bash
OSS_BUCKET=xxx make deploy
make deploy
```

## 测试
Expand All @@ -67,6 +63,9 @@ make invoke_python

# PHP
make invoke_php

# .NET
make invoke_dotnetcore
```

## 示例工程源码
Expand Down
10 changes: 9 additions & 1 deletion rds-mysql/template.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ Resources:
MYSQL_PORT: 33061
MYSQL_DBNAME: mydb
MYSQL_USER: fc-user
MYSQL_PASSWORD: my-secret-pw
MYSQL_PASSWORD: my-secret-pw
python:
Properties:
EnvironmentVariables:
MYSQL_HOST: host.docker.internal
MYSQL_PORT: 33061
MYSQL_DBNAME: mydb
MYSQL_USER: fc-user
MYSQL_PASSWORD: my-secret-pw
32 changes: 32 additions & 0 deletions rds-redis/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
STACK_NAME ?= redis-test

start_redis:
bin/start_redis.sh

stop_redis:
docker stop redis_for_fc

clean:
docker rm -f redis_for_fc

build:
IGNORE_TPL_VALIDATION=true fun build

deploy:
fun package
fun deploy --use-ros --stack-name $(STACK_NAME)

invoke_java:
IGNORE_TPL_VALIDATION=true fun invoke java

invoke_nodejs:
IGNORE_TPL_VALIDATION=true fun invoke nodejs

invoke_dotnetcore:
IGNORE_TPL_VALIDATION=true fun invoke dotnetcore

invoke_python:
IGNORE_TPL_VALIDATION=true fun invoke nodejs

invoke_php:
IGNORE_TPL_VALIDATION=true fun invoke php
77 changes: 77 additions & 0 deletions rds-redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 函数计算访问 Redis 示例集

## 依赖工具

本项目是在 MacOS 下开发的,涉及到的工具是平台无关的,对于 Linux 和 Windows 桌面系统应该也同样适用。在开始本例之前请确保如下工具已经正确的安装,更新到最新版本,并进行正确的配置。

* [Docker](https://www.docker.com/)
* [Funcraft](https://github.com/aliyun/fun)

Fun 工具依赖于 docker 来模拟本地环境。

对于 MacOS 用户可以使用 [homebrew](https://brew.sh/) 进行安装:

```bash
brew cask install docker
brew tap vangie/formula
brew install fun
```

Windows 和 Linux 用户安装请参考:

1. https://github.com/aliyun/fun/blob/master/docs/usage/installation.md

安装好后,记得先执行 `fun config` 初始化一下配置。

**注意**, 如果你已经安装过了 funcraft,确保 funcraft 的版本在 3.5.3 以上。

```bash
$ fun --version
3.5.3
```

## 初始化

```bash
git clone http://github.com/vangie/fc-db
cd fc-db/rds-redis
```

## 构建

```bash
make build
```

## 部署

```bash
make deploy
```

## 测试

```bash
# java
make invoke_java

# nodejs
make invoke_nodejs

# python
make invoke_python

# PHP
make invoke_php

# .NET
make invoke_dotnetcore
```

## 示例工程源码

* [Nodejs 示例工程](nodejs)
* [Python 示例工程](python)
* [Java 示例工程](java)
* [PHP 示例工程](php)
* [Dotnetcore 示例工程](dotnetcore)
12 changes: 12 additions & 0 deletions rds-redis/bin/start_redis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

CONTAINER_NAME=redis_for_fc
REDIS_PASSWORD=foobared

if [ ! "$(docker ps -a | grep $CONTAINER_NAME)" ]; then
docker run --name $CONTAINER_NAME \
-p 6379:6379 \
redis redis-server --requirepass "$REDIS_PASSWORD"
else
docker start $CONTAINER_NAME
fi
42 changes: 42 additions & 0 deletions rds-redis/dotnetcore/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.IO;
using System.Text;
using Aliyun.Serverless.Core;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.StackExchangeRedis;

namespace redis
{
public class RedisHandler
{
private static String host = Environment.GetEnvironmentVariable("REDIS_HOST");
private static String port = Environment.GetEnvironmentVariable("REDIS_PORT");
private static String passwd = Environment.GetEnvironmentVariable("REDIS_PASSWORD");
private static String connStr = $"{host}:{port},password={passwd},connectTimeout=5000,writeBuffer=40960";

public Stream Handler(Stream input, IFcContext context)
{
RedisCache cache = new RedisCache(new RedisCacheOptions
{
Configuration = connStr
});

var counterBytes = cache.Get("dotnet_counter");
var counter = 0;
if (counterBytes != null)
{
counter = BitConverter.ToInt32(counterBytes, 0);
}
Console.WriteLine($"Counter: {counter}");

cache.Set("dotnet_counter", BitConverter.GetBytes(counter + 1));

counterBytes = BitConverter.GetBytes(counter);
MemoryStream output = new MemoryStream();
output.Write(counterBytes, 0, counterBytes.Length);
output.Seek(0, SeekOrigin.Begin);
return output;
}

}
}
12 changes: 12 additions & 0 deletions rds-redis/dotnetcore/redis.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aliyun.Serverless.Core" Version="1.0.1" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.1" />
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions rds-redis/dotnetcore/template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
mysql-test-rds-mysql-6976C51BE0E8:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: This is mysql service
Role: 'acs:ram::1986114430573743:role/mysql-test-rds-mysqlrole-03b637810896'
VpcConfig:
VpcId: vpc-hp3cpshxpkf5qu018z0w6
VSwitchIds:
- vsw-hp3d1hkejje4tny27cqpr
SecurityGroupId: sg-hp3ikp9yw76ps8qxkief
InternetAccess: true
donet-test:
Type: 'Aliyun::Serverless::Function'
Properties:
Initializer: 'mysql::mysql.MysqlHandler::Init'
InitializationTimeout: 3
Handler: 'mysql::mysql.MysqlHandler::InsertData'
Runtime: dotnetcore2.1
Timeout: 60
MemorySize: 512
CodeUri: './bin/Release/netcoreapp2.1/publish/'
EnvironmentVariables:
MYSQL_DBNAME: mydb
MYSQL_HOST: XXX.mysql.huhehaote.rds.aliyuncs.com
MYSQL_PASSWORD: Passwd
MYSQL_PORT: '3306'
MYSQL_USER: username
34 changes: 34 additions & 0 deletions rds-redis/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>example</groupId>
<artifactId>fc-java-redis</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>fc-java-redis</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.aliyun.fc.runtime</groupId>
<artifactId>fc-java-core</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.test.skip>true</maven.test.skip>
</properties>
</project>
40 changes: 40 additions & 0 deletions rds-redis/java/src/main/java/example/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package example;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import redis.clients.jedis.Jedis;
import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.StreamRequestHandler;

public class App implements StreamRequestHandler {

private String host = System.getenv("REDIS_HOST");
private int port = Integer.parseInt(System.getenv("REDIS_PORT"));
private String passwd = System.getenv("REDIS_PASSWORD");

@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
try {
Jedis jedis = new Jedis(host, port);
jedis.auth(passwd);

String counter = jedis.get("counter");
if (counter == null) {
counter = "0";
}

System.out.println("counter: " + counter);

counter = Integer.toString(Integer.parseInt(counter) + 1);

jedis.set("counter", counter);
jedis.quit();
jedis.close();

outputStream.write(counter.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit f0c4c32

Please sign in to comment.