我有两个服务(Service1和Service2)的服务网格。启用了Sidecar。我已经使用了bookinfo官方Istio教程中的基本网关,并配置了VirtualService:
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: bookinfo-gateway
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: virtual-template-service
spec:
gateways:
- bookinfo-gateway
hosts:
- "*"
http:
- match:
- uri:
prefix: /api/home/method1
- uri:
prefix: /api/home/method2
route:
- destination:
host: service1.service1-production.svc.cluster.local
port:
number: 80
- match:
- uri:
prefix: /api/home2
route:
- destination:
host: service2.service2-production.svc.cluster.local
port:
number: 80
apiVersion: v1
kind: Service
metadata:
labels:
app: {{ .Chart.Name }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
component: app
name: {{ .Release.Name }}
spec:
ports:
- name: http
port: {{ .Values.service.port }}
protocol: TCP
targetPort: {{ .Values.service.port }}
selector:
app: {{ .Chart.Name }}
release: {{ .Release.Name }}
component: app
sessionAffinity: None
type: ClusterIP
Service1的方法1与Service2不交互。当我请求Service1 / api / home / method1时。一切正常。
Service1的方法2确实与Service2进行了交互。它通过POST方法发送了一个简单的异步HTTP请求。而且这种方法不起作用。
在Service1的附带日志中,我看到该请求已发送,但状态为500。在应用程序日志中,我看到了错误:No such address or device
。
在Service2边车中,日志没有任何活动。
对service2 / api / home2的请求也可以正常工作。
我不明白我在做什么错。你怎么看待这件事?请帮忙。
更新
两个基于.net核心的应用程序。所以我用webclient发送请求。示例:
public static async Task<string> RequestToService2Async(string request)
{
using (var client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
var response = await client.UploadStringTaskAsync(new Uri(http://service2/api/home2), "POST", request);
return response;
}
}
Service1侧面记录:
"GET /api/home/method2HTTP/1.1" 500 - 0 0 28 5 "20.100.3.1" "PostmanRuntime/7.3.0" "083fbcbc-9598-9973-b0c1-b897afdc9df0" "Service1" "127.0.0.1:80" inbound|80||Service1.Service1-production.svc.cluster.local - 20.100.3.110:80 20.100.3.1:0 outbound|80||Service1.Service1-production.svc.cluster.local
应用程序日志中出现错误
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HLHJ7PTBHFRI", Request id "0HLHJ7PTBHFRI:00000002": An unhandled exception was thrown by the application.
System.Net.WebException: No such device or address No such device or address ---> System.Net.Http.HttpRequestException: No such device or address ---> System.Net.Sockets.SocketException: No such device or address
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at System.Net.HttpWebRequest.SendRequest()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
UPDATE 2.0
当我在Uri中使用MyService2ClusterIP / api / home2-一切正常!因此,我无法使用uri中的主机名进行请求。