Redis
Detailed information on the Redis state store component
配置
To setup Redis state store create a component of type state.redis
. 请参阅本指南,了解如何创建和应用状态存储配置。
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: <NAME>
namespace: <NAMESPACE>
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: <HOST>
- name: redisPassword
value: <PASSWORD>
- name: enableTLS
value: <bool> # Optional. Allowed: true, false.
- name: failover
value: <bool> # Optional. Allowed: true, false.
- name: sentinelMasterName
value: <string> # Optional
- name: maxRetries
value: # Optional
- name: maxRetryBackoff
value: # Optional Allowed: true, false.
- name: failover
value: <bool> # Optional. Allowed: true, false.
- name: sentinelMasterName
value: <string> # Optional
- name: maxRetries
value: # Optional
- name: maxRetryBackoff
value: # Optional Allowed: true, false.
- name: failover
value: <bool> # Optional. Allowed: true, false.
- name: sentinelMasterName
value: <string> # Optional
- name: maxRetries
value: # Optional
- name: maxRetryBackoff
value: # Optional
TLS: If the Redis instance supports TLS with public certificates it can be configured to enable or disable TLS true
or false
.
Failover: When set to true
enables the failover feature. The redisHost should be the sentinel host address. See Redis Sentinel Documentation
Warning
以上示例将 Secret 明文存储。 更推荐的方式是使用 Secret 组件, 这里。
If you wish to use Redis as an actor store, append the following to the yaml.
- name: actorStateStore
value: "true"
元数据字段规范
字段 | 必填 | 详情 | 示例 |
---|---|---|---|
redisHost | Y | Redis的连接地址 | localhost:6379 , redis-master.default.svc.cluster.local:6379 |
redisPassword | Y | Redis的密码 无默认值 可以用secretKeyRef 来引用密钥。 | “” , “KeFg23!” |
consumerID | N | 消费组 ID | “mygroup” |
enableTLS | N | 如果Redis实例支持使用公共证书的TLS,可以配置为启用或禁用。 默认值为 “false” | “true” , “false” |
maxRetries | N | Maximum number of retries before giving up. Defaults to 3 | 5 , 10 |
maxRetryBackoff | N | Minimum backoff between each retry. Defaults to 2 seconds | 3000000000 |
failover | N | Property to enabled failover configuration. Needs sentinalMasterName to be set. 默认值为 “false” | “true” , “false” |
sentinelMasterName | N | The sentinel master name. See Redis Sentinel Documentation | “” , “127.0.0.1:6379” |
actorStateStore | N | 是否将此状态存储给 Actor 使用。 默认值为 “false” | “true” , “false” |
Setup Redis
Dapr can use any Redis instance - containerized, running on your local dev machine, or a managed cloud service. If you already have a Redis store, move on to the Configuration section.
A Redis instance is automatically created as a Docker container when you run dapr init
We can use Helm to quickly create a Redis instance in our Kubernetes cluster. 这种方法需要安装Helm。
安装 Redis 到你的集群: Note that we’re explicitly setting an image tag to get a version greater than 5, which is what Dapr’ pub/sub functionality requires. If you’re intending on using Redis as just a state store (and not for pub/sub), you do not have to set the image version.
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install redis bitnami/redis
执行
kubectl get pods
来查看现在正在集群中运行的Redis容器。Add
redis-master:6379
as theredisHost
in your redis.yaml file. 例如:metadata:
- name: redisHost
value: redis-master:6379
Next, we’ll get the Redis password, which is slightly different depending on the OS we’re using:
Windows:执行
kubectl get secret --namespace default redis -o jsonpath="{.data.redis-password}" > encoded.b64
,这将创建一个有你的加密后密码的文件。 接下来,执行certutil -decode encoded.b64 password.txt
,它将把你的redis密码放在一个名为password.txt
的文本文件中。 复制密码,删除这两个文件。Linux/MacOS:执行
kubectl get secret --namespace default redis -o jsonpath="{.data.redis-password}" | base64 --decode
并复制输出的密码。
Add this password as the
redisPassword
value in your redis.yaml file. 例如:metadata:
- name: redisPassword
value: lhDOkwTlp0
Note: this approach requires having an Azure Subscription.
- Open this link to start the Azure Cache for Redis creation flow. 如有必要,请登录。
- Fill out necessary information and check the “Unblock port 6379” box, which will allow us to persist state without SSL.
- 点击“创建”来启动您的 Redis 实例的部署。
- Once your instance is created, you’ll need to grab the Host name (FQDN) and your access key.
- for the Host name navigate to the resources “Overview” and copy “Host name”
- for your access key navigate to “Access Keys” under “Settings” and copy your key.
- Finally, we need to add our key and our host to a
redis.yaml
file that Dapr can apply to our cluster. If you’re running a sample, you’ll add the host and key to the providedredis.yaml
. If you’re creating a project from the ground up, you’ll create aredis.yaml
file as specified in Configuration. Set theredisHost
key to[HOST NAME FROM PREVIOUS STEP]:6379
and theredisPassword
key to the key you copied in step 4. Note: In a production-grade application, follow secret management instructions to securely manage your secrets.
NOTE: Dapr pub/sub uses Redis Streams that was introduced by Redis 5.0, which isn’t currently available on Azure Managed Redis Cache. Consequently, you can use Azure Managed Redis Cache only for state persistence.
Note
作为dapr init
命令的一部分,Dapr CLI会在自托管模式下自动部署本地redis实例。
相关链接
- Dapr组件的基本格式
- 阅读 本指南 以获取配置状态存储组件的说明
- 状态管理构建块