之前一直在学习和使用基于Spring Cloud的微服务架构,但项目团队成员有部分还在使用C#技术体系,因此就动了想使用C#搭建一个微服务架构案例的念头,C#开发微服务一般使用net core或最新的net6。那么首先就是对之前Spring Cloud中用到的中间件和微服务支撑和治理的相关工具在C#中肯定也是要用到的,经过简单的分析查阅发现有些是通用的(比如redis、数据库),有些不是(比如Nacos、Eureka)。本文就以服务注册与发现为例来介绍一下,在C#中服务注册与发现的中间件工具Consul。
Consul 是一套开源的分布式服务发现和配置管理系统,由 HashiCorp 公司用 Go 语言开发。提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。它具有很多优点。包括: 基于 raft 协议,比较简洁; 支持健康检查, 同时支持 HTTP 和 DNS 协议 支持跨数据中心的 WAN 集群 提供图形界面 跨平台,支持 Linux、Mac、Windows。
官网下载地址:https://www.consul.io/downloads
1、在浏览器中输入官网下载地址,打开Consul官网,如下图:
2、因为我的电脑是Windows的,因此下载的是Amd64的,下载后是一个压缩包,如下图
3、把压缩包consul_1.12.2_windows_amd64.zip解压到指定位置(具体根据你自己的需要),这里我解压到d:\consul目录下,如下图:
1、在使用Consul之前,先了解一些Consul涉及到的一些概念
agent: 组成 consul 集群的每个成员上都要运行一个 agent,可以通过 consul agent 命令来启动。agent 可以运行在 server 状态或者 client 状态。自然的,运行在 server 状态的节点被称为 server 节点;运行在 client 状态的节点被称为 client 节点。
Client: Consul 的 Client模式,就是客户端模式。是 Consul 节点的一种模式,这种模式下,所有注册到当前节点的服务会被转发到 Server,本身是不持久化这些信息。
Server:Consul 的 Server 模式,表明这个 Consul 是个 Server ,这种模式下,功能和 Client 都一样,唯一不同的是,它会把所有的信息持久化的本地,这样遇到故障,信息是可以被保留的。
Server-Leader:Server 是它们的老大,它和其它 Server 不一样的一点是,它需要负责同步注册的信息给其它的 Server ,同时也要负责各个节点的健康监测。
raft:Server 节点之间的数据一致性保证协议使用的是 raft,而 zookeeper 用的 paxos,etcd采用的也是raft
服务发现协议:Consul 采用 http 和 DNS 协议,etcd 只支持 http 。
服务注册:Consul 支持两种方式实现服务注册,一种是通过 Consul 的服务注册 Http API,由服务自己调用 API 实现注册,另一种方式是通过 json 格式的配置文件实现注册,将需要注册的服务以 json 格式的配置文件给出。Consul 官方建议使用第二种方式。
2、再了解一下consul.exe命令的一些参数
-advertise value
Sets the advertise address to use.
-advertise-wan value
Sets address to advertise on WAN instead of -advertise address.
-allow-write-http-from value
Only allow write endpoint calls from given network. CIDR format, can be specified multiple times.
-alt-domain value
Alternate domain to use for DNS interface.
-auto-reload-config
Watches config files for changes and auto reloads the files when modified.
-bind value
Sets the bind address for cluster communication.
-bootstrap
Sets server to bootstrap mode.
-bootstrap-expect value
Sets server to expect bootstrap mode.
-check_output_max_size value
Sets the maximum output size for checks on this agent
-client value
Sets the address to bind for client access. This includes RPC, DNS, HTTP, HTTPS and gRPC (if configured).
-config-dir value
Path to a directory to read configuration files from. This will read every file ending in '.json' as configuration in this directory in alphabetical order. Can be specified multiple times.
-config-file value
Path to a file in JSON or HCL format with a matching file extension. Can be specified multiple times.
-config-format string
Config files are in this format irrespective of their extension. Must be 'hcl' or 'json'
-data-dir value
Path to a data directory to store agent state.
-datacenter value
Datacenter of the agent.
-default-query-time value
the amount of time a blocking query will wait before Consul will force a response. This value can be overridden by the 'wait' query parameter.
-dev
Starts the agent in development mode.
-disable-host-node-id
Setting this to true will prevent Consul from using information from the host to generate a node ID, and will cause Consul to generate a random node ID instead.
-disable-keyring-file
Disables the backing up of the keyring to a file.
-dns-port value
DNS port to use.
-domain value
Domain to use for DNS interface.
-enable-local-script-checks
Enables health check scripts from configuration file.
-enable-script-checks
Enables health check scripts.
-encrypt value
Provides the gossip encryption key.
-grpc-port value
Sets the gRPC API port to listen on (currently needed for Envoy xDS only).
-hcl value
hcl config fragment. Can be specified multiple times.
-http-port value
Sets the HTTP API port to listen on.
-https-port value
Sets the HTTPS API port to listen on.
-join value
Address of an agent to join at start time. Can be specified multiple times.
-join-wan value
Address of an agent to join -wan at start time. Can be specified multiple times.
-log-file value
Path to the file the logs get written to
-log-json
Output logs in JSON format.
-log-level value
Log level of the agent.
-log-rotate-bytes value
Maximum number of bytes that should be written to a log file
-log-rotate-duration value
Time after which log rotation needs to be performed
-log-rotate-max-files value
Maximum number of log file archives to keep
-max-query-time value
the maximum amount of time a blocking query can wait before Consul will force a response. Consul applies jitter to the wait time. The jittered time will be capped to MaxQueryTime.
-node value
Name of this node. Must be unique in the cluster.
-node-id value
A unique ID for this node across space and time. Defaults to a randomly-generated ID that persists in the data-dir.
-node-meta key:value
An arbitrary metadata key/value pair for this node, of the format key:value. Can be specified multiple times.
-non-voting-server
(Enterprise-only) DEPRECATED: -read-replica should be used instead
-pid-file value
Path to file to store agent PID.
-primary-gateway value
Address of a mesh gateway in the primary datacenter to use to bootstrap WAN federation at start time with retries enabled. Can be specified multiple times.
-protocol value
Sets the protocol version. Defaults to latest.
-raft-protocol value
Sets the Raft protocol version. Defaults to latest.
-read-replica
(Enterprise-only) This flag is used to make the server not participate in the Raft quorum, and have it only receive the data replication stream. This can be used to add read scalability to a cluster in cases where a high volume of reads to servers are needed.
-recursor value
Address of an upstream DNS server. Can be specified multiple times.
-rejoin
Ignores a previous leave and attempts to rejoin the cluster.
-retry-interval value
Time to wait between join attempts.
-retry-interval-wan value
Time to wait between join -wan attempts.
-retry-join value
Address of an agent to join at start time with retries enabled. Can be specified multiple times.
-retry-join-wan value
Address of an agent to join -wan at start time with retries enabled. Can be specified multiple times.
-retry-max value
Maximum number of join attempts. Defaults to 0, which will retry indefinitely.
-retry-max-wan value
Maximum number of join -wan attempts. Defaults to 0, which will retry indefinitely.
-segment value
(Enterprise-only) Sets the network segment to join.
-serf-lan-allowed-cidrs value
Networks (eg: 192.168.1.0/24) allowed for Serf LAN. Can be specified multiple times.
-serf-lan-bind value
Address to bind Serf LAN listeners to.
-serf-lan-port value
Sets the Serf LAN port to listen on.
-serf-wan-allowed-cidrs value
Networks (eg: 192.168.1.0/24) allowed for Serf WAN (other datacenters). Can be specified multiple times.
-serf-wan-bind value
Address to bind Serf WAN listeners to.
-serf-wan-port value
Sets the Serf WAN port to listen on.
-server
Switches agent to server mode.
-server-port value
Sets the server port to listen on.
-syslog
Enables logging to syslog.
-ui
Enables the built-in static web UI server.
-ui-content-path value
Sets the external UI path to a string. Defaults to: /ui/
-ui-dir value
Path to directory containing the web UI resources.
1、以开发模式启动consul.exe,如下:
consul.exe agent -dev -bind 192.168.0.2 -data-dir="D:\consul\consuldata"
-bind表示在在多网卡的计算机上用于在consul集群中标识节点的ip地址。
2、在浏览器中输入http://127.0.0.1:8500进入consul控制台,如下图:
上面的命令是不能在另一台电脑的浏览器上访问consul控制台的,要想启用远程访问需要用到-client参数。
-client,Sets the address to bind for client access. This includes RPC, DNS, HTTP, HTTPS and gRPC (if configured).
使用如下命令从新启动consul.exe:
consul.exe agent -dev -server -client 0.0.0.0 -bind 192.168.0.2 -data-dir="d:\consul\consuldata"
这是就可以在另一台电脑(要与192.168.0.2在同一网段,或者是能ping通192.168.0.2)通过http://192.168.0.2:8500访问控制台,如下图:
以上介绍的事开发者服务模式,具体用于生产环境据需要以集群模式部署(一般部署3~5个节点),架构如下图:
Server1,Server2,Server3 构成了一个Consul集群,通过Raft选举算法选举出来一个Leader,然后,通过ClientN来取找到这个Leader,不论它们之间谁挂掉,总有一个是Leader的,那么这个ClientN就可以去找到这个Leader,从而实现了高可用。
具体部署环境需要4台服务器
序号 | 节点名称 | 节点说明 | 分配的IP |
---|---|---|---|
1 | Server1 | Leader | 192.168.0.2 |
2 | Server2 | Follower | 192.168.0.3 |
3 | Server3 | Follower | 192.168.0.4 |
4 | Client1 | Client | 192.168.0.5 |
启动命令如下:
Consul Server1
consul.exe agent -server -bootstrap-expect 3 -data-dir "d:\consul\consuldata" -node=Server2 -bind=192.168.0.2 -ui -client 0.0.0.0
Consul Server2
consul.exe agent -server -bootstrap-expect 3 -data-dir "d:\consul\consuldata" -node=Server2 -bind=192.168.0.3 -ui -client 0.0.0.0 -join=192.168.0.2
Consul Server3
consul.exe agent -server -bootstrap-expect 3 -data-dir "d:\consul\consuldata" -node=Server2 -bind=192.168.0.4 -ui -client 0.0.0.0 -join=192.168.0.2
Consul Client1
consul.exe agent -node=Client1 -bind 0.0.0.0 -client=0.0.0.0 -data-dir="d:\consul\consuldata" -join 192.168.0.2
大部分集群节点都是绑定自身的IP地址,除了第一个服务其他的服务都要 join 这个参数来实现加入集群。然后再浏览器中打开你客户端的IP的端口 8500 默认是这个地址进入管理控制台。