网关

优质
小牛编辑
113浏览
2023-12-01

ketchup 的网关结合kong

注册ketchup转换器到kong

配置gateway.json

{
  //注册中心配置
  "Consul": {
    "ConnectionString": "192.168.180.55:8500",
    "IsHealthCheck": true
   },
  "Gateway": {
    "Address": "192.168.3.11",//ip
    "Port": 8090,//端口
    "KongAddress": "192.168.190.114:8001",//kong 的adminapi 地址
    "Protocol": "http",//协议
    "Path": "/api" //api路由
  }
}

配置服务方法映射客户端

在GatewayModule配置如下:

private Dictionary<string, Type> ClientMaps()
{
   return new Dictionary<string, Type>()
   {
       {"SayHello", typeof(RpcTest.RpcTestClient)}//key:服务方法。value:客户端类
   };
}

Protos文件

将所有需要映射的服务.proto文件放到 Protos文件夹下

注册服务路由到kong

服务模块中添加kong的设置

builder.GetInstances<IKongNetProvider>().AddKongSetting();

给服务配置kong路由参数

[KongRoute(Name = nameof(SayHello), Paths = new[] { "/sample/SayHello" })] //paths格式:/服务名/方法名

其他配置参数说明

public class KongRoute
{
    public string Name { get; set; } //路由名
    public string[] Hosts { get; set; } //路由host
    public string[] Methods { get; set; } = new[] { "POST" }; //路由请求方式
    public string[] Protocols { get; set; } = new[] { "http" };//路由请求协议
    public int Https_redirect_status_code { get; set; } = 426;//kong 默认https路由错误码
    public string[] Paths { get; set; }//路由路径
    public string[] Tags { get; set; }//路由标记
}