blog icon indicating copy to clipboard operation
blog copied to clipboard

Golang: micro service framework - go-micro

Open hhstore opened this issue 6 years ago • 7 comments

 相关:

  • #115

hhstore avatar Sep 03 '18 07:09 hhstore

micro service framwork diff:

  • https://github.com/go-kit/kit
  • https://github.com/nytimes/gizmo
    • 纽约时报开源
  • https://github.com/gocircuit/circuit

中文文档:

  • https://lixiangyun.gitbooks.io/go-micro/content/Overview/Features.html

对比其他框架:

  • https://stackshare.io/microservices-tools/stackups
  • 对比 goa: https://github.com/goadesign/goa
  • 对比 kite: https://github.com/koding/kite
  • 对比 go-chassis: https://github.com/go-chassis/go-chassis
  • 对比 go-kit: https://github.com/go-kit/kit
    • go-kit cli: https://github.com/kujtimiihoxha/kit
  • 对比 rpcx: https://github.com/smallnest/rpcx
  • 对比 tarsgo: https://github.com/TarsCloud/TarsGo
    • 腾讯开源, 微服务框架

ref:

  • https://toutiao.io/posts/428478/app_preview
  • https://www.jianshu.com/p/0c34a75569b1

关于 micro service 的定义:

  • https://martinfowler.com/articles/microservices.html

go-micro:

项目说明:

  • Micro包含开源库和工具,可帮助开发微服务。
  • go-micro - 用于编写微服务的可插拔Go RPC框架; 服务发现,客户端/服务器rpc,发布/订阅等
  • go-plugins - go-micro的插件,包括etcd,kubernetes,nats,rabbitmq,grpc等。
  • micro - 包含传统入口点的微服务工具包; API网关,CLI,Slack Bot,代理和Web UI。
  • 可以在github.com/micro中找到各种其他库和服务。

官方示例合集:

  • https://github.com/micro/examples
    • 涵盖各种类型的示例代码
    • API/service/ 等
    • https://github.com/micro/examples/tree/master/booking
      • 订房服务, 比较完整
    • https://github.com/micro/examples/tree/master/form
      • 表单提交, API, web 等

ref:

  • 项目结构: https://micro.mu/docs/architecture.html
  • https://micro.mu/blog/2016/04/18/micro-architecture.html

项目用户列表:

  • https://micro.mu/docs/users.html
  • 上海-华尔街见闻: https://zhuanlan.zhihu.com/p/26777189

示例Demo:

  • https://github.com/micro/examples/tree/master/greeter

go-micro 教程:

  • https://wuyin.io/tags/%E5%BE%AE%E6%9C%8D%E5%8A%A1/

    • 该作者翻译了一个系列教程, 好人
    • https://github.com/wuYin/shippy
    • 对应教程的代码部分
  • 专题10篇:

    • https://github.com/wuYin/shippy
    • https://wuyin.io/2018/05/10/microservices-part-1-introduction-and-consignment-service/
      • 代码: https://github.com/wuYin/shippy/tree/feature/part1
    • https://wuyin.io/2018/05/12/microservices-part-2-use-go-micro-and-dockerising/
    • https://wuyin.io/2018/05/22/microservices-part-3-docker-compose-and-mongodb-with-orm/

基于 go-micro 的项目:

  • https://github.com/wiatingpub/MTBSystem
    • https://gitee.com/xi_fan/MTBSystem
    • 在线电影院订票系统, (国内开发者一个毕设)

FAQ:

1. API,Web和SRV服务有什么区别?

  • https://lixiangyun.gitbooks.io/go-micro/content/Overview/FAQ.html

  • API服务:

    • https://github.com/micro/micro/tree/master/api
    • API服务由Micro Api提供,默认命名空间为go.micro.api。
    • micro API符合API网关模式。
  • Web服务:

    • https://github.com/micro/micro/tree/master/web
    • Web服务由Micro Web提供,默认名称空间为go.micro.web。
    • 我们相信web应用程序是微服务世界中的一等公民,因此可以将web仪表板作为微服务来构建。
    • Micro网络是一个反向代理,它会根据服务解析的路径将HTTP请求转发到相应的Web应用程序。
  • SRV服务:

    • SRV服务基本上是标准的RPC服务,通常这是你写的服务。
    • 我们通常称它们为RPC或后端服务,因为它们主要应该是后端架构的一部分,并且永远不会面向公众。 -默认情况下,我们使用命名空间go.micro.srv,但是您应该使用您的域com.example.srv。

