By the first three articles, the successful launch of the consul, registration service, then the question is, anyone can register also how to play, there must be a safety mechanism.
The gameplay is simple acl add a toekn, added that how?
Step one: Start configuration commands
dir--config : file configuration file folders, all of which will be read * .json data format, the file name must be the end of .json oh.
consul agent -bootstrap-expect 1 -server -data -dir D: \ HNHPC \ microcells frame \ consul_1.5.3_windows_amd64 \ consul -node = 192.168.1.161 -bind = 192.168.1.161 -enable-script-checks = true -datacenter 0.0.0.0 -ui hnhpc -client = = -config the dir- D: \ HNHPC \ microcells frame \ consul_1.5.3_windows_amd64 \ config
Step Two: In ConsulManager class, fill in the value of the master [245d0a09-7139-bbea-aadc-ff170a0562b1]
The third step: Start the core by the system toekn registration, no token of limiting the registration.
using Consul; using System; using System.Collections.Generic; using System.Text; namespace Core.Consul { internal class ConsulManager { private static ConsulClient client = null; static ConsulManager() { if (client == null) { var address = ServiceManagerSection.Instance.Address; ConsulClientConfiguration config = new ConsulClientConfiguration() { Address = new Uri(address), Token = "245d0a09-7139-bbea-aadc-ff170a0562b1" }; client = new ConsulClient(config); } } private ConsulManager() { } /// <summary> /// 注册服务 /// </summary> public static void Register(string name, string address, int port, string checkHttpAddress = "") { var service = new AgentServiceRegistration(); service.Name = name; service.Address = address; service.Port = port; service.ID = address + ":" + port; AgentServiceCheck checkHttp = new AgentServiceCheck(); if (string.IsNullOrEmpty(checkHttpAddress)) { if (port == 80) { checkHttpAddress = string.Format("http://{0}/default/test", address); } else { checkHttpAddress = string.Format("http://{0}:{1}/default/test", address, port); } } checkHttp.HTTP = checkHttpAddress; checkHttp.Interval = new TimeSpan(0, 0, 10); checkHttp.DeregisterCriticalServiceAfter = new TimeSpan(0, 0, 120); service.Checks = new List<AgentServiceCheck>() { checkHttp }.ToArray(); var result = client.Agent.ServiceRegister(service).Result; if (result.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception($@"注册{address}-{name}服务失败"); } } public static void Remove(string address, int port) { string id = address + ":" + port; client.Agent.ServiceDeregister(id); } public static Dictionary<string, AgentService> FindAll() { var result = client.Agent.Services().Result.Response; return result; } } }
{ "acl" : { "enabled" : true, "default_policy" : "deny", "down_policy" : "extend-cache", "tokens" :{ "master": "245d0a09-7139-bbea-aadc-ff170a0562b1" } } }
其实还有更多的玩法,大家可以启动之后一个一个的设置来玩玩