日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/11/29 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微服务框架---搭建 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] <command> [<args>] Available commands are:acl Interact with Consul's ACLsagent Runs a Consul agentcatalog Interact with the catalogconfig Interact with Consul's Centralized Configurationsconnect Interact with Consul Connectdebug 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.moddownload 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-microcompile 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.測試

轉載于:https://www.cnblogs.com/Paul-watermelon/p/11463128.html

總結

以上是生活随笔為你收集整理的微服务框架---搭建 go-micro环境的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。