当前位置:网站首页>Basic use and cluster construction of consult

Basic use and cluster construction of consult

2022-06-25 10:05:00 @@Mr.Fu

One 、 Concept of registry

  • Concept
    Be able to register the micro service address 【ip: port 】 The component of is the registry .
    Pictured :
     Insert picture description here

  • Purpose
    Ensure the dynamic scalability of microservices .

Two 、 The usage scenario of the registry

  • scene
    The main scenario is to use... In microservices .
    Pictured :
     Insert picture description here

3、 ... and 、 Technical selection of the registration center

  • type
    • zookeeper
    • consul
    • etcd
    • eureka
  • characteristic
    FeatureConsulzookeeperetcdeuerka
    Service health check Service status , Memory , Hard disk, etc. ( weak ) A long connection ,keepalive Connect heartbeat With support
    Multi-data center Support ---
    kv Storage service Support Support Support -
    Uniformity raftpaxosraft-
    capcacpcpap
    Use interface ( Multilingualism ) Support http and dns client http/grpchttp(sidecar)
    watch Support Total quantity / Support long polling Support Support long pollingpolling Support long polling/ Most of the increments
    Self monitoring metrics-metricsmetrics
    Security acl /httpsaclhttps Support ( weak )-
    spring cloud Integrate Support Support Support Support
  • Consul Three concepts of
    • server: Store the microservice address 【ip: port 】
    • client: Connect Consul
    • agent: Background services
  • Application architecture integration principle
    Pictured :
     Insert picture description here