2. Micro是否支持gRPC?

  • 是的。
  • 在micro/go-plugins中有传输,客户端和服务器的插件。
  • 如果你想快速入门,只需使用micro/go-grpc。
  • https://github.com/micro/go-plugins
  • https://github.com/micro/go-grpc

hhstore avatar Sep 03 '18 07:09 hhstore

go-micro 支持 rest API:

  • https://github.com/micro/examples/blob/master/greeter/api/rest/rest.go

  • https://lixiangyun.gitbooks.io/go-micro/content/Components/API.html

    • 支持rest API
    • https://github.com/micro/examples/blob/master/greeter/api/rest/rest.go
    • 依赖 https://github.com/emicklei/go-restful 实现

go-micro 集成各种插件服务:

1. 集成 swagger:

  • http://aloyzh.com/2018/06/go-integrate-swagger/
  • 依赖包:
    • https://github.com/googleapis/googleapis

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger


  • 主要步骤:
      1. .proto文件转换为swagger.json文件
      1. 合并所有swagger.json成为一个
      1. Serve the API

2. 问题跟踪系统:

  • https://gitissue.com/issues/5a20634a9a954719b8dd694e

  • Trace | Distributed tracing: Zipkin

  • 参考示例项目用法:

    • https://github.com/henter/go-zipkin-demo
    • go-micro + zipkin 实现 demo

go-micro 官方文档摘要:

  • 中文文档: https://lixiangyun.gitbooks.io/go-micro/content/Overview/Features.html

  • https://github.com/hb-go/micro

  • 参考此项目了解细节.

ref: 参考实践:

  • https://golangcaff.com/articles/171/go-practical-micro-service-service-discovery

    • https://github.com/yuansir/go-micro-consul-cluster
    • 代码API部分, 调用 RPC service, 参看:
      • https://github.com/yuansir/go-micro-consul-cluster/blob/master/api/hotel/main.go#L68
      • https://github.com/yuansir/go-micro-consul-cluster/blob/master/api/hotel/main.go#L80
      • https://github.com/yuansir/go-micro-consul-cluster/blob/master/api/hotel/main.go#L134
      • https://github.com/yuansir/go-micro-consul-cluster/blob/master/api/hotel/main.go#L150
    • 酒店调用示例, RPC调用微服务.
  • https://www.jianshu.com/nb/7093363

    • micro 4篇文章, 比较有价值
    • https://www.jianshu.com/p/36d9376321cd
    • https://www.jianshu.com/p/97157e088566
    • https://www.jianshu.com/p/4d71da02ac43
    • https://www.jianshu.com/p/b6e68821fbd3
  • https://www.cnblogs.com/li-peng/category/1292629.html

    • 两篇, 用 etcd v3 做服务注册
    • https://www.cnblogs.com/li-peng/p/9558421.html
    • https://www.cnblogs.com/li-peng/p/9598879.html

0. 项目脚手架: micro

  • https://micro.mu/docs/toolkit.html
go get -u github.com/micro/micro

#
brew install protobuf

#
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

go get -u github.com/micro/protoc-gen-micro


使用示例:

# 创建:
micro new github.com/micro/example

# 编译pb:
protoc --proto_path=. --micro_out=. --go_out=. proto/example/example.proto

# 运行:
go run main.go

cli 工具支持命令:

  • new: https://micro.mu/docs/new.html
    • 新建项目
  • api: https://micro.mu/docs/api.html
  • web: https://micro.mu/docs/web.html
  • cli: https://micro.mu/docs/cli.html#usage
  • bot: https://micro.mu/docs/bot.html
    • 集成slack等

1. 安装指南:

  • https://micro.mu/docs/install-guide.html

go get github.com/micro/go-micro
go get github.com/micro/micro

# Demo:
go get github.com/micro/examples/greeter/srv && srv

2. 配合 grpc-gateway 项目:

  • https://micro.mu/docs/grpc-gateway.html

3. 服务定义:

  • https://micro.mu/docs/go-micro.html

