api

command
v0.0.0-...-fabbfc5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

API

本示例介绍使用Micro API(以下简称API)中的请求处理类型api,这里后面的api意思不是Micro API工具,而是指具体的对外的API接口服务。

另外,我们专门定义有api请求响应的proto文件,api.Request/Response

要使用api类型的API服务,我们得使用这些proto原型。

使用方式

使用protoc生成相应的代码

protoc --proto_path=${GOPATH}/src:. --go_out=. --micro_out=Mgithub.com/micro/go-micro/api/proto/api.proto=github.com/micro/go-micro/v2/api/proto:. api/api.proto 

会在 import_proto_path 这个路径下搜索proto/api.proto中导入proto文件

运行API网关,我们传入api指令运行:

micro api --handler=api

再运行本api服务

go run api.go

调用服务

通过URL /example/call,就会调用go.micro.api.example服务的Example.Call接口

请求头的数据会被传到最终调用的接口

curl -H 'head-1: I am a header' "http://localhost:8080/example/call?name=john"

而POST路由 /example/foo/bar,可以通过它调用go.micro.api.example服务的Foo.Bar接口

curl -H 'Content-Type: application/json' -d '{data:123}' http://localhost:8080/example/foo/bar

设置命名空间

可以通过--namespace指定服务命令空间

micro api --handler=api --namespace=com.foobar.api

或者通过环境变量的方式

MICRO_API_NAMESPACE=com.foobar.api micro api --handler=api

切记,如果启动时指定命名空间,则代码中的服务名也要注意同步修改前缀,即把micro.Name的参数改成对应的命令空间前缀,以便API通过解析路由找到它。

service := micro.NewService(
        micro.Name("com.foobar.api.example"),
)

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL