The documentation you are viewing is for Dapr v1.10 which is an older version of Dapr. For up-to-date documentation, see the latest version.

Redis

Redis 状态存储组件的详细信息

配置

要设置 Redis 状态储存,请创建一个类型为 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

TLS: 如果Redis 实例支持公开证书的TLS,它可以配置为启用或禁用 TLS truefals

Failover: When set to true enables the failover feature. The redisHost should be the sentinel host address. See Redis Sentinel Documentation

If you wish to use Redis as an actor store, append the following to the yaml.

  - name: actorStateStore
    value: "true"

元数据字段规范

字段 必填 详情 Example
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

  1. 安装 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
    
  2. 执行kubectl get pods来查看现在正在集群中运行的Redis容器。

  3. Add redis-master:6379 as the redisHost in your redis.yaml file. 例如:

        metadata:
        - name: redisHost
          value: redis-master:6379
    
  4. 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.

  1. Open this link to start the Azure Cache for Redis creation flow. 如有必要,请登录。
  2. Fill out necessary information and check the “Unblock port 6379” box, which will allow us to persist state without SSL.
  3. 点击“创建”来启动您的 Redis 实例的部署。
  4. 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.
  5. 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 provided redis.yaml. If you’re creating a project from the ground up, you’ll create a redis.yaml file as specified in Configuration. Set the redisHost key to [HOST NAME FROM PREVIOUS STEP]:6379 and the redisPassword 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.

相关链接