public OrdersClientController()
        {
            var client = new Client();
            var services = client.Agent.Services().Response;
            foreach (var service in services)
            {
                bool isOrderService = service.Value.Tags.Any(tag => tag == "Orders");
                if (isOrderService)
                {
                    var server = new Server()
                    {
                        Timeout = 5000,
                        Uri = new Uri(string.Format("{0}:{1}/", service.Value.Address, service.Value.Port))
                    };
                    _serverList.Add(server);
                }
            }

            _retryPolicy = Policy
                .Handle<AggregateException>()
                .Or<Exception>()
                .WaitAndRetry(new[]
                {
                    TimeSpan.FromMilliseconds(50),
                    TimeSpan.FromMilliseconds(150),
                    TimeSpan.FromMilliseconds(250),
                }, (exception, timespan) =>
                {
                    LogError(exception);
                    TrySelectNextServer();
                });

            TrySelectNextServer();
        }
 static void Main(string[] args)
 {
     var registrationService = new OrdersSideKickRegistration();
     var client = new Client();
     registrationService.Register(client);
     registrationService.DisplayRegisteredServices(client);
 }
        public void Shutdown(HostControl hostcontrol)
        {
            var client = new Client();
            var configuration = OrderServerConfiguration.GetConfiguration();
            client.Agent.ServiceDeregister(configuration.Server.Id);

            return;
        }
示例#4
0
 public static string GetConfig()
 {
     var client = new Client();
     var key = "ServiceConfig:" + ServiceName;
     var response = client.KV.Get(key);
     var res = Encoding.UTF8.GetString(response.Response.Value);
     return res;
 }
 public void SetUp()
 {
     var clientConfig = new ConsulClientConfiguration();
     clientConfig.Address = "127.0.0.1:9500";
     var client = new Client(clientConfig);
     var adapter = new ConsulAdapter(client);
     var flipper = new Flipper(adapter);
     _feature = flipper.Feature("unobtanium");
 }
示例#6
0
        public static ServiceInformation[] FindService(string name)
        {
            Logger.Information("{ServiceName} lookup {OtherServiceName}", ServiceName, name);
            var client = new Client();
            var others = client.Health.Service(name, null, true);

            return
                others.Response.Select(other => new ServiceInformation(other.Service.Address, other.Service.Port))
                    .ToArray();
        }
示例#7
0
        private static void StartReaper()
        {
            Task.Factory.StartNew(async () =>
            {
                await Task.Delay(1000).ConfigureAwait(false);
                Logger.Information("Reaper: started..");
                var client = new Client();
                var lookup = new HashSet<string>();
                while (true)
                {
                    try
                    {
                        var checks = client.Agent.Checks();
                        foreach (var check in checks.Response)
                        {
                            if (Equals(check.Value.Status, CheckStatus.Critical))
                            {
                                //dont delete new services
                                if (lookup.Contains(check.Value.ServiceID))
                                {
                                    client.Agent.ServiceDeregister(check.Value.ServiceID);
                                    Logger.Information("Reaper: Removing {ServiceId}", check.Value.ServiceID);
                                }
                                else
                                {
                                    Logger.Information("Reaper: Marking {ServiceId}", check.Value.ServiceID);
                                    lookup.Add(check.Value.ServiceID);
                                }

                            }
                            else
                            {
                                //if service is ok, remove it from reaper set
                                lookup.Remove(check.Value.ServiceID);
                            }
                        }
                    }
                    catch(Exception x)
                    {
                        Logger.Error(x,"Crashed");
                    }

                    await Task.Delay(5000).ConfigureAwait(false);
                }
            });
        }
        /// <summary>
        /// Registers the specified client.
        /// </summary>
        /// <param name="client">The client.</param>
        public void Register(Client client)
        {
            var gatewayConfiguration = HttpGatewayConfiguration.GetConfiguration();
            foreach (OrderAPIServerElement serverDefinition in gatewayConfiguration.OrderServiceConfiguration.Servers)
            {
                var registration = new AgentServiceRegistration()
                {
                    ID =serverDefinition.Id,
                    Name = serverDefinition.Name,
                    Address = serverDefinition.Address,
                    Port = serverDefinition.Port,
                    Tags = new []{"Orders"}
                };

                //clear any old registration - we don't respond to services running/not running in this version
                client.Agent.ServiceDeregister(registration.ID);
                client.Agent.ServiceRegister(registration);
            }
        }
示例#9
0
        public ConsulAdapter(string ApplicationName, int ApplicationPort, string ApplicationIp, int OwnerLimit)
        {
            _appname = ApplicationName;
            _apport = ApplicationPort;
            _appadress = ApplicationIp;
            _spath = string.Concat(_appname, "/Semaphore"); // What to do with applications with the same name?
            _limit = OwnerLimit;

            try
            {
                _client = new Client();
                _semaphore = _client.Semaphore(_spath, _limit);
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not create a client session to consul: {0}", e);
                Console.ReadLine();
            }
        }
        // Issue #1 - Don't want a trailing slash, just host and port (this needs to be done at the 'Root' of the drive)
        public ConsulPSDriveInfo(PSDriveInfo driveInfo)
            : base(driveInfo.Name,driveInfo.Provider,driveInfo.Root.TrimEnd('/'),driveInfo.Description,driveInfo.Credential,driveInfo.DisplayRoot)
        {
            // TODO: Check if the consul root is valid?

            // TODO: Support DataCenter, HttpAuthentication

            // connection is specified as a URI to the consul http interface. .
            var consulUri = new Uri(Root);

            var config = new ConsulClientConfiguration
            {
                Address = consulUri.Host + ":" + consulUri.Port,
                Scheme = consulUri.Scheme
            };

            // AuthToken taken from Credential UserName if available.
            if (driveInfo.Credential != null && !string.IsNullOrWhiteSpace(driveInfo.Credential.UserName))
                config.Token = driveInfo.Credential.UserName;

            ConsulClient = new Client(config);
        }
