当前位置:网站首页>go-micro教程 — 第一章 快速入门
go-micro教程 — 第一章 快速入门
2022-06-30 15:48:00 【西木Qi】
go-micro教程 — 第一章 快速入门
一、所需依赖
go-micro 需要 protoc-gen-micro 来生成代码。所以需要一下依赖:
- protobuf
- protoc-gen-go
- protoc-gen-micro
下载最新版本的依赖
go get https://github.com/protocolbuffers/protobuf/releases
go get github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro
二、安装 micro
目前最新版本的micro是3.0版本的,所以这里我们安装的也是3.0版本的micro。
2.1 Go Get方式安装:
go install github.com/micro/micro/[email protected]
2.2 二进制版本
# MacOS
curl -fsSL https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh | /bin/bash
# Linux
wget -q https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh -O - | /bin/bash
# Windows
powershell -Command "iwr -useb https://raw.githubusercontent.com/micro/micro/master/scripts/install.ps1 | iex"
2.3 Docker 镜像
docker pull micro/micro
三、运行 micro 服务
在开始编写服务之前,让我们运行一个现有的服务,因为它只需几个命令!
3.1 启动 micro server 服务
首先,我们必须启动micro server。执行此操作的命令是:
micro server
3.2 登录
在使用 micro server 前需要先登录服务,默认的账号密码如下:
账号/密码:admin/micro
E:\Goland\go-micro-demo>micro login
Enter username: admin
Enter password:
Successfully logged in.
查看 micro server 中正在运行的服务,命令:
E:\Goland\go-micro-demo>micro services
api
auth
broker
config
events
network
proxy
registry
runtime
store
web
一共11个,所有这些服务都是micro server中的子模块,在浏览器端查看 localhost:8082 。
这很酷,但它仍然不是我们推出的东西!让我们开始一项服务,我们可以真正相信它的存在。如果我们去github.com/micro/services,我们会看到一堆由微作者编写的服务。其中之一是helloworld。
3.3 运行 helloworld
运行服务的命令是micro run。
micro run github.com/micro/services/helloworld
查看正在运行的服务的状态:
E:\Goland\go-micro-demo>micro status
NAME VERSION SOURCE STATUS BUILD UPDATED METADATA
helloworld latest github.com/micro/services/helloworld running n/a 4s ago owner=admin, group=micro
我们还可以查看服务的日志以验证它是否正在运行:
E:\Goland\go-micro-demo>micro logs helloworld
2022-6-06 17:52:21 file=service/service.go:195 level=info Starting [service] helloworld
2022-6-06 17:52:21 file=grpc/grpc.go:902 level=info Server [grpc] Listening on [::]:33975
2022-6-06 17:52:21 file=grpc/grpc.go:732 level=info Registry [service] Registering node: helloworld-67627b23-3336-4b92-a032-09d8d13ecf95
四、调用服务
Micro 自动为您的服务生成 CLI 命令,格式为:micro [service] [method],默认方法为“调用”。参数可以作为标志传递,因此我们可以使用以下方式调用我们的服务:
$ micro helloworld call --name=Jane
{
"msg": "Hello Jane"
}
如果我们想知道服务有哪些节点,我们可以运行以下命令:
E:\Goland\go-micro-demo>micro helloworld --help
NAME:
micro helloworld
VERSION:
latest
USAGE:
micro helloworld [command]
COMMANDS:
call
使用 API 方式调用:
Micro 在 8080端口上开放了一个 http API,所以可以使用 curl 方式调用。
curl "http://localhost:8080/helloworld?name=John"
边栏推荐
- 【牛客网刷题系列 之 Verilog快速入门】~ 位拆分与运算
- 2022 Blue Bridge Cup group B -2022- (01 backpack to calculate the number of schemes)
- 24:第三章:开发通行证服务:7:自定义异常(来表征程序中出现的错误);创建GraceExceptionHandler来全局统一处理异常(根据异常信息,构建对应的API统一返回对象的,JSON数据);
- What is XR extended reality and what are the XR cloud streaming platforms
- POJ Project Summer
- 华为帐号多端协同,打造美好互联生活
- 为了使远程工作不受影响,我写了一个内部的聊天室 | 社区征文
- Li Zexiang, a legendary Chinese professor, is making unicorns in batches
- How to connect the Internet Reading Notes - Summary
- The inspiration from infant cognitive learning may be the key to the next generation of unsupervised machine learning
猜你喜欢

Installing jupyter notebook under Anaconda
MySQL开放远程连接权限的两种方法

Mathematical modeling for war preparation 33- grey prediction model 2

Tencent two sides: @bean and @component are used on the same class. What happens?

Symantec electronic sprint technology innovation board: Tan Jian, the actual controller, is an American who plans to raise 620million yuan

What is XR extended reality and what are the XR cloud streaming platforms

24:第三章:开发通行证服务:7:自定义异常(来表征程序中出现的错误);创建GraceExceptionHandler来全局统一处理异常(根据异常信息,构建对应的API统一返回对象的,JSON数据);

牛客网:有多少个不同的二叉搜索树

中国传奇教授李泽湘,正在批量制造独角兽

Raft介绍
随机推荐
Additional: (not written yet, don't look at ~ ~ ~) corsfilter filter;
19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project
【牛客网刷题系列 之 Verilog快速入门】~ 位拆分与运算
附加:(还没写,别看~~~)WebMvcConfigurer接口;
dart:字符串replace相关的方法解决替换字符
MySQL master-slave configuration
Rong Lianyun launched rphone based on Tongxin UOS to create a new ecology of localization contact center
更多龙蜥自研特性!生产可用的 Anolis OS 8.6 正式发布
Unsupported major. minor version 52.0
halcon知识:矩阵专题【02】
Li Zexiang, a legendary Chinese professor, is making unicorns in batches
2022蓝桥杯国赛B组-费用报销-(线性dp|状态dp)
牛客网:乘积为正数的最长连续子数组
华为帐号多端协同,打造美好互联生活
I 用c I 实现“栈”
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
Interpretation of gaussdb's innovative features: partial result cache accelerates operators by caching intermediate results
halcon知识:区域专题【07】
Symantec electronic sprint technology innovation board: Tan Jian, the actual controller, is an American who plans to raise 620million yuan
register_ Chrdev and CDEV_ init cdev_ Add usage differences