在上一篇文章中说到,dapr默认的状态是不可能跨appid的,也就是只能在相同的应用内访问自己设置的状态数据,dapr支持三种状态的共享配置:appid,nam,none,是通过修改components下的statestore.yaml文件中的keyPrefix配置项来实现的。

说明:本篇所有demo都是接上一篇的代码和配置,不同部分会罗列出来。

appid模式

  这种配置是默认的,在上篇中有涉及到,这里为了明确比对,所以列举了出来。

1、statestore.yaml

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: redis:6379
- name: redisPassword
value: ""
- name: actorStateStore
value: "true"
- name: keyPrefix
value: "appid"

2、通过order访问pay设置的key,访问不到数据

Dapr牵手.NET学习笔记:状态管理进阶(一)_redis


 3、查看redis中的keys,以各服务的appid作为前缀

Dapr牵手.NET学习笔记:状态管理进阶(一)_数据_02

name模式

1、statestore.yaml

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: redis:6379
- name: redisPassword
value: ""
- name: actorStateStore
value: "true"
- name: keyPrefix
value: "name"

2、通过order访问pay设置的key,能访问到数据

Dapr牵手.NET学习笔记:状态管理进阶(一)_redis_03

 3、查看redis中的keys,有状态存储的名称作为前缀

Dapr牵手.NET学习笔记:状态管理进阶(一)_数据_04

多个name的情况该如何实现呢?下面是order服务和pay服务分别有自己的组件配置文件,文件夹分别是components_order,components_pay,状态组件的名称分别是statestore_order和statestore_pay,参见下面的配置文件。

1、docker-compose.yml

version: '3.4'

services:
#┌────────────────────────────────┐
#│ ordersystem app + Dapr sidecar │
#└────────────────────────────────┘
ordersystem:
image: ${DOCKER_REGISTRY-}ordersystem
depends_on:
- redis
- placement
build:
context: ../
dockerfile: /OrderSystem/Dockerfile
ports:
- "3500:3500"
volumes:
- ../OrderSystem:/OrderSystem
networks:
- b2c-dapr
ordersystem-dapr:
image: "daprio/daprd:latest"
command: [ "./daprd", "-app-id", "order", "-app-port", "80","-placement-host-address", "placement:50006","-components-path","/components"]
build:
context: ../
depends_on:
- ordersystem
network_mode: "service:ordersystem"
volumes:
- ../components_order:/components


#┌─────────────────────────┐
#│ Dapr placement1 service │
#└─────────────────────────┘
placement:
image: "daprio/dapr"
command: ["./placement", "-port", "50006"]
ports:
- "50006:50006"
networks:
- b2c-dapr

#┌────────────────────┐
#│ Redis1 state store │
#└────────────────────┘
redis:
image: "redis:latest"
ports:
- "6380:6379"
networks:
- b2c-dapr

###################################################################################################################

#┌───────────────────────────────────┐
#│ paymentsystem1 app + Dapr sidecar │
#└───────────────────────────────────┘
paymentsystem1:
image: ${DOCKER_REGISTRY-}paymentsystem
depends_on:
- redis
- placement
build:
context: ../
dockerfile: /PaymentSystem/Dockerfile
ports:
- "3601:3500"
volumes:
- ../PaymentSystem:/PaymentSystem
networks:
- b2c-dapr
paymentsystem1-dapr:
image: "daprio/daprd:latest"
command: [ "./daprd", "-app-id", "pay", "-app-port", "80","-placement-host-address", "placement:50006","-components-path","/components" ]
build:
context: ../
depends_on:
- paymentsystem1
network_mode: "service:paymentsystem1"
volumes:
- ../components_pay:/components

#┌───────────────────────────────────┐
#│ paymentsystem2 app + Dapr sidecar │
#└───────────────────────────────────┘
paymentsystem2:
image: ${DOCKER_REGISTRY-}paymentsystem
depends_on:
- redis
- placement
build:
context: ../
dockerfile: /PaymentSystem/Dockerfile
volumes:
- ../PaymentSystem:/PaymentSystem
ports:
- "3602:3500"
networks:
- b2c-dapr
paymentsystem2-dapr:
image: "daprio/daprd:latest"
command: [ "./daprd", "-app-id", "pay", "-app-port", "80" ,"-placement-host-address", "placement:50006","-components-path","/components"]
build:
context: ../
depends_on:
- paymentsystem2
network_mode: "service:paymentsystem2"
volumes:
- ../components_pay:/components

networks:
b2c-dapr:

2、components_order文件夹下的statstore.yaml

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore_order
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: redis:6379
- name: redisPassword
value: ""
- name: actorStateStore
value: "true"
- name: keyPrefix
value: "name"

3、components_pay文件夹下的statstore.yaml

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore_pay
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: redis:6379
- name: redisPassword
value: ""
- name: actorStateStore
value: "true"
- name: keyPrefix
value: "name"

4、通过order访问pay设置的key,能访问到数据

Dapr牵手.NET学习笔记:状态管理进阶(一)_docker_05

5、查看redis中的keys,有状态存储的名称作为前缀

Dapr牵手.NET学习笔记:状态管理进阶(一)_docker_06

none模式

1、statestore.yaml

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: redis:6379
- name: redisPassword
value: ""
- name: actorStateStore
value: "true"
- name: keyPrefix
value: "none"

2、通过order访问pay设置的key,能访问到数据

Dapr牵手.NET学习笔记:状态管理进阶(一)_redis_07

3、查看redis中的keys,没有任何前缀

Dapr牵手.NET学习笔记:状态管理进阶(一)_redis_08

   dapr通过简单的配置实现了三种状态数据的共享机制,方便,简捷。所以在使用dapr前,就要把各服务的数据共用规划好,方便配置使用。

  ​想要更快更方便的了解相关知识,可以关注微信公众号 

Dapr牵手.NET学习笔记:状态管理进阶(一)_数据_09