restclient

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2020 License: MIT Imports: 15 Imported by: 0

README

restclient使用方法

安装

go get github.com/hanguangbaihuo/sparrow_cloud_go/

发送Get请求示例

import "github.com/hanguangbaihuo/sparrow_cloud_go/restclient"

func processGet(ctx iris.Context) {
    serviceAddr := "sparrow-product-svc:8001"
    apiPath := "/api/sparrow_products/products/show/"
    res, err := restclient.Get(ctx, serviceAddr, apiPath, nil)
    if err != nil {
    // do something
    }
    fmt.Println(res.Body, res.Code)
}

发送Post请求示例

import "github.com/hanguangbaihuo/sparrow_cloud_go/restclient"

type Data struct { 
	Name string  `json:"name"` 
	Price int  `json:"price"` 
	Offer int  `json:"offer"` 
}

func processPost(ctx iris.Context) {
    serviceAddr := "sparrow-product-svc:8001"
    apiPath := "/api/sparrow_products/products/create/"
    data := Data{"test", 99, 0}
    res, err := restclient.Post(ctx, serviceAddr, apiPath, data)
    if err != nil {
    // do something
    }
    fmt.Println(res.Body, res.Code)
}

restclient中的方法

目前该package共有5个方法,分别是Get,Post,Put,Patch,Delete方法
每个方法的参数和返回值完全一样,是XXX(ctx context.Context, serviceAddr string, apiPath string, payload interface{}, kwargs ...map[string]string) (Response, error)
参数用途:
ctx:用来获取中间件中存储的数据信息,此处用来获取追踪链中的数据
serviceAddr:跨服务调用的服务地址,格式类似"sparrow-product-svc:8001"
apiPath:请求服务的api,格式类似"/api/sparrow_products/products/create/"
payload:请求服务接口所需要的数据
kwargs:用来或者添加一些额外信息,见下方的kwargs详解
方法参数中kwargs详解
kwargs类型是map[string]string
其中参数包括以下几部分:
timeout:建立连接和发送接收数据超时设置,默认为10s
protocol:默认为"http",构建url所用
token:是否添加到请求头中的Authorization,如果设置,则请求头中的Authorization为"token "+token,默认为空
Content-Type:默认为"application/json"
Accept: 默认为"application/json"
operationname:追踪链中的操作名称,用来识别本次跨服务调用的用途,如果未设置,则默认为目标url。

举例:
kwargs := map[string]string{"timeout":"10","operationname":"create_product"}
res, err := restclient.Get(ctx, serviceAddr, apiPath, nil, kwargs)

方法返回Response

返回的结构体如下:
type  Response  struct {
	Body string
	Code int
}
Body是返回的数据
Code是返回的状态码

需要讨论是否将Body类型从string改为[]byte,便于json解析

本地代理

直接在环境变量添加http_proxy
例如http_proxy="12.34.56.78:8888" go run main.go

追踪链使用方法

// 先在iris的应用中添加追踪链中间件,见github.com/hanguangbaihuo/sparrow_cloud_go/middleware/opentracing/

import "github.com/hanguangbaihuo/sparrow_cloud_go/restclient"
func main() {
    // 追踪链配置:初始化Jaeger为GlobalTracer
    closer := opentracing.InitGlobalTracer("YourServiceName")
    defer closer.Close()
    // 初始化iris app
    app := iris.New()
    // 使用opentracing中间件,从header中提取父span,并存储至中间件
    app.Use(opentracing.Serve("YourServiceName"))
    ...
    app.Get("/test", processRequest)
    app.Listen()
}

func processRequest(ctx iris.Context) {
    serviceAddr := "sparrow-product-svc:8001"
    apiPath := "/api/sparrow_products/products/show/"
    res, err := restclient.Get(ctx, serviceAddr, apiPath, nil)
    if err != nil {
    // do something
    }
    fmt.Println(res.Body, res.Code)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithSpan

func ContextWithSpan(span opentracing.Span)

ContextWithSpan set the global parent span in spaopentracing middleware ActiveSpan.

func SpanFromContext

func SpanFromContext() opentracing.Span

SpanFromContext returns the `Span` previously setted by opentracing middleware, or `nil` if no such `Span` could be found.

func StartSpanFromContext

func StartSpanFromContext(operationName string, opts ...opentracing.StartSpanOption) opentracing.Span

StartSpanFromContext starts and returns a Span with `operationName`, using any Span found within `ctx` as a ChildOfRef. If no such parent could be found, StartSpanFromContext creates a root (parentless) Span.

The second return value is a context.Context object built around the returned Span.

Example usage:

SomeFunction(ctx context.Context, ...) {
    sp, ctx := opentracing.StartSpanFromContext(ctx, "SomeFunction")
    defer sp.Finish()
    ...
}

func StartSpanFromContextWithTracer

func StartSpanFromContextWithTracer(tracer opentracing.Tracer, operationName string, opts ...opentracing.StartSpanOption) opentracing.Span

StartSpanFromContextWithTracer starts and returns a span with `operationName` using a span found within the context as a ChildOfRef. If that doesn't exist it creates a root span. It also returns a context.Context object built around the returned span.

It's behavior is identical to StartSpanFromContext except that it takes an explicit tracer as opposed to using the global tracer.

Types

type Response

type Response struct {
	Body string
	Code int
}

func Delete

func Delete(serviceAddr string, apiPath string, payload interface{}, kwargs ...map[string]string) (Response, error)

func Get

func Get(serviceAddr string, apiPath string, payload interface{}, kwargs ...map[string]string) (Response, error)

func Patch

func Patch(serviceAddr string, apiPath string, payload interface{}, kwargs ...map[string]string) (Response, error)

func Post

func Post(serviceAddr string, apiPath string, payload interface{}, kwargs ...map[string]string) (Response, error)

func Put

func Put(serviceAddr string, apiPath string, payload interface{}, kwargs ...map[string]string) (Response, error)

Jump to

Keyboard shortcuts

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