static void Main(string[] args) { //1.启动consol //2.启动CRL.Ocelot,配置见configuration.json var server = new ServerCreater().CreateApi(); server.Register <ITestService, TestService>(); var listener = new ServerListener(); listener.Start("http://localhost:809/");//启用apiService1 #region 注册服务 var consulClient = new CRL.Core.ConsulClient.Consul("http://localhost:8500"); consulClient.UseOcelotGatewayDiscover("http://localhost:3400"); //使用网关服务发现 var info = new CRL.Core.ConsulClient.ServiceRegistrationInfo { Address = "localhost", Name = "apiService1", ID = "apiService1", Port = 809, Tags = new[] { "v1" } }; consulClient.DeregisterService(info.ID); var a = consulClient.RegisterService(info);//注册apiService1 #endregion #region 发现调用服务 var clientConnect = new ApiClientConnect(""); clientConnect.UseOcelotApiGatewayDiscover("http://localhost:3400", "apiService1");//服务发现 var clientConnect2 = new ApiClientConnect(""); clientConnect2.UseOcelotApiGateway("http://localhost:3400");//直接使用网关 #endregion label1: var service1 = clientConnect.GetClient <ITestService>(); var msg = service1.Login(); Console.WriteLine("服务发现调用成功:" + msg); var service2 = clientConnect2.GetClient <ITestService>("serviceTest");//传入网关调用前辍 msg = service2.Login(); Console.WriteLine("服务网关调用成功:" + msg); Console.ReadLine(); goto label1; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGrpcService <GreeterService>(); endpoints.MapGrpcService <HealthCheckService>(); endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); }); }); //注册服务 var consulClient = new CRL.Core.ConsulClient.Consul("http://localhost:8500"); var info = new CRL.Core.ConsulClient.ServiceRegistrationInfo { Address = "127.0.0.1", Name = "grpcServer", ID = "grpcServer1", Port = 50001, Tags = new[] { "v1" }, Check = new CRL.Core.ConsulClient.CheckRegistrationInfo() { GRPC = "127.0.0.1:50001", Interval = "10s", GRPCUseTLS = false, DeregisterCriticalServiceAfter = "90m" } }; consulClient.DeregisterService(info.ID); var a = consulClient.RegisterService(info); }