欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解go-micro微服務consul配置及注冊中心

 更新時間:2023年01月10日 11:19:18   作者:夏沫的夢  
這篇文章主要為大家介紹了go-micro微服務consul配置及注冊中心示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

一 Consul介紹

Consul是HashiCorp公司推出的開源工具,用于實現(xiàn)分布式系統(tǒng)的服務發(fā)現(xiàn)與配置。 Consul是分布式的、高可用的、可橫向擴展的。

1. 注冊中心Consul基本介紹

  • Consul是一種服務網(wǎng)格解決方案
  • 提供具有服務發(fā)現(xiàn),配置和分段功能的全功能控制平面
  • Consul 附帶-個簡單的內(nèi)置代理,可以開箱即用

2.注冊中心Consul關鍵功能

服務發(fā)現(xiàn):

  • 客戶端可以注冊服務,程序可以輕松找到它們所依賴的服務

運行狀況檢查:

  • Consul客戶端可以提供任意數(shù)量的運行狀況檢查

KV 存儲:

  • 應用程序可以將Consul的層級鍵/值存儲用于任何目的,包括動態(tài)配置,功能標記,協(xié)調(diào),領導者選舉等

安全服務通信:

  • Consul 可以為服務生成和分發(fā)TLS證書,建立相互的TLS連接

多數(shù)據(jù)中心:

  • Consul 支持多個數(shù)據(jù)中心

3.注冊中心Consul兩個重要協(xié)議

  • Gossip Protocol (八卦協(xié)議)
  • Raft Protocol ( 選舉協(xié)議)

對于想要學習Consul原理的,可以自行百度詳細了解這兩個協(xié)議。

二 Consul安裝

1.使用docker拉取鏡像

  • 打開終端,輸入以下命令:
docekr pull consul

等待一段時間后拉取成功

  • 啟動命令:
docker run -d -p 8500:8500 consul

consul會被運行在本機的8500端口上

  • 檢查是否運行
docker ps
  • 可視化界面

打開瀏覽器,輸入http://127.0.0.1:8500

三 Config配置

  • 在config目錄下新建一個config.yaml

可以把配置相關信息先放在config.yaml里,之后放在consul中。

  • 編寫代碼:
name: "Account"
title: "賬號功能"
mode: "dev"
port: 9580
version: "v0.0.1"
log:
  level: "debug"
  filename: "Account.log"
  max_size: 200
  max_age: 30
  max_backips: 7
mysql:
  host: "127.0.0.1"
  port: 3306
  user: "root"
  password: "xxx"
  dbname: "micro"
  max_open_conns: 200
  max_idle_conns: "50"
redis:
  host: "127.0.0.1"
  port: 6379
  password: "xxx"
  db: 4
  pool_size: 100
email:
  user: "xxx@qq.com"
  pass: "xxx"
  host: "smtp.qq.com"
  port: 465
  rename: "Account"
# 配置、注冊中心
consul:
  host: "localhost"
  port: 8500
  prefix: "/micro/config"
  consulRegistry: "127.0.0.1:8500"
# 鏈路追蹤
jaeger:
  serviceName: "go.micro.service.account"
  addr: "localhost:6831"
# 監(jiān)控服務
prometheus:
  host: "0.0.0.0"
  port: 9089
# 限流
ratelimit:
  QPS: 1000
# 微服務
micro:
  name: "go.micro.service.account"
  version: "latest"
  address: ":9580"
  • 注意,以下字段自行修改:
    • mysql.password
    • redis.password
    • email.user
    • email.pass

四 Consul代碼編寫

  • 在micro目錄下新建一個consul.go文件

1.設置consul配置中心

// GetConsulConfig 設置配置中心
func GetConsulConfig(host string, port int64, prefix string) (config.Config, error) {
   consulSource := consul.NewSource(
      //設置配置中心的地址
      consul.WithAddress(host+":"+strconv.FormatInt(port, 10)),
      //設置前綴,不設置默認前綴 /micro/config
      consul.WithPrefix(prefix),
      //是否移除前綴,這里是設置為true,表示可以不帶前綴直接獲取對應配置
      consul.StripPrefix(true),
   )
   //配置初始化
   newConfig, err := config.NewConfig()
   if err != nil {
      return newConfig, err
   }
   //加載配置
   err = newConfig.Load(consulSource)
   return newConfig, err
}

