关于gRPC和golang实现微服务的记录(二)
go-kit和gRPC结合使用
什么是Go Kit
Go kit is a collection of Go (golang) packages (libraries) that help you build robust, reliable, maintainable microservices. It was originally conceived as a toolkit to help larger (so-called modern enterprise) organizations adopt Go as an implementation language. But it very quickly “grew downward”, and now serves smaller startups and organizations just as well. For more about the origins of Go kit, see Go kit: Go in the modern enterprise.
Go Kit是使用golang编写的一个微服务工具包,帮助开发者能够快速展开微服务开发的工作
Transports 传输层
Go Kit 可以自由的去选择使用HTTP或者gRPC作为传输方式,你也可以在一个项目中使用HTTP API和RPC服务
1 | // 使用gRPC |
Endpoints 端点层
类似MVC设计模式下的Controller中的Action Handler,如果你使用了两个传输方式,你也需要两个方法来请求同一个端点(endpoint)
1 | // endpoint |
Services 服务层
实现所有业务逻辑的地方,将多个端点沾合在一起,Go Kit中,服务通常被定义为interface接口,这些接口包含需要被实现的业务(业务逻辑应该不了解端点或特别是传输域概念:您的服务不应该知道有关HTTP头或gRPC错误代码的任何信息)
1 | // 服务接口 |
Middlewares 中间件
目前在学习中了解到了使用etcd作为注册中心,Go Kit提供的负载均衡,使用DelayingLimiter限流器。熔断机制和服务请求追踪还未去接触
使用Etcd注册中心 & 负载均衡 & 限流器的使用
关于etcd存储的文章学习:https://www.cnblogs.com/softidea/p/6517959.html
1 | // 下载最近的etcd存储仓库:https://github.com/etcd-io/etcd/releases & https://www.linuxidc.com/Linux/2015-02/112763.htm |
测试负载均衡,只需在新建个server_1.go文件,将服务端代码复制进去,修改监听端口(50052),客服端进行轮询
1 | package main |