Four 、 The project of the registration center has landed

  • Tools
    • Consul
      Download address of online disk :
      link :https://pan.baidu.com/s/1zN4PiFVuNAPysMabKdDkzg
      Extraction code :lnk7
    • demo project
  • step
    • Consul Run the command

       #  Start and run in the root directory  
        consul.exe agent -dev   # Start the command in development mode 
        consul agent -server -bootstrap-expect [ Amount of node data ]  -data-dir d:/consul/data  [ Data storage address ]
      

      The operation results are as follows :
       Insert picture description here
       Insert picture description here

    • demo project

      • nuget install

          Consul
        
      • appsettings.json Add configuration information

          {
                  
            ......
            "ConsulRegistry": {
                  
              "Name": "testService",
              "RegistryAddress": "http://127.0.0.1:8500",
              "HealthCheckAddress": "/HealthCheck"
            }
          } 
        
      • Add configuration entity class , adopt json File mapping to entity configuration class

          public class ServiceRegistryConfig
          {
              /// <summary>
              ///  service ID
              /// </summary>
              public string Id {get;set;}
              /// <summary>
              ///  The service name 
              /// </summary>
              public string Name {get;set;}   
              /// <summary>
              ///  Service Tag 
              /// </summary>
              public string[] Tags { get; set; }
              /// <summary>
              ///  Service address 
              /// </summary>
              public string Address { get; set; }
              /// <summary>
              ///  Service port number 
              /// </summary>
              public int Port { get; set; }
              /// <summary>
              ///  Service registration address 
              /// </summary>
              public string RegistryAddress { get; set; }
              /// <summary>
              ///  Service health check address 
              /// </summary>
              public string HealthCheckAddress { get; set; } 
            }
        
      • Microservice registration

        • Create a service registration implementation class
             public class ServiceRegistry : IServiceRegistry
              {
                  public void Register(ServiceRegistryConfig serviceRegistryConfig)
                  {
                      //1  establish consul Connection of the client 
                      var consulClient = new ConsulClient(configuration =>
                      {
                          configuration.Address = new Uri(serviceRegistryConfig.RegistryAddress);
                      });
                      //2  establish  consul Service registration object 
                      var registration = new AgentServiceRegistration()
                      {
                          ID = serviceRegistryConfig.Id,
                          Name = serviceRegistryConfig.Name,
                          Address = serviceRegistryConfig.Address,
                          Port = serviceRegistryConfig.Port,
                          Tags = serviceRegistryConfig.Tags,
                          // health examination 
                          Check = new AgentServiceCheck()
                          {
                              // Set health check timeout 
                              Timeout = TimeSpan.FromSeconds(10),
                              // Service stopped 5 Log out of service in seconds 
                              DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(50),
                              // Health examination address 
                              HTTP = serviceRegistryConfig.HealthCheckAddress,
                              // Interval between health checks 
                              Interval = TimeSpan.FromSeconds(10)
                          }
                      };
                      // Service registration 
                      consulClient.Agent.ServiceRegister(registration);
                      // Close the connection and free up resources 
                      consulClient.Dispose(); 
                  }
                   /// <summary>
                   ///  Registry logout 
                   /// </summary>
                   /// <param name="serviceRegistryConfig"></param>
                  public void Deregister(ServiceRegistryConfig serviceRegistryConfig) 
                  {
                   var consulClient = new ConsulClient(configuration => {
                       configuration.Address = new Uri(serviceRegistryConfig.Address);
                   });
                      consulClient.Agent.ServiceDeregister(serviceRegistryConfig.Id);
                  }
              }
          
        • Create a service registration interface class
              public interface IServiceRegistry
              {
                  void Register(ServiceRegistryConfig serviceRegistryConfig);
                  void Deregister(ServiceRegistryConfig serviceRegistryConfig);
              }
          
        • Create a service registration extension class
          //consul Service registration class 
              public static class ConsulExtensions
              {
                 /// <summary>
                 ///  Registry extension 
                 /// </summary>
                 /// <param name="services"></param>
                 /// <param name="Configuration"></param>
                 /// <returns></returns>
                  public static IServiceCollection AddConsul(this IServiceCollection services, IConfiguration Configuration) 
                  {
                      // take json Mapping files to entity classes 
                      services.Configure<ServiceRegistryConfig>(Configuration.GetSection("ConsulRegistry"));
                      services.AddSingleton<IServiceRegistry, ServiceRegistry>();
                      return services; 
                  }
              }
             //  Register the service when the service starts 
             public static class ConsulApplicationBuilderExtension
              {
                  public static IApplicationBuilder UseConsulRegistry(this IApplicationBuilder app)
                  {
                      // Get configuration information 
                      var serviceNode = app.ApplicationServices.GetRequiredService<IOptions<ServiceRegistryConfig>>().Value;
                      // Get lifecycle 
                      var lifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
                      // Get the strength of service registration 
                      var consulRegistry = app.ApplicationServices.GetRequiredService<IServiceRegistry>();
                      // Get service address information 
                      var features = app.Properties["server.Features"] as FeatureCollection;
                      var address = features.Get<IServerAddressesFeature>().Addresses.First();
                      var uri = new Uri(address);
                      // Service registration 
                      serviceNode.Id = Guid.NewGuid().ToString();
                      serviceNode.Address = $"{uri.Scheme}://{ uri.Host}";
                      serviceNode.Port = uri.Port;
                      serviceNode.HealthCheckAddress = $"{uri.Scheme}://{uri.Host}:{uri.Port}{serviceNode.HealthCheckAddress}";
                      consulRegistry.Register(serviceNode);
                      // Log off the service when the server is shut down 
                      lifetime.ApplicationStopping.Register(() => {
                          consulRegistry.Deregister(serviceNode);
                      });
                      return app;
                  }
              }
           // stay startup Class ConfigureServices  Method  
              services.AddConsul(Configuration);
           // stay startup Class Configure  Method start service 
              app.UseConsulRegistry();
          
          The operation results are as follows :
           Insert picture description here
      • Microservice discovery

        • stay appsettings.json Add... To the file Consul Registered address configuration
               ......
                  "ConsulDiscoverys": {
                         "RegistryAddress": "http://127.0.0.1:8500"
                        }
              .....
          
        • New configuration class
               public class ServiceDiscoveryConfig
              {
                  public string RegistryAddress { get; set; }
              }
          
        • New service discovery interface class
               public interface IConsulDiscovery
              {
                  Task<List<ServiceUrl>> Discovery(string  ServiceName);
              }  
          
        • New service discovery implementation class
              public class ConsulDiscovery : IConsulDiscovery
              {
                  private readonly IConfiguration Configuration;
                  public ConsulDiscovery(IConfiguration _Configuration) {
                      Configuration = _Configuration;
                  }
                  /// <summary>
                  ///  Service discovery  
                  /// </summary>
                  /// <param name="ServiceName"> The service name </param>
                  /// <returns> Return to a collection </returns>
                  public async Task<List<ServiceUrl>> Discovery(string ServiceName)
                  {
                      List<ServiceUrl> list = new List<ServiceUrl>();
                      ServiceDiscoveryConfig serviceDiscoveryConfig = new ServiceDiscoveryConfig();
                      serviceDiscoveryConfig = Configuration.GetSection("ConsulDiscoverys").Get< ServiceDiscoveryConfig >();
                      //1  Establishing a connection 
                      var consulClient = new ConsulClient(Options => {
                          Options.Address = new Uri(serviceDiscoveryConfig.RegistryAddress);
                      });
                      // 2  Get the registered service address according to the service name 
                      var queryResult = await  consulClient.Catalog.Service("ServiceName");
                      // 3  Inject the registered address into the collection 
                      foreach (var item in queryResult.Response)
                      {
                          list.Add(new ServiceUrl() { Url = item.Address+":"+ item.ServicePort });
                      }
                      return list;
                  }
              }
          
        • Add an extension of discovery service in the extension class
              public static IServiceCollection AddConsulDiscovery(this IServiceCollection services) 
              {
                  services.AddSingleton<IConsulDiscovery, ConsulDiscovery>();
                  return services;
              }
          
        • stay Startup class ConfigureServices method
            // Service discovery 
            services.AddConsulDiscovery();
          
        • Inject the interface of service discovery into the controller constructor
              private readonly IConsulDiscovery consulDiscovery;
              public HomeController(IConsulDiscovery _consulDiscovery) 
              { 
                  consulDiscovery = _consulDiscovery;
              }
              .....
              // Then, in other methods, get the service address according to the service name ; Be careful : The return value is a collection .
          

