ApiLoadBalancingMng_RESTful.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Sers.Core.Module.Api.RouteMap;
  2. using Sers.Core.Module.Rpc;
  3. using Sers.ServiceCenter.Entity;
  4. using System.Runtime.CompilerServices;
  5. using Vit.Extensions;
  6. namespace Sers.Gover.Base
  7. {
  8. /// <summary>
  9. /// 当前可调用的ApiService
  10. /// </summary>
  11. public class ApiLoadBalancingMng_RESTful: ApiLoadBalancingMng
  12. {
  13. /// <summary>
  14. /// 如 "GET"
  15. /// </summary>
  16. /// <param name="apiNode"></param>
  17. /// <returns></returns>
  18. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  19. static string GetHttpMethod(ApiNode apiNode)
  20. {
  21. var method = apiNode.apiDesc?.HttpMethodGet()?.ToUpper();
  22. if (string.IsNullOrEmpty(method)) method = "_";
  23. return method;
  24. }
  25. /// <summary>
  26. /// 通过负载均衡算法 获取可调用的ApiNode
  27. /// </summary>
  28. /// <param name="rpcData"></param>
  29. /// <param name="routeType"></param>
  30. /// <returns></returns>
  31. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  32. public override ApiNode GetCurApiNodeByLoadBalancing(RpcContextData rpcData, out ERouteType routeType)
  33. {
  34. var method= rpcData.http.method?.ToUpper();
  35. if (string.IsNullOrEmpty(method)) method = "_";
  36. var oriRoute = rpcData.route;
  37. #region 去除query string(url ?后面的字符串)
  38. //{
  39. // // b2?a=c
  40. // var index = oriRoute.IndexOf('?');
  41. // if (index >= 0)
  42. // {
  43. // oriRoute = oriRoute.Substring(0, index);
  44. // }
  45. //}
  46. #endregion
  47. var route = "/"+method + oriRoute;
  48. var lb = routeMap.Routing(route, out routeType);
  49. if (lb != null)
  50. {
  51. return lb.GetCurApiNodeBalancing();
  52. }
  53. route = "/_" + oriRoute;
  54. lb = routeMap.Routing(route, out routeType);
  55. if (lb != null)
  56. {
  57. return lb.GetCurApiNodeBalancing();
  58. }
  59. return null;
  60. }
  61. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  62. protected override string GetApiRoute(ApiNode apiNode)
  63. {
  64. var route = "/" + GetHttpMethod(apiNode) + apiNode.apiDesc.route;
  65. return route;
  66. }
  67. }
  68. }