golang微服务框架go-micro 入门笔记1.搭建 go-micro环境

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

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

本章节的目的是让你们最快速搭建好 go-micro环境
软件 环境
操做系统 win10
golang go 12.7/AMD64
micro micro version 1.8.4
consul consul 1.5.4

安装micro

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

#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请自行安装gitlinux

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

安装 protoc

访问以下网址git

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

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

protoc-3.9.1-win64.zip

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

e:\dev\protoc-3.9.1-win64

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

安装protoc-gen-micro插件

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

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

安装 consul

下载windows版本api

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插件请求接口
rested接口

推荐阅读

扫微信二维码实现网站登录提供体验地址和源代码

开源项目golang go语言后台管理框架restgo-admin

支持手势触摸,可左右滑动的日历插件

你必须知道的18个互联网业务模型
推荐阅读
扫微信二维码实现网站登录提供体验地址和源代码
开源项目golang go语言后台管理框架restgo-admin
支持手势触摸,可左右滑动的日历插件
你必须知道的18个互联网业务模型

相关文章
相关标签/搜索