Dapr(二) 分布式应用运行时搭建及服务调用

本文详细介绍了如何在Linux环境下搭建Dapr单机环境,包括Dapr的安装、服务调用的实现,以及服务A和B的创建与启动。通过Dapr的RPC调用,实现了服务间的通信,最后探讨了服务调用的基本原理。
摘要由CSDN通过智能技术生成

结合上一期 Dapr(一) 基于云原生了解Dapr(Dapr(一) 基于云原生了解Dapr-CSDN博客)

目录

1. Dapr单机环境部署应用

1.1 ping github.com

1.2 安装dapr环境

1.1.3 创建一个dapr文件夹

1.1.4 拷贝脚本

1.1.5 运行脚本

1.1.6 检查是否启动成功

2.0 Dapr的服务调用

2.1 Dapr的RPC调用

2.2  Nuget引入Dapr.AspNetCore

2.3 Dapr_ServiceA Program.cs修改

2.4 Dapr_ServiceA ServiceAController.cs

2.5 Dapr_ServiceB ServiceBController.cs

3.0 启动服务

3.1 Dapr_ServiceA和Dapr_ServiceB复制粘贴至Linux系统下的dapr文件夹下。

3.2 启动Dapr_ServiceA

3.3 启动Dapr_ServiceB

3.4 检查服务是否都启动成功

4.0 测试运行

4.1 浏览器输入 192.168.157.157/2000/swagger/index.html     192.168.157.157/3000/swagger/index.html

4.2 执行Api进行服务与服务间的调用

5.0 服务调用原理分析


1. Dapr单机环境部署应用

1.1 ping github.com

Dapr单机版需要从github进行下载,我们只能用魔法才能打败魔法。我们开启魔法之后,在Linux系统中使用 ping github.com

Linux虚拟机已经成功 ping 通 github.com。

1.2 安装dapr环境

下载shell脚本 https://raw.githubusercontent.com/dapr/cli/master/install/install.sh

#!/usr/bin/env bash

# ------------------------------------------------------------
# Copyright 2021 The Dapr Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------

# Dapr CLI location
: ${DAPR_INSTALL_DIR:="/usr/local/bin"}

# sudo is required to copy binary to DAPR_INSTALL_DIR for linux
: ${USE_SUDO:="false"}

# Http request CLI
DAPR_HTTP_REQUEST_CLI=curl

# GitHub Organization and repo name to download release
GITHUB_ORG=dapr
GITHUB_REPO=cli

# Dapr CLI filename
DAPR_CLI_FILENAME=dapr

DAPR_CLI_FILE="${DAPR_INSTALL_DIR}/${DAPR_CLI_FILENAME}"

getSystemInfo() {
    ARCH=$(uname -m)
    case $ARCH in
        armv7*) ARCH="arm";;
        aarch64) ARCH="arm64";;
        x86_64) ARCH="amd64";;
    esac

    OS=$(echo `uname`|tr '[:upper:]' '[:lower:]')

    # Most linux distro needs root permission to copy the file to /usr/local/bin
    if [[ "$OS" == "linux" || "$OS" == "darwin" ]] && [ "$DAPR_INSTALL_DIR" == "/usr/local/bin" ]; then
        USE_SUDO="true"
    fi
}

verifySupported() {
    releaseTag=$1
    local supported=(darwin-amd64 linux-amd64 linux-arm linux-arm64)
    local current_osarch="${OS}-${ARCH}"

    for osarch in "${supported[@]}"; do
        if [ "$osarch" == "$current_osarch" ]; then
            echo "Your system is ${OS}_${ARCH}"
            return
        fi
    done

    if [ "$current_osarch" == "darwin-arm64" ]; then
        if isReleaseAvailable $releaseTag; then
            return
        else
            echo "The darwin_arm64 arch has no native binary for this version of Dapr, however you can use the amd64 version so long as you have rosetta installed"
            echo "Use 'softwareupdate --install-rosetta' to install rosetta if you don't already have it"
            ARCH="amd64"
            return
        fi
    fi

    echo "No prebuilt binary for ${current_osarch}"
    exit 1
}

runAsRoot() {
    local CMD="$*"

    if [ $EUID -ne 0 -a $USE_SUDO = "true" ]; then
        CMD="sudo $CMD"
    fi

    $CMD || {
        echo "Please visit https://docs.dapr.io/getting-started/install-dapr-cli/ for instructions on how to install without sudo."
        exit 1
    }
}

checkHttpRequestCLI() {
    if type "curl" > /dev/null; then
        DAPR_HTTP_REQUEST_CLI=curl
    elif type "wget" > /dev/null; then
        DAPR_HTTP_REQUEST_CLI=wget
    else
        echo "Either curl or wget is required"
        exit 1
    fi
}

checkExistingDapr() {
    if [ -f "$DAPR_CLI_FILE" ]; then
        echo -e "\nDapr CLI is detected:"
        $DAPR_CLI_FILE --version
        echo -e "Reinstalling Dapr CLI - ${DAPR_CLI_FILE}...\n"
    else
        echo -e "Installing Dapr CLI...\n"
    fi
}

getLatestRelease() {
    local daprReleaseUrl="https://ap
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值