问题描述
我们有5个可靠的actor服务,这些服务将从无状态的aspnet核心Web API调用。 现在,服务结构应用程序正在生产中运行,但是作为向天蓝色迁移的一部分,我们希望所有自定义事件并跟踪到应用程序见解。
如何将其添加到SF?请注意,我正在寻找可靠的演员服务。在可靠的服务中添加这种方法很简单,但是Actor服务却面临着挑战。
我参考了本https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-tutorial-monitoring-aspnet教程,以获取可靠的服务,但是如果使用.NET Framework编写的可靠的actor服务,则无法使用。
解决方法
引用此GitHub公开问题的示例:Add documentation for configure actor services
设置服务上下文
- 创建自己的actor服务类并在那里初始化上下文:
internal class MyActorService : ActorService
{
public MyActorService(
StatefulServiceContext context,ActorTypeInformation actorTypeInfo,Func<ActorService,ActorId,ActorBase> actorFactory = null,Func<ActorBase,IActorStateProvider,IActorStateManager> stateManagerFactory = null,IActorStateProvider stateProvider = null,ActorServiceSettings settings = null)
: base(context,actorTypeInfo,actorFactory,stateManagerFactory,stateProvider,settings)
{
FabricTelemetryInitializerExtension.SetServiceCallContext(this.Context);
}
}
- 按以下方式注册演员服务:
ActorRuntime.RegisterActorAsync<MyActor>(
(context,actorType) => new MyActorService(context,actorType)).GetAwaiter().GetResult();
启用服务远程关联
首先,请确保正确设置了服务远程处理。您可以找到更多信息here。
远程处理V2(推荐)
初始化服务远程依赖/请求跟踪模块,如下所示。
注意:您还可以通过CreateFabricTelemetryInitializer
方法设置服务上下文。在这种情况下,您无需致电SetServiceCallContext
public MyActorServiceNetCore(
StatefulServiceContext context,ActorServiceSettings settings = null)
: base(context,settings)
{
var config = TelemetryConfiguration.Active;
config.InstrumentationKey = "<your ikey>";
config.TelemetryInitializers.Add(FabricTelemetryInitializerExtension.CreateFabricTelemetryInitializer(this.Context));
var requestTrackingModule = new ServiceRemotingRequestTrackingTelemetryModule();
var dependencyTrackingModule = new ServiceRemotingDependencyTrackingTelemetryModule();
requestTrackingModule.Initialize(config);
dependencyTrackingModule.Initialize(config);
}
远程处理V1
如果您仍要使用Service Remoting V1并跟踪关联,则可以按照以下说明进行操作,但强烈建议您升级到Remoting V2。
- 在发送方像下面一样创建代理
//IActorService actorServiceProxy = ActorServiceProxy.Create(new Uri(serviceUri),partitionKey);
CorrelatingActorProxyFactory actorProxyFactory = new CorrelatingActorProxyFactory(serviceContext,callbackClient => new FabricTransportActorRemotingClientFactory(callbackClient));
IActorService actorServiceProxy = actorProxyFactory.CreateActorServiceProxy<IActorService>(new Uri(serviceUri),partitionKey);
- 按如下所示初始化侦听器:
internal class MyActorService : ActorService
{
public MyActorService(
StatefulServiceContext context,settings)
{
}
protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
{
return new[]
{
new ServiceReplicaListener(
context => new FabricTransportActorServiceRemotingListener(
context,new CorrelatingRemotingMessageHandler(this),new FabricTransportRemotingListenerSettings()))
};
}
}
- 注册您自己的演员服务:
// ActorRuntime.RegisterActorAsync<MyActor>(
// (context,actorType) => new ActorService(context,actorType)).GetAwaiter().GetResult();
ActorRuntime.RegisterActorAsync<MyActor>(
(context,actorType)).GetAwaiter().GetResult();
最初由@ yantang-msft发表在https://github.com/microsoft/ApplicationInsights-ServiceFabric/issue_comments/434430800