如何做微服务异步调用? #
我们通过微服务异步调用有2种方式:
- 通过nuget引用SDK方式微服务调用
- 通过nacos微服务异步调用对方提供的接口
接下来我们分别讲解在项目中如何通过2种方式进行使用。
一、通过nuget引用SDK方式微服务调用 #
如下图,引用对方SDK

在program服务中注入第三方微服务
//注册System服务请求,微服务调用,示范例子
//builder.Services.AddSettingsApiService(builder.Configuration);
//模拟用,实际项目不用自己项目注入自己的微服务
builder.Services.AddDemoApiService(builder.Configuration);
项目即可直接注入使用
 private readonly IExchangeRateService _exchangeRateService;
        /// <summary>
        /// 构造
        /// </summary>
        public SyncExchangeRateHandler()
        {
            _exchangeRateService = App.GetService<IExchangeRateService>();
        }
二、通过nacos微服务异步调用对方提供的接口 #
首先定义IExchangeRateApi异步调用接口。
如下面订单查询接口,默认从nacos中读取相关接口,无需指定服务器地址,其中IExchangeRateApi定义在nacos中。
using Mall3s.Oms.Entitys.Dto.ExchangeRate;
using Mall3s.Nacos.Attributes;
using WebApiClient;
using WebApiClient.Attributes;
namespace Mall3s.Oms.SDK.WebApi
{
    /// <summary>
    /// 汇率接口
    /// </summary>
    [ApiName("DemoApi")]    //ApiName值为空或者不写ApiName属性,api名称默认类名,此处为IExchangeRateApi
    [TraceFilter(OutputTarget = OutputTarget.Debug)]    //debug窗口日志
    public interface IExchangeRateApi : IHttpApi
    {
        /// <summary>
        /// 分页获取汇率列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("api/Demo/BaseData/ExchangeRate")]
        ITask<dynamic> GetList(ExchangeRateListQueryInput input);
    }
}
- 定义微服务注入类 - using Mall3s.Dependency; using Mall3s.Nacos.Extensions; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System.Reflection; namespace Mall3s.Oms.SDK.Extensions { /// <summary> /// 扩展包 /// </summary> [SuppressSniffer] public static class DemoApiServiceExtensions { /// <summary> /// 该项目不允许引用不存在于nuget的包 /// Demo对外api扩展,默认读取配置 WebApiConfig(配置文件放在nacos的netcore-webapi.json,项目需加载该配置) /// 配置解析 /// { /// "WebApiConfig":{ /// "DemoApi":{ /// "Uri":"http://Mall3s-demo", //服务url,也可以只写服务名 “Mall3s-demo”,对应nacos的服务名 /// "Group":"DEFAULT_GROUP" //分组名,服务名所在的分组名,对应nacos配置的Group,空则默认“DEFAULT_GROUP” /// "Cluster":"DEFAULT" //集群名,分组所在的集群,只有一个nacos,填写默认DEFAULT就可以了,空则默认“DEFAULT” /// } /// } /// } /// </summary> /// <param name="services"></param> /// <param name="group"></param> /// <param name="cluster"></param> /// <returns></returns> public static IServiceCollection AddDemoApiService(this IServiceCollection services, IConfiguration configuration) { //调用该方法,assembly 参数写 Assembly.GetExecutingAssembly() 就可以了 services.AddWebApiBaseService(configuration, Assembly.GetExecutingAssembly()); return services; } } }
- nacos中配置接口地址 - 只需要在nacos中查找netcore-webapi.json:  - 新增接口服务调用地址。 - 注意,您也可以直接调用外部接口,如第三方提供的物流查询等等,都可以通过该方式定义。 - 如: -  "Qyapi":{ -  "Uri":"https://qyapi.weixin.qq.com" -  },  
- program中注入微服务。 - builder.Services.AddDemoApiService(builder.Configuration);
- 项目中使用 - private readonly IExchangeRateService _exchangeRateService; /// <summary> /// 构造 /// </summary> public SyncExchangeRateHandler() { _exchangeRateService = App.GetService<IExchangeRateService>(); }
