0

我正在尝试在 OpenShift 中实现 SideCar 模式。我的示例有两个项目 API。我在不同的容器和单个 pod 中运行这两个项目。下面是我的部署配置,指定了 2 个图像。

 containers:
        - env:
            - name: ASPNETCORE_URLS
              value: 'http://*:8080'
          image: 'docker-registry.default.svc:5000/it-mfg/hello-api-n:4.0'
          imagePullPolicy: Always
          name: hello-api-n
          ports:
            - containerPort: 8080
              name: http
              protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
        - env:
            - name: ASPNETCORE_URLS
              value: 'http://*:8090'
          image: 'docker-registry.default.svc:5000/it-mfg/hello-sidecar-api-n:4.0'
          imagePullPolicy: Always
          name: hello-sidecar-api-n
          ports:
            - containerPort: 8090
              name: http
              protocol: TCP
          resources: {}

我想通过本地主机将 api 从一个容器打到另一个容器中的 api。当我转到 Pod 终端并选择 hello-api-n 容器并执行 http://localhost:8080/FromSidecar 时,通信没有发生。

实际上,当我点击 /FromSidecar 时,内部代码实现对 localhost:8090/hello 进行 API 调用,这是一个不同的容器。

以下是我面临的错误

I have no name!@hello-api-n-deployment-config-3-xvlsd:/app$ curl -v http://localhost:8080/FromSidecar
* Uses proxy env variable no_proxy == 'localhost'
*   Trying ::1:8080...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /FromSidecar HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Date: Wed, 05 May 2021 09:00:22 GMT
< Server: Kestrel
< Content-Length: 0
<
* Connection #0 to host localhost left intact
I have no name!@hello-api-n-deployment-config-3-xvlsd:/app$

我正在尝试做的示例来自以下 https://github.com/cesaroll/dotnet-sidecar

更新代码:

项目 A 容器代码:

 [HttpGet]
        [Route("FromSidecar")]
        public async Task<IActionResult> GetFromSidecar()
        {
            return Ok(await _helloService.RetrieveHelloMessage());
        }

你好服务:

 public async Task<HelloMessage> RetrieveHelloMessage()
        {
            //string sidecarUrl = configuration.GetSection("SideCar").GetSection("SideCarUrl").Value;
            
           **string URL = "http://localhost:8090/Hello" /// hitting Container B**
            var client = new HttpClient();

            var response = client.GetAsync(URL).Result;

            if (!response.IsSuccessStatusCode)
            {
                return new HelloMessage()
                {
                    Message = $"Error Status Code: {response.StatusCode}, Reason: {response.ReasonPhrase}"
                };
            }
                
            _logger.LogInformation(response.Content.ReadAsStringAsync().Result);
                
            var helloMessage = await response.Content.ReadFromJsonAsync<HelloMessage>();
                
            _logger.LogInformation(helloMessage?.Message);

            return helloMessage;

        }

项目 B 容器代码:

  public IActionResult Get()
        {
            return Ok(new {Message = "Hello from Sidecar Api"});
        }
4

1 回答 1

0

通信工作正常,您可以看到HTTP/1.1 404 Not Found容器的响应。该错误404意味着您正在查找的资源不存在。连接已建立Connected to localhost

于 2021-05-05T14:22:21.917 回答