You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.3 KiB
3.3 KiB
Actors | Dapr.Client 文档
LINGYUN.Abp.Dapr.Client
实现了Dapr文档中的服务间调用,项目设计与Volo.Abp.Http.Client一致,通过配置文件即可无缝替代Volo.Abp.Http.Client
配置使用
模块按需引用
[DependsOn(typeof(AbpDaprClientModule))]
public class YouProjectModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
// 注册代理类似于 Volo.Abp.Http.Client 模块
context.Services.AddDaprClientProxies(
typeof(YouProjectInterfaceModule).Assembly, // 搜索 YouProjectInterfaceModule 模块下的远程服务定义
RemoteServiceName
);
}
}
1、接口定义
// IApplicationService 实现了 IRemoteService
public interface ISystemAppService : IApplicationService
{
Task<string> GetAsync();
}
public class SystemInterfaceModule : AbpModule
{
}
2、服务端
引用 Volo.Abp.AspNetCore.Mvc
- 实现接口
public class SystemAppService : ApplicationService, ISystemAppService
{
public Task<string> GetAsync()
{
retuen Task.FromResult("System");
}
}
- 创建模块
[DependsOn(
typeof(SystemInterfaceModule),
typeof(AbpAspNetCoreMvcModule))]
public class SystemServerModule : AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
PreConfigure<IMvcBuilder>(mvcBuilder =>
{
mvcBuilder.AddApplicationPartIfNotExists(typeof(SystemServerModule).Assembly);
});
}
}
- 发布到Dapr
# --app-port .net程序映射端口
# -H 对外暴露 http 监听端口
# -G 对外暴露 grpc 监听端口
dapr run --app-id myapp --app-port 5000 -H 50000 -G 40001 -- dotnet run
3、客户端
引用 LINGYUN.Abp.Dapr.Client
- 配置文件 appsettings.json
{
"RemoteServices": {
"System": {
"AppId": "myapp",
"BaseUrl": "http://127.0.0.1:50000"
}
}
}
- 客户端代码
// 模块依赖
[DependsOn(
typeof(AbpDaprClientModule))]
public class SystemActorClientModule : AbpModule
{
private const string RemoteServiceName = "System";
public override void ConfigureServices(ServiceConfigurationContext context)
{
// 注册代理类似于 Volo.Abp.Http.Client 模块
context.Services.AddDaprClientProxies(
typeof(SystemInterfaceModule).Assembly, // 搜索 SystemInterfaceModule 模块下的IRemoteService定义创建代理
RemoteServiceName
);
}
}
// 调用方法,直接依赖注入即可
public class InvokeClass
{
private readonly ISystemAppService _systemAppService;
public InvokeClass(ISystemAppService systemAppService)
{
_systemAppService = systemAppService;
}
public async Task InvokeAsync()
{
await _systemAppService.GetAsync();
}
}
配置项说明
- AbpRemoteServiceOptions.RemoteServices 配置Dapr.AppId
{
"RemoteServices": {
"System": {
"AppId": "myapp",
"BaserUrl": "http://127.0.0.1:50000"
}
}
}