示例#1
0
        /// <summary>
        /// 获取key/value集合
        /// </summary>
        /// <param name="prefix">键前缀</param>
        /// <returns></returns>
        public async Task <ConsulApiQueryResult <ConsulKVPair[]> > kvGetListAsync(string prefix)
        {
            var queryApiUriBuilder = new ConsulApiUriBuilder()
                                     .Path(string.Format("{0}/{1}", kvApiUrl, prefix))
                                     .Param("recurse", string.Empty);

            var response = await httpClient.GetAsync(queryApiUriBuilder.buildApiUri()).ConfigureAwait(false);

            return(await response.formatConsulApiQueryResultAsync <ConsulKVPair[]>().ConfigureAwait(false));
        }
示例#2
0
        /// <summary>
        /// 获取raw value信息
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public async Task <ConsulApiQueryResult <string> > kvGetRawAsync(string key)
        {
            var queryApiUriBuilder = new ConsulApiUriBuilder()
                                     .Path(string.Format("{0}/{1}", kvApiUrl, key))
                                     .Param("raw", string.Empty);

            var response = await httpClient.GetAsync(queryApiUriBuilder.buildApiUri()).ConfigureAwait(false);

            return(await response.formatConsulApiQueryResultAsync().ConfigureAwait(false));
        }
示例#3
0
        /// <summary>
        /// 删除key/value集合
        /// </summary>
        /// <param name="prefix">键前缀</param>
        /// <returns></returns>
        public async Task <ConsulApiWriteResult <bool> > kvDeleteListAsync(string prefix)
        {
            var deleteApiUriBuilder = new ConsulApiUriBuilder()
                                      .Path(string.Format("{0}/{1}", kvApiUrl, prefix))
                                      .Param("recurse", string.Empty);

            var response = await httpClient.DeleteAsync(deleteApiUriBuilder.buildApiUri()).ConfigureAwait(false);

            return(await response.formatConsulApiWriteResultAsync <bool>().ConfigureAwait(false));
        }
示例#4
0
        /// <summary>
        /// 获取key集合
        /// </summary>
        /// <param name="prefix">键前缀</param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public async Task <ConsulApiQueryResult <string[]> > kvGetKeysAsync(string prefix, string separator)
        {
            var queryApiUriBuilder = new ConsulApiUriBuilder()
                                     .Path(string.Format("{0}/{1}", kvApiUrl, prefix))
                                     .Param("keys", string.Empty);

            if (!string.IsNullOrEmpty(separator))
            {
                queryApiUriBuilder.Param("separator", separator);
            }

            var response = await httpClient.GetAsync(queryApiUriBuilder.buildApiUri()).ConfigureAwait(false);

            return(await response.formatConsulApiQueryResultAsync <string[]>().ConfigureAwait(false));
        }
示例#5
0
        /// <summary>
        /// 查询服务
        /// </summary>
        /// <param name="serviceName">服务名称</param>
        /// <param name="tag">服务标签</param>
        /// <param name="passingOnly">只返回健康的服务</param>
        /// <returns></returns>
        public async Task <ConsulApiQueryResult <ConsulServiceEntry[]> > healthServiceAsync(string name, string tag, bool passingOnly)
        {
            var queryHealthServiceApiUrl = string.Format("{0}/{1}", healthServiceApiUrl, name);

            var queryApiUriBuilder = new ConsulApiUriBuilder().Path(queryHealthServiceApiUrl);

            if (!string.IsNullOrEmpty(tag))
            {
                queryApiUriBuilder.Param("tag", tag);
            }
            if (passingOnly)
            {
                queryApiUriBuilder.Param("passing", string.Empty);
            }
            var response = await httpClient.GetAsync(queryApiUriBuilder.buildApiUri()).ConfigureAwait(false);

            return(await response.formatConsulApiQueryResultAsync <ConsulServiceEntry[]>().ConfigureAwait(false));
        }
示例#6
0
        /// <summary>
        /// 上传key/value信息
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="addOnly">是否只添加</param>
        /// <param name="flags">自定义标识</param>
        /// <returns></returns>
        public async Task <ConsulApiWriteResult <bool> > kvPutAsync(string key, string value, bool addOnly, ulong flags)
        {
            var putApiUriBuilder = new ConsulApiUriBuilder().Path(string.Format("{0}/{1}", kvApiUrl, key));

            if (addOnly)
            {
                putApiUriBuilder.Param("cas", "0");
            }
            if (flags > 0)
            {
                putApiUriBuilder.Param("flags", flags.ToString());
            }

            var content  = new StringContent(value, Encoding.UTF8);
            var response = await httpClient.PutAsync(putApiUriBuilder.buildApiUri(), content).ConfigureAwait(false);

            return(await response.formatConsulApiWriteResultAsync <bool>().ConfigureAwait(false));
        }