Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置。 Consul是分布式的、高可用的、可横向扩展的。
服务发现:
运行状况检查:
KV 存储:
安全服务通信:
多数据中心:
对于想要学习Consul原理的,可以自行百度详细了解这两个协议。
docekr pull consul
等待一段时间后拉取成功
docker run -d -p 8500:8500 consul
consul会被运行在本机的8500端口上
docker ps
打开浏览器,输入http://127.0.0.1:8500

可以把配置相关信息先放在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" # 监控服务 prometheus: host: "0.0.0.0" port: 9089 # 限流 ratelimit: QPS: 1000 # 微服务 micro: name: "go.micro.service.account" version: "latest" address: ":9580"
// 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
}
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"`
}
var(
ConsulInfo *ConsulConfig
)
// GetAccountFromConsul 获取 consul 的配置
func GetAccountFromConsul(config config.Config, path ...string) error {
consulData := &ConsulConfig{}
config.Get(path...).Scan(consulData)
ConsulInfo = consulData
return nil
}
点击Key/Value,再点击Create

选择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"
}
}
// 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)
以上就是详解go-micro微服务consul配置及注册中心的详细内容,更多关于go micro consul配置注册的资料请关注脚本之家其它相关文章!
编程 | 2023-02-24 21:36
编程 | 2023-02-21 12:51
编程 | 2023-02-21 12:47
编程 | 2023-02-21 00:15
编程 | 2023-02-21 00:08
编程 | 2023-02-20 21:46
编程 | 2023-02-20 21:42
编程 | 2023-02-20 21:36
编程 | 2023-02-20 21:32
编程 | 2023-02-20 18:12
网友评论