当前位置:网站首页>Basic use and cluster construction of consult
Basic use and cluster construction of consult
2022-06-25 10:05:00 【@@Mr.Fu】
List of articles
One 、 Concept of registry
Concept
Be able to register the micro service address 【ip: port 】 The component of is the registry .
Pictured :
Purpose
Ensure the dynamic scalability of microservices .
Two 、 The usage scenario of the registry
scene
The main scenario is to use... In microservices .
Pictured :
3、 ... and 、 Technical selection of the registration center
- type
- zookeeper
- consul
- etcd
- eureka
- characteristic
Feature Consul zookeeper etcd euerka 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 raft paxos raft - cap ca cp cp ap Use interface ( Multilingualism ) Support http and dns client http/grpc http(sidecar) watch Support Total quantity / Support long polling Support Support long polling polling Support long polling/ Most of the increments Self monitoring metrics - metrics metrics Security acl /https acl https 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 :
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
- Consul
- 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 :


demo project
nuget install
Consulappsettings.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
The operation results are as follows ://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();
- Create a service registration implementation class
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 .
- stay appsettings.json Add... To the file Consul Registered address configuration
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
The operation result is as shown in the figure :# 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



- node 1
Look at the master node
# Switch to consul The root directory consul.exe infoCluster 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 leaderProfile 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
边栏推荐
- Free platform for wechat applet making, steps for wechat applet making
- Gradle download warehouse is slow
- [MySQL learning notes 22] index
- js工具函数,自己封装一个节流函数
- PHP obtains the IP address, and the apache2 server runs without error
- SQL to object thinking vs SQL of object thinking
- 瑞萨RA系列-开发环境搭建
- ShardingSphere-Proxy 4.1 分库分表
- Ruiji takeout project (II)
- What functions should smart agriculture applet system design have
猜你喜欢

Nano data World Cup data interface, CSL data, sports data score, world cup schedule API, real-time data interface of football match

Reza RA series - development environment construction

Ruiji takeout project (II)

纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口

Etcd tutorial - Chapter 4 etcd cluster security configuration

Flutter dialog: cupertinoalertdialog

ShardingSphere-Proxy 5.0 分库分表(一)

Why should the terminal retail industry choose the member management system

Mengyou Technology: tiktok live broadcast with goods elements hot topics retention skills shaping image highlight selling points

How to "transform" small and micro businesses (I)?
随机推荐
Match a mobile number from a large number of mobile numbers
CyCa 2022 children's physical etiquette primary teacher class Shenzhen headquarters station successfully concluded
How to do the wechat selling applet? How to apply for applets
Android database security: after the user exits, the transaction rollback log still stores relevant data information
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
Computational Thinking and economic thinking
Cubemx stm32f105rb USB flash drive reading and writing detailed tutorial
How to make small programs on wechat? How to make small programs on wechat
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
Force buckle -104 Maximum depth of binary tree
(forwarding articles) after skipping multiple pages, shuttle returns to the first page and passes parameters
What are the PMP scores?
manhattan_slam环境配置
How do dating applets make millions a year? What is the profit model?
Solution to the problem of repeated startup of esp8266
Kotlin advanced set
Oracle function trigger
Wechat official account can reply messages normally, but it still prompts that the service provided by the official account has failed. Please try again later
Methodchannel of flutter
How do wechat sell small commodity programs do? How to open wechat apps to sell things?