写一个服务:

  • https://micro.mu/docs/go-micro.html#writing-a-service

MQ 发布/订阅:

  • https://micro.mu/docs/go-micro.html#publish--subscribe

4. 配置读取:

  • https://micro.mu/docs/go-config.html#sources
  • 支持环境变量/配置中心等各种读取方式

5. 插件集:

  • https://micro.mu/docs/plugins.html
  • https://github.com/micro/go-plugins
    • 插件仓库
  • 使用示例: https://micro.mu/docs/plugins-go-micro.html

6. 部署:

基于 docker:

  • https://micro.mu/docs/deploy-docker.html

基于 kubernetes:

  • https://micro.mu/docs/deploy-kubernetes.html

hhstore avatar Sep 03 '18 08:09 hhstore

go micro:

1. 准备环境: consul 依赖服务注册

  • https://github.com/service-mesh/devops/blob/master/deploy/compose/local/devops-consul.yml

docker-compose -f devops-consul.yml up -d


2. 创建项目:

  • 手把手创建项目:

cd $GOPATH

# 创建一个demo项目:
micro new micro_demo

# 创建成功有提示信息: 告诉你先生成 protobuf文件:
cd $GOPATH/src/micro_demo

protoc --proto_path=. --go_out=. --micro_out=. proto/example/example.proto




3. 启动项目:


cd GOPATH/src/micro_demo

(py27-base) ζ go run main.go                                                                                                  
2018/09/21 15:27:36 Listening on [::]:49408
2018/09/21 15:27:36 Broker Listening on [::]:49409
2018/09/21 15:27:36 Registering node: go.micro.srv.demo2-cfd100c4-bd6f-11e8-bb1a-a45e60b7c119




4. 查看启动的微服务:

# henry@hh-rmbp: ~/iGit/iSpace/iGo/libdir/src/demo2                                                               
(py27-base) ζ micro list services                                                                                             
consul
go.micro.srv.demo2
topic:go.micro.srv.demo2


  • 调用测试: https://micro.mu/docs/toolkit.html#call-service
# henry@hh-rmbp: ~/iGit/iSpace/iGo/libdir/src/demo2                                                                (15:31:08)  
(py27-base) ζ micro call go.micro.srv.demo2 Example.Call '{"name": "John"}'                                                   
{
        "msg": "Hello John"
}

  • 启动web API: micro web https://micro.mu/docs/web.html#run
  • 打开浏览器查看

# henry@hh-rmbp: ~/iGit/iSpace/iGo/libdir/src/demo2                                                                (15:35:55)  
(py27-base) ζ micro web                                                                                                       
2018/09/21 15:35:57 Listening on [::]:8082
2018/09/21 15:35:57 Listening on [::]:49562
2018/09/21 15:35:57 Broker Listening on [::]:49563
2018/09/21 15:35:57 Registering node: go.micro.web-faae005c-bd70-11e8-a266-a45e60b7c119



hhstore avatar Sep 21 '18 07:09 hhstore

从0到1创建一个完整的micro项目:

  • 完整创建步骤
  • 以 gitlab 为例

1. 创建git项目:

  • 创建一个空项目:

  • https://gitlab.com/ceres-mirror/exchange-trade-proxy4

  • base URLL: gitlab.com/ceres-mirror/exchange-trade-proxy4

git clone [email protected]:ceres-mirror/exchange-trade-proxy4.git

2. 安装 micro 环境:

go get -u github.com/micro/micro
#
brew install protobuf
#
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
go get -u github.com/micro/protoc-gen-micro

# 创建API依赖:
go get -t github.com:micro/go-api

3. 使用micro脚手架初始化工程:

  • micro new -h 选项: https://lixiangyun.gitbooks.io/go-micro/content/Components/New.html
  • 创建命令:
  • 目录不存在的层次, 会自动创建



# exchange:
micro new --type srv --alias exchange --namespace "com.ceres.trade.proxy" gitlab.com/ceres-mirror/exchange-trade-proxy4/server/exchange/srv
micro new --type web --alias exchange --namespace "com.ceres.trade.proxy" gitlab.com/ceres-mirror/exchange-trade-proxy4/server/exchange/web
micro new --type api --alias exchange --namespace "com.ceres.trade.proxy" gitlab.com/ceres-mirror/exchange-trade-proxy4/server/exchange/api
micro new --type fnc --alias exchange --namespace "com.ceres.trade.proxy" gitlab.com/ceres-mirror/exchange-trade-proxy4/server/exchange/fnc


