在上一篇文章中說(shuō)到,dapr默認(rèn)的狀態(tài)是不可能跨appid的,也就是只能在相同的應(yīng)用內(nèi)訪問(wèn)自己設(shè)置的狀態(tài)數(shù)據(jù),dapr支持三種狀態(tài)的共享配置:appid,nam,none,是通過(guò)修改components下的statestore.yaml文件中的keyPrefix配置項(xiàng)來(lái)實(shí)現(xiàn)的。
說(shuō)明:本篇所有demo都是接上一篇的代碼和配置,不同部分會(huì)羅列出來(lái)。
appid模式
這種配置是默認(rèn)的,在上篇中有涉及到,這里為了明確比對(duì),所以列舉了出來(lái)。
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、通過(guò)order訪問(wèn)pay設(shè)置的key,訪問(wèn)不到數(shù)據(jù)
3、查看redis中的keys,以各服務(wù)的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、通過(guò)order訪問(wèn)pay設(shè)置的key,能訪問(wèn)到數(shù)據(jù)
3、查看redis中的keys,有狀態(tài)存儲(chǔ)的名稱作為前綴
多個(gè)name的情況該如何實(shí)現(xiàn)呢?下面是order服務(wù)和pay服務(wù)分別有自己的組件配置文件,文件夾分別是components_order,components_pay,狀態(tài)組件的名稱分別是statestore_order和statestore_pay,參見(jiàn)下面的配置文件。
1、docker-compose.yml
version: '3.4'services:#┌────────────────────────────────┐#│ ordersystem app + Dapr sidecar │#└────────────────────────────────┘o(jì)rdersystem: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、通過(guò)order訪問(wèn)pay設(shè)置的key,能訪問(wèn)到數(shù)據(jù)
5、查看redis中的keys,有狀態(tài)存儲(chǔ)的名稱作為前綴
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、通過(guò)order訪問(wèn)pay設(shè)置的key,能訪問(wèn)到數(shù)據(jù)
3、查看redis中的keys,沒(méi)有任何前綴
dapr通過(guò)簡(jiǎn)單的配置實(shí)現(xiàn)了三種狀態(tài)數(shù)據(jù)的共享機(jī)制,方便,簡(jiǎn)捷。所以在使用dapr前,就要把各服務(wù)的數(shù)據(jù)共用規(guī)劃好,方便配置使用。
總結(jié)
以上是生活随笔為你收集整理的Dapr牵手.NET学习笔记:状态管理进阶(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。