开始使用

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

ketchup 是一个微服务框架,grpc提供服务远程调用,采用consul作为注册中心,采用哈希,轮询,随机,加权随机算法作为负载均衡算法。使用redis作为分布式缓存,rabbitmq 作为消息中间件。

如何运行起来

Program中

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
        return Host.CreateDefaultBuilder(args)
            .UseServiceProviderFactory(new AutofacServiceProviderFactory())
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                config.SetBasePath(Directory.GetCurrentDirectory());                    
                config.AddJsonFile("config/server.json", true, true); 
            }).ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .ConfigureKestrel(options =>
                    {
                        var config = AppConfig.ServerOptions;
                        //使用http2协议
                        options.Listen(new IPEndPoint(IPAddress.Any, config.Port),
                            listenOptions => { listenOptions.Protocols = HttpProtocols.Http2; });
                    })
                    .UseStartup<Startup>();
            });
    }

Startup中

配置config

      public Startup(IConfiguration configuration)
      {
        AppConfig.Configuration = (IConfigurationRoot)configuration;
      }

添加grpc服务

      public void ConfigureServices(IServiceCollection services)
      {
        // Add things to the service collection.
        services.AddGrpc(grpc => grpc.Interceptors.Add<HystrixCommandIntercept>());
      }

添加autofa 注册服务

      public void ConfigureContainer(ContainerBuilder builder)
      {
        // Add things to the Autofac ContainerBuilder.
        builder.AddCoreService().RegisterModules();
      }

配置中间件

      public void Configure(IApplicationBuilder app)
      {
        // Set up the application for development.
        ServiceLocator.Current = app.ApplicationServices.GetAutofacRoot();
        app.UseRouting();
        app.UseKetchupServer();
      }

服务端server.json配置

"Server": {
    "Name": "sample", //服务名称
    "Ip": "192.168.3.11", //服务地址
    "Port": "5003", //服务端口
    "EnableHttp": true, //是否开启http
    "Security": {
      "Whitelist": "*", //白名单
      "BlackList": "" //黑名单
    }
    //"KongAddress": "192.168.190.114:8001" //网关kong 地址(默认不使用)
    //"Weight": 5 //权重  --当负载策略为RandomWeight时设置权重值
},
"Consul": {
  "ConnectionString": "127.0.0.1:8500",//consul 的链接地址
  "IsHealthCheck": true//是否进行健康检查
}

客户端client.json配置

 "Consul": {
     "ConnectionString": "127.0.0.1:8500",
     "IsHealthCheck": true
 }