可以在启动时初始化 Consul 键值存储吗?
Possible to initialise Consul Key Value store at startup?
我可以在启动时使用 /consul/config 目录初始化 Consul 服务。当我启动 Consul 容器时,我希望能够将我的应用程序设置初始化到 Consul kv 存储中。这可能吗?
有几个项目可能对这种情况感兴趣:
我想要一种简单的方法来在 Consul 中为微服务项目设置应用程序设置。我确信有许多现有工具,但我想要一些轻量级且适合我的开发过程的工具。因此,我没有尝试使用@mgyongyosi 的一些建议,而是编写了一个快速的 dotnet 控制台应用程序来完成(不是那么)繁重的工作。在项目根目录下我创建了一个目录结构Consul/kv
。该路径下的子目录表示 Consul KV 存储中的键,叶子上的 json
文件表示应用程序设置。请注意,每个叶目录预计只有一个 json
文件。该应用程序使用 Consul.NET nuget 包与 Consul 通信。
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Consul;
namespace ConfigureConsulKv
{
class Program
{
private static readonly string _basePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"Projects", "MyProject", "Consul", "kv");
static void Main(string[] args)
{
RecurseDirectory(_basePath);
}
static void RecurseDirectory(string path)
{
foreach (var dir in Directory.EnumerateDirectories(path)) RecurseDirectory(dir);
var file = Directory.EnumerateFiles(path, "*.json").FirstOrDefault();
if (!string.IsNullOrWhiteSpace(file))
{
var key = path.Substring(_basePath.Length + 1);
var json = File.ReadAllBytes(file);
Console.WriteLine($"key {key} file {file}");
using (var client = new ConsulClient())
{
var attempt = client.KV.Put(new KVPair(key) { Value = json }).Result;
Console.WriteLine($"status {attempt.StatusCode}");
}
}
}
}
}
我可以在启动时使用 /consul/config 目录初始化 Consul 服务。当我启动 Consul 容器时,我希望能够将我的应用程序设置初始化到 Consul kv 存储中。这可能吗?
有几个项目可能对这种情况感兴趣:
我想要一种简单的方法来在 Consul 中为微服务项目设置应用程序设置。我确信有许多现有工具,但我想要一些轻量级且适合我的开发过程的工具。因此,我没有尝试使用@mgyongyosi 的一些建议,而是编写了一个快速的 dotnet 控制台应用程序来完成(不是那么)繁重的工作。在项目根目录下我创建了一个目录结构Consul/kv
。该路径下的子目录表示 Consul KV 存储中的键,叶子上的 json
文件表示应用程序设置。请注意,每个叶目录预计只有一个 json
文件。该应用程序使用 Consul.NET nuget 包与 Consul 通信。
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Consul;
namespace ConfigureConsulKv
{
class Program
{
private static readonly string _basePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"Projects", "MyProject", "Consul", "kv");
static void Main(string[] args)
{
RecurseDirectory(_basePath);
}
static void RecurseDirectory(string path)
{
foreach (var dir in Directory.EnumerateDirectories(path)) RecurseDirectory(dir);
var file = Directory.EnumerateFiles(path, "*.json").FirstOrDefault();
if (!string.IsNullOrWhiteSpace(file))
{
var key = path.Substring(_basePath.Length + 1);
var json = File.ReadAllBytes(file);
Console.WriteLine($"key {key} file {file}");
using (var client = new ConsulClient())
{
var attempt = client.KV.Put(new KVPair(key) { Value = json }).Result;
Console.WriteLine($"status {attempt.StatusCode}");
}
}
}
}
}