1. Basic environment
Remarks, I uninstalled the go environment first, and deleted
go path
all the files in the directory to configure. If you have the previous environment, it may affect each other.
-
mac
operating system -
go
version ofgo version go1.14.14 darwin/amd64
-
The editor uses
goland2020.1
Second, protobuf
the configuration of the environment
-
2. Use the command to
go env
view yourgo path
address andgo path
create abin
folder in the directory -
3. Unzip the thing downloaded from the first point
bin
andprotoc
copy the following to thego path
nextbin
folder -
4. Check the installed version
protoc --version
-
5.
protoc-gen-go
Installation (used to generate code for the correspondinggo
languagemicro
)go get -u github.com/golang/protobuf/proto go get -u github.com/golang/protobuf/protoc-gen-go go get github.com/micro/micro/v2/cmd/protoc-gen-micro
-
6. There will be the following files
go path
in thebin
directory under check➜ bin tree . . ├── protoc ├── protoc-gen-go └── protoc-gen-micro 0 directories, 3 files ➜ bin
Three, testprotobuf
-
1. Create a
say.proto
filesyntax = "proto3"; package go_micro_demo; // 创建服务 service Say { rpc SayHello(SayReq) returns (SayRes) {} } message SayReq { string message = 1; } message SayRes { string answer = 1; }
-
2. Execute the command in the black window where the file is located
# 使用下面这个命令会生成一个say.pb.go的文件 # protoc 文件名 --go_out=. protoc say.proto --go_out=. # 如果要生成python文件 # protoc say.proto --python_out=. # 生成微服务需要的文件 protoc --go_out=. --micro_out=. say.proto
-
3. Finally, you can generate such a file directory to indicate that your configuration is successful
➜ say tree . . ├── say.pb.go ├── say.pb.micro.go └── say.proto 0 directories, 3 files ➜ say
Fourth, the first simple microservice program
-
1. Use to
goland
create amod
project -
2. Basic directory
➜ go_micro_demo tree . . ├── client.go ├── go.mod ├── proto │ └── say │ └── say.proto └── service.go 2 directories, 4 files
-
3. Use the above command to
proto
generate the correspondinggo
code from the file -
4. Install the microservice dependency package
go get github.com/micro/go-micro/v2
-
5. Writing on the server side
package main import ( "context" "fmt" "github.com/micro/go-micro/v2" go_micro_demo "go_micro_demo/proto/say" ) type SayServer struct { } // SayHello方法去say.pb.micro.go文件中(RegisterSayHandler)中复制 func (s *SayServer) SayHello(ctx context.Context, in *go_micro_demo.SayReq, out *go_micro_demo.SayRes) error { out.Answer = "服务器端接收的数据:\"" + in.Message + "\"" return nil } func main() { // 1.创建服务 service := micro.NewService(micro.Name("say.hello.server")) // 2.初始化服务 service.Init() // 3.注册微服务 go_micro_demo.RegisterSayHandler(service.Server(), new(SayServer)) // 4.运行微服务 if err := service.Run(); err != nil { fmt.Println(err) } }
-
6, the preparation of the client
package main import ( "context" "fmt" "github.com/micro/go-micro/v2" go_micro_demo "go_micro_demo/proto/say" ) func main() { // 1.实例化 service := micro.NewService(micro.Name("say.hello.client")) // 2.初始化 service.Init() // 3.连接到对应的服务端 sayServer := go_micro_demo.NewSayService("say.hello.server", service.Client()) // 4.调用方法通讯 res, err := sayServer.SayHello(context.TODO(), &go_micro_demo.SayReq{ Message: "我是客户端"}) if err != nil { fmt.Println("连接错误") } fmt.Println(res) }
Five, use the docker
created microservice project
-
1. Find the mirror and download the mirror
docker search micro/micro docker pull micro/micro
-
2. Use
docker
commands to create microservice projectsRun in the folder where you need to create the project
# docker run --rm -v $(pwd):$(pwd) -w $(pwd) micro/micro new [项目名称] # 下面是创建一个用户项目 docker run --rm -v $(pwd):$(pwd) -w $(pwd) micro/micro new user
-
3.
proto
Generatego
files from files-
First
proto
modify the file to// 表示生成一个user的文件夹生成user的文件 option go_package = "user;user";
-
Run command
protoc --go_out=. --micro_out=. user.proto
-
-
4. Change the created project
v3
tov2
version-
go.mod
Modified places in the file// github.com/micro/micro/v3 v3.0.0 github.com/micro/go-micro/v2 v2.9.1
-
Modify
main.go
file
-
Modified
handler
external files
-
-
5. Download the latest dependency package
go mod tidy
-
6. Run
main.go
file test