当前位置:网站首页>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
边栏推荐
- 独步武林,架构选型手册(包含 PDF)
- How to make small programs on wechat? How to make small programs on wechat
- Flutter replaces the default icon of Gaud positioning
- STM32 receives data by using idle interrupt of serial port
- Consul的基本使用与集群搭建
- 【mysql学习笔记20】mysql体系结构
- How to "transform" small and micro businesses (II)?
- Redis(二)分布式锁与Redis集群搭建
- [buuctf.reverse] 121-125
- 力扣-104. 二叉树的最大深度
猜你喜欢

i++ 和 ++i的真正区别

Rxjs TakeUntil 操作符的学习笔记

How to "transform" small and micro businesses (I)?

Learning notes of rxjs takeuntil operator

Jetpack compose layout (IV) - constraintlayout
![[MySQL learning notes 22] index](/img/08/db7b765f7ddaa5706e3f7d00690d23.png)
[MySQL learning notes 22] index

Ruiji takeout project (II)

Cubemx stm32f105rb USB flash drive reading and writing detailed tutorial

Notes on writing questions in C language -- monkeys eat peaches

How much does a small program cost? How much does a small program cost? It's clear at a glance
随机推荐
链表 删除链表中的节点
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
可穿戴设备或将会泄露个人隐私
Test Development Engineer
Best producer consumer code
PMP考试多少分算通过?
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
Methodchannel of flutter
Flutter replaces the default icon of Gaud positioning
[MySQL learning notes 22] index
I put a two-dimensional code with rainbow candy
字符串 最长公共前缀
STM32 receives data by using idle interrupt of serial port
[MySQL learning notes 20] MySQL architecture
How to apply for a widget on wechat how to get a widget on wechat
vscode试图过程写入管道不存在
js工具函数,自己封装一个节流函数
Huipay international permet au commerce électronique transfrontalier de devenir une plate - forme de paiement transfrontalière conforme!
从海量手机号中匹配某一个手机号
i++ 和 ++i的真正区别