consul基礎使用練習
當在Consul上註冊服務後,之後只要有服務的增減,Consul都可以自動發現,並將新增的node馬上對外堤供服務
consul基礎使用練習
想像去郵局辦事,consul類似於發號碼牌的那個機器
伺服端(櫃台人員)去找consul註冊服務,比如說櫃台1~3提供儲蓄,櫃台4~6提供郵寄服務
客戶進來透過服務發現,把要辦的事丟給consul,它查看哪個櫃台健康,幫你匹配對應的服務或叫號
(感覺這例子不太完善,就…類似的理解啦,實際用了才會懂)
-
下載
# 官網: https://www.consul.io/downloads # ubuntu shell: (下載>解壓縮>把二進位執行檔丟進去) wget https://releases.hashicorp.com/consul/1.10.3/consul_1.10.3_linux_amd64.zip unzip consul_1.10.3_linux_amd64.zip sudo mv consul /usr/local/bin/
-
測試
consul -h
-
啟動server
consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node=n1 -bind=192.168.234.128 -ui -rejoin -config-dir=/etc/consul.d/ -client 0.0.0.0
這個IP=192.168.234.128=ubuntu的ip
*若/etc/consul.d/不存在要先自己創
-
查看服務
http://192.168.234.128:8500/
-
註冊自訂的健康檢查服務,範例:
剛剛自訂的配置檔路徑/etc/consul.d/下新增一個web.json
{"service": { "name": "web", "tags": ["extract", "verify", "compare", "idcard"], "address": "192.168.234.128", "port": 9000, "check": { "id": "api", "name": "HTTP API on port 9000", "http": "http://localhost:9000", "interval": "10s", "timeout": "1s" } } }
-
重啟server並查看服務
- 結果是不健康,因為沒有東西回應它
透過consul來傳遞一個簡單服務案例:
- person.proto
syntax="proto3";
package pb;
option go_package="/.;pb";
message Person{
string name=1;
int32 age=2;
}
service hello{
rpc sayHello(Person) returns(Person);
}
- server
package main
import (
"context"
"fmt"
"github.com/hashicorp/consul/api"
"github.com/my/repo/consul/pb"
)
type Children struct {
}
// 綁定方法實現接口
func (c *Children) SayHello(ctx context.Context, p *pb.Person) (*pb.Person, error) {
p.Name = "hello" + p.Name
return p, nil
}
func main() {
// 1. 把grpc註冊到consul,初始化
consulConfig := api.DefaultConfig()
// 2. 創建consul對象
consulClient, err := api.NewClient(consulConfig)
if err != nil {
fmt.Println("api.NewClient err", err)
return
}
// 3. 告訴consul將要註冊的服務消息
reg := api.AgentServiceRegistration{
ID: "bj38",
Tags: []string{"grpc", "consul"},
Name: "grpc And Consul",
Address: "127.0.0.1",
Port: 8800,
Check: &api.AgentServiceCheck{
CheckID: "consul grpc test",
TCP: "127.0.0.1:8800",
Timeout: "1s",
Interval: "5s",
},
}
// 4. 註冊到consul上
consulClient.Agent().ServiceRegister(®)
fmt.Println("服務啟動...")
}
- client
package main
import (
"context"
"fmt"
"strconv"
"github.com/hashicorp/consul/api"
"github.com/my/repo/consul/pb"
"google.golang.org/grpc"
)
func main() {
// 初始化consul
consulConfig := api.DefaultConfig()
consulClient, err := api.NewClient(consulConfig)
if err != nil {
fmt.Println("api.NewClient err", err)
}
// 服務發現,從consul上獲取健康的服務
services, _, err := consulClient.Health().Service("grpc And Consul", "grpc", true, nil)
if err != nil {
fmt.Println("consulClient.Health().Service err", err)
return
}
// 拼接出服務的IP跟端口
fmt.Println(services[0].Service.Address)
fmt.Println(services[0].Service.Port)
addr := services[0].Service.Address + ":" + strconv.Itoa(services[0].Service.Port)
grpcConn, _ := grpc.Dial(addr, grpc.WithInsecure())
grpcClient := pb.NewHelloClient(grpcConn)
var person pb.Person
person.Name = "Tom~"
person.Age = 18
p, err := grpcClient.SayHello(context.TODO(), &person)
fmt.Println(p, err)
}
- 記得隨便用個cmd 開啟consul當橋梁
$ consul agent -dev
上次修改於 2021-09-28