• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# ConsulClient类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: KV_Put_Get_Delete

        public async Task KV_Put_Get_Delete()
        {
            var client = new ConsulClient();
            var kv = client.KV;

            var key = GenerateTestKeyName();

            var value = Encoding.UTF8.GetBytes("test");

            var getRequest = await kv.Get(key);
            Assert.Equal(System.Net.HttpStatusCode.NotFound, getRequest.StatusCode);
            Assert.Null(getRequest.Response);

            var pair = new KVPair(key)
            {
                Flags = 42,
                Value = value
            };

            var putRequest = await kv.Put(pair);
            Assert.Equal(System.Net.HttpStatusCode.OK, putRequest.StatusCode);
            Assert.True(putRequest.Response);

            try
            {
                // Put a key that begins with a '/'
                var invalidKey = new KVPair("/test")
                {
                    Flags = 42,
                    Value = value
                };
                await kv.Put(invalidKey);
                Assert.True(false, "Invalid key not detected");
            }
            catch (InvalidKeyPairException ex)
            {
                Assert.IsType<InvalidKeyPairException>(ex);
            }

            getRequest = await kv.Get(key);
            Assert.Equal(System.Net.HttpStatusCode.OK, getRequest.StatusCode);
            var res = getRequest.Response;

            Assert.NotNull(res);
            Assert.True(StructuralComparisons.StructuralEqualityComparer.Equals(value, res.Value));
            Assert.Equal(pair.Flags, res.Flags);
            Assert.True(getRequest.LastIndex > 0);

            var del = await kv.Delete(key);
            Assert.True(del.Response);

            getRequest = await kv.Get(key);
            Assert.Null(getRequest.Response);
        }
开发者ID:geffzhang,项目名称:consuldotnet,代码行数:54,代码来源:KVTest.cs


示例17: Catalog_Nodes

        public async Task Catalog_Nodes()
        {
            var client = new ConsulClient();
            var nodeList = await client.Catalog.Nodes();

            Assert.NotEqual((ulong)0, nodeList.LastIndex);
            Assert.NotEqual(0, nodeList.Response.Length);
            // make sure deserialization is working right
            Assert.NotNull(nodeList.Response[0].Address);
            Assert.NotNull(nodeList.Response[0].Name);
        }
开发者ID:geffzhang,项目名称:consuldotnet,代码行数:11,代码来源:CatalogTest.cs


示例18: Session_CreateDestroy

        public async Task Session_CreateDestroy()
        {
            var client = new ConsulClient();
            var sessionRequest = await client.Session.Create();
            var id = sessionRequest.Response;
            Assert.True(sessionRequest.RequestTime.TotalMilliseconds > 0);
            Assert.False(string.IsNullOrEmpty(sessionRequest.Response));

            var destroyRequest = await client.Session.Destroy(id);
            Assert.True(destroyRequest.Response);
        }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:11,代码来源:SessionTest.cs


示例19: Session_CreateDestroy

        public async Task Session_CreateDestroy()
        {
            var client = new ConsulClient();
            var sessionRequest = await client.Session.Create();
            var id = sessionRequest.Response;
            Assert.NotEqual(TimeSpan.Zero, sessionRequest.RequestTime);
            Assert.False(string.IsNullOrEmpty(sessionRequest.Response));

            var destroyRequest = await client.Session.Destroy(id);
            Assert.True(destroyRequest.Response);
        }
开发者ID:geffzhang,项目名称:consuldotnet,代码行数:11,代码来源:SessionTest.cs


示例20: Lock_EphemeralAcquireRelease

 public async Task Lock_EphemeralAcquireRelease()
 {
     var client = new ConsulClient();
     const string keyName = "test/lock/ephemerallock";
     var sessionId = await client.Session.Create(new SessionEntry { Behavior = SessionBehavior.Delete });
     using (var l = client.AcquireLock(new LockOptions(keyName) { Session = sessionId.Response }, CancellationToken.None))
     {
         Assert.True(l.IsHeld);
         await client.Session.Destroy(sessionId.Response);
     }
     Assert.Null((await client.KV.Get(keyName)).Response);
 }
开发者ID:jkabonick-ft,项目名称:consuldotnet,代码行数:12,代码来源:LockTest.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# ContaPolizaDetalle类代码示例发布时间:2022-05-24
下一篇:
C# ConstructorInitializerSyntax类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap