Dapr

  • Dapr使用方式

独立模式、Kubernetes、SDK

  • 本地机器安装Dapr
1.下载地址: https://oreil.ly/TeRug
2. 示例代码库: https://oreil.ly/4_LKA
  • 参考文章

1.https://zhuanlan.zhihu.com/p/415785657
2.Dapr文档: https://docs.dapr.io/zh-hans/

  • 执行命令

1.dapr init


image.png

2.运行dapr sidecar

dapr run --app-id myapp --dapr-http-port 3500

3.保存状态

curl -X POST -H "Content-Type: application/json" -d '[{ "key": "name", "value": "wudy.yu"}]' http://localhost:3500/v1.0/state/statestore
image.png

4.获取状态

wudy.yu@promote ~ % curl http://localhost:3500/v1.0/state/statestore/name
"wudy.yu"%   

查看状态如何在redis中存储

wudy.yu@promote ~ % docker exec -it dapr_redis redis-cli
127.0.0.1:6379> keys *
1) "myapp||name"
127.0.0.1:6379> hgetall myapp||name
1) "data"
2) "\"wudy.yu\""
3) "version"
4) "1"
127.0.0.1:6379> 

  • 快速定义一个组件

1.创建一个 JSON 密钥存储

{
   "my-secret" : "I'm Peter.li"
}

2.创建一个密钥存储Dapr 组件

// 在目录: /Users/wudy.yu/go/www/my_project/src/mytest.com 下面创建一个localSecretStore.yaml

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: my-secret-store
  namespace: default
spec:
  type: secretstores.local.file
  version: v1
  metadata:
  - name: secretsFile
    value: /Users/wudy.yu/go/www/my_project/src/mytest.com/mysecrets.json
  - name: nestedSeparator
    value: ":"

3.运行Dapr sidecar

dapr run --app-id myapp --dapr-http-port 3500  --components-path   /Users/wudy.yu/go/www/my_project/src/mytest.com

4.获取一个密钥

curl http://localhost:3500/v1.0/secrets/my-secret-store/my-secret
image.png

dapr 常见命令

dapr status -k   //  查看dapr状态
  • Pub/Sub

参考文档: https://docs.dapr.io/getting-started/quickstarts/pubsub-quickstart/

step1:安装python(3.10.5)

step2: 下载dapr官方demo代码 
git clone https://github.com/dapr/quickstarts.git


image.png
// subscribe to  topic
cd /Users/wudy.yu/cloud/demo/quickstarts/pub_sub/python/sdk/order-processor

pip3 install -r requirements.txt

dapr run --app-id order-processor --components-path  /Users/wudy.yu/cloud/demo/quickstarts/pub_sub/components  --app-port 5001 -- python3 app.py
// publish a topic
cd /Users/wudy.yu/cloud/demo/quickstarts/pub_sub/python/sdk/checkout

pip3 install -r requirements.txt

dapr  run --app-id checkout --components-path  /Users/wudy.yu/cloud/demo/quickstarts/pub_sub/components  -- python3 app.py

Pub/Sub使用kafka组件

  • 1.要设置Apache Kafka pubsub,请创建一个pubsub.kafka类型的组件
// pubsub.yaml (默认路径:/Users/wudy.yu/cloud/dapr)
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: kafka-pubsub
  namespace: default
spec:
  type: pubsub.kafka
  version: v1
  metadata:
      # Kafka broker connection setting
    - name: brokers
      value: "dapr-kafka.myapp.svc.cluster.local:9092"
    - name: authRequired
      value: "true"
    - name: saslUsername
      value: "adminuser"
    - name: saslPassword
      value: "KeFg23!"
    - name: maxMessageBytes
      value: 1024

你可能感兴趣的:(Dapr)