5、 ... and 、 Registry Center Consul High availability

  • Cluster building 【 It's better to have 3 An example 】
    • step

      • node 1
         # 1、 New configuration file , route :D:/Assembly/Consul/node1/basic.json
         {  "datacenter": "dc1",  "data_dir": "D:/Assembly/Consul/node1",  "log_level": "INFO",  "server": true,  "node_name": "node1",  "ui": true,  "bind_addr": "127.0.0.1",  "client_addr": "127.0.0.1",  "advertise_addr": "127.0.0.1",  "bootstrap_expect": 3,  "ports":{    "http": 8500,    "dns": 8600,    "server": 8300,    "serf_lan": 8301,    "serf_wan": 8302}}
         # 2、 Run the command 
         # 2、 Switch to consul The root directory :
         consul.exe agent -config-file=D:/Assembly/Consul/node1/basic.json
        
      • node 2
         # 1、 New configuration file , route :D:/Assembly/Consul/node2/basic.json
         {  "datacenter": "dc1",  "data_dir": "D:/Assembly/Consul/node2",  "log_level": "INFO",  "server": true,  "node_name": "node2",  "bind_addr": "127.0.0.1",  "client_addr": "127.0.0.1",  "advertise_addr": "127.0.0.1",  "ports":{    "http": 8510,    "dns": 8610,    "server": 8310,    "serf_lan": 8311,    "serf_wan": 8312}} 
          # 2、 Switch to consul The root directory :
          consul.exe agent -config-file=D:/Assembly/Consul/node2/basic.json -retry-join=127.0.0.1:8301
        
      • node 3
         # 1、 New configuration file , route : File address :D:/Assembly/Consul/node3/basic.json
         {  "datacenter": "dc1",  "data_dir": "D:/Assembly/Consul/node3",  "log_level": "INFO",  "server": true,  "node_name": "node3",  "bind_addr": "127.0.0.1",  "client_addr": "127.0.0.1",  "advertise_addr": "127.0.0.1",  "ports":{    "http": 8520,    "dns": 8620,    "server": 8320,    "serf_lan": 8321,    "serf_wan": 8322}}
         # 2、 Switch to consul The root directory :
          consul.exe agent -config-file=D:/Assembly/Consul/node3/basic.json -retry-join=127.0.0.1:8301
        
        The operation result is as shown in the figure :
         Insert picture description here
         Insert picture description here
         Insert picture description here
         Insert picture description here
    • Look at the master node

       # Switch to consul The root directory 
       consul.exe info
      
    • Cluster setup command

          -bind: Bind an address for the node 
          -enable-script-checks=true: Set the check service to available 
          -join: Join an existing cluster 
          -server  Indicates the currently used server Pattern 
          -node: Specify the name of the current node in the cluster  
          -config-file -  Configuration file to load 
          -config-dir: Specify profile , Define the , Default to all .json The end of the file will be read 
          -datacenter:  The data center has no name , If it is not set, it defaults to dc
          -client:  Client mode 
          -ui:  Use consul Self contained ui Interface  
          -data-dir consul Directory where data is stored 
          -bootstrap: To control a server Whether in bootstrap Pattern , In a datacenter There can only be one of them server be in bootstrap Pattern , When one server be in bootstrap Mode time , You can vote for yourself raft leader.
          -bootstrap-expect: In a datacenter We hope to provide server Number of nodes , When the value is provided ,consul Wait until the designated sever The number will guide the whole cluster , The mark cannot be used with bootstrap public 
          
           These two parameters are very important ,  A choice , If two parameters are not used , Will appear even if you use join take agent If you join the cluster, you will still report  
          2018/10/14 15:40:00 [ERR] agent: failed to sync remote state: No cluster leader
      
    • Profile parameters

          ui:  amount to -ui  Command line flags .
          acl_token:agent Will use this token and consul server For the request 
          acl_ttl: control TTL Of cache, The default is 30s
          addresses: A nested object , You can set the following key:dns、http、rpc
          advertise_addr: Equate to -advertise
          bootstrap: Equate to -bootstrap
          bootstrap_expect: Equate to -bootstrap-expect
          bind_addr: Equate to -bindca_file: Provide CA File path , Used to check the link of the client or server 
          cert_file: It has to be with key_file Together 
          check_update_interval:
          client_addr: Equate to -client
          datacenter: Equate to -dc
          data_dir: Equate to -data-dir
          disable_anonymous_signature: Anonymous signatures are prohibited during update checks 
          enable_debug: Turn on debug Pattern 
          enable_syslog: Equate to -syslog
          encrypt: Equate to -encrypt
          key_file: Provide the path of the private key 
          leave_on_terminate: The default is false, If true, When agent Receive a TERM When it's a signal , It will send leave Information to other nodes in the cluster .
          log_level: Equate to -log-level node_name: Equate to -node 
          ports: This is a nested object , You can set the following key:dns(dns Address :8600)、http(http api Address :8500)、rpc(rpc:8400)、serf_lan(lan port:8301)、serf_wan(wan port:8302)、server(server rpc:8300) 
          protocol: Equate to -protocol
          rejoin_after_leave: Equate to -rejoin
          retry_join: Equate to -retry-join
          retry_interval: Equate to -retry-interval 
          server: Equate to -server
          syslog_facility: When enable_syslog After being provided , This parameter controls which level of information is sent , Default Local0
          ui_dir: Equate to -ui-dir
      
原网站

版权声明
本文为[@@Mr.Fu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250915500886.html