当前位置:网站首页>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"
边栏推荐
- ArcMap operation series: 80 plane to latitude and longitude 84
- Rong Lianyun launched rphone based on Tongxin UOS to create a new ecology of localization contact center
- Table responsive layout tips for super nice
- Mysql8 error: error 1410 (42000): you are not allowed to create a user with grant solution
- MC Instruction Decoder
- 2022蓝桥杯国赛B组-费用报销-(线性dp|状态dp)
- 如何得到股票开户的优惠活动?在线开户安全么?
- 搬运两个负载均衡的笔记,日后省的找
- MC Instruction Decoder
- How cloudxr promotes the future development of XR
猜你喜欢

Raft介绍

Rong Lianyun launched rphone based on Tongxin UOS to create a new ecology of localization contact center

KDD 2022 | how far are we from the general pre training recommendation model? Universal sequence representation learning model unisrec for recommender system

Rongsheng biology rushes to the scientific innovation board: it plans to raise 1.25 billion yuan, with an annual revenue of 260million yuan

go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)

9:第三章:电商工程分析:4:【通用模块】;(待写……)

RT-Thread 堆区大小设置

搬运两个负载均衡的笔记,日后省的找

19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project

牛客网:乘积为正数的最长连续子数组
随机推荐
POJ Project Summer
[time series database incluxdb] code example for configuring incluxdb+ data visualization and simple operation with C under Windows Environment
抖快B为啥做不好综艺
MySQL开放远程连接权限的两种方法
I implement "stack" with C I
附加:(还没写,别看~~~)WebMvcConfigurer接口;
Distributed machine learning: model average Ma and elastic average easgd (pyspark)
RTP sending PS stream zero copy scheme
Bidding announcement: remote disaster recovery project of Shenzhen Finance Bureau database
如何得到股票开户的优惠活动?在线开户安全么?
How to connect the Internet Reading Notes - Summary
Niuke.com: minimum cost of climbing stairs
Create statement for Oracle export view
15年做糊21款硬件,谷歌到底栽在哪儿?
How the edge computing platform helps the development of the Internet of things
华为帐号多端协同,打造美好互联生活
Mathematical modeling for war preparation 33- grey prediction model 2
Compulsory national standard for electronic cigarette GB 41700-2022 issued and implemented on October 1, 2022
POJ Project Summer
Three development trends of enterprise application viewed from the third technological revolution