当前位置:首页 > Windows程序 > 正文

使用C# 和Consul进行分布式系统协调

2021-03-27 Windows程序

标签:

使用C# 和Consul进行分布式系统协调

随着大数据时代的到来,分布式是解决大数据问题的一个主要手段,随着越来越多的分布式的服务,,如何在分布式的系统中对这些服务做协调变成了一个很棘手的问题。今天我们就来看看如何使用C# ,利用开源对分布式服务做协调。

在对分布式的应用做协调的时候,主要会碰到以下的应用场景:

业务发现(service discovery)

找到分布式系统中存在那些可用的服务和节点

名字服务 (name service)

通过给定的名字知道到对应的资源

配置管理 (configuration management)

如何在分布式的节点中共享配置文件,保证一致性。

故障发现和故障转移 (failure detection and failover)

当某一个节点出故障的时候,如何检测到并通知其它节点, 或者把想用的服务转移到其它的可用节点

领导选举(leader election)

如何在众多的节点中选举一个领导者,来协调所有的节点

分布式的锁 (distributed exclusive lock)

如何通过锁在分布式的服务中进行同步

消息和通知服务 (message queue and notification)

如何在分布式的服务中传递消息,以通知的形式对事件作出主动的响应

Consul

Consul是用Go开发的分布式服务协调管理的工具,它提供了服务发现,健康检查,Key/Value存储等功能,并且支持跨数据中心的功能。consul提供的一些关键特性:

service discovery:consul通过DNS或者HTTP接口使服务注册和服务发现变的很容易,一些外部服务,例如saas提供的也可以一样注册。

health checking:健康检测使consul可以快速的告警在集群中的操作。和服务发现的集成,可以防止服务转发到故障的服务上面。

key/value storage:一个用来存储动态配置的系统。提供简单的HTTP接口,可以在任何地方操作。

multi-datacenter:无需复杂的配置,即可支持任意数量的区域。

Consul基于HTTP的API可以方便的和各种语言进行绑定,C# 语言绑定https://github.com/PlayFab/consuldotnet

Consul在Cluster上的每一个节点都运行一个Agent,这个Agent可以使用Server或者Client模式。Client负责到Server的高效通信,相对为无状态的。 Server负责包括选举领导节点,维护cluster的状态,对所有的查询做出响应,跨数据中心的通信等等。

consul官网已经有编译好的二进制包,支持各种平台:win、linux等等,下载符合你平台的软件包:在这里,下载包:0.5.2_windows_386.zip。解压完毕后只有一个consul文件。

D:\GitHub\consuldotnet\Consul.Test>consul

usage: consul [--version] [--help] <command> [<args>]

Available commands are:

agent Runs a Consul agent

configtest Validate config file

event Fire a new event

exec Executes a command on Consul nodes

force-leave Forces a member of the cluster to enter the "left" state

info Provides debugging information for operators

join Tell Consul agent to join cluster

keygen Generates a new encryption key

keyring Manages gossip layer encryption keys

leave Gracefully leaves the Consul cluster and shuts down

lock Execute a command holding a lock

maint Controls node or service maintenance mode

members Lists the members of a Consul cluster

monitor Stream logs from a Consul agent

reload Triggers the agent to reload configuration files

version Prints the Consul version

watch Watch for changes in Consul

consul安装完毕后,agent就可以启动了,agent可以运行在server或者client模式,每个数据中心至少有一个agent运行在server模式,一般建议是3或者5个server。部署单个server是非常不好的,因为在失败场景中出现数据丢失是不可避免的。本文涵盖的是创建一个新的数据中心,所有其他的agents都运行在client模式,这是一个非常轻量级的服务注册进程,它会运行健康监测,并将查询结果转发到服务。agent必须运行在集群中的每一个节点上。

consul.exe agent -config-file test_config.json

我们先运行一个agent在server模式:

D:\GitHub\consuldotnet\Consul.Test> consul.exe agent -config-file test_config.json

==> WARNING: Bootstrap mode enabled! Do not enable unless necessary

==> WARNING: Windows is not recommended as a Consul server. Do not use in production.

==> WARNING: It is highly recommended to set GOMAXPROCS higher than 1

==> Starting Consul agent...

==> Starting Consul agent RPC...

==> Consul agent running!

Node name: ‘GEFFZHANG-NB‘

Datacenter: ‘dc1‘

Server: true (bootstrap: true)

Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)

Cluster Addr: 192.168.1.4 (LAN: 8301, WAN: 8302)

Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false

Atlas: <disabled>

==> Log data will now stream in as it occurs:

2015/08/09 09:14:48 [INFO] serf: EventMemberJoin: GEFFZHANG-NB 192.168.1.4

2015/08/09 09:14:48 [INFO] serf: EventMemberJoin: GEFFZHANG-NB.dc1 192.168.1.4

2015/08/09 09:14:48 [INFO] raft: Node at 192.168.1.4:8300 [Follower] entering Follower state

2015/08/09 09:14:48 [INFO] consul: adding server GEFFZHANG-NB (Addr: 192.168.1.4:8300) (DC: dc1)

2015/08/09 09:14:48 [INFO] consul: adding server GEFFZHANG-NB.dc1 (Addr: 192.168.1.4:8300) (DC: dc1)

2015/08/09 09:14:48 [ERR] agent: failed to sync remote state: No cluster leader

2015/08/09 09:14:50 [WARN] raft: Heartbeat timeout reached, starting election

2015/08/09 09:14:50 [INFO] raft: Node at 192.168.1.4:8300 [Candidate] entering Candidate state

2015/08/09 09:14:50 [DEBUG] raft: Votes needed: 1

2015/08/09 09:14:50 [DEBUG] raft: Vote granted. Tally: 1

2015/08/09 09:14:50 [INFO] raft: Election won. Tally: 1

2015/08/09 09:14:50 [INFO] raft: Node at 192.168.1.4:8300 [Leader] entering Leader state

2015/08/09 09:14:50 [INFO] consul: cluster leadership acquired

2015/08/09 09:14:50 [INFO] consul: New leader elected: GEFFZHANG-NB

2015/08/09 09:14:50 [INFO] raft: Disabling EnableSingleNode (bootstrap)

2015/08/09 09:14:50 [DEBUG] raft: Node 192.168.1.4:8300 updated peer set (2): [192.168.1.4:8300]

2015/08/09 09:14:50 [DEBUG] consul: reset tombstone GC to index 2

2015/08/09 09:14:50 [INFO] consul: member ‘GEFFZHANG-NB‘ joined, marking health alive

2015/08/09 09:14:51 [INFO] agent: Synced service ‘consul‘

2015/08/09 09:16:03 [DEBUG] agent: Service ‘consul‘ in sync

2015/08/09 09:17:30 [DEBUG] agent: Service ‘consul‘ in sync

2015/08/09 09:18:38 [DEBUG] agent: Service ‘consul‘ in sync

2015/08/09 09:19:47 [DEBUG] http: Request /v1/status/peers (0)

2015/08/09 09:19:52 [DEBUG] agent: Service ‘consul‘ in sync

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/68647.html