1、安装 dapr

一、linux 离线安装

  1. 安装 docker [官网]

    # 1、Run the following command to uninstall all conflicting packages:
    $ for pkg in docker.io docker-doc docker-compose \
      docker-compose-v2 podman-docker containerd runc; \
      do sudo apt-get remove $pkg; done
    
    # 2、Set up Docker's apt repository.
    # Add Docker's official GPG key:
    $ sudo apt-get update
    $ sudo apt-get install ca-certificates curl
    $ sudo install -m 0755 -d /etc/apt/keyrings
    $ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
    $ sudo chmod a+r /etc/apt/keyrings/docker.asc
    
    # 3、Add the repository to Apt sources:
    $ echo \
     "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \
     https://download.docker.com/linux/ubuntu \
     $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
     sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    
    # apt update
    $ sudo apt-get update
    
    # 4、Install the Docker packages.
    $ sudo apt-get install docker-ce docker-ce-cli \
      containerd.io docker-buildx-plugin docker-compose-plugin
    
    # 5、run docker
    $ sudo service docker start
    
  2. 安装 dapr

    1、下载 dapr 离线包 [官网]

    # 切换root用户
    $ sudo -i
    
    # 转到根目录
    $ cd /root/
    
    # 下载离线包
    $ wget https://github.com/dapr/installer-bundle/releases/download/v1.13.0/daprbundle_linux_amd64.tar.gz
    

    2、解压

    $ tar -zvxf daprbundle_linux_amd64.tar.gz
    

    3、拷贝 dapr 至 /usr/local/bin

    # 转到解压文件夹
    $ cd daprbundle
    
    # 复制 dapr 文件
    $ cp /root/daprbundle/dapr /usr/local/bin
    

    3、初始化 dapr

    $ dapr init --from-dir .
    

    4、运行 dapr_zipkin 容器

    $ docker run --name "dapr_zipkin" --restart always -d -p 9411:9411 openzipkin/zipkin
    

    5、运行 dapr_redis 容器

    $ docker run --name "dapr_redis" --restart always -d -p 6379:6379 redislabs/rejson
    

    6、初始化 slim(可省略)

    $ dapr init --slim --from-dir .
    
  3. 运行 dapr sidecar [官网]

    # 运行 sidecar
    $ dapr run --app-id myapp --dapr-http-port 3500 --log-level debug
    
    # 设置缓存
    $ curl -X POST -H "Content-Type: application/json" -d '[{ "key": "name", "value": "Bruce Wayne"}]' http://localhost:3500/v1.0/state/statestore -v
    
    # 获取缓存
    $curl http://localhost:3500/v1.0/state/statestore/name 
    
  4. 卸载 dapr

    # 卸载 dapr
    $ dapr uninstall
    
    # 删除容器
    $ docker rm -f dapr_redis dapr_zipkin
    
    $ 删除 dapr 文件
    $ rm /usr/local/bin/dapr
    
    $ 删除文件夹
    $ rm -rf /root/daprbundle daprbundle_linux_amd64.tar.gz
    

二、错误&解决

  1. 缓存设置
    $ curl -X POST -H "Content-Type: application/json" -d '[{ "key": "name", "value": "Bruce Wayne"}]' http://localhost:3500/v1.0/state/statestore -v
    

    1、错误日志 myapp

    DEBU[0003] api error: code = FailedPrecondition desc = state store statestore is not configured  app_id=myapp instance=root scope=dapr.runtime.http type=log ver=1.13.0
    

    2、错误响应

    {"errorCode":"ERR_STATE_STORE_NOT_CONFIGURED","message":"state store statestore is not configured","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","domain":"dapr.io","metadata":{"appID":"myapp"},"reason":"DAPR_STATE_NOT_CONFIGURED"}]}
    

    3、解决 [参考]

    # 创建statestore配置
    $ touch /root/.dapr/components/statestore.yaml
    
    # 打开配置
    $ vi /root/.dapr/components/statestore.yaml
    
    # 写入配置[参考](https://docs.dapr.io/getting-started/quickstarts/statemanagement-quickstart/#step-3-view-the-order-processor-outputs)
    apiVersion: dapr.io/v1alpha1
    kind: Component
    metadata:
      name: statestore
    spec:
      type: state.redis
      version: v1
      metadata:
      - name: redisHost
        value: localhost:6379
      - name: redisPassword
        value: ""
      - name: actorStateStore
        value: "true"
    
  2. 待续

三、参考

  1. [官网]
posted @ 2024-03-16 16:57  1764564459  阅读(126)  评论(0编辑  收藏  举报