示例#11
0
 public static void RegisterService(string serviceName, string serviceId, string version, Uri uri)
 {
     ServiceName = serviceName;
     ServiceId = serviceId;
     var client = new Client();
     client.Agent.ServiceRegister(new AgentServiceRegistration
     {
         Address = uri.Host,
         ID = serviceId,
         Name = serviceName,
         Port = uri.Port,
         Tags = new[] {version},
         Check = new AgentServiceCheck
         {
             HTTP = uri + "status",
             Interval = TimeSpan.FromSeconds(1),
             TTL = TimeSpan.Zero,
             Timeout = TimeSpan.Zero
         }
     });
     StartReaper();
 }
        public void DisplayRegisteredServices(Client client)
        {
            Console.WriteLine("List of services registred by agent");
            var services = client.Agent.Services().Response;
            foreach (var key in services.Keys)
            {
                var service = services[key];
                var serviceDefinition = new StringBuilder();
                serviceDefinition.AppendLine("Service Definition [");
                serviceDefinition.AppendFormat("ID: {0}", service.ID);
                serviceDefinition.AppendLine();
                serviceDefinition.AppendFormat("Address: {0}", service.Address);
                serviceDefinition.AppendLine();
                serviceDefinition.AppendFormat("Port: {0}", service.Port);
                serviceDefinition.AppendLine();
                foreach (var tag in service.Tags)
                {
                    serviceDefinition.AppendFormat("Tag: {0}", tag);
                }
                serviceDefinition.AppendLine("]");

                Console.WriteLine(serviceDefinition);
            }
        }
示例#13
0
 internal Status(Client c)
 {
     _client = c;
 }
示例#14
0
 bool CreateConsulClient()
 {
     ConsulClient = new Client();
     //Deregister any leftover service
     ConsulClient.Agent.ServiceDeregister("AmazingService").Wait(5000);
     AgentServiceRegistration svcreg = new AgentServiceRegistration
     {
         Name = "AmazingService",
         Port = ListenPort,
         Address = ListenAddress,
         //,
         //        Tags = new[] { "consultest", "owintest" },
         Check = new AgentServiceCheck
         {
             TTL = TimeSpan.FromSeconds(5)
         }
     };
     //if (_consulClient.Agent.ServiceRegister(svcreg).Wait(5000)) _semaphore = _consulClient.Semaphore("AmazingService/lock", 1);
     return ConsulClient.Agent.ServiceRegister(svcreg).Wait(5000);
 }
示例#15
0
 private bool CreateConsulClient()
 {
     MyLogger.Trace("Entering " + MethodBase.GetCurrentMethod().Name);
     string agentname;
     try
     {
         ConsulClient = new Client();
         agentname = ConsulClient.Agent.NodeName;
     }
     catch (Exception ex)
     {
         MyLogger.Error("Could not create Consul Client: {0}", ex.Message);
         MyLogger.Debug(ex);
         return false;
     }
     MyLogger.Debug("ConsulClient created with agent {0}", agentname);
     return true;
 }
        private void RegisterService()
        {
            var client = new Client();

            var configuration = OrderServerConfiguration.GetConfiguration();
            var registration = new AgentServiceRegistration()
            {
                ID = configuration.Server.Id,
                Name = configuration.Server.Name,
                Address = configuration.Server.Address,
                Port = configuration.Server.Port,
                Tags = new[] { "Orders" }
            };

            //clear any old registration - we don't respond to services running/not running in this version
            client.Agent.ServiceDeregister(registration.ID);
            client.Agent.ServiceRegister(registration);
        }
示例#17
0
 public Raw(Client client)
 {
     Client = client;
 }
示例#18
0
 internal Health(Client c)
 {
     _client = c;
 }
 public string TestNamespace(string name)
 {
     var client = new Client();
     var adapter = new ConsulAdapter(client, name);
     return adapter.Namespace;
 }
示例#20
0
 public KV(Client c)
 {
     _client = c;
 }
 private Client GetConsulClient()
 {
     var uri = new Uri(connectionString);
     var client = new Client(new ConsulClientConfiguration() { Address = uri.Authority, Scheme = uri.Scheme});
     return client;
 }
示例#22
0
 internal Semaphore(Client c)
 {
     _client = c;
     _cts = new CancellationTokenSource();
 }
示例#23
0
 internal Lock(Client c)
 {
     _client = c;
     _cts = new CancellationTokenSource();
 }
示例#24
0
 internal DisposableLock(Client client, LockOptions opts, CancellationToken ct)
     : base(client)
 {
     Opts = opts;
     CancellationToken = Acquire(ct);
 }
示例#25
0
 internal AutoSemaphore(Client c, SemaphoreOptions opts)
     : base(c)
 {
     Opts = opts;
     CancellationToken = Acquire();
 }
示例#26
0
 internal Catalog(Client c)
 {
     _client = c;
 }
示例#27
0
 internal Event(Client c)
 {
     _client = c;
 }
示例#28
0
 internal Raw(Client c)
 {
     _client = c;
 }