可以使用官网的获取最新二进制方式来实现:
# MacOS
curl -fsSL https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh | /bin/bash
# Linux
wget -q https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh -O - | /bin/bash
# Windows
powershell -Command "iwr -useb https://raw.githubusercontent.com/micro/micro/master/scripts/install.ps1 | iex"
cd $GOPATH/pkg
sudo rm -rf *
ls
go get -u -v github.com/golang/protobuf@latest
ls -la $GOPATH/bin | grep protoc-gen-go
由于网络原因,需要多执行几次
go get -u -v github.com/micro/micro/v2
最后会有如下错误,是由于go.mod的异常导致的,可以暂时不管
go get: github.com/mholt/[email protected] updating to
github.com/mholt/[email protected]: parsing go.mod:
module declares its path as: github.com/caddyserver/certmagic
but was required as: github.com/mholt/certmagic
cd $GOPATH/pkg/mod/github.com/micro/micro/v2*
# explain why packages or modules are needed(解释为什么需要依赖)
go mod why
# add missing and remove unused modules(拉取缺少的模块,移除不用的模块)
go mod tidy
# verify dependencies have expected content (验证依赖是否正确)
go mod verify
sudo $GOROOT/bin/go build -o /bin/micro main.go
cd ~
micro --version
为了方便访问,指定IP
ifconfig
consul agent -dev -client=192.168.56.14
mkdir -p $GOPATH/src/www/kokutas/com/pb
创建service【srv】服务,并自定义命名空间www.kokutas.com【默认是go.micro】
1.服务名称的最后一级不要使用下划线,否则生成的handler/最后一级目录名.go中的服务还要做修改,【protobuf生成的时候的服务命名规则问题】
2.type的类型不再是之前的srv,而是service
3.默认为当前路径创建,不再使用$GOPATH的路径,如果需要的话,需要自行先进入$GOPATH路径
cd $GOPATH/src
micro new --type service --namespace www.kokutas.com www/kokutas/com/user
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
go get -u github.com/micro/protoc-gen-micro/v2
修改$GOPATH/src/www/kokutas/com/user/proto/user/user.proto不然老是编译的时候提示警告信息
package www.kokutas.com.service.user;
option go_package = "proto/user;www_kokutas_com_service_user";
cd $GOPATH/src
cd www/kokutas/com/user
tree $GOPATH/src/www/kokutas/com/
protoc --proto_path=.:$GOPATH/src --go_out=$GOPATH/src/www/kokutas/com/pb --micro_out=$GOPATH/src/www/kokutas/com/pb proto/user/user.proto
方便后续导包异常的处理【将proto文件从本地导入(pb.go文件所在目录)】
tree $GOPATH/src/www/kokutas/com
cd $GOPATH/src/www/kokutas/com/pb/proto/user/ && go mod init www/kokutas/com/pb/proto/user && cd $GOPATH/src/www/kokutas/com/user
explain why packages or modules are needed(解释为什么需要依赖)
cd $GOPATH/src/www/kokutas/com/user
go mod why
www/kokutas/com/user imports
www/kokutas/com/user/proto/user: package www/kokutas/com/user/proto/user is not in GOROOT (/usr/local/go/src/www/kokutas/com/user/proto/user)
修改$GOPATH/src/www/kokutas/com/user下的go.mod 文件
追加【本地只能是相对路径(=>后面的部分必须是相对路径),换行符不要动】
cd $GOPATH/src/www/kokutas/com/user
tee -a $GOPATH/src/www/kokutas/com/user/go.mod <<-'EOF'
// 添加是针对proto文件的本地包导入问题处理:只有这个能被go mod why检测到
require "www/kokutas/com/user/proto/user" v0.0.0
replace "www/kokutas/com/user/proto/user" => "../pb/proto/user"
// 解决go get -u -v github.com/micro/micro/v2的问题:
// module declares its path as: github.com/caddyserver/certmagic
// but was required as: github.com/mholt/certmagic
replace github.com/mholt/certmagic => github.com/caddyserver/certmagic latest
EOF
再次执行
go mod why
add missing and remove unused modules(拉取缺少的模块,移除不用的模块)
cd $GOPATH/src/www/kokutas/com/user
go mod tidy
verify dependencies have expected content (验证依赖是否正确)
cd $GOPATH/src/www/kokutas/com/user
go mod verify
print module requirement graph (打印模块依赖图)
cd $GOPATH/src/www/kokutas/com/user
go mod graph
修改的是$GOPATH/src/www/kokutas/com/user/main.go
package main
import (
"www/kokutas/com/user/handler"
"www/kokutas/com/user/subscriber"
"github.com/micro/go-micro/v2"
log "github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/registry"
user "www/kokutas/com/user/proto/user"
// 添加注册插件的consul支持
"github.com/micro/go-plugins/registry/consul/v2"
)
func main() {
// 新建consul注册器
consulReg := consul.NewRegistry(
// 注册的consul信息
registry.Addrs("192.168.56.14:8500"),
)
// New Service
service := micro.NewService(
micro.Name("www.kokutas.com.service.user"),
micro.Version("latest"),
// 服务添加consul支持
micro.Registry(consulReg),
)
// Initialise service
service.Init()
// Register Handler
user.RegisterUserHandler(service.Server(), new(handler.User))
// Register Struct as Subscriber
micro.RegisterSubscriber("www.kokutas.com.service.user", service.Server(), new(subscriber.User))
// Run service
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
$GOPATH/src/www/kokutas/com/user/go.mod
module www/kokutas/com/user
go 1.14
require github.com/micro/go-micro/v2 v2.5.0
require (
github.com/micro/go-micro v1.18.0 // indirect
github.com/micro/go-plugins/registry/consul/v2 v2.5.0 // indirect
google.golang.org/protobuf v1.21.0 // indirect
// 添加是针对proto文件的本地包导入问题处理:只有这个能被go mod why检测到
www/kokutas/com/user/proto/user v0.0.0
)
replace www/kokutas/com/user/proto/user => ../pb/proto/user
// 解决go get -u -v github.com/micro/micro/v2的问题:
// module declares its path as: github.com/caddyserver/certmagic
// but was required as: github.com/mholt/certmagic
replace github.com/mholt/certmagic => github.com/caddyserver/certmagic v0.10.12
cd $GOPATH/src/www/kokutas/com/user
go run main.go --registry=consul
新开一个终端,创建client【web】服务,并自定义命名空间www.kokutas.com【默认是go.micro】
cd $GOPATH/src
micro new --type web --namespace www.kokutas.com www/kokutas/com/web
explain why packages or modules are needed(解释为什么需要依赖)
cd $GOPATH/src
cd www/kokutas/com/web
go mod why
go: finding module for package github.com/micro/go-micro/v2/logger
go: finding module for package github.com/micro/go-micro/v2/client
go: finding module for package github.com/micro/go-micro/v2/web
go: found github.com/micro/go-micro/v2/logger in github.com/micro/go-micro/v2 v2.5.0
www/kokutas/com/web/handler imports
path/to/service/proto/web: package path/to/service/proto/web is not in GOROOT (/usr/local/go/src/path/to/service/proto/web)
修改$GOPATH/src/www/kokutas/com/web下的go.mod 文件
追加【本地只能是相对路径(=>后面的部分必须是相对路径),换行符不要动】
tee -a $GOPATH/src/www/kokutas/com/web/go.mod <<-'EOF'
// 添加是针对proto文件的本地包导入问题处理:只有这个能被go mod why检测到
require "path/to/service/proto/web" v0.0.0
replace "path/to/service/proto/web" => "../pb/proto/user"
// 解决go get -u -v github.com/micro/micro/v2的问题:
// module declares its path as: github.com/caddyserver/certmagic
// but was required as: github.com/mholt/certmagic
replace github.com/mholt/certmagic => github.com/caddyserver/certmagic latest
EOF
再次执行
go mod why
add missing and remove unused modules(拉取缺少的模块,移除不用的模块)
cd $GOPATH/src/www/kokutas/com/web
go mod tidy
verify dependencies have expected content (验证依赖是否正确)
cd $GOPATH/src/www/kokutas/com/web
go mod verify
print module requirement graph (打印模块依赖图)
cd $GOPATH/src/www/kokutas/com/web
go mod graph
修改的是$GOPATH/src/www/kokutas/com/web/main.go
package main
import (
"net/http"
"www/kokutas/com/web/handler"
log "github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/web"
// 添加注册插件的consul支持
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-plugins/registry/consul/v2"
)
func main() {
// 新建consul注册器
consulReg := consul.NewRegistry(
// 注册的consul信息
registry.Addrs("192.168.56.14:8500"),
)
// create new web service
service := web.NewService(
web.Name("www.kokutas.com.web.web"),
web.Version("latest"),
web.Address("192.168.56.14:8080"),
web.Registry(consulReg),
)
// initialise service
if err := service.Init(); err != nil {
log.Fatal(err)
}
// register html handler
service.Handle("/", http.FileServer(http.Dir("html")))
// register call handler
service.HandleFunc("/web/call", handler.WebCall)
// run service
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
修改$GOPATH/src/www/kokutas/com/web/handler/handler.go
package handler
import (
"context"
"encoding/json"
"net/http"
"time"
web "path/to/service/proto/web"
"github.com/micro/go-micro/v2/client"
)
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
// NewUserService和www.kokutas.com.service.user要与proto/user.pb.go中的一致
webClient := web.NewUserService("www.kokutas.com.service.user", 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
}
}
$GOPATH/src/www/kokutas/com/web/go.mod
module www/kokutas/com/web
go 1.14
require github.com/micro/go-micro/v2 v2.5.0
require (
github.com/micro/go-plugins/registry/consul/v2 v2.5.0 // indirect
google.golang.org/protobuf v1.21.0 // indirect
// 添加是针对proto文件的本地包导入问题处理:只有这个能被go mod why检测到
path/to/service/proto/web v0.0.0
)
replace path/to/service/proto/web => ../pb/proto/user
// 解决go get -u -v github.com/micro/micro/v2的问题:
// module declares its path as: github.com/caddyserver/certmagic
// but was required as: github.com/mholt/certmagic
replace github.com/mholt/certmagic => github.com/caddyserver/certmagic v0.10.12
cd $GOPATH/src/www/kokutas/com/web/
go run main.go --registry=consul
浏览器打开:
http://192.168.56.14:8500
http://192.168.56.14:8080