# trade:
micro new --type srv --alias trade --namespace "com.ceres.trade.proxy" gitlab.com/ceres-mirror/exchange-trade-proxy4/server/trade/srv
micro new --type web --alias trade --namespace "com.ceres.trade.proxy" gitlab.com/ceres-mirror/exchange-trade-proxy4/server/trade/web
micro new --type api --alias trade --namespace "com.ceres.trade.proxy"  gitlab.com/ceres-mirror/exchange-trade-proxy4/server/trade/api


# order:
micro new --type srv --alias order --namespace "com.ceres.trade.proxy" gitlab.com/ceres-mirror/exchange-trade-proxy4/server/order/srv
micro new --type web --alias order --namespace "com.ceres.trade.proxy" gitlab.com/ceres-mirror/exchange-trade-proxy4/server/order/web
micro new --type api --alias order --namespace "com.ceres.trade.proxy" gitlab.com/ceres-mirror/exchange-trade-proxy4/server/order/api


4. 编译服务:

  • gen pb:
cd ~/src/gitlab.com/ceres-mirror/exchange-trade-proxy4/server/trade/api
protoc --proto_path=. --go_out=. --micro_out=. proto/example/example.proto


  • 遇到 gen 报错, 可能是多个 GOPATH 问题, 改成一个, 可解决.

5. 预先启动 consul:

  • 参考上面

6. 启动服务:

  • 打开多个terminal窗口, 分别启动:
# 服务1:
./exchange-srv
# 服务2:
./exchange-web

# 启动 :8082 端口访问web端
micro web  

# 启动:
micro api  

  • web端: image

  • web端调用:

image

7. 调用服务:

  • 命令行调用:
# 查看服务列表:
# henry@hh-rmbp: ~/iGit/iSpace/iGo                                                                                  (15:35:15)  
(py27-base) ζ micro list services                                                                                              
com.ceres.trade.proxy.srv.exchange
com.ceres.trade.proxy.web.exchange
consul
go.micro.api
go.micro.web
topic:com.ceres.trade.proxy.srv.exchange


# 调用API:
# henry@hh-rmbp: ~/iGit/iSpace/iGo                                                                                  (15:35:23)  
(py27-base) ζ micro call com.ceres.trade.proxy.srv.exchange Example.Call '{"name": "john"}'    
{
        "msg": "Hello john"
}


hhstore avatar Sep 23 '18 06:09 hhstore

go-micro example:

  • https://github.com/micro/examples
  • 给了很多重要示例, 非常重要, 必看

greeter:

  • https://github.com/micro/examples/tree/master/greeter
  • 给了 API 各种调用方式, 必须看, 对 gin, go-restful 等包都有支持.
  • 给了 Python的调用方式

booking:

  • https://github.com/micro/examples/tree/master/booking
  • 完整的服务示例
  • 而且 docker-compose.yml 给了服务定义结构:
    • 1个 API 实例
    • 3组 srv 示例,
    • API 调用 srv 示例
  • 完美的服务案例.

hhstore avatar Mar 14 '19 03:03 hhstore

go-micro:

  • https://github.com/microhq

推荐目录结构:

  • https://github.com/micro/services
user/
    api/	# api routes
    web/	# web html
    client/	# generated clients
    service/	# core service types
    handler/	# request handlers
    subscriber/	# message subscribers
    proto/	# proto generated code
    main.go	# service main
    user.mu	# mu definition
    README.md	# readme

hhstore avatar May 01 '19 10:05 hhstore

go-micro v4:

  • https://github.com/asim/go-micro
  • 作者又回来维护 go-micro 了. 可喜可贺.
  • 可惜了 go-micro 这个项目. 错过了最好的时机.
  • 虽然我尝试使用 go-kratos, go-zero, 还是有很多不爽的地方.
  • 要不要再给 go-micro 一个机会?

示例:

  • https://github.com/go-micro/examples

micro:

示例:

  • https://github.com/micro/services

hhstore avatar Apr 08 '22 08:04 hhstore