dubbo-go-pixiu

使用 dubbo 通用性请求

POST 请求 samples

建议

使用此方式,你能够给一个集群定义一个接口来请求对应 dubbo 提供的服务

接口配置

name: pixiu
description: pixiu sample
resources:
  - path: '/api/v1/test-dubbo/:application/:interface'
    type: restful
    description: common
    methods:
      - httpVerb: POST
        onAir: true
        timeout: 100s
        inboundRequest:
          requestType: http
        integrationRequest:
          requestType: dubbo
          mappingParams:
            - name: requestBody.values
              mapTo: 0
              opt:
                open: true
                usable: true
                name: values
            - name: requestBody.types
              mapTo: 1
              opt:
                open: true
                name: types
            - name: uri.application
              mapTo: 2
              opt:
                open: true
                name: application
            - name: uri.interface
              mapTo: 3
              opt:
                open: true
                name: interface
            - name: queryStrings.method
              mapTo: 4
              opt:
                open: true
                name: method
            - name: queryStrings.group
              mapTo: 5
              opt:
                open: true
                name: group
            - name: queryStrings.version
              mapTo: 6
              opt:
                open: true
                name: version
          paramTypes: ["object", "object", "string", "string", "string", "string", "string"]
          # 这个是必须注意的,实际传给 dubbo 的 paramTypes,在最终调用的时候优先级高于 paramTypes。
          toParamTypes: ["string"]
          clusterName: "test_dubbo"

测试例子

curl host:port/api/v1/test-dubbo/UserService/com.dubbogo.proxy.UserService?group=test&version=1.0.0&method=GetUserByName -X POST -d '{"types":["string"],"values":"tc"}' --header "Content-Type: application/json"

result

{
  "age": 18,
  "code": 1,
  "iD": "0001",
  "name": "tc",
  "time": "2020-12-20T20:54:38.746+08:00"
}
curl host:port/api/v1/test-dubbo/UserService/com.dubbogo.proxy.UserService?group=test&version=1.0.0&method=GetUserByCode -X POST -d '{"types":["int"],"values":1}' --header "Content-Type: application/json"

result

{
  "age": 18,
  "code": 1,
  "iD": "0001",
  "name": "tc",
  "time": "2020-12-20T20:54:38.746+08:00"
}
curl host:port/api/v1/test-dubbo/UserService/com.dubbogo.proxy.UserService?group=test&version=1.0.0&method=UpdateUserByName -X POST -d '{"types":["string","body"],"values":["tc",{"id":"0001","code":1,"name":"tc","age":15}]}' --header "Content-Type: application/json"

result

true

特殊配置

可配码

const (
	optionKeyTypes       = "types"
	optionKeyGroup       = "group"
	optionKeyVersion     = "version"
	optionKeyInterface   = "interface"
	optionKeyApplication = "application"
	optionKeyMethod      = "method"
	optionKeyValues      = "values"
)

选择项

组装泛化调用的参数

// GenericService uses for generic invoke for service call
type GenericService struct {
	Invoke       func(ctx context.Context, req []interface{}) (interface{}, error) `dubbo:"$invoke"`
	referenceStr string
}

dubbo 泛化类型

用于 dubbogo GenericService#Invoke 函数的第二个参数。

用于 dubbogo GenericService#Invoke 函数的第一个参数。

Dubbo 组配置 ReferenceConfig#Group

Dubbo 版本配置 ReferenceConfig#Version

Dubbo 接口配置 ReferenceConfig#InterfaceName

目前暂时用于缓存,索引的一部分查找对应的缓存对象。

值的处理,用于 GenericService#Invoke 函数的第三个参数。

解释

单个参数

请求体

{
    "types": [
        "string"
    ],
    "values": "tc"
}
            - name: requestBody.types
              mapTo: 1
              opt:
                open: true
                name: types
多个参数

请求体

{
  "types": [
    "java.lang.String",
    "object"
  ],
  "values": [
    "tc",
    {
      "id": "0001",
      "code": 1,
      "name": "tc",
      "age": 99
    }
  ]
}

请注意这种特殊情况的配置目前自由度不是很高,如果有不能满足的场景请及时反馈到问题

上一页