Use docker plus go.mod to build a go-micro V2 microservice project (1)

1. Basic environment

Remarks, I uninstalled the go environment first, and deleted go pathall the files in the directory to configure. If you have the previous environment, it may affect each other.

  • macoperating system

  • goversion of

    go version go1.14.14 darwin/amd64
    
  • The editor usesgoland2020.1

Second, protobufthe configuration of the environment

  • 1. Download the version corresponding to your computer

  • 2. Use the command to go envview your go pathaddress and go pathcreate a binfolder in the directory

  • 3. Unzip the thing downloaded from the first point binand protoccopy the following to the go pathnext binfolder

  • 4. Check the installed version

    protoc --version
    
  • 5. protoc-gen-goInstallation (used to generate code for the corresponding golanguage micro)

    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 pathin the bindirectory under check

    ➜  bin tree .                   
    .
    ├── protoc
    ├── protoc-gen-go
    └── protoc-gen-micro
    
    0 directories, 3 files
    ➜  bin 
    

Three, testprotobuf

  • 1. Create a say.protofile

    syntax = "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 golandcreate a modprojectInsert picture description here

  • 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 protogenerate the corresponding gocode 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 dockercreated microservice project

  • 1. Find the mirror and download the mirror

    docker search micro/micro
    docker pull micro/micro
    
  • 2. Use dockercommands to create microservice projects

    Run 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. protoGenerate gofiles from files

    • First protomodify the file to

      // 表示生成一个user的文件夹生成user的文件
      option go_package = "user;user";
      
    • Run command

      protoc --go_out=. --micro_out=. user.proto
      
  • 4. Change the created project v3to v2version

    • go.modModified places in the file

      // github.com/micro/micro/v3 v3.0.0
      github.com/micro/go-micro/v2 v2.9.1
      
    • Modify main.gofile
      Insert picture description here

    • Modified handlerexternal files
      Insert picture description here

  • 5. Download the latest dependency package

    go mod tidy
    
  • 6. Run main.gofile test

Guess you like

Origin blog.csdn.net/kuangshp128/article/details/113273218