6.7 服务注册与服务发现、服务配置 #
- 官方文档: http://www.cloudwego.io/zh/docs/kitex/tutorials/third-party/service_discovery/consul/
- CAP理论:
- C: 一致性、A: 可用性、P: 分区容错性
- 在工程实践中最多只能做到其中2个



服务注册-Sever端 #
- consul:
https://developer.hashicorp.com/consul/docs/intro
1go get github.com/kitex-contrib/registry-consul - 服务注册:
- 服务注册 demo/demo_proto/main.go
1 2 3 4 5r, err := consul.NewConsulRegister(conf.GetConf().Registry.RegistryAddress[0]) if err != nil { log.Fatal(err) } opts = append(opts, server.WithRegistry(r)) - 修改配置文件中的服务注册端口为consul的
8500:demo/demo_proto/conf/test/conf.yaml
- 服务注册 demo/demo_proto/main.go
- 启动容器:docker compose up -d
- 启动Sever端:

服务发现-Client端 #
| |
服务配置 #
- 方式一:配置文件。 File config:
conf.go解析yaml配置文件
- 方式二:环境变量。
- Linux env:export APP_ENV=online
- .env file:APP_ENV=online
- Docker env:ENV GO_ENV=online
go get github.com/joho/godotenv
- **方式三:配置中心tcc。**一般和注册中心用同一个
