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

Posted dotNET跨平台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dapr牵手.NET学习笔记:状态管理进阶相关的知识,希望对你有一定的参考价值。

在上一篇文章中说到,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,访问不到数据

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

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,能访问到数据

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-}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,能访问到数据

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

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,能访问到数据

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

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

以上是关于Dapr牵手.NET学习笔记:状态管理进阶的主要内容,如果未能解决你的问题,请参考以下文章

Dapr牵手.NET学习笔记:状态管理之docker-compose发布

Dapr牵手.NET学习笔记:开篇

Dapr牵手.NET学习笔记:跨物理机负载均衡服务调用

Dapr牵手.NET学习笔记:Actor一个场景

Dapr牵手.NET学习笔记:想入非非的服务调用

Dapr牵手.NET学习笔记:绑定