Post

Micro hello world

go-Micro

1 环境搭建

work version
System windows 8.1
golang go version go1.13.4 windows/amd64
micro micro version 1.18.0
Consul Consul v1.6.2
protoc libprotoc 3.10.1

安装依赖

  • micro

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
      #linux 下
      export GO111MODULE=on
      export GOPROXY=https://goproxy.io
      # windows下设置如下环境变量
      setx GO111MODULE on
      setx GOPROXY https://goproxy.io
    
      # 使用如下指令安装
      go get -u -v github.com/micro/micro
      go get -u -v github.com/micro/go-micro
      $ micro --help;
      NAME:
         micro - A microservice runtime
    
      USAGE:
         micro.exe [global options] command [command options] [arguments...]
    
      VERSION:
         1.18.0
    
  • protoc protoc download

    1
    2
    3
    4
    5
    6
    7
    
        下载依赖
        go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
        go get -u github.com/micro/protoc-gen-micro
        $ protoc  // 查看是否安装成功!
        Usage: D:\file\gopath\bin\bin\protoc.exe [OPTION] PROTO_FILES
        Parse PROTO_FILES and generate output based on the options given:
          -IPATH, --proto_path=PATH   Specify the directory in which to search for
    
  • consul consul download

    1
    2
    3
    
        $ consul --help;
        Usage: consul [--version] [--help] <command> [<args>]
        Available commands are:
    

hello

  • micro new lichaocheng.top/microapp/hello

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
     $ micro new lichaocheng.top/microapp/hello
     Creating service go.micro.srv.hello in D:\file\gopath\src\lichaocheng.top\microapp\hello
    
     .
     ├── main.go
     ├── generate.go
     ├── plugin.go
     ├── handler
     │   └── hello.go
     ├── subscriber
     │   └── hello.go
     ├── proto\hello
     │   └── hello.proto
     ├── Dockerfile
     ├── Makefile
     ├── README.md
     └── go.mod
    
    
     download protobuf for micro:
    
     brew install protobuf
     go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
     go get -u github.com/micro/protoc-gen-micro
    
     compile the proto file hello.proto:
    
     cd D:\file\gopath\src\lichaocheng.top\microapp\hello
     protoc --proto_path=.:$GOPATH/src --go_out=. --micro_out=. proto/hello/hello.proto
    
  • 生成 protoc golang 和 micro golang 代码

    $ pwd /d/file/gopath/src/lichaocheng.top/microapp/hello Cc@ChaoChengLi MINGW64 /d/file/gopath/src/lichaocheng.top/microapp/hello $ protoc –proto_path=. –go_out=. –micro_out=. proto/hello/hello.proto

  • go run 运行

    $ go run main.go 2020-01-08 15:47:06.698568 I | Transport [http] Listening on [::]:7830 2020-01-08 15:47:06.698568 I | Broker [http] Connected to [::]:7831 2020-01-08 15:47:06.718288 I | Registry [mdns] Registering node: go.micro.srv.hello-114e6b46-c746-4ac4-a68c-ed1493afd1ea 2020-01-08 15:47:06.825266 I | Subscribing go.micro.srv.hello-114e6b46-c746-4ac4-a68c-ed1493afd1ea to topic: go.micro.srv.hello 2020-01-08 15:47:06.841276 I | Subscribing go.micro.srv.hello-114e6b46-c746-4ac4-a68c-ed1493afd1ea to topic: go.micro.srv.hello

  • micro api

    注意其中的–namespace 参数,我们每一个微服务都属于一个命名空间,通过 api 暴露出来该命名空间后,满足 go.micro.srv.*格式的微服务都可以访问。如 go.micro.srv.hello 可以通过如下格式访问

    1
    2
    3
    4
    5
    6
    7
    
    $ micro api --namespace=go.micro.srv
    2020-01-08 16:02:18 [api] Registering API Default Handler at /
    2020-01-08 16:02:18 [api] HTTP API Listening on [::]:8080
    2020-01-08 16:02:18 [api] Starting [service] go.micro.api
    2020-01-08 16:02:18 [api] Server [grpc] Listening on [::]:8026
    2020-01-08 16:02:18 [api] Broker [http] Connected to [::]:8027
    2020-01-08 16:02:18 [api] Registry [mdns] Registering node: go.micro.api-3ccde675-c78b-4b20-a0d8-9b2674e2f1b2
    
  • 测试(测试未通过)

    测试报错,待处理

This post is licensed under CC BY 4.0 by the author.