第四篇,新手注冊Consul 服務帶token


通過前三篇文章,成功啟動了consul,注冊了服務,那么問題來了,誰都可以注冊還怎么玩,必須要有一個安全的機制。

簡單的玩法就是acl加一個toekn,那怎么加了?

 

第一步: 啟動配置命令

-config-dir :配置文件的文件夾,將讀取里面所有的*.json 格式數據,文件名必須是.json結尾哦。

 

consul agent -bootstrap-expect 1 -server -data-dir D:\HNHPC\微服務框架\consul_1.5.3_windows_amd64\consul -node=192.168.1.161 -bind=192.168.1.161  -enable-script-checks=true -datacenter=hnhpc -client=0.0.0.0 -ui -config-dir D:\HNHPC\微服務框架\consul_1.5.3_windows_amd64\config

 

 

第二步:在ConsulManager類中,填寫 master的 值【245d0a09-7139-bbea-aadc-ff170a0562b1】

第三步:啟動core系統就通過toekn注冊了,沒有token的就限制了注冊。

 

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"
    }
  }
}

其實還有更多的玩法,大家可以啟動之后一個一個的設置來玩玩

 

不懂可以掃二維碼交流

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM