在上一篇文章中说到,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.redisversion: v1metadata:- name: redisHostvalue: redis:6379- name: redisPasswordvalue: ""- name: actorStateStorevalue: "true"- name: keyPrefixvalue: "appid"
2、通过order访问pay设置的key,访问不到数据
3、查看redis中的keys,以各服务的appid作为前缀
name模式
1、statestore.yaml
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:name: statestore
spec:type: state.redisversion: v1metadata:- name: redisHostvalue: redis:6379- name: redisPasswordvalue: ""- name: actorStateStorevalue: "true"- name: keyPrefixvalue: "name"
2、通过order访问pay设置的key,能访问到数据
3、查看redis中的keys,有状态存储的名称作为前缀
多个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-}ordersystemdepends_on:- redis- placementbuild:context: ../dockerfile: /OrderSystem/Dockerfileports:- "3500:3500"volumes: - ../OrderSystem:/OrderSystem networks:- b2c-daprordersystem-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:- ordersystemnetwork_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-}paymentsystemdepends_on:- redis- placementbuild:context: ../dockerfile: /PaymentSystem/Dockerfileports:- "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:- paymentsystem1network_mode: "service:paymentsystem1"volumes: - ../components_pay:/components #┌───────────────────────────────────┐#│ paymentsystem2 app + Dapr sidecar │#└───────────────────────────────────┘ paymentsystem2:image: ${DOCKER_REGISTRY-}paymentsystemdepends_on:- redis- placement build:context: ../dockerfile: /PaymentSystem/Dockerfilevolumes: - ../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:- paymentsystem2network_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.redisversion: v1metadata:- name: redisHostvalue: redis:6379- name: redisPasswordvalue: ""- name: actorStateStorevalue: "true"- name: keyPrefixvalue: "name"
3、components_pay文件夹下的statstore.yaml
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:name: statestore_pay
spec:type: state.redisversion: v1metadata:- name: redisHostvalue: redis:6379- name: redisPasswordvalue: ""- name: actorStateStorevalue: "true"- name: keyPrefixvalue: "name"
4、通过order访问pay设置的key,能访问到数据
5、查看redis中的keys,有状态存储的名称作为前缀
none模式
1、statestore.yaml
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:name: statestore
spec:type: state.redisversion: v1metadata:- name: redisHostvalue: redis:6379- name: redisPasswordvalue: ""- name: actorStateStorevalue: "true"- name: keyPrefixvalue: "none"
2、通过order访问pay设置的key,能访问到数据
3、查看redis中的keys,没有任何前缀
dapr通过简单的配置实现了三种状态数据的共享机制,方便,简捷。所以在使用dapr前,就要把各服务的数据共用规划好,方便配置使用。