python微服务框架 tortues_微服务框架---搭建 go-micro环境

1.安装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

2.安装 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

3.安装protoc-gen-micro插件

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

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

4.安装 consul

//下载windows版本

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

//解压到

e:\dev\consul

添加e:\dev\consul到环境变量path

使用如下指查看是否安装成功,如下所示安装成功

>consul

Usage: consul [--version] [--help] []

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

5.创建微服务 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

6.生成适配proto的golang代码

#切换到项目目录下

>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

7.启动restful api接口支持支持

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

# 访问http://127.0.0.1:8080/hello/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

8.测试

1493082-20190905104203353-197386462.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值