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; }
static void Main(string[] args) { clientConnect = new ApiClientConnect("https://api.weixin.qq.com"); //clientConnect.UseConsulDiscover("http://127.0.0.1:8500", "serviceName");//使用consul发现服务 //clientConnect.UseOcelotApiGateway("http://127.0.0.1:3400");//直接使用ocelot网关 //clientConnect.UseOcelotApiGatewayDiscover("http://127.0.0.1:3400", "serviceName");//使用ocelot网关发现服务 clientConnect.UseBeforRequest((request, members, url) => { //如果需要设置发送头信息 request.SetHead("token", "test"); }); clientConnect.UseAfterRequest((url, content) => { Console.WriteLine($"response is {content}"); }); //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET var client = clientConnect.GetClient <IToken>(); client.Test(new args() { name = "args", name2 = "sss", uArgs = new userInfo() { errcode = "111" } }); //如果参数正确,返回token var result = client.token("grant_type", "appid", "secret"); Console.WriteLine(result.ToJson()); getInfo(); Console.ReadLine(); }