没有输出的输入是不完整的

0%

【微服务Go-micro第一篇】环境配置

go-micro 是一个基于Go语言的分布式框架,可以用来对外提供微服务。
本篇文章介绍如何在linux系统下进行环境配置,并写好自己的第一个helloworld程序。

一、依赖安装

  1. 依赖安装 protoc-gen-micro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 依赖
go get github.com/micro/micro/v2/cmd/protoc-gen-micro@master

// 上述包依赖两个protoc 和protoc-gen-go

// 第一个依赖安装
//[protobuf下载](https://github.com/protocolbuffers/protobuf/releases)
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protoc-3.11.4-linux-x86_64.zip

unzip protoc-3.11.4-linux-x86_64.zip -d /root/go/gotool/

// 路径修改
vim ~/.bash_profile
PATH=/root/go/gotool/bin:$PATH
source ~/.bash_profile

// 检查是否安装成功,如果出现版本号,则说明安装成功。
protoc --version


//第二个依赖protoc-gen-go的安装
go get -u github.com/golang/protobuf/protoc-gen-go
  1. 依赖安装 etcd
1
2
3
4
5
6
7
8
wget https://github.com/etcd-io/etcd/releases/download/v3.4.7/etcd-v3.4.7-linux-amd64.tar.gz

tar -xzf etcd-v3.4.7-linux-amd64.tar.gz

cp etcd* /usr/local/bin/

// 通过如下命令来启动
etcd

要声明etcd来进行注册服务,在bash_profile中添加如下内容

1
export MICRO_REGISTRY=etcd

如果碰到用go get 命令报403错误可以使用代理来解决。

1
2
3
4
5
vim ~/.bash_profile
//添加如下内容

export GOPROXY=https://goproxy.io // 设置代理
source ~/.bash_profile
  1. 通过go-module来管理依赖
    在bash_profile中添加如下内容
1
export GO111MODULE=on

二、helloworld程序

2.1 官方实例的helloworld

1
2
3
4
5
6
go get github.com/micro/micro/v2 

git clone https://github.com/micro/examples.git
cd examples/service
go run main.go
go run main.go --run_client

结果会出现

1
hello john

说明实验成功。

2.2 自己编写helloworld测试

  1. 构造项目
1
2
3
4
5
// 建立项目
mkdir hello && cd hello

// 利用模块来管理相关的依赖。
go mod init hello
  1. 编写服务原型
1
2
3
// 在hello目录下建立服务原型
mkdir proto
vim proto/greeter.proto

greeter.proto 内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
syntax = "proto3";

service Greeter {
rpc Hello(Request) returns (Response) {}
}

message Request {
string name = 1;
}

message Response {
string greeting = 2;
}
  1. 根据服务原型生成对应的代码
1
2
// 在hello目录下执行
protoc --proto_path=. --micro_out=. --go_out=. proto/greeter.proto

此时在hello/proto目录下会有三个文件

1
2
3
greeter.pb.go  
greeter.pb.micro.go
greeter.proto
  1. 编写main.go文件作为服务器代码。
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
30
31
32
33
34
35
36
vim main.go
// 内容如下
package main

import (
"context"
"fmt"

micro "github.com/micro/go-micro/v2"
proto "hello/proto"
)

type Greeter struct{}

func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
rsp.Greeting = "Hello " + req.Name
return nil
}

func main() {
// Create a new service. Optionally include some options here.
service := micro.NewService(
micro.Name("greeter"),
)

// Init will parse the command line flags.
service.Init()

// Register handler
proto.RegisterGreeterHandler(service.Server(), new(Greeter))

// Run the server
if err := service.Run(); err != nil {
fmt.Println(err)
}
}
  1. 编写client.go文件作为客户端代码。
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
30
vim client.go
// 内容如下
package main

import (
"context"
"fmt"

micro "github.com/micro/go-micro/v2"
proto "hello/proto"
)

func main() {
// Create a new service
service := micro.NewService(micro.Name("greeter.client"))
// Initialise the client and parse command line flags
service.Init()

// Create new greeter client
greeter := proto.NewGreeterService("greeter", service.Client())

// Call the greeter
rsp, err := greeter.Hello(context.TODO(), &proto.Request{Name: "kingwen"})
if err != nil {
fmt.Println(err)
}

// Print response
fmt.Println(rsp.Greeting)
}
  1. 运行程序
1
2
3
etcd 
go main.go
go client.go

结果出现

1
hello.kingwen。

说明程序搭建成功。

最后看一下代码结构

1
2
3
4
5
6
7
8
9
10
├── client.go
├── default.etcd
│ └── member
├── go.mod
├── go.sum
├── main.go
└── proto
├── greeter.pb.go
├── greeter.pb.micro.go
└── greeter.proto

三、参考链接

  1. 基于 Go Module 管理依赖并将注册中心调整为 Etcd
  2. go-micro-github地址
  3. micro官方文档