Skip to content

老陈是一个普通的文艺二逼青年

For The Dream

初识micro——安装入门

Written by chen

一、准备工作

开启代理(因为某些原因),下载源代码。

1
2
3
$ /etc/init.d/cow start
$ enableproxy
 

二、安装

安装micro,以及相关服务consul、protobuf。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 安装micro
$ go get -u -v github.com/micro/micro
 
# 安装 brew
$ sudo apt install linuxbrew-wrapper
 
# 安装consul
$ brew install consul
 
# 安装 protobuf
$ brew install protobuf
$ go get -u -v github.com/golang/protobuf/{proto,protoc-gen-go}
$ go get -u -v github.com/micro/protoc-gen-micro
 

三、运行

3.1、安装案例

安装完成后,我们可以下载官方案例,然后进行运行查看是否安装成功。

1
2
3
# 案例
$ go get -u -v github.com/micro/examples
 

3.2、启动相关服务

1
2
3
4
5
6
# 启动 consul 带UI启动
 
$ mkdir /etc/consul.d/ # 建立配置文件目录,为了以后增加配置
 
$ consul agent -server -bootstrap -data-dir /tmp/consul/ -node=web  -config-dir /etc/consul.d/ -client 0.0.0.0 -ui >/dev/null 2>&1 &
 

3.3、运行hello world

1
2
3
4
5
6
7
8
9
# 启动service
$ go run src/github.com/micro/examples/helloworld/main.go
 
# 请求访问
$ micro call greeter Greeter.Hello '{"name": "John"}'
{
    "greeting": "Hello John"
}
 

3.4、 其他方式

serve-client 模式

1
2
3
4
5
6
7
# serve
$ go run src/github.com/micro/examples/greeter/srv/main.go
 
# clinet
$ go run src/github.com/micro/examples/greeter/cli/main.go
Hello John
 

api 模式 (基于启动serve)

1
2
3
4
5
6
7
8
9
10
# api
$ go run src/github.com/micro/examples/greeter/api/api.go
 
# 启动api服务
$ micro api
 
# 访问
$ curl http://localhost:8080/greeter/say/hello?name=John
{"message":"Hello John"}
 

web模式(基于启动serve)

1
2
3
4
5
6
7
8
$ go get -u -v github.com/micro/go-web # 安装go-web服务
# web
$ go run src/github.com/micro/examples/greeter/web/web.go
# 启动web服务
$ micro web
# 访问
http://localhost:8082/greeter
 

micro · golang, micro

Copyright © 2022 老陈是一个普通的文艺二逼青年. 沪ICP备13044041号-1