8.4 go-micro使用
8.4.1 、创建服务端
micro new service helloworld
**8.4.2、初始化服务端项目 **
go mod init helloworld
go mod tidy
下载项目所需依赖
linux中通过make命令下载对应依赖或者编译项目
make proto update tidy
windows中需要依次执行Makefile中的命令
下载包
go get -u google.golang.org/protobuf/proto
go install github.com/golang/protobuf/protoc-gen-go@latest
go install github.com/asim/go-micro/cmd/protoc-gen-micro/v4@latest
protoc --proto_path=. --micro_out=. --go_out=:. proto/helloworld.proto
8.4.3 配置服务端consul
引入consul包
"github.com/go-micro/plugins/v4/registry/consul"
实例化consul
consulReg := consul.NewRegistry()
注册consul
srv := micro.NewService (
micro.Name(service),
micro.Version(version),
micro.Registry(consulReg),
)
服务端main.go完整代码:
package main
import (
"fmt"
"helloworld/handler"
pb "helloworld/proto"
"github.com/asim/go-micro/plugins/registry/consul/v4"
"go-micro.dev/v4"
log "go-micro.dev/v4/logger"
"go-micro.dev/v4/registry" //注意
)
var (
service = "helloworld"
version = "latest"
)
func main() {
consulReg := consul.NewRegistry (
registry.Addrs("192.168.234.132:8500"),
)
// registry.Addrs()
// consulReg := consul.NewRegistry()
fmt.Println(consulReg.Options())
// Create service
srv := micro.NewService(
micro.Name(service),
micro.Address("192.168.0.10:8080"),
micro.Version(version),
micro.Registry(consulReg),
)
srv.Init()
// Register handler
pb.RegisterHelloworldHandler(srv.Server(), new(handler.Helloworld))
// Run service
if err := srv.Run(); err != nil {
log.Fatal(err)
}
}
接着我们再来看一下我们重点需要操作的文件,handle.go,内容如下:
package handler
import (
"context"
"io"
"time"
log "go-micro.dev/v4/logger"
pb "helloworld/proto"
)
type Helloworld struct{}
func (e *Helloworld) Call(ctx context.Context, req *pb.CallRequest, rsp
*pb.CallResponse) error {
log.Infof("Received Helloworld.Call request: %v", req)
rsp.Msg = "Hello " + req.Name
return nil
}
8.4.4 、使用go-micro创建微服务客户端
微服务客户端可以是我们的gin web,也可以是一个go文件,首先给大家演示一下 go-micro
https://github.com/go-micro/cli
micro new client helloworld
8.4.5、初始化客户端项目
go mod init helloworld-client
go mod tidy
下载项目所需依赖
linux中通过make命令下载对应依赖或者编译项目
make update tidy
windows中需要依次执行Makefile中的命令
下载包
go get -u google.golang.org/protobuf/proto
go install github.com/golang/protobuf/protoc-gen-go@latest
go install github.com/asim/go-micro/cmd/protoc-gen-micro/v4@latest
复制服务端的proto目录到客户端里面
8.4.6 配置客户端consul
我们来看一下客户端的main.go文件,注意service名称需要和服务端定义的统一,引入proto的包名称
需要改为客户端的包名称
package main
import (
"context"
"time"
pb "helloworld-client/proto"
"github.com/asim/go-micro/plugins/registry/consul/v4"
"go-micro.dev/v4"
log "go-micro.dev/v4/logger"
"go-micro.dev/v4/registry"
)
var (
service = "helloworld"
version = "latest"
)
func main() {
consulReg := consul.NewRegistry(
registry.Addrs("192.168.234.132:8500"),
)
// Create service
srv := micro.NewService(
micro.Registry(consulReg),
)
srv.Init()
// Create client
c := pb.NewHelloworldService(service, srv.Client())
for {
// Call service
rsp, err := c.Call(context.Background(), &pb.CallRequest{Name: "John"})
if err != nil {
log.Fatal(err)
}
log.Info(rsp)
time.Sleep(1 * time.Second)
}
}