php - 选择动态端点时 Guzzle 不使用基本 uri(在本地工作,但在 K8S 上不工作)

标签 php docker lumen guzzle6

我正在构建一个 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');

Controller
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 标识符)有效!

端点生产:
- 获取/联系人作品!
- 发布/联系人工作!
- 获取/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 在失败的端点上被忽略,但这在我的本地计算机上不会发生。

最佳答案

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

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

关于php - 选择动态端点时 Guzzle 不使用基本 uri(在本地工作,但在 K8S 上不工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57736882/

相关文章:

javascript - 通过ajax传递时 header 不会重定向

selenium - 如何将 Jenkins 构建输出与测试结果绑定(bind)?

php - 无法在 Lumen 中使用 Laravel/Socialite

Laravel/Lumen - 如何从网站触发工作流程?

php - 使用 xmlhttprequest 检索 json 格式的数据

php - 将变量从 shell exec 传递到 mysql_query

PHP 和 MySQL - 从列中获取特定结果的数量

python - 如何在alpine容器中安装python2.6?

docker - 为 docker run 配置选项

mysql - 最后插入的 id 与 Lumen/Laravel 的内部交易