微软文档:https://docs.microsoft.com/zh-cn/dotnet/architecture/dapr-for-net-developers/getting-started
官方文档:https://docs.dapr.io/zh-hans/getting-started/install-dapr-cli/
==================
接下来用CentOS7演示
==================
一、安装 Dapr CLI
1、进入官网快速入门,找到压缩包来安装(前面的命令我没成功过-.-//)
2、在github中找到最新稳定版,展开“Assets”,下载名为“dapr_linux_amd64.tar.gz”的压缩包
3、打开CentOS系统,并安装Docker环境,参考安装:https://www.cnblogs.com/shousiji/p/14918631.html
4、可以通过FTP工具,将dapr压缩包上传到CentOS的“/usr/local/bin”目录中,并且cd到这个目录,再执行解压命令
(先解压再上传,和先上传再解压,会导致这个文件的权限不一样,前者就可能会出现“权限不足的情况”)
tar -zvxf dapr_linux_amd64.tar.gz
5、验证是否成功,执行命令
dapr
出现如图表示成功
二、本地初始化 Dapr
1、运行init CLI 命令
dapr init
#或者安装指定版本
dapr init --runtime-version 1.4.0
安装需要几分钟,如果这里一直在打转,取消后再搭个梯子重新来吧
2、验证Dapr 版本
dapr --version
像这样:
3、验证容器
docker ps
默认会装上这几个镜像:
三、安装.NET5 SDK
1、回到根目录
cd ~
2、将 Microsoft 包签名密钥添加到受信任密钥列表
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
3、安装SDK
sudo yum install dotnet-sdk-5.0
4、验证SDK
dotnet --info
四、.NET5程序
1、创建一个.NET5 Web项目,命名为“SGZ.DepartmentService”,安装Nuget包“Dapr.AspNetCore”
2、创建控制器如下
[Route("[controller]/[action]")] [ApiController] public class DepartmentController : ControllerBase { private static readonly List<DepartmentDto> _departments = new() { new DepartmentDto{ ID = "D001", Name = "部门1" }, new DepartmentDto{ ID = "D002", Name = "部门2" }, new DepartmentDto{ ID = "D003", Name = "部门3" }, new DepartmentDto{ ID = "D004", Name = "部门4" }, new DepartmentDto{ ID = "D005", Name = "部门5" } }; [HttpGet] public IActionResult GetList() { return Ok(_departments); } }
3、服务注册到Dapr,需Startup类加入
4、发布站点到CentOS的“/usr/web/DepartmentService”目录,并且cd到这个目录,执行命令
dapr run --app-id DepartmentService --app-port 680 --dapr-http-port 681 -- dotnet SGZ.DepartmentService.dll --urls "http://*:680"
出现如图即为成功
5、新开一个窗口,进行服务访问
格式: curl http://localhost:<dapr-port>/v1.0/invoke/<application-id>/method/<method-name>
示例:
curl http://localhost:681/v1.0/invoke/DepartmentService/method/Department/GetList
成功返回结果
五、服务间的访问
1、整体结构
2、部门服务不变,就步骤四那样;这里设计人员服务访问部门服务,故控制器如下
[Route("[controller]/[action]")] [ApiController] public class PersonnelController : ControllerBase { private readonly Dapr.Client.DaprClient _daprClient; private static readonly List<PersonnelDto> _personnels = new() { new PersonnelDto{ ID = "S001", Name = "人员1" }, new PersonnelDto{ ID = "S002", Name = "人员2" }, new PersonnelDto{ ID = "S003", Name = "人员3" } }; public PersonnelController(Dapr.Client.DaprClient daprClient) { _daprClient = daprClient; } [HttpGet] public IActionResult GetList() { //var client = new Dapr.Client.DaprClientBuilder().Build(); //参数二:要访问的服务(步骤四第4点,命令中配置的app-id名称) //参数三:要访问的action地址,根据你配置的路由格式来填写 var dlist = _daprClient .InvokeMethodAsync<List<DepartmentDto>>(HttpMethod.Get, "DepartmentService", "Department/GetList"); object obj = new { DepartmentList = dlist, PersonnelList = _personnels }; return Ok(obj); } }
3、服务注册到Dapr,需Startup类加入
4、新开一个窗口连接到CentOS,将部门服务发布到“/usr/web/DepartmentService”目录,并cd到这个目录,执行命令
dapr run --app-id DepartmentService --app-port 680 --dapr-http-port 681 -- dotnet SGZ.DepartmentService.dll --urls "http://*:680"
5、新开一个窗口连接到CentOS,将人员服务发布到“/usr/web/PersonnelService”目录,并cd到这个目录,执行命令
dapr run --app-id PersonnelService --app-port 690 --dapr-http-port 691 -- dotnet SGZ.PersonnelService.dll --urls "http://*:690"
6、新开一个窗口连接到CentOS,访问人员服务
curl http://localhost:691/v1.0/invoke/PersonnelService/method/Personnel/GetList
可以看到,连同部门信息也查询到了
六、发布订阅
默认使用的组件是Redis,可以改为其它组件,像Kafka、RabbitMQ等等,感兴趣的去查看文档,这里就不在叙述了
1、人员服务添加函数和特性,并部署到“/usr/web/PersonnelService”目录
[Route("[controller]/[action]")] [ApiController] public class PersonnelController : ControllerBase { private readonly Dapr.Client.DaprClient _daprClient; private static readonly List<PersonnelDto> _personnels = new() { new PersonnelDto{ ID = "S001", Name = "人员1" }, new PersonnelDto{ ID = "S002", Name = "人员2" }, new PersonnelDto{ ID = "S003", Name = "人员3" } }; public PersonnelController(Dapr.Client.DaprClient daprClient) { _daprClient = daprClient; } //参数一取值固定pubsub,参数二自定义 [Topic("pubsub", "PersonnelAdd")] [HttpPost] public IActionResult Add(PersonnelDto dto) { Console.WriteLine("进行了人员添加操作"); _personnels.Add(dto); return Ok(dto); } [HttpGet] public IActionResult GetList() { return Ok(_personnels); } }
2、部门服务如法炮制,并部署到“/usr/web/DepartmentService”目录
[Route("[controller]/[action]")] [ApiController] public class DepartmentController : ControllerBase { private static readonly List<DepartmentDto> _departments = new() { new DepartmentDto { ID = "D001", Name = "部门1" }, new DepartmentDto { ID = "D002", Name = "部门2" }, new DepartmentDto { ID = "D003", Name = "部门3" }, new DepartmentDto { ID = "D004", Name = "部门4" }, new DepartmentDto { ID = "D005", Name = "部门5" } }; [Topic("pubsub", "DepartmentAdd")] [HttpPost] public IActionResult Add(DepartmentDto dto) { Console.WriteLine("进行了部门添加操作"); Console.WriteLine($"ID:{dto.ID};Name:{dto.Name};aaa:{dto.aaa};BBB:{dto.BBB};"); _departments.Add(dto); return Ok(dto); } [HttpGet] public IActionResult GetList() { return Ok(_departments); } }
3、部门服务和人员服务是订阅者,需要各自加入:endpoints.MapSubscribeHandler();
4、创建一个聚合服务Web项目,命令为“SGZ.AggregationService”,需要注册到Dapr,Startup类加入
5、添加控制器,完后部署到“/usr/web/AggregationService”目录
[Route("[controller]/[action]")] [ApiController] public class AggregationController : ControllerBase { private readonly Dapr.Client.DaprClient _daprClient; public AggregationController(Dapr.Client.DaprClient daprClient) { _daprClient = daprClient; } [HttpPost] public async Task<IActionResult> Add() { var dDto = new DepartmentDto { ID = Guid.NewGuid().ToString(), Name = "baoanke", aaa = "aaa", BBB = "bbb" }; await _daprClient.PublishEventAsync("pubsub", "DepartmentAdd", dDto); var pDto = new PersonnelDto { ID = Guid.NewGuid().ToString(), Name = "zhangsan" }; await _daprClient.PublishEventAsync("pubsub", "PersonnelAdd", pDto); return Content("OK"); } [HttpGet] public async Task<IActionResult> GetList() { //var client = new Dapr.Client.DaprClientBuilder().Build(); //参数二:要访问的服务(步骤四第4点,命令中配置的app-id名称) //参数三:要访问的action地址,根据你配置的路由格式来填写 var dlist = await _daprClient .InvokeMethodAsync<List<DepartmentDto>>(HttpMethod.Get, "DepartmentService", "Department/GetList"); var plist = await _daprClient .InvokeMethodAsync<List<PersonnelDto>>(HttpMethod.Get, "PersonnelService", "Personnel/GetList"); object obj = new { DepartmentList = dlist, PersonnelList = plist }; return Ok(obj); } }
6、开启3个窗口,cd到各个服务的目录下,依次执行命令
#运行部门服务 dapr run --app-id DepartmentService --app-port 680 --dapr-http-port 681 -- dotnet SGZ.DepartmentService.dll --urls "http://*:680" #运行人员服务 dapr run --app-id PersonnelService --app-port 690 --dapr-http-port 691 -- dotnet SGZ.PersonnelService.dll --urls "http://*:690" #运行聚合服务 dapr run --app-id AggregationService --app-port 670 --dapr-http-port 671 -- dotnet SGZ.AggregationService.dll --urls "http://*:670"
7、打开postman,访问聚合服务的添加接口:http://192.168.134.130:670/Aggregation/Add,并成功返回OK信息(端口需要防火墙开启)
部门服务和人员服务窗口也打印出了相关信息
8、访问聚合服务的列表接口:http://192.168.134.130:670/Aggregation/GetList,可以看到返回的结果有新的数据
不过奇怪的是只有id添加成功,其它字段都不行!
控制台打印也确实只有ID有值。后面试了多种方式都不行,等下个版本再看看了,本文Dapr版本是1.4.0
本文发布订阅版代码:https://files.cnblogs.com/files/shousiji/net5_dapr.rar