【问题标题】:Guzzle not using base uri when a dynamic endpoint is selected (Works locally but not on K8S)选择动态端点时 Guzzle 不使用基本 uri(在本地工作,但在 K8S 上不工作)
【发布时间】:2019-08-31 09:42:58
【问题描述】:

我正在构建一个 API 网关和一堆微服务来运行我的公司。我选择使用 PHP,因为它是我最有经验的。

本地设置:Opensuse Tumbleweed、PHPStorm、php7.3、SQLite、docker 远程设置:GKE、PHP7.3 Percona Xtra DB 和 Docker

我正在使用 Laravel Lumen 框架 5.8。

我的网关通过 Guzzle6 Http Client 与微服务通信,并且在本地运行良好。当它被推送到集群时,使用 Gitlab 运行 ci/cd 管道将其编译为 docker 映像并将其部署到 Google Cloud 上的 Kubernetes。

我试过在“”和“”之间切换, 我已经重写了整个代码, 我查看了 Guzzle 文档, 我在 docker 中阅读了许多类似行为的堆栈溢出问题

路线

    $router->get('/customers','CustomerController@getAll');
    $router->post('/customers','CustomerController@createCustomer');
    $router->get('/customers/{customer}','CustomerController@getCustomer');
    $router->put('/customers/{customer}','CustomerController@updateCustomer');
    $router->patch('/customers/{customer}','CustomerController@updateCustomer');
    $router->delete('/customers/{customer}','CustomerController@deleteCustomer');

控制器

public function updateCustomer(Request $request, $customer)
    {
        return $this->successResponse($this->customerService->updateCustomer($request->all(), $customer));
    }

    public function deleteCustomer($customer)
    {
        return $this->successResponse($this->customerService->deleteCustomer($customer));
    }

服务

public function createCustomer($data)
    {
        return $this->performRequest('POST','', $data);
    }

    public function getCustomer($customer)
    {
        return $this->performRequest('GET', "/{$customer}");
    }

    public function updateCustomer($data, $customer)
    {
        return $this->performRequest('PUT', "{$customer}", $data);
    }

    public function deleteCustomer($customer)
    {
        return $this->performRequest('DELETE', "{$customer}");
    }

执行请求

public function performRequest($method, $requestUrl, $formParams = [], $headers = [])
    {
        $client = new Client([
            'base_uri' => $this->baseUri,
        ]);
        $response = $client->request($method, $requestUrl, ['form_params' => $formParams, 'headers' => $headers]);
        return $response->getBody()->getContents();
    }

本地端点: - 获取/联系人作品! - 发布/联系人工作! - 获取 /contacts/(联系人 UUID 标识符)有效! - PUT/PATCH /contacts/(联系人 UUID 标识符)工作! - 删除 /contacts/(联系人 UUID 标识符) WORKS!

端点生产: - 获取/联系人作品! - 发布/联系人工作! - 获取 /contacts/(联系人 UUID 标识符)失败! - PUT/PATCH /contacts/(联系人 UUID 标识符)失败! - 删除 /contacts/(联系人 UUID 标识符)失败!

Sentry Bug Tracker 显示 GuzzleHttp\Exception\RequestException cURL错误3:(见http://curl.haxx.se/libcurl/c/libcurl-errors.html

在 sentry 上查看 URL 时,在失败的端点上会忽略基本 URI,但在我的本地计算机上不会发生这种情况。

【问题讨论】:

  • 您能否进一步描述失败的原因?它会给出404吗?您确定联系人确实存在于数据库中吗?
  • 嗨@MaartenDev 它给出了一个 500 内部服务器错误,它向哨兵错误跟踪器发送了一个 3 的 guzzle 错误代码,这是格式错误的 url。
  • 它应该将请求发送到 customer-microservice.customer-microservice.cluster.local/customers/(uuid) 而不是只发送到 /(uuid)
  • uuid 是通过普通的 GET 请求并复制粘贴使用 Postman 测试的 ID 获得的
  • 500 错误表明该微服务出现问题。有记录吗?您确定日志记录配置正确吗?

标签: php docker lumen guzzle6


【解决方案1】:

url 不包含协议(http) 添加此将修复格式错误的 url 错误。

最终网址: http://customer-microservice.customer-microservice.svc.cluster.local

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 2013-03-29
    • 2015-02-07
    相关资源
    最近更新 更多