[英]Http Communication between Azure Service Fabric services
我的情況:
問題:
我正在嘗試將我的狀態服務和無狀態WebAPI之間的通信從RPC更改為HTTP通信,因為我想避免使用接口在服務之間進行通信。 這甚至可以通過HTTP通信嗎? 如果是這樣,我的無狀態WebAPI如何在不使用接口的情況下調用有狀態應用程序中的特定方法?
更新:
感謝alltej,我開始閱讀有關HttpCommunicationListeners的更多信息。 本教程( https://docs.microsoft.com/nl-nl/azure/service-fabric/service-fabric-concepts-partitioning )很好地解釋了Http Communication。
在下面的代碼片段中:“CreateServiceReplicaListeners()”調用CreateInternalListener(),然后調用“ProcessInternalRequest()”,然后最終調用“AddUserAsync()”。
protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
{
return new[] { new ServiceReplicaListener(context => this.CreateInternalListener(context))};
}
private ICommunicationListener CreateInternalListener(ServiceContext context)
{
EndpointResourceDescription internalEndpoint = context.CodePackageActivationContext.GetEndpoint("ProcessingServiceEndpoint");
string uriPrefix = String.Format(
"{0}://+:{1}/{2}/{3}-{4}/",
internalEndpoint.Protocol,
internalEndpoint.Port,
context.PartitionId,
context.ReplicaOrInstanceId,
Guid.NewGuid());
string nodeIP = FabricRuntime.GetNodeContext().IPAddressOrFQDN;
string uriPublished = uriPrefix.Replace("+", nodeIP);
return new HttpCommunicationListener(uriPrefix, uriPublished, this.ProcessInternalRequest);
}
private async Task ProcessInternalRequest(HttpListenerContext context, CancellationToken cancelRequest)
{
string output = null;
string user = context.Request.QueryString["lastname"].ToString();
try
{
output = await this.AddUserAsync(user);
}
catch (Exception ex)
{
output = ex.Message;
}
using (HttpListenerResponse response = context.Response)
{
if (output != null)
{
byte[] outBytes = Encoding.UTF8.GetBytes(output);
response.OutputStream.Write(outBytes, 0, outBytes.Length);
}
}
}
private async Task<string> AddUserAsync(string user)
{
IReliableDictionary<String, String> dictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<String, String>>("dictionary");
using (ITransaction tx = this.StateManager.CreateTransaction())
{
bool addResult = await dictionary.TryAddAsync(tx, user.ToUpperInvariant(), user);
await tx.CommitAsync();
return String.Format(
"User {0} {1}",
user,
addResult ? "sucessfully added" : "already exists");
}
}
}
MyCustomHttpListener是一個必須創建的類,它是您自己的實現ICommunicationListener的自定義偵聽器。 它包含您需要覆蓋的三種方法。 有關OwinCommunicationListener,請參閱此處的示例: https ://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-communication-webapi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.