1. 程式人生 > >GRPC+CONSUL 服務發現

GRPC+CONSUL 服務發現

GRPC簡介

gRPC 是一個高效能、開源和通用的 RPC 框架,面向移動和 HTTP/2 設計。目前提供 C、Java 和 Go 語言版本,分別是:grpc, grpc-java, grpc-go. 其中 C 版本支援 C, C++, Node.js, Python, Ruby, Objective-C, PHP 和 C# 支援.

gRPC 基於 HTTP/2 標準設計,帶來諸如雙向流、流控、頭部壓縮、單 TCP 連線上的多複用請求等特。這些特性使得其在移動裝置上表現更好,更省電和節省空間佔用。

CONSUL簡介

Consul有多個元件,但總體而言,它是發現和配置基礎架構中的服務的工具。它提供了幾個關鍵功能:

  • 服務發現:Consul可以提供服務,比如 api或mysql,其他客戶可以使用領事來發現給定服務的提供者。使用DNS或HTTP,應用程式可以輕鬆找到他們所依賴的服務。

  • 健康檢查:Consul客戶端可以提供任何數量的健康檢查,或者與給定服務相關聯(“是Web伺服器返回200 OK”),還是與本地節點(“記憶體使用率低於90%”)。操作員可以使用此資訊來監視群集執行狀況,服務發現元件使用此資訊將流量從不健康的主機中引匯出去。

  • KV Store:應用程式可以將Consul的分層鍵/值儲存用於許多目的,包括動態配置,功能標記,協調,master選舉等等。簡單的HTTP API使其易於使用。

  • 多資料中心:Consul支援多個數據中心。這意味著Consul的使用者不必擔心構建額外的抽象層以擴充套件到多個區域。

Consul旨在與DevOps社群和應用程式開發人員保持友好,使其成為現代化,彈性基礎架構的完美選擇。

GRPC 服務搭建(golang)

詳見:http://www.yuuuu.wang/post/grpc-wei-fu-wu-shi-li-xie-redis-huan-cun-fu-yuan-ma.html

consul

下載consul 地址:https://www.consul.io/downloads.html
解壓將consul放到 /usr/local/bin
起consul服務

consul agent -server -bootstrap -advertise=127.0.0.1 -client=127.0.0.1 -data-dir=/tmp/node -ui -node=s1

-server 指定為server模式
-bootstrap 指定為引導模式
-advertise 設定監聽地址
-client 指定ip可以訪問ui
-data-dir 資料存放路徑
-ui 是否開啟web
-node 指定當前節點名稱

更多引數說明

-advertise=<value>
 Sets the advertise address to use.
-advertise-wan=<value>
 Sets address to advertise on WAN instead of -advertise address.
-bind=<value>
 Sets the bind address for cluster communication.
-bootstrap
 Sets server to bootstrap mode.
-bootstrap-expect=<value>
 Sets server to expect bootstrap mode.
-client=<value>
 Sets the address to bind for client access. This includes RPC, DNS,
 HTTP and HTTPS (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 JSON file to read configuration from. Can be specified
 multiple times.
-config-format=<value>
 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.
-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-script-checks
 Enables health check scripts.
-encrypt=<value>
 Provides the gossip encryption key.
-hcl=<value>
 hcl config fragment. Can be specified multiple times.
-http-port=<value>
 Sets the HTTP 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-level=<value>
 Log level of the agent.
-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) 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.
-pid-file=<value>
 Path to file to store agent PID.
-protocol=<value>
 Sets the protocol version. Defaults to latest.
-raft-protocol=<value>
 Sets the Raft protocol version. Defaults to latest.
-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-bind=<value>
 Address to bind Serf LAN listeners to.
-serf-wan-bind=<value>
 Address to bind Serf WAN listeners to.
-server
 Switches agent to server mode.
-syslog
 Enables logging to syslog.
-ui
 Enables the built-in static web UI server.
-ui-dir=<value>
 Path to directory containing the web UI resources.

將GRPC服務註冊到consul (服務發現,GOLANG)

var (
    serv = flag.String("service", "Cache", "elcLog RedisCache")
    port = flag.Int("port", 50051, "listening port") //本地服務的埠
    reg  = flag.String("reg", "127.0.0.1:8500", "register address") //consul的地址
    ttl = 15
)
func main() {
    flag.Parse()
    base := services.BaseService{}
    //起RPC服務
    gRPCport := base.GetVal("rpcserve")
    base.LogInfo("index" , gRPCport)
    lis , err := net.Listen("tcp", gRPCport)
    if err != nil {
        log.Fatal("failed to listen: %v", err)
    }
    //註冊服務到consul
    err = consul.Register(*serv, "127.0.0.1", *port, *reg, time.Second*10, ttl) //將該grpc服務註冊到consul
    if err != nil {
        panic(err)
    }
    s := grpc.NewServer()
    pb.RegisterGreeterServer(s, &server{})
    s.Serve(lis)
}

consul目錄結構如下

├── register.go
├── resolver.go
└── watcher.go

檔案原始碼:https://github.com/juelite/grpc-consul-service-discovery