0

我们有 5 个可靠的参与者服务,这些服务是从无状态的 aspnet 核心 web api 调用的。现在,Service Fabric 应用程序正在生产中运行,但作为迁移到 Azure 的一部分,我们希望所有自定义事件和跟踪到应用程序洞察力。

我们如何将其添加到 SF 中?请注意,我正在寻找可靠的演员服务。在可靠的服务中添加它有点简单,但我在 Actor 服务方面面临挑战。

我参考了这个https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-tutorial-monitoring-aspnet教程以获得可靠的服务,但在编写可靠的参与者服务的情况下同样不起作用在 .NET 框架中。

4

1 回答 1

0

引用此开放 GitHub 问题中的示例:为配置参与者服务添加文档

设置服务上下文

  1. 创建您自己的演员服务类并在那里初始化上下文:
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);
    }
}
  1. 注册演员服务,如下所示:
ActorRuntime.RegisterActorAsync<MyActor>(
    (context, actorType) => new MyActorService(context, actorType)).GetAwaiter().GetResult();

为服务远程启用关联

首先,确保 Service Remoting 设置正确。你可以在这里找到更多信息。

远程处理 V2(推荐)

初始化服务远程处理依赖/请求跟踪模块,如下所示。

注意:您也可以通过该CreateFabricTelemetryInitializer方法设置服务上下文。在这种情况下,您无需致电SetServiceCallContext

public MyActorServiceNetCore(
            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)
        {
            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。

  1. 在发件人端创建如下代理
//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);
  1. 像下面这样初始化监听器:
    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)
        {
        }

        protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
        {
            return new[]
            {
                new ServiceReplicaListener(
                    context => new FabricTransportActorServiceRemotingListener(
                        context,
                        new CorrelatingRemotingMessageHandler(this),
                        new FabricTransportRemotingListenerSettings()))
            };
        }
    }
  1. 注册你自己的演员服务:
// ActorRuntime.RegisterActorAsync<MyActor>(
//     (context, actorType) => new ActorService(context, actorType)).GetAwaiter().GetResult();
ActorRuntime.RegisterActorAsync<MyActor>(
    (context, actorType) => new MyActorService(context, actorType)).GetAwaiter().GetResult();

最初由 @yantang-msft 在https://github.com/microsoft/ApplicationInsights-ServiceFabric/issue_comments/434430800发布

其他资源:service-fabric-application-insights-example

于 2020-10-18T14:49:20.870 回答