欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

go-micro 入门教程1.搭建 go-micro环境

程序员文章站 2023-11-21 13:10:34
golang微服务框架go-micro 入门笔记1.搭建 go-micro环境 ......

微服务的本质是让专业的人做专业的事情,做出更好的东西。

golang具备高并发,静态编译等特性,在性能、安全等方面具备非常大的优势。go-micro是基于golang的微服务编程框架,go-micro操作简单、编码高效、功能强大。但是网络上资料偏少,本系列文章定位最简单最容易上手的go-micro入门教程,所有案列来自实操,而非网络上的复制粘贴。

本章节的目的是让大家最快速搭建好go-micro环境

软件 环境
操作系统 win10
golang go 12.7/amd64
micro micro version 1.8.4
consul consul 1.5.4

安装micro

最快速有效的方法是使用go1.11以上版本并且设置环境变量

#linux 下
export go111module=on
export goproxy=https://goproxy.io
# windows下设置如下环境变量
setx go111module on
setx goproxy https://goproxy.io
# 使用如下指令安装
go get -u -v github.com/micro/micro
go get -u -v github.com/micro/go-micro

如果没有git请自行安装git

#下载地址
https://git-scm.com/downloads/

安装 protoc

访问如下网址

https://github.com/protocolbuffers/protobuf/releases

下载,不同的版本文件名称不一样,我们这里选择protoc-3.9.1-win64.zip

protoc-3.9.1-win64.zip

解压到目标文件架,我们以e:\dev为例

e:\dev\protoc-3.9.1-win64

添加e:\dev\protoc-3.9.1-win64\bin到环境变量path

安装protoc-gen-micro插件

这个插件主要作用是通过.proto文件生成适用于go-micro的代码

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

安装 consul

下载windows版本

https://www.consul.io/downloads.html

解压到

e:\dev\consul

添加e:\dev\consul到环境变量path
使用如下指查看是否安装成功,如下所示安装成功

>consul
usage: consul [--version] [--help] <command> [<args>]
available commands are:
    acl            interact with consul's acls
    agent          runs a consul agent
    catalog        interact with the catalog
    config         interact with consul's centralized configurations
    connect        interact with consul connect
    debug          records a debugging archive for operators

hello,world

创建微服务

使用如下指令创建微服务

>micro new techidea8.com/microapp/hello
creating service go.micro.srv.hello in e:\winlion\gopath\src\techidea8.com\microapp\hello

.
├── main.go
├── plugin.go
├── handler
│   └── hello.go
├── subscriber
│   └── hello.go
├── proto\hello
│   └── hello.proto
├── dockerfile
├── makefile
├── readme.md
└── go.mod


download protobuf for micro:

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

compile the proto file hello.proto:

cd e:\winlion\gopath\src\techidea8.com\microapp\hello
protoc --proto_path=.:$gopath/src --go_out=. --micro_out=. proto/hello/hello.proto

生成适配proto的golang代码

注意:在win系统下$gopath环境变量无效,因此如上脚本将创建微服务失败,因此我们需要对如上脚本进行处理

#切换到项目目录下
>cd /d e:\winlion\gopath\src\techidea8.com\microapp\hello

# 根据proto生成文件
>protoc --proto_path=. --go_out=. --micro_out=. proto/hello/hello.proto

启动应用

>go run main.go
2019/08/19 13:00:46 transport [http] listening on [::]:54689
2019/08/19 13:00:46 broker [http] connected to [::]:54690
2019/08/19 13:00:46 registry [mdns] registering node: go.micro.srv.hello-4851dce2-ab5d-4e4c-801e-44dae5d93f26
2019/08/19 13:00:46 subscribing go.micro.srv.hello-4851dce2-ab5d-4e4c-801e-44dae5d93f26 to topic: go.micro.srv.hello
2019/08/19 13:00:46 subscribing go.micro.srv.hello-4851dce2-ab5d-4e4c-801e-44dae5d93f26 to topic: go.micro.srv.hello

查看是否启动

>micro list services
go.micro.srv.hello
topic:go.micro.srv.hello

启动restful api接口支持支持

注意其中的--namespace参数,我们每一个微服务都属于一个命名空间,通过api暴露出来该命名空间后,满足go.micro.srv.*格式的微服务都可以访问。如go.micro.srv.hello可以通过如下格式访问

http://127.0.0.1:8080/user/call
>micro api --namespace=go.micro.srv
2019/08/19 13:07:11 registering api default handler at /
2019/08/19 13:07:11 http api listening on [::]:8080
2019/08/19 13:07:11 transport [http] listening on [::]:54934
2019/08/19 13:07:11 broker [http] connected to [::]:54935
2019/08/19 13:07:11 registry [mdns] registering node: go.micro.api-1753185c-b8e1-49c4-aa0f-617f243a8e2a

测试

restd插件请求接口
go-micro 入门教程1.搭建 go-micro环境

推荐阅读