当前位置: 首页>>代码示例>>C#>>正文


C# ConsulClient类代码示例

本文整理汇总了C#中ConsulClient的典型用法代码示例。如果您正苦于以下问题:C# ConsulClient类的具体用法?C# ConsulClient怎么用?C# ConsulClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ConsulClient类属于命名空间,在下文中一共展示了ConsulClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Health_Checks

        public async Task Health_Checks()
        {
            var client = new ConsulClient();

            var registration = new AgentServiceRegistration()
            {
                Name = "foo",
                Tags = new[] { "bar", "baz" },
                Port = 8000,
                Check = new AgentServiceCheck
                {
                    TTL = TimeSpan.FromSeconds(15)
                }
            };
            try
            {
                await client.Agent.ServiceRegister(registration);
                var checks = await client.Health.Checks("foo");
                Assert.NotEqual((ulong)0, checks.LastIndex);
                Assert.NotEqual(0, checks.Response.Length);
            }
            finally
            {
                await client.Agent.ServiceDeregister("foo");
            }
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:26,代码来源:HealthTest.cs

示例2: Client_SetQueryOptions

 public async Task Client_SetQueryOptions()
 {
     var client = new ConsulClient();
     var opts = new QueryOptions()
     {
         Datacenter = "foo",
         Consistency = ConsistencyMode.Consistent,
         WaitIndex = 1000,
         WaitTime = new TimeSpan(0, 0, 100),
         Token = "12345"
     };
     var request = client.Get<KVPair>("/v1/kv/foo", opts);
     try
     {
         await request.Execute();
     }
     catch (Exception)
     {
         // ignored
     }
     Assert.Equal("foo", request.Params["dc"]);
     Assert.True(request.Params.ContainsKey("consistent"));
     Assert.Equal("1000", request.Params["index"]);
     Assert.Equal("1m40s", request.Params["wait"]);
     Assert.Equal("12345", request.Params["token"]);
 }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:26,代码来源:ClientTest.cs

示例3: Session_CreateRenewDestroyRenew

        public async Task Session_CreateRenewDestroyRenew()
        {
            var client = new ConsulClient();
            var sessionRequest = await client.Session.Create(new SessionEntry() { TTL = TimeSpan.FromSeconds(10) });

            var id = sessionRequest.Response;
            Assert.NotEqual(TimeSpan.Zero, sessionRequest.RequestTime);
            Assert.False(string.IsNullOrEmpty(sessionRequest.Response));

            var renewRequest = await client.Session.Renew(id);
            Assert.NotEqual(TimeSpan.Zero, renewRequest.RequestTime);
            Assert.NotNull(renewRequest.Response.ID);
            Assert.Equal(sessionRequest.Response, renewRequest.Response.ID);
            Assert.Equal(renewRequest.Response.TTL.Value.TotalSeconds, TimeSpan.FromSeconds(10).TotalSeconds);

            var destroyRequest = await client.Session.Destroy(id);
            Assert.True(destroyRequest.Response);

            try
            {
                renewRequest = await client.Session.Renew(id);
                Assert.True(false, "Session still exists");
            }
            catch (SessionExpiredException ex)
            {
                Assert.IsType<SessionExpiredException>(ex);
            }
        }
开发者ID:geffzhang,项目名称:consuldotnet,代码行数:28,代码来源:SessionTest.cs

示例4: Client_DefaultConfig_env

        public void Client_DefaultConfig_env()
        {
            const string addr = "1.2.3.4:5678";
            const string token = "abcd1234";
            const string auth = "username:password";
            Environment.SetEnvironmentVariable("CONSUL_HTTP_ADDR", addr);
            Environment.SetEnvironmentVariable("CONSUL_HTTP_TOKEN", token);
            Environment.SetEnvironmentVariable("CONSUL_HTTP_AUTH", auth);
            Environment.SetEnvironmentVariable("CONSUL_HTTP_SSL", "1");
            Environment.SetEnvironmentVariable("CONSUL_HTTP_SSL_VERIFY", "0");

            var config = new ConsulClientConfiguration();

            Assert.Equal(addr, string.Format("{0}:{1}", config.Address.Host, config.Address.Port));
            Assert.Equal(token, config.Token);
            Assert.NotNull(config.HttpAuth);
            Assert.Equal("username", config.HttpAuth.UserName);
            Assert.Equal("password", config.HttpAuth.Password);
            Assert.Equal("https", config.Address.Scheme);
            Assert.True((config.Handler as WebRequestHandler).ServerCertificateValidationCallback(null, null, null,
                SslPolicyErrors.RemoteCertificateChainErrors));

            Environment.SetEnvironmentVariable("CONSUL_HTTP_ADDR", string.Empty);
            Environment.SetEnvironmentVariable("CONSUL_HTTP_TOKEN", string.Empty);
            Environment.SetEnvironmentVariable("CONSUL_HTTP_AUTH", string.Empty);
            Environment.SetEnvironmentVariable("CONSUL_HTTP_SSL", string.Empty);
            Environment.SetEnvironmentVariable("CONSUL_HTTP_SSL_VERIFY", string.Empty);
            ServicePointManager.ServerCertificateValidationCallback = null;

            var client = new ConsulClient(config);

            Assert.NotNull(client);
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:33,代码来源:ClientTest.cs

示例5: Catalog_Datacenters

        public async Task Catalog_Datacenters()
        {
            var client = new ConsulClient();
            var datacenterList = await client.Catalog.Datacenters();

            Assert.NotEqual(0, datacenterList.Response.Length);
        }
开发者ID:geffzhang,项目名称:consuldotnet,代码行数:7,代码来源:CatalogTest.cs

示例6: Agent_Services_CheckPassing

        public async Task Agent_Services_CheckPassing()
        {
            var client = new ConsulClient();
            var registration = new AgentServiceRegistration()
            {
                Name = "foo",
                Tags = new[] { "bar", "baz" },
                Port = 8000,
                Check = new AgentServiceCheck
                {
                    TTL = TimeSpan.FromSeconds(15),
                    Status = CheckStatus.Passing
                }
            };

            await client.Agent.ServiceRegister(registration);

            var services = await client.Agent.Services();
            Assert.True(services.Response.ContainsKey("foo"));

            var checks = await client.Agent.Checks();
            Assert.True(checks.Response.ContainsKey("service:foo"));

            Assert.Equal(CheckStatus.Passing, checks.Response["service:foo"].Status);

            await client.Agent.ServiceDeregister("foo");
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:27,代码来源:AgentTest.cs

示例7: ACL_CreateDestroy

        public async Task ACL_CreateDestroy()
        {
            Skip.If(string.IsNullOrEmpty(ConsulRoot));

            var client = new ConsulClient(new ConsulClientConfiguration() { Token = ConsulRoot });
            var aclEntry = new ACLEntry()
            {
                Name = "API Test",
                Type = ACLType.Client,
                Rules = "key \"\" { policy = \"deny\" }"
            };
            var res = await client.ACL.Create(aclEntry);
            var id = res.Response;

            Assert.NotEqual(0, res.RequestTime.TotalMilliseconds);
            Assert.False(string.IsNullOrEmpty(res.Response));

            var aclEntry2 = await client.ACL.Info(id);

            Assert.NotNull(aclEntry2.Response);
            Assert.Equal(aclEntry2.Response.Name, aclEntry.Name);
            Assert.Equal(aclEntry2.Response.Type, aclEntry.Type);
            Assert.Equal(aclEntry2.Response.Rules, aclEntry.Rules);

            Assert.True((await client.ACL.Destroy(id)).Response);
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:26,代码来源:ACLTest.cs

示例8: Health_State

        public async Task Health_State()
        {
            var client = new ConsulClient();

            var checks = await client.Health.State(CheckStatus.Any);
            Assert.NotEqual((ulong)0, checks.LastIndex);
            Assert.NotEqual(0, checks.Response.Length);
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:8,代码来源:HealthTest.cs

示例9: Health_Service

        public async Task Health_Service()
        {
            var client = new ConsulClient();

            var checks = await client.Health.Service("consul", "", true);
            Assert.NotEqual((ulong)0, checks.LastIndex);
            Assert.NotEqual(0, checks.Response.Length);
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:8,代码来源:HealthTest.cs

示例10: Catalog_Service

        public async Task Catalog_Service()
        {
            var client = new ConsulClient();
            var serviceList = await client.Catalog.Service("consul");

            Assert.NotEqual((ulong)0, serviceList.LastIndex);
            Assert.NotEqual(0, serviceList.Response.Length);
        }
开发者ID:geffzhang,项目名称:consuldotnet,代码行数:8,代码来源:CatalogTest.cs

示例11: Agent_Members

        public async Task Agent_Members()
        {
            var client = new ConsulClient();

            var members = await client.Agent.Members(false);

            Assert.NotNull(members);
            Assert.Equal(1, members.Response.Length);
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:9,代码来源:AgentTest.cs

示例12: Catalog_Node

        public async Task Catalog_Node()
        {
            var client = new ConsulClient();

            var node = await client.Catalog.Node(client.Agent.NodeName);

            Assert.NotEqual((ulong)0, node.LastIndex);
            Assert.NotNull(node.Response.Services);
        }
开发者ID:geffzhang,项目名称:consuldotnet,代码行数:9,代码来源:CatalogTest.cs

示例13: Lock_OneShot

        public void Lock_OneShot()
        {
            var client = new ConsulClient();
            const string keyName = "test/lock/oneshot";
            var lockOptions = new LockOptions(keyName)
            {
                LockTryOnce = true
            };

            Assert.Equal(Lock.DefaultLockWaitTime, lockOptions.LockWaitTime);

            lockOptions.LockWaitTime = TimeSpan.FromMilliseconds(1000);

            var lockKey = client.CreateLock(lockOptions);

            lockKey.Acquire(CancellationToken.None);

            var contender = client.CreateLock(new LockOptions(keyName)
            {
                LockTryOnce = true,
                LockWaitTime = TimeSpan.FromMilliseconds(1000)
            });

            var stopwatch = Stopwatch.StartNew();

            Exception didExcept = null;

            Task.WaitAny(
                Task.Run(() =>
                {
                    // Needed because async asserts don't work in sync methods!
                    try
                    {
                        contender.Acquire();
                    }
                    catch (Exception e)
                    {
                        didExcept = e;
                    }
                }),
                Task.Delay((int)(2 * lockOptions.LockWaitTime.TotalMilliseconds)).ContinueWith((t) => Assert.True(false, "Took too long"))
                );


            Assert.False(stopwatch.ElapsedMilliseconds < lockOptions.LockWaitTime.TotalMilliseconds);
            Assert.False(contender.IsHeld, "Contender should have failed to acquire");
            Assert.IsType<LockMaxAttemptsReachedException>(didExcept);

            lockKey.Release();

            contender.Acquire();
            Assert.True(contender.IsHeld);

            contender.Release();
            contender.Destroy();
        }
开发者ID:geffzhang,项目名称:consuldotnet,代码行数:56,代码来源:LockTest.cs

示例14: Health_Node

        public async Task Health_Node()
        {
            var client = new ConsulClient();

            var info = await client.Agent.Self();
            var checks = await client.Health.Node((string)info.Response["Config"]["NodeName"]);

            Assert.NotEqual((ulong)0, checks.LastIndex);
            Assert.NotEqual(0, checks.Response.Length);
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:10,代码来源:HealthTest.cs

示例15: Agent_Self

        public async Task Agent_Self()
        {
            var client = new ConsulClient();

            var info = await client.Agent.Self();

            Assert.NotNull(info);
            Assert.False(string.IsNullOrEmpty(info.Response["Config"]["NodeName"]));
            Assert.False(string.IsNullOrEmpty(info.Response["Member"]["Tags"]["bootstrap"].ToString()));
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:10,代码来源:AgentTest.cs


注:本文中的ConsulClient类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。