Example #1
0
        internal void AddServiceInfo(string provider, string serviceName, string serviceAddress, int servicePort)
        {
            var serviceInfo = new ServiceInfo()
            {
                Provider = provider,
                Address = serviceAddress,
                Port = servicePort
            };

            var list = new Dictionary<string, ServiceInfo>();
            list.Add(serviceAddress + servicePort, serviceInfo);

            Services.AddOrUpdate(serviceName, list, (key, value) =>
            {
                value.Remove(serviceAddress + servicePort);
                value.Add(serviceAddress + servicePort, serviceInfo);
                return value;
            });
        }
Example #2
0
        //private async Task<ServiceDto> CallConsul(HttpContext context, ILogger log, string serviceName)
        //{
        //    try
        //    {
        //        var data = new ServiceDto();
        //        log.Verbose("Searching service in Consul...");

        //        using (var client = new ConsulClient())
        //        {
        //            var consulCatalog = await client.Catalog.Service(serviceName);

        //            if (consulCatalog.Response.Length > 0)
        //            {
        //                var service = consulCatalog.Response[0];
        //                data.Address = IPAddress.Parse(service.Address);
        //                data.Port = service.ServicePort;

        //                log.Information("Sending request to {Host}:{port}...", service.Address, data.Port);
        //            }
        //            else
        //            {
        //                log.Warning("Service not found!");
        //                context.Response.Send(404);
        //            }
        //        }

        //        return data;
        //    }
        //    catch (Exception e)
        //    {
        //        log.Error(e, "Cannot call Consul");
        //        context.Response.Send(500);
        //        return null;
        //    }
        //}

        private static async Task CallService(HttpContext context, HttpRequest request, ILogger log, ServiceInfo serviceInfo)
        {
            IPAddress serviceAdress = IPAddress.Parse(serviceInfo.Address);
            int servicePort = serviceInfo.Port;

            using (context)
            {
                try
                {
                    using (var client = new TcpClient())
                    {
                        client.Connect(serviceAdress, servicePort);

                        using (var realService = client.GetStream())
                        {
                            log.Verbose("Sending request...");

                            await request.RedirectToAsync(realService);

                            log.Verbose("Waiting response...");

                            await context.Response.RedirectFromAsync(realService);

                            log.Information("Finished!");
                        }
                    }
                }
                catch (Exception e)
                {
                    log.Error(e, "Cannot call service");
                    context.Response.Send(500);
                }
            }
        }