2.獲取consul配置中心的數(shù)據(jù)

  • 編寫結(jié)構(gòu)體(嵌套結(jié)構(gòu))
type Account struct {
   Name    string `json:"name"`
   Title   string `json:"title"`
   Mode    string `json:"mode"`
   Port    int64  `json:"port"`
   Version string `json:"version"`
}
type Mysql struct {
   Host     string `json:"host"`
   User     string `json:"user"`
   Pwd      string `json:"pwd"`
   Database string `json:"database"`
   Port     int64  `json:"port"`
}
type Log struct {
   Level string `json:"level"`
   Filename string `json:"filename"`
   MaxSize int64 `json:"max_size"`
   MaxAge int64 `json:"max_age"`
   MaxBackips int64 `json:"max_backips"`
}
type Redis struct {
   Host string `json:"host"`
   Port int64 `json:"port"`
   Password string `json:"password"`
   Db int64 `json:"db"`
   PoolSize int64 `json:"pool_size"`
}
type Email struct {
   User string `json:"user"`
   Pass string `json:"pass"`
   Host string `json:"host"`
   Port int64 `json:"port"`
   Rename string `json:"rename"`
}
type Consul struct {
   Host string `json:"host"`
   Port int64 `json:"port"`
   Prefix string `json:"prefix"`
   ConsulRegistry string `json:"consulRegistry"`
}
type Jaeger struct {
   ServiceName string `json:"serviceName"`
   Addr string `json:"addr"`
}
type Prometheus struct {
   Host string `json:"host"`
   Port int64 `json:"port"`
}
type Ratelimit struct {
   QPS int64 `json:"QPS"`
}
type Micro struct {
   Name string `json:"name"`
   Version string `json:"version"`
   Address string `json:"address"`
}
type ConsulConfig struct {
   Account Account `json:"account"`
   Mysql Mysql `json:"mysql"`
   Log Log `json:"log"`
   Redis Redis `json:"redis"`
   Email Email `json:"email"`
   Consul Consul `json:"consul"`
   Jaeger Jaeger `json:"jaeger"`
   Prometheus Prometheus `json:"prometheus"`
   Ratelimit Ratelimit `json:"ratelimit"`
   Micro Micro `json:"micro"`
}
  • 獲取consul數(shù)據(jù)
var(
   ConsulInfo *ConsulConfig
)
// GetAccountFromConsul 獲取 consul 的配置
func GetAccountFromConsul(config config.Config, path ...string) error {
   consulData := &ConsulConfig{}
   config.Get(path...).Scan(consulData)
   ConsulInfo = consulData
   return nil
}

3.consul可視化界面數(shù)據(jù)編寫

點擊Key/Value,再點擊Create

  • 輸入項目名稱: micro/config/account

選擇JSON

  • 輸入以下代碼:
{
"account":{
"name": "Account",
"title": "賬號功能",
"mode": "dev",
"port": 9580,
"version": "v0.0.1"
},
"log":{
"level": "debug",
"filename": "Account.log",
"max_size": 200,
"max_age": 30,
"max_backips": 7
},
"mysql":{
"host":"127.0.0.1",
"user":"root",
"pwd":"xxx",
"database":"micro",
"port":3306
},
"redis":{
"host": "127.0.0.1",
"port": 6379,
"password": "123456",
"db": 4,
"pool_size": 100
},
"consul":{
"host": "localhost",
"port": 8500,
"prefix": "/micro/config",
"consulRegistry": "127.0.0.1:8500"
},
"email":{
"user": "xxx@qq.com",
"pass": "xxx",
"host": "smtp.qq.com",
"port": 465,
"rename": "Account"
},
"jaeger":{
"serviceName": "go.micro.service.account",
"addr": "localhost:6831"
},
"prometheus":{
"host": "0.0.0.0",
"port": 9089
},
"ratelimit":{
"QPS": 1000
},
"micro":{
"name": "go.micro.service.account",
"version": "latest",
"address": ":9580"
}
}
  • 注意JSON格式,點擊Save

4. main.go代碼編寫

// 1.配置中心
consulConfig, err := micro.GetConsulConfig("localhost", 8500, "/micro/config")
if err != nil {
   fmt.Printf("Init consulConfig failed, err: %v\n", err)
}
// 2.注冊中心
consulRegistry := consul.NewRegistry(func(options *registry.Options) {
   options.Addrs = []string{
      "127.0.0.1:8500",
   }
})
if err := micro.GetAccountFromConsul(consulConfig, "account"); err != nil {
   fmt.Printf("Init consul failed, err: %v\n", err)
}
fmt.Println(micro.ConsulInfo)
  • 這時候,micro中的ConsulInfo可以用來使用consul中的數(shù)據(jù)了,使用 . 取數(shù)據(jù)

五 最后

  • 至此,go-micro微服務consul配置、注冊中心開發(fā)工作就正式完成。
  • 接下來就開始Mysql的代碼編寫了,希望大家關注博主和關注專欄,第一時間獲取最新內(nèi)容,每篇博客都干貨滿滿。

以上就是詳解go-micro微服務consul配置及注冊中心的詳細內(nèi)容,更多關于go micro consul配置注冊的資料請關注腳本之家其它相關文章!

相關文章

  • golang?墻上時鐘與單調(diào)時鐘的實現(xiàn)

    golang?墻上時鐘與單調(diào)時鐘的實現(xiàn)

    本文主要介紹了golang?墻上時鐘與單調(diào)時鐘的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • go?zero微服務實戰(zhàn)系服務拆分

    go?zero微服務實戰(zhàn)系服務拆分

    這篇文章主要為大家介紹了go?zero微服務實戰(zhàn)系服務拆分的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Go語言編程實現(xiàn)支持六種級別的日志庫?

    Go語言編程實現(xiàn)支持六種級別的日志庫?

    這篇文章主要為大家介紹了使用Golang編寫一個支持六種級別的日志庫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • Golang中channel使用的一些小技巧

    Golang中channel使用的一些小技巧

    這篇文章主要介紹了Golang中channel使用的一些小技巧,本文講解了關閉2次、讀取的時候channel提前關閉了、向已經(jīng)關閉的channel寫數(shù)據(jù)等技巧及這實例代碼,需要的朋友可以參考下
    2015-07-07
  • Golang字符串類型原理及其使用方法

    Golang字符串類型原理及其使用方法

    本文主要介紹了Golang字符串類型原理及其使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-09-09
  • Go語言學習之goroutine詳解

    Go語言學習之goroutine詳解

    Goroutine是建立在線程之上的輕量級的抽象。它允許我們以非常低的代價在同一個地址空間中并行地執(zhí)行多個函數(shù)或者方法,這篇文章主要介紹了Go語言學習之goroutine的相關知識,需要的朋友可以參考下
    2020-02-02
  • Go語言字符串基礎示例詳解

    Go語言字符串基礎示例詳解

    這篇文章主要為大家介紹了Go語言字符串基礎的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2021-11-11
  • 詳解為什么說Golang中的字符串類型不能修改

    詳解為什么說Golang中的字符串類型不能修改

    在接觸Go這么語言,可能你經(jīng)常會聽到這樣一句話。對于字符串不能修改,可能你很納悶,日常開發(fā)中我們對字符串進行修改也是很正常的,為什么又說Go中的字符串不能進行修改呢?本文就來通過實際案例給大家演示一下
    2023-03-03
  • Golang負載均衡和保活設計原理示例探究

    Golang負載均衡和?;钤O計原理示例探究

    這篇文章主要為大家介紹了Golang負載均衡和?;钤O計原理示例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • Go并發(fā)編程sync.Cond的具體使用

    Go并發(fā)編程sync.Cond的具體使用

    Go 標準庫提供 Cond 原語的目的是,為等待 / 通知場景下的并發(fā)問題提供支持,本文主要介紹了Go并發(fā)編程sync.Cond的具體使用,具有一定的參考價值,感興趣的可以了解一下
    2022-05-05

最新評論