aliyun / alibaba-cloud-sdk-go

Alibaba Cloud SDK for Go
Apache License 2.0
1.16k stars 271 forks source link

dysmsapi 在某种情况下会请求错误地址 #492

Closed cupen closed 3 years ago

cupen commented 3 years ago

alibaba-cloud-sdk版本: v1.61.1099

我有个服务使用了 dysmsapi ,在我的不同机器上运作结果不一样,有的正常,有的不正常。 其中的不正常的结果是: Post https://dysmsapi-proxy.cn-beijing.aliyuncs.com dial tcp 100.67.112.134:443: i/o timeout

那么 dysmsapi 发短信的接口在什么情况下会请求 https://dysmsapi-proxy.cn-beijing.aliyuncs.com 这个地址? 我看了好几层代码比较蒙圈。

我的调用代码如下,并没有什么特别之处。

    request := dysmsapi.CreateSendSmsRequest()
    request.Scheme = "https"
    request.TemplateCode = "SMS_xxxxxx"
    request.SignName = signName
    request.TemplateParam = string(params)
    request.PhoneNumbers = strconv.Itoa(phoneNumber)

    resp, err := client.SendSms(request)
JacksonTian commented 3 years ago

你设置过:

// Proxy network request
client.Network = "proxy"; 

cupen commented 3 years ago

你设置过:

// Proxy network request
client.Network = "proxy"; 

没有。

cupen commented 3 years ago

我打开了 debug 日志。 export DEBUG=sdk 后运行我的测试程序。

> POST /?   略......
> Host: dysmsapi-proxy.cn-beijing.aliyuncs.com
> Content-Type: application/x-www-form-urlencoded
> x-sdk-client: golang/1.0.0
> x-sdk-invoke-type: normal
> Accept-Encoding: identity
> x-acs-version: 2017-05-25
> x-acs-action: SendSms
> User-Agent: AlibabaCloud (linux; amd64) Golang/1.16.3 Core/0.0.1
> x-sdk-core-version: 0.0.1
>
 Retry Times: 0.
<
cupen commented 3 years ago

我的完整代码如下,并没有多余的动作。怀疑是什么环境变量导致的。

func Send(signName string, phoneNumber int, verifyCode string) error {
    endpoint := cfg.Endpoint
    accessKeyId := cfg.AccessKeyId
    accessSecret := cfg.AccessSecret
    client, err := dysmsapi.NewClientWithAccessKey(endpoint, accessKeyId, accessSecret)

    params, _ := json.Marshal(map[string]interface{}{
        "code": verifyCode,
    })

    request := dysmsapi.CreateSendSmsRequest()
    request.Scheme = "https"
    request.TemplateCode = "SMS_略"
    request.SignName = signName
    request.TemplateParam = string(params)
    request.PhoneNumbers = strconv.Itoa(phoneNumber)

    resp, err := client.SendSms(request)
    if err != nil {
        log.Error("send sms failed", zap.Reflect("resp", resp), zap.Error(err))
        return err
    }

    if !resp.IsSuccess() {
        log.Error("send sms failed", zap.Reflect("resp", resp), zap.Error(err))
    }
    return nil
}
JacksonTian commented 3 years ago

endpoint 呢?

cupen commented 3 years ago

endpoint 呢?

我没设置过 endpoint ,是需要设置么?

cupen commented 3 years ago

我正在 sdk 代码里加日志, endpoint 不是我传的,是 sdk 代码自己生成的,我还在找具体的逻辑。 https://github.com/aliyun/alibaba-cloud-sdk-go/blob/b042a0dab326e846954e0a4dc726850c25a0d585/sdk/client.go#L365-L377

cupen commented 3 years ago

我把 client.EndpointMap 按 json 格式打印出来了。

{
    "ap-northeast-1": "dysmsapi.ap-southeast-1.aliyuncs.com",
    "ap-northeast-2-pop": "dysmsapi.ap-southeast-1.aliyuncs.com",
    "ap-south-1": "dysmsapi.ap-southeast-1.aliyuncs.com",
    "ap-southeast-1": "dysmsapi.ap-southeast-1.aliyuncs.com",
    "ap-southeast-2": "dysmsapi.ap-southeast-1.aliyuncs.com",
    "ap-southeast-3": "dysmsapi.ap-southeast-1.aliyuncs.com",
    "ap-southeast-5": "dysmsapi.ap-southeast-1.aliyuncs.com",
    "cn-beijing": "dysmsapi-proxy.cn-beijing.aliyuncs.com",
    "eu-central-1": "dysmsapi.ap-southeast-1.aliyuncs.com",
    "eu-west-1": "dysmsapi.ap-southeast-1.aliyuncs.com",
    "eu-west-1-oxs": "dysmsapi.ap-southeast-1.aliyuncs.com",
    "me-east-1": "dysmsapi.ap-southeast-1.aliyuncs.com",
    "rus-west-1-pop": "dysmsapi.ap-southeast-1.aliyuncs.com",
    "us-east-1": "dysmsapi.ap-southeast-1.aliyuncs.com",
    "us-west-1": "dysmsapi.ap-southeast-1.aliyuncs.com"
}

我的 regionId 是 cn-beijing ,它对应的地址是错的。

cupen commented 3 years ago

endpoint 呢?

等下,这个 endpoint 对应的参数是 regionId 我命名有误。我传的是 cn-beijing 。

cupen commented 3 years ago

我去阿里云工单那问了,被告知正确的 endpoint 地址是 "dysmsapi.aliyuncs.com",貌似 client.EndpointMap 里的地址都作废了么? 我现在改用文档中 v3 的例子,不过它用的代码在另一个 git repo 。 貌似你们会把这些功能拆分到不同的仓库来维护。 https://help.aliyun.com/document_detail/215760.html?spm=a2c4g.11186623.6.659.3e3640cdJgPRem

cupen commented 3 years ago

V2 和 V3 我都实现了。 V3 测试正常,我就先用 V3 了。 目前有点微妙,我的项目线上用的还是 V2 api,运行正常。测试环境用的 V2 就一直是那个错误,我上面的贴的 Send 代码 endpoint 参数其实是 regionId,值为 cn-beijing . 而 https://dysmsapi-proxy.cn-beijing.aliyuncs.com 这个 endpoint 是 sdk 内部根据 regionId 获取的。

晚点得空我再看看。

cupen commented 3 years ago

嗯。我有的环境正常,有的环境不正常,是因为使用的这个仓库的代码版本不一样。 正常的用的版本是: github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20190822031555-8d633f101563

不正常的用的是: github.com/aliyun/alibaba-cloud-sdk-go v1.61.1110

这个 issue 我先关了,晚点我提供个测试例子。

JacksonTian commented 3 years ago

有些内部原因,导致这个配置是错误的。一律按 https://next.api.aliyun.com/product/Dysmsapi 上提供的服务地址对应来设置吧。

cupen commented 3 years ago

好的,感谢告知。 我项目改用新的 sdk v3 了,继续纠这个问题仅仅只是有点担心是不是我机器的问题。