golang web开发之resty模块


1. Resty简介

微服务开发中服务间调用的主流方式有两种HTTP、RPC,HTTP相对来说比较简单。本文将使用 Resty 包来实现基于HTTP的微服务调用。

Resty 是一个简单的HTTP和REST客户端工具包,简单是指使用上非常简单。Resty在使用简单的基础上提供了非常强大的功能,涉及到HTTP客户端的方方面面,可以满足我们日常开发使用的大部分需求。

go get安装

go get github.com/go-resty/resty/v2

使用Resty提交HTTP请求

package main

import (
  "fmt"
  "github.com/go-resty/resty/v2"
)

func main() {
  client := resty.New()
  resp, err := client.R().Get("http://httpbin.org/get")
  fmt.Printf("\nError: %v", err)
  fmt.Printf("\nResponse Status Code: %v", resp.StatusCode())
  fmt.Printf("\nResponse Status: %v", resp.Status())
  fmt.Printf("\nResponse Time: %v", resp.Time())
  fmt.Printf("\nResponse Received At: %v", resp.ReceivedAt())
  fmt.Printf("\nResponse Body: %v", resp)   
}

执行输出:

Error: <nil>
Response Status Code: 200
Response Status: 200 OK
Response Time: 752.38195ms
Response Received At: 2020-12-18 16:04:16.211559068 +0800 CST m=+0.753544183
Response Body: {
  "args": {}, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Host": "httpbin.org", 
    "User-Agent": "go-resty/2.3.0 (https://github.com/go-resty/resty)", 
    "X-Amzn-Trace-Id": "Root=1-5fdc6280-1de9ea8f1558a5545c902521"
  }, 
  "origin": "212.129.130.147", 
  "url": "http://httpbin.org/get"
}

2. GET方法

client := resty.New()

resp, err := client.R().
    Get("https://httpbin.org/get")

resp, err := client.R().
      SetQueryParams(map[string]string{
          "page_no": "1",
          "limit": "20",
          "sort":"name",
          "order": "asc",
          "random":strconv.FormatInt(time.Now().Unix(), 10),
      }).
      SetHeader("Accept", "application/json").
      SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F").
      Get("/search_result")

resp, err := client.R().
      SetQueryString("productId=232&template=fresh-sample&cat=resty&source=google&kw=buy a lot more").
      SetHeader("Accept", "application/json").
      SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F").
      Get("/show_product")

resp, err := client.R().
      SetResult(AuthToken{}).
      ForceContentType("application/json").
      Get("v2/alpine/manifests/latest")

Resty 提供 SetQueryParams 方法设置请求的查询字符串,使用 SetQueryParams
方法我们可以动态的修改请求参数。

  • SetQueryString 也可以设置请求的查询字符串,如果参数中有变量的话,需要拼接字符串。
  • SetHeader 设置请求的HTTP头,以上代码设置了 Accept 属性。
  • SetAuthToken 设置授权信息,本质上还是设置HTTP头,以上例子中HTTP头会附加 Authorization: BearerBC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F 授权属性。
  • SetResult 设置返回值的类型,Resty自动解析json通过 resp.Result().(*AuthToken) 获取。

3. POST方法

client := resty.New()

resp, err := client.R().
      SetBody(User{Username: "testuser", Password: "testpass"}).
      SetResult(&AuthSuccess{}).
      SetError(&AuthError{}).
      Post("https://myapp.com/login")

resp, err := client.R().
      SetBody(map[string]interface{}{"username": "testuser", "password": "testpass"}).
      SetResult(&AuthSuccess{}).
      SetError(&AuthError{}).
      Post("https://myapp.com/login")

resp, err := client.R().
      SetHeader("Content-Type", "application/json").
      SetBody(`{"username":"testuser", "password":"testpass"}`).
      SetResult(&AuthSuccess{}).
      Post("https://myapp.com/login")

resp, err := client.R().
      SetHeader("Content-Type", "application/json").
      SetBody([]byte(`{"username":"testuser", "password":"testpass"}`)).
      SetResult(&AuthSuccess{}).
      Post("https://myapp.com/login")

POST请求的代码和GET请求类似,只是最后调用了 Post 方法。POST请求可以附带BODY,代码中使用 SetBody 方法设置POST BODY。
SetBody 参数类型为结构体或 map[string]interface{} 时, Resty 自动附加HTTP头 Content-Type: application/json ,当参数为string或[]byte类型时由于很难推断内容的类型,所以需要手动设置 Content-Type 请求头。 SetBody 还支持其他类型的参数,例如上传文件时可能会用到的io.Reader。 SetError 设置HTTP状态码为4XX或5XX等错误时返回的数据类型。

4. PUT方法

client := resty.New()

resp, err := client.R().
      SetBody(Article{
        Title: "go-resty",
        Content: "This is my article content, oh ya!",
        Author: "Jeevanandam M",
        Tags: []string{"article", "sample", "resty"},
      }).
      SetAuthToken("C6A79608-782F-4ED0-A11D-BD82FAD829CD").
      SetError(&Error{}).
      Put("https://myapp.com/article/1234")

PATCH,DELETE,HEAD,OPTIONS请求也是一样的, Resty 为我们提供了一致的方式发起不同请求。

5. 高级应用

5.1 代理

使用 Resty 作为HTTP客户端使用的话,添加代理似乎是一个常见的需求。 Resty 提供了 SetProxy 方法为请求添加代理,还可以调用 RemoveProxy 移除代理。代码如下:

client := resty.New()

client.SetProxy("http://proxyserver:8888")

client.RemoveProxy()

5.2 重试

client := resty.New()

client.
    SetRetryCount(3).
    SetRetryWaitTime(5 * time.Second).
    SetRetryMaxWaitTime(20 * time.Second).
    SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {
        return 0, errors.New("quota exceeded")
    })

client.AddRetryCondition(
    func(r *resty.Response) (bool, error) {
        return r.StatusCode() == http.StatusTooManyRequests
    },
)

由于网络抖动带来的接口稳定性的问题 Resty 提供了重试功能来解决。以上代码我们可以看到 SetRetryCount 设置重试次数, SetRetryWaitTimeSetRetryMaxWaitTime 设置等待时间。 SetRetryAfter 是一个重试后的回调方法。除此之外还可以调用 AddRetryCondition 设置重试的条件。

6. 中间件

Resty 提供了和Gin类似的中间件特性。 OnBeforeRequestOnAfterResponse 回调方法,可以在请求之前和响应之后加入自定义逻辑。参数包含了 resty.Client 和当前请求的 resty.Request 对象。成功时返回 nil ,失败时返回 error 对象。

client := resty.New()

client.OnBeforeRequest(func(c *resty.Client, req *resty.Request) error {

    return nil
})

client.OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {

    return nil
})

参考链接:
https://github.com/go-resty/resty

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ghostwritten

口渴,请赏一杯下午茶吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值