go-micro v2 看下 api 模式服务怎么注册到网关(即通过 http 申请拜访)
须要先下载 micro 的 example 相干代码:
https://github.com/microhq/ex…
验证 api 性能
门路地位:micro\examples\api\api\api.go
先执行 micro 命令
micro api --handler=api
启动 api.go 服务
go run api.go
而后浏览器拜访:http://localhost:8080/example…
能够失去响应:
{"message":"got your request john"}
mdns 改成 etcd
默认 micro v2 是应用 mdns,而且 micro api --handler=api
不反对 consul!
api.go 代码 main 办法 etcd 相干配置:
// etcd 配置
service := micro.NewService(micro.Name("go.micro.api.example"),
micro.Registry(etcd.NewRegistry(registry.Addrs("127.0.0.1:2379"))),
)
启动 api 服务:
micro --registry=etcd --registry_address=127.0.0.1:2379 api --handler=api
启动 web 服务:
micro --registry=etcd --registry_address=127.0.0.1:2379 web
换成 etcd 之后的服务,无奈间接通过 micro list services
进行查看!须要指定 etcd:
micro --registry=etcd --registry_address=127.0.0.1:2379 list services
查看 Web
- http://localhost:8082/services
验证 http 服务
- http://localhost:8080/example/call?name=john
报错问题记录
报错信息:
2021-04-02 10:10:48 file=v2@v2.9.1/service.go:200 level=info Starting [service] go.micro.api.example
2021-04-02 10:10:48 file=grpc/grpc.go:864 level=info Server [grpc] Listening on [::]:6591
2021-04-02 10:10:48 file=grpc/grpc.go:697 level=info Registry [etcd] Registering node: go.micro.api.example-d9bba26f-53dc-499b-8ce7-5fc511ad1d0a
{"level":"warn","ts":"2021-04-02T10:10:53.122+0800","caller":"clientv3/retry_interceptor.go:61","msg":"retrying of unary invoker failed","target":"endpoint://client-1c863b71-70bb-48d5-842b-daa6671513d3/172.18.0.58:2379","attempt":0,"error":"rpc error: code = DeadlineExceeded desc = context deadline exceeded"}
没有启动 etcd 所致,启动 etcd:
PS C:UsersYC> cd D:CodeCodeToolsetcd
PS D:CodeCodeToolsetcd> etcd
etcd 交互,须要应用 etcdctl:
PS D:CodeCodeToolsetcd> etcdctl set foot bar
bar
PS D:CodeCodeToolsetcd> etcdctl get foo
bar
查看 etcd micro 相干 key:
etcdctl get --from-key /micro
参考
- GitHub – microhq/examples
- 《Go Micro 中文文档》| Go 技术论坛
- go-micro V2 从零开始(五)集成 etcd 注册核心