wget https://studygolang.com/dl/golang/go1.13.6.linux-amd64.tar.gz
tar zxvf go1.13.6.linux-amd64.tar.gz
vim /etc/profile
export GOROOT=/usr/local/go
export GOPATH=/www/go/
export PATH=$PATH:${GOROOT}/bin:$GOPATH/bin
export GO111MODULE=on
设置go 代理
export GOPROXY=https://goproxy.io
protoc 编译工具
再GitHub上下载对应版本:
https://github.com/google/protobuf/releases
tar -zxvf protobuf
cd protobuf
./configure --prefix=/usr/local/protobuf
make
make check
make install
配置环境变量
vim /etc/profile
添加:
export PATH=$PATH:/usr/local/protobuf/bin/
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/
保存并执行
source /etc/profile
也添加到下面文件中
~/.profile
配置动态连接库
vim /etc/ld.so.conf
新起一行 添加
/usr/local/protobuf/lib
保存退出后执行
ldconfig
检查是否安装成功
protoc --version
安装 go-micro
go get github.com/micro/go-micro
go get github.com/micro/protoc-gen-micro
go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro
创建 srv 项目
root@hadoop:/www/go/src# micro new --type "srv" day2/micro/rpc/srv
Creating service go.micro.srv.srv in /www/go/src/day2/micro/rpc/srv
.
├── main.go
├── generate.go
├── plugin.go
├── handler
│?? └── srv.go
├── subscriber
│?? └── srv.go
├── proto/srv
│?? └── srv.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 srv.proto:
cd /www/go/src/day2/micro/rpc/srv
protoc --proto_path=.:$GOPATH/src --go_out=. --micro_out=. proto/srv/srv.proto
创建 web
root@hadoop:/www/go/src# micro new --type "web" day2/micro/rpc/web
Creating service go.micro.web.web in /www/go/src/day2/micro/rpc/web
.
├── main.go
├── plugin.go
├── handler
│?? └── handler.go
├── html
│?? └── index.html
├── Dockerfile
├── Makefile
├── README.md
└── go.mod
修改handler
https://blog.csdn.net/weixin_43736299/article/details/90473364
root@hadoop:/www/go/src/day2/micro/rpc/web/handler# vim handler.go
package handler
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/micro/go-micro/client"
//web "path/to/service/proto/web"
//將srv中的proto的文件导入进行通信使用
web "day2/micro/rpc/srv/proto/srv"
)
func WebCall(w http.ResponseWriter, r *http.Request) {
// decode the incoming request as json
var request map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
http.Error(w, err.Error(), 500)
return
}
// call the backend service
//替换掉原有的服务名
webClient := web.NewWebService("go.micro.srv.srv", client.DefaultClient)
//webClient := web.NewWebService("go.micro.srv.web", client.DefaultClient)
rsp, err := webClient.Call(context.TODO(), &web.Request{
Name: request["name"].(string),
})
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// we want to augment the response
response := map[string]interface{}{
"msg": rsp.Msg,
"ref": time.Now().UnixNano(),
}
// encode and write the response as json
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
启动 单机 consul
consul agent -dev
运行程序
go run main.go
关注公众号,有更多精彩内容
有疑问加站长微信联系(非本文作者)