Exemplo n.º 1
0
        /// <summary>
        /// 设置配置信息
        /// </summary>
        /// <typeparam name="T">consul配置信息类型</typeparam>
        /// <param name="consulIP">consul服务IP</param>
        /// <param name="consulPort">consul服务端口</param>
        /// <param name="config">consul配置信息</param>
        /// <param name="prefixKey">key前缀,一般用来区分不同服务</param>
        /// <returns></returns>
        public static bool PutConfig <T>(string consulIP, int consulPort, T config, string prefixKey) where T : class
        {
            //配置信息字典
            var dicConfig = new Dictionary <string, object>();

            Type type = typeof(T);
            //获取对象属性
            var pros = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            //找出有配置特性的属性,并把属性对应放入字典
            foreach (var pro in pros)
            {
                //特性
                var attr = pro.GetCustomAttributes <ConsulConfigAttribute>().FirstOrDefault();
                if (attr == null)
                {
                    continue;
                }

                dicConfig[attr.Key] = CommHelper.EmitGet(config, pro.Name);
            }

            using (var kv = new ConsulKV(consulIP, consulPort, prefixKey))
            {
                //写入consul
                return(kv.PutAll(dicConfig));
            }
        }
Exemplo n.º 2
0
        public string EmitSetGetGenericType(string name)
        {
            var model = new Model();

            CommHelper.EmitSet(model, "Name", name);

            return(CommHelper.EmitGet <Model, string>(model, "Name"));
        }
Exemplo n.º 3
0
        public string EmitSetGetObject(string name)
        {
            var model = new Model();

            CommHelper.EmitSet(model, "Name", name);

            return(CommHelper.EmitGet(model, "Name") as string);
        }