1.go-micro版本2.9
2.micro版本2.9.3
3.golang1.51环境配置
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\aaa\AppData\Local\go-build
set GOENV=C:\Users\aaa\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\goResources\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\goResources
set GOPRIVATE=
set GOPROXY=https://goproxy.cn
set GOROOT=D:\Go\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=D:\Go\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
运行代码出现下面的错误
4.文件目录结构:
5.下面我测试的helloworld代码,运行报错:
(1)sever.go
package main
import (
"context"
"fmt"
"github.com/micro/go-micro"
helloworld "gocode/basic-code/go-high/微服务/go-micro/go-Micro-helloworld/proto"
)
type HW struct {
}
func (g*HW)Hello(ctx context.Context,req*helloworld.HelloRequest,rsp *helloworld.HelloResponse)error{
rsp.Greeting="nihao:"+req.Name
return nil
}
func main(){
service:=micro.NewService(micro.Name("HelloWorld"),
micro.Version("1.0"),
micro.Metadata(map[string]string{"type":"helloworld"}))
service.Init()
helloworld.RegisterHelloWorldHandler(service.Server(),new(HW))
if err:=service.Run();err!=nil{
fmt.Println(err)//运行服务
}
}
(2)Client.go
package main
import (
"context"
"fmt"
"github.com/micro/go-micro"
helloworld "gocode/basic-code/go-high/微服务/go-micro/go-Micro-helloworld/proto"
)
func main(){
service:=micro.NewService(micro.Name("HelloWorld"),
micro.Version("1.0"),
micro.Metadata(map[string]string{"type":"helloworld"}))
service.Init()
greeter:=helloworld.NewHelloWorldService("HelloWorld",service.Client())
rep,err:=greeter.Hello(context.TODO(),&helloworld.HelloRequest{Name:"haha "})
if err!=nil{
fmt.Println(err)
}
fmt.Println(rep.Greeting)
}
(3)proto下的helloworld.proto
syntax="proto3";
service HelloWorld{
rpc Hello(HelloRequest) returns(HelloResponse){}
}
message HelloRequest{
string name=1;
}
message HelloResponse{
string greeting=2;
}
然后根据上面的自动生成相关代码
6.运行报错