(更新时间)2021年5月30日 商城高并发秒杀系统(.NET Core版) 08-动态中台调用组件的封装

举报
愚公搬代码 发表于 2021/10/20 01:10:35 2021/10/20
【摘要】 一:动态中台调用组件的封装 /// <summary> /// 中台ServiceCollection扩展方法 /// </summary> public static cla...

一:动态中台调用组件的封装

/// <summary>
///  中台ServiceCollection扩展方法
/// </summary>
public static class DynamicMiddlewareServiceCollectionExtensions
{
    /// <summary>
    /// 添加动态中台
    /// </summary>
    /// <typeparam name="IMiddleService"></typeparam>
    /// <param name="services"></param>
    /// <returns></returns>
    public static IServiceCollection AddDynamicMiddleware<TMiddleService, TMiddleImplementation>(this IServiceCollection services)
        where TMiddleService : class
        where TMiddleImplementation : class, TMiddleService
    {
        AddDynamicMiddleware<TMiddleService, TMiddleImplementation>(services, options => {});
        return services;
    }

    /// <summary>
    /// 添加动态中台
    /// </summary>
    /// <typeparam name="IMiddleService"></typeparam>
    /// <param name="services"></param>
    /// <returns></returns>
    public static IServiceCollection AddDynamicMiddleware<TMiddleService, TMiddleImplementation>(this IServiceCollection services, Action<DynamicMiddlewareOptions> options)
        where TMiddleService : class
        where TMiddleImplementation : class, TMiddleService
    {
        DynamicMiddlewareOptions dynamicMiddlewareOptions = new DynamicMiddlewareOptions();
        options(dynamicMiddlewareOptions);

        // 1、注册服务发现
        services.AddServiceDiscovery(dynamicMiddlewareOptions.serviceDiscoveryOptions);

        // 2、注册负载均衡
        services.AddLoadBalance(dynamicMiddlewareOptions.loadBalanceOptions);

        // 3、添加中台服务
        services.AddMiddleware(dynamicMiddlewareOptions.middlewareOptions);

        // 4、注册动态中台Url服务
        services.AddSingleton<IDynamicMiddleUrl, DefaultDynamicMiddleUrl>();

        // 5、注册动态服务
        services.AddSingleton<TMiddleService, TMiddleImplementation>();
        return services;
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

二:配置选项

/// <summary>
/// 中台配置选项
/// </summary>
public class DynamicMiddlewareOptions
{
    public DynamicMiddlewareOptions()
    {
        serviceDiscoveryOptions = options => { };
        loadBalanceOptions = options => { };
        middlewareOptions = options => { };
    }

    /// <summary>
    /// 服务发现选项
    /// </summary>
    public Action<ServiceDiscoveryOptions> serviceDiscoveryOptions { set; get; }

    /// <summary>
    /// 负载均衡选项
    /// </summary>
    public Action<LoadBalanceOptions> loadBalanceOptions { set; get; }

    // 中台选项
    public Action<MiddlewareOptions> middlewareOptions { set; get; }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

三:动态中台url

/// <summary>
/// 默认获取Url
/// </summary>
public class DefaultDynamicMiddleUrl : IDynamicMiddleUrl
{
    private readonly IServiceDiscovery serviceDiscovery;
    private readonly ILoadBalance loadBalance;

    public DefaultDynamicMiddleUrl(IServiceDiscovery serviceDiscovery, ILoadBalance loadBalance)
    {
        this.serviceDiscovery = serviceDiscovery;
        this.loadBalance = loadBalance;
    }

    public string GetMiddleUrl(string urlShcme, string serviceName)
    {
        // 1、获取服务url
        IList<ServiceNode> serviceUrls = serviceDiscovery.Discovery(serviceName);

        if (serviceUrls.Count == 0)
        {
            throw new FrameException($"{serviceName} 服务不存在");
        }

        // 2、url负载均衡
        ServiceNode serviceUrl = loadBalance.Select(serviceUrls);

        // 3、创建url
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.Append(urlShcme);
        stringBuilder.Append("://");
        stringBuilder.Append(serviceUrl.Url);
        return stringBuilder.ToString();
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
/// <summary>
/// 动态中台url
/// </summary>
public interface IDynamicMiddleUrl
{
    /// <summary>
    ///  获取中台Url
    /// </summary>
    /// <param name="urlShcme">中台url</param>
    /// <param name="serviceName">服务名称</param>
    /// <returns></returns>
    public string GetMiddleUrl(string urlShcme, string serviceName);
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

四:相关依赖的类文件

/// <summary>
/// 动态中台服务,从注册中心动态获取服务
/// </summary>
public class DefaultDynamicMiddleService : IDynamicMiddleService
{
    private readonly IMiddleService middleService; // 中台组件
    private readonly IDynamicMiddleUrl middleUrl; // 动态url组件

    public DefaultDynamicMiddleService(IMiddleService middleService, IDynamicMiddleUrl middleUrl)
    {
        this.middleService = middleService;
        this.middleUrl = middleUrl;
    }

    public void Delete(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
    {
        // 1、获取中台url  https://localhost:5001   servicenode localhost:5001
        string url = middleUrl.GetMiddleUrl(urlShcme, serviceName);

        // 2、请求
        MiddleResult middleResult = middleService.Delete(url + serviceLink, middleParam);

        // 3、判断是否成功
        IsSuccess(middleResult);
    }

    public IList<T> GetList<T>(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
        where T : new()
    {
        // 1、获取中台url
        string url = middleUrl.GetMiddleUrl(urlShcme, serviceName);

        // 2、请求
        MiddleResult middleResult = middleService.Get(url + serviceLink, middleParam);

        // 3、判断是否成功
        IsSuccess(middleResult);

        // 4、结果进行转换对象
        return ConvertUtil.MiddleResultToList<T>(middleResult);
    }

    public T Get<T>(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
        where T : new()
    {
        // 1、获取中台url
        string url = middleUrl.GetMiddleUrl(urlShcme, serviceName);

        // 2、请求
        MiddleResult middleResult = middleService.Get(url + serviceLink, middleParam);

        // 3、判断是否成功
        IsSuccess(middleResult);

        // 4、结果进行转换对象
        return ConvertUtil.MiddleResultToObject<T>(middleResult);
    }

    public void Post(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
    {
        // 1、获取中台url
        string url = middleUrl.GetMiddleUrl(urlShcme, serviceName);

        // 2、请求
        MiddleResult middleResult = middleService.Post(url + serviceLink, middleParam);

        // 3、判断是否成功
        IsSuccess(middleResult);
    }

    public dynamic PostDynamic(string urlShcme, string serviceName, string serviceLink, dynamic middleParam)
    {
        // 1、获取中台url
        string url = middleUrl.GetMiddleUrl(urlShcme, serviceName);

        // 2、请求
        MiddleResult middleResult = middleService.PostDynamic(url + serviceLink, middleParam);

        // 3、判断是否成功
        IsSuccess(middleResult);

        return middleResult.Result;
    }

    public void Post(string urlShcme, string serviceName, string serviceLink, IList<IDictionary<string, object>> middleParams)
    {
        // 1、获取中台url
        string url = middleUrl.GetMiddleUrl(urlShcme, serviceName);

        // 2、请求
        MiddleResult middleResult = middleService.Post(url + serviceLink, middleParams);

        // 3、判断是否成功
        IsSuccess(middleResult);
    }

    public void Put(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
    {
        // 1、获取中台url
        string url = middleUrl.GetMiddleUrl(urlShcme, serviceName);

        // 2、请求
        MiddleResult middleResult = middleService.Put(url + serviceLink, middleParam);
        // 3、判断是否成功
        IsSuccess(middleResult);
    }

    public dynamic PutDynamic(string urlShcme, string serviceName, string serviceLink, dynamic middleParam)
    {
        // 1、获取中台url
        string url = middleUrl.GetMiddleUrl(urlShcme, serviceName);

        // 2、请求
        MiddleResult middleResult = middleService.PutDynamic(url + serviceLink, middleParam);
        // 3、判断是否成功
        IsSuccess(middleResult);

        return middleResult.Result;
    }

    public void Put(string urlShcme, string serviceName, string serviceLink, IList<IDictionary<string, object>> middleParams)
    {
        // 1、获取中台url
        string url = middleUrl.GetMiddleUrl(urlShcme, serviceName);

        // 2、请求
        MiddleResult middleResult = middleService.Put(url + serviceLink, middleParams);
        // 3、判断是否成功
        IsSuccess(middleResult);
    }

    private void IsSuccess(MiddleResult middleResult)
    {
        if (!middleResult.ErrorNo.Equals("0"))
        {
            throw new FrameException(middleResult.ErrorNo, middleResult.ErrorInfo);
        }
    }

    public IList<IDictionary<string, object>> GetList(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
    {
        // 1、获取中台url
        string url = middleUrl.GetMiddleUrl(urlShcme, serviceName);

        // 2、请求
        MiddleResult middleResult = middleService.Get(url + serviceLink, middleParam);

        // 3、判断是否成功
        IsSuccess(middleResult);

        return middleResult.resultList;
    }

    public IDictionary<string, object> Get(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
    {
        // 1、获取中台url
        string url = middleUrl.GetMiddleUrl(urlShcme, serviceName);

        // 2、请求
        MiddleResult middleResult = middleService.Get(url + serviceLink, middleParam);

        // 3、判断是否成功
        IsSuccess(middleResult);

        return middleResult.resultDic;
    }

    /// <summary>
    /// 返回动态结果
    /// </summary>
    /// <param name="urlShcme"></param>
    /// <param name="serviceName"></param>
    /// <param name="serviceLink"></param>
    /// <param name="middleParam"></param>
    /// <returns></returns>
    public dynamic GetDynamic(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
    {
        // 1、获取中台url
        string url = middleUrl.GetMiddleUrl(urlShcme, serviceName);

        // 2、请求
        MiddleResult middleResult = middleService.Get(url + serviceLink, middleParam);

        // 3、判断是否成功
        IsSuccess(middleResult);

        return middleResult.Result;
    }

    public dynamic DeleteDynamic(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
    {
        // 1、获取中台url
        string url = middleUrl.GetMiddleUrl(urlShcme, serviceName);

        // 2、请求
        MiddleResult middleResult = middleService.Delete(url + serviceLink, middleParam);

        // 3、判断是否成功
        IsSuccess(middleResult);

        return middleResult.Result;
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
/// <summary>
/// 中台服务(服务交互)
/// </summary>
public interface IDynamicMiddleService
{
    public IList<IDictionary<string, object>> GetList(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam);
    public IDictionary<string, object> Get(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam);
    public dynamic GetDynamic(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam);
    public IList<T> GetList<T>(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
        where T : new();

    /// <summary>
    /// Get请求
    /// </summary>
    /// <param name="middleUrl">中台链接</param>
    /// <param name="middleParam">中台参数</param>
    /// <returns></returns>
    public T Get<T>(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam)
        where T : new();

    /// <summary>
    /// Post请求
    /// </summary>
    /// <param name="middleUrl">中台链接</param>
    /// <param name="middleParam">中台参数</param>
    /// <returns></returns>
    public void Post(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam);

    /// <summary>
    /// Post请求,动态参数
    /// </summary>
    /// <param name="middleUrl">中台链接</param>
    /// <param name="middleParam">中台参数</param>
    /// <returns></returns>
    public dynamic PostDynamic(string urlShcme, string serviceName, string serviceLink, dynamic middleParam);

    /// <summary>
    /// Post请求
    /// </summary>
    /// <param name="middleUrl">中台链接</param>
    /// <param name="middleParams">中台集合参数</param>
    /// <returns></returns>
    public void Post(string urlShcme, string serviceName, string serviceLink, IList<IDictionary<string, object>> middleParams);


    /// <summary>
    /// Delete请求
    /// </summary>
    /// <param name="middleUrl">中台链接</param>
    /// <param name="middleParam">中台参数</param>
    /// <returns></returns>
    public void Delete(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam);

    /// <summary>
    /// Delete请求
    /// </summary>
    /// <param name="middleUrl">中台链接</param>
    /// <param name="middleParam">中台参数</param>
    /// <returns></returns>
    public dynamic DeleteDynamic(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam);

    /// <summary>
    /// Put请求
    /// </summary>
    /// <param name="middleUrl">中台链接</param>
    /// <param name="middleParam">中台参数</param>
    /// <returns></returns>
    public void Put(string urlShcme, string serviceName, string serviceLink, IDictionary<string, object> middleParam);

    /// <summary>
    /// Put请求,动态参数
    /// </summary>
    /// <param name="middleUrl">中台链接</param>
    /// <param name="middleParam">中台参数</param>
    /// <returns></returns>
    public dynamic PutDynamic(string urlShcme, string serviceName, string serviceLink, dynamic middleParam);

    /// <summary>
    /// Put请求
    /// </summary>
    /// <param name="middleUrl">中台链接</param>
    /// <param name="middleParams">中台参数</param>
    /// <returns></returns>
    public void Put(string urlShcme, string serviceName, string serviceLink, IList<IDictionary<string, object>> middleParams);

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86

文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。

原文链接:codeboy.blog.csdn.net/article/details/117400082

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。