In distributed applications, it is often necessary to modify configuration properties, such as updating database connection strings and updating cache addresses. If there are not many applications, you can manually modify it, once there are many applications, distributed configuration is very important, this article explains the Key/Value storage architecture of .net/c# based on Consul to implement distributed configuration.
Install Consul
Brief introduction
Consul is a service discovery and registration tool that is distributed and highly scalable.
Consul mainly includes the following features:
- Service discovery: Support for service registration and discovery for both HTTP and DNS protocols.
- Monitoring Checks: Supports multiple health checks.
- Key/Value Storage: Supports distributed KV data storage through HTTP API.
- Multi-data center support: Any number of data centers are supported.
Installation
Download Address:
The hyperlink login is visible.
This article uses Windows 10 x64 as a test environment.
After downloading and unzipping, run it using the cmd command, the code is as follows:
dev means running in development mode, and the -client parameter can be used to specify what IP is allowed to be accessed by the client, for example, -client 127.0.0.1 means it can be used, and in development mode, data will not be persisted.
(dev mode, cluster mode is recommended for production environments)
Access:http://127.0.0.1:8500/ui/dc1/servicesSee below:
KV interface
API Documentation Address:
The hyperlink login is visible.
First, I use an online web page and create a new key/value pair.
Get the value via the GET request /v1/kv/:key path.
http://127.0.0.1:8500/v1/kv/test
[ { "LockIndex": 0, "Key": "test", "Flags": 0, "Value": "aXRzdnNlLmNvbQ==", "CreateIndex": 194, "ModifyIndex": 194 }
]
- CreateIndex is an internal index value that represents when the entry was created.
- ModifyIndex is the last index to modify this key. This index corresponds to the header value returned by X-Consul-Index in the response, and can be set by ? index query parameter is used to establish blocking queries. You can even perform blocking queries against the entire subtree of the KV store: what if? recurse provides a query, and the returned value X-Consul-Index corresponds to the latest value of ModifyIndex in the prefix, using the blocking query ?index will wait until any key in that prefix is updated.
- LockIndex is the number of times the key in the lock has been successfully acquired. If the lock is held, the Session key will provide the session that owns the lock.
- The key is just the full path of the entry.
- Flags are opaque unsigned integers that can be attached to each entry. Customers can choose to use this option, but it makes sense for their application.
- Value is a base64 encoded blob of data.
.NET/C# Key/Value read and write
First, create a .NET 4.6.1 project and use nuget to install the necessary packages, as follows:
The code is as follows:
The renderings are as follows:
- The key key is case-sensitive
- Duplicate key, value values will be overwritten
(End) |