当前位置:网站首页>Dapr .NET Core示例
Dapr .NET Core示例
2022-06-09 15:41:00 【风神修罗使】
环境配置
单机本地环境
- 下载二进制版本
- 将压缩包中
dapr.exe文件解压到c:\dapr目录 - 将
c:\dapr目录放入到系统PATH环境变量 - 启动powershell,运行dapr命令,检查输出是否正常
- 运行
dapr init命令安装Dapr运行时(需以管理员方式启动终端)
PS C:\WINDOWS\system32> dapr init
Making the jump to hyperspace...
Downloading binaries and setting up components...
Success! Dapr is up and running
- 检查dapr是否运行正常,通过
docker ps命令
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
032a6f9fd0b0 daprio/dapr "./placement" 29 minutes ago Up 29 minutes 0.0.0.0:6050->50005/tcp dapr_placement
3c109a9886e2 redis "docker-entrypoint.s…" 30 minutes ago Up 30 minutes 0.0.0.0:6379->6379/tcp xenodochial_kapitsa
- 本地Dapr集成了一个
Redis,用于实现状态存储
注意: 你需要安装Docker for Windows并且将容器设置为Linux容器。
服务发现与调用示例(独立模式)
前置条件
- 已配置好本地开发环境
- Docker For Windows
- 安装了
.NET Core 3.0SDK
步骤(VS Code)
- 在代码目录创建一个
example目录,用于保存解决方案 - VS Code中打开
example目录,通过Ctrl+Shift+`新建一个终端 - 在终端中键入以下命令,创建解决方案
dotnet new sln
- 创建一个新项目,采webapi模板,名称为
dapr-example
dotnet new webapi -n dapr-example
- 将
invoke-service项目加入到解决方案
dotnet sln example.sln add ./dapr-example/dapr-example.csproj
- 生成的项目中,默认包含一个天气控制器
- 取消HTTPS自动跳转设置,在
Startup.cs,Configure方法中,注释掉app.UseHttpsRedirection(); - 演示Dapr的服务调用,在终端中切换到项目目录,然后使用dapr启动应用
cd invoke-service
dapr run --app-id dapr-example --app-port 5000 dotnet run
注意: 以上dapr run命令,通过app-id指定了应用的ID,通过app-port指定了应用的端口(webapi默认使用5000作为http端口),后跟dotnet run命名启动当前项目。可参考Dapr文档服务调用
测试,通过浏览器打开
http://localhost:5000/WeatherForecast连接(此时我们没有通过Dapr代理访问)下面,可以通过Dapr公开的代理地址来访问我们的服务,根据Dapr服务调用API规范,其代理调用规则为:
POST/GET/PUT/DELETE http://localhost:<Dapr端口>/v1.0/invoke/<id>/method/<method-name>
Dapr端口可从Dapr启动日志中获取,如以下日志表示Dapr公开的HTTP端口为14633(通过Dapr也可使用gRPC方式进行服务调用)
?[0m?[93;1m== DAPR == time="2019-10-29T11:18:34+08:00" level=info msg="http server is running on port 14633"
?[0m?[93;1m== DAPR == time="2019-10-29T11:18:34+08:00" level=info msg="gRPC server is running on port 14634"
故,我们可通过以下地址来调用示例方法:
GET http://localhost:14633/v1.0/invoke/dapr-example/method/WeatherForecast
注意: Dapr的服务调用是有dapr sidecar来实现的,在被调用的服务中无需注入任何与dapr相关的代码。
边栏推荐
- kubernetes 证书合集
- ps如何把字体导入
- Nuxt. JS how to deploy artalk and the problems encountered
- 数据存储需求多样化加剧,分而治之成大势所趋
- Practice of flask apscheduler stepping on pits
- 3ds max error prompt 1603, vc2005 installation failed, error troubleshooting
- 微信小程序使用canvas绘图,并保存下载到本地。圆形头像,虚线网络图片
- May training (day 31) - status compression
- 五月集训(第25天) —— 树状数组
- LeetCode 6077. 巫师的总力量和
猜你喜欢

How to solve the problem that Epson printer cannot print

ps怎么复制图层

MySQL advanced optimization 01 - 40000 word detailed database performance analysis tool (in-depth, comprehensive and detailed, for collection and standby)

618 大促来袭,浅谈如何做好大促备战

wps如何取消隐藏的单元格工作表

LeetCode 327. Number of interval sums

LeetCode 1420. 生成数组

WPS how to unhide cell worksheets

ps填充快捷键是什么

Medical device supply chain collaborative management system: commercial digital upgrade, data-driven supply chain efficient collaboration
随机推荐
五月集训(第31天) —— 状态压缩
餐饮行业SaaS租户多门店系统加速餐饮数字化运营,实现降本增效
70 DOM reading XML
R语言plotly可视化:使用plotly可视化模型在整个数据空间的分类轮廓线(等高线)、使用meshgrid创建一个网格,其中每个点之间的距离由mesh_size变量表示
3ds max error prompt 1603, vc2005 installation failed, error troubleshooting
Cordova 开发,提示net::ERR_CLEARTEXT_NOT_PERMITTED
defi存币生息理财dapp系统开发逻辑
数据安全刻不容缓,国产智能化厂商首获SOC 2鉴证报告有何意义?
Practice of flask apscheduler stepping on pits
数据存储需求多样化加剧,分而治之成大势所趋
五月集训(第27天) —— 图
May training (day 23) - dictionary tree
R语言使用aov函数进行双因素方差分析(Two-way factorial ANOVA)、gplots包的plotmeans函数在双因素方差分析中显示交互作用、包括均值,误差条、95%置信区间、样本量
May training (day 24) - segment tree
Easyexcel merge cells
May training (day 31) - status compression
纸业供应链协同管理系统:全链路数字化,实现供应链平台订单智能管控
The aquatic industry digital B2B e-commerce platform realizes fine management and drives enterprise performance growth
技术自媒体变现心得分享 —— 开始尝试认真做 CSDN 的一年后的复盘
70 DOM读取XML