public static IClusterClient CreateClusterClient(string hostName, IEnumerable <IConfigurationSource> configurationSources) { var configBuilder = new ConfigurationBuilder(); foreach (var source in configurationSources) { configBuilder.Add(source); } var configuration = configBuilder.Build(); var builder = new ClientBuilder(); builder.Properties["Configuration"] = configuration; builder.Configure <ClusterOptions>(configuration); builder.ConfigureServices(services => { TryConfigureTestClusterMembership(configuration, services); TryConfigureFileLogging(configuration, services, hostName); }); ConfigureAppServices(configuration, builder); builder.GetApplicationPartManager().ConfigureDefaults(); return(builder.Build()); }
public IGrainFactory Build() { if (IsLocal) { return(_services.GetService <IGrainFactory>()); } else { IClientBuilder build = new ClientBuilder(); if (_options.Configure == null) { _logger.LogError( $"{_serviceName} There is no way to connect to Orleans, please configure it in OrleansClientOptions.Configure"); } _options.Configure(build); build.Configure <ClusterOptions>(opt => { if (!string.IsNullOrEmpty(_options.ClusterId)) { opt.ClusterId = _options.ClusterId; } if (!string.IsNullOrEmpty(_options.ServiceId)) { opt.ServiceId = _options.ServiceId; } }); var client = build.Build(); return(this.ConnectClient(_serviceName, client)); } }
public static async Task <IClusterClient> InitializeClient(string[] args) { int initializeCounter = 0; var initSucceed = false; while (!initSucceed) { try { var builder = new ClientBuilder(); var client = builder .Configure <ClusterOptions>(options => { options.ClusterId = Constants.ClusterId; options.ServiceId = Constants.ServiceId; }) .AddRabbitMqStream(Constants.StreamProviderNameDefault, configurator => { configurator.ConfigureRabbitMq ( host: "elevate.rabbit.local", port: Constants.RmqProxyPort, virtualHost: "/", user: "******", password: "******", queueName: Constants.StreamNameSpaceCustomers ); }) .UseLocalhostClustering() .ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(CustomerCommandHandlerGrain).Assembly).WithReferences()) .ConfigureLogging(logging => logging.AddConsole()) .Build(); await client.Connect(); initSucceed = client.IsInitialized; if (initSucceed) { return(client); } } catch (Exception exc) { Console.WriteLine(exc.Message); initSucceed = false; } if (initializeCounter++ > 10) { return(null); } Console.WriteLine("Client Init Failed. Sleeping 5s..."); Thread.Sleep(TimeSpan.FromSeconds(5)); } return(null); }
public HostedClusterClient( IBuildConfig buildConfig, IOrleansConfig clusterConfig) { var clientBuilder = new ClientBuilder() .AddSimpleMessageStreamProvider(StreamProvider.Transient) .UseAzureStorageClustering(options => { options.ConnectionString = clusterConfig.AzureStorageConnectionString; options.TableName = clusterConfig.StorageTableName; }) .Configure <ClusterOptions>(options => { options.ServiceId = clusterConfig.ServiceId; options.ClusterId = clusterConfig.ClusterId; }); if (buildConfig.Equals(BuildConfig.Debug)) { clientBuilder .Configure <ClusterMembershipOptions>(x => x.ValidateInitialConnectivity = false); } Client = clientBuilder.Build(); }
protected virtual IClusterClient InitGrainClient() { if (_clusterClient == null) { lock (_locker) { if (_clusterClient != null) { return(_clusterClient); } var gateways = new[] { new IPEndPoint(IPAddress.Loopback, BasePort + (int)PortOffsets.SiloGateway), }; var grainClientBuilder = new ClientBuilder(); grainClientBuilder.UseStaticClustering(gateways); grainClientBuilder .Configure <ClusterOptions>(options => { options.ClusterId = "dev"; options.ServiceId = "dev"; }) .Configure <SerializationProviderOptions>(options => { options.SerializationProviders.Add(typeof(OrleansCustomSerialization)); if (_customSerializer != null) { // IF the custom serializer inherits the default one, // replace it (as base class will supports all registered serialization types) if (_customSerializer.IsSubclassOf(typeof(OrleansCustomSerialization))) { options.SerializationProviders.Remove(typeof(OrleansCustomSerialization)); } options.SerializationProviders.Add(_customSerializer); } }) .UseServiceProviderFactory(o => { /*var o2NBinding = Host.Kernel.Get<IOrleansToNinjectBinding>(); * o2NBinding.ConfigureServices(o); * Host.Kernel.Rebind(Type.GetType("Gigya.Microdot.Orleans.Ninject.Host.NinjectOrleansBinding.IGlobalServiceProvider, Gigya.Microdot.Orleans.Ninject.Host")).To(Type.GetType("Gigya.Microdot.Orleans.Ninject.Host.NinjectOrleansBinding.MicrodotServiceProviderWithScope, Gigya.Microdot.Orleans.Ninject.Host")).InSingletonScope();*/ return(Host.Kernel.Get <IServiceProvider>()); }); var grainClient = grainClientBuilder.Build(); grainClient.Connect().GetAwaiter().GetResult(); _clusterClient = grainClient; } } return(_clusterClient); }
public static ClientStateBuilder AddState(this ClientBuilder self) { self.Configure(s => { s.AddSingleton <ClientStateManager>(); }); self.AddMiddleware <ClientStateMiddleware>(); return(new ClientStateBuilder(self)); }
protected virtual IClusterClient InitGrainClient() { if (_clusterClient == null) { lock (_locker) { if (_clusterClient != null) { return(_clusterClient); } var gateways = new[] { new IPEndPoint(IPAddress.Loopback, BasePort + (int)PortOffsets.SiloGateway), }; var grainClientBuilder = new ClientBuilder(); grainClientBuilder.UseStaticClustering(gateways); grainClientBuilder .Configure <ClusterOptions>(options => { options.ClusterId = "dev"; options.ServiceId = "dev"; }) .Configure <SerializationProviderOptions>(options => { options.SerializationProviders.Add(typeof(OrleansCustomSerialization)); if (_customSerializer != null) { // IF the custom serializer inherits the default one, // replace it (as base class will supports all registered serialization types) if (_customSerializer.IsSubclassOf(typeof(OrleansCustomSerialization))) { options.SerializationProviders.Remove(typeof(OrleansCustomSerialization)); } options.SerializationProviders.Add(_customSerializer); } }); var grainClient = grainClientBuilder.Build(); grainClient.Connect().GetAwaiter().GetResult(); _clusterClient = grainClient; } } return(_clusterClient); }
public static IServiceCollection AddOrleansHttpGateway(this IServiceCollection services, Action <OrleansHttpGatewayOptions> configure , Action <OrleansClientOptions, IClientBuilder> clientBuild) { services.AddRouting(); services.TryAddSingleton <IGrainBuilder, DefaultGrainBuilder>(); services.TryAddSingleton <IGrainFactoryGateway, DefaultGrainFactoryGateway>(); services.TryAddSingleton <IGrainMethodInvoker, DynamicGrainMethodInvoker>(); services.TryAddSingleton <IGrainReference, DefaultGrainReference>(); services.TryAddSingleton <IRouteDataResolve, DefaultRouteDataResolve>(); services.TryAddSingleton <IParameterBinder, DefaultParameterBinder>(); services.TryAddSingleton(typeof(IKeyedServiceCollection <,>), typeof(KeyedServiceCollection <,>)); //JsonSerializer services.AddSingleton <JsonSerializer>((IServiceProvider serviceProvider) => { JsonSerializerSettings settings = serviceProvider.GetService <JsonSerializerSettings>() ?? new JsonSerializerSettings(); if (!settings.Converters.OfType <ImmutableConverter>().Any()) { settings.Converters.Add(new ImmutableConverter()); } return(JsonSerializer.Create(settings)); }); services.Configure <OrleansHttpGatewayOptions>(options => { configure?.Invoke(options); }); var option = new OrleansHttpGatewayOptions(); configure?.Invoke(option); foreach (var item in option.Clients) { var clientOption = item.Value; services.AddTransientNamedService <IClientBuilder>(clientOption.Assembly.Location, (service, key) => { var build = new ClientBuilder(); build.Configure <ClusterOptions>(opt => { opt.ClusterId = clientOption.ClusterId; opt.ServiceId = clientOption.ServiceId; }); clientBuild?.Invoke(clientOption, build); return(build); }); } return(services); }
protected override IEnumerable <ServiceInstanceListener> CreateServiceInstanceListeners() { return(new ServiceInstanceListener[] { new ServiceInstanceListener(serviceContext => new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) => { ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}"); var serviceName = new Uri("fabric:/RWProtoTwo/RiftRunServerService"); var builder = new ClientBuilder(); builder.Configure <ClusterOptions>(options => { options.ServiceId = serviceName.ToString(); options.ClusterId = "RitRunProto"; }); builder.UseAzureStorageClustering(options => options.ConnectionString = ConnectionString); builder.ConfigureApplicationParts(parts => { parts.AddFromApplicationBaseDirectory(); }); var client = builder.Build(); ConnectToServer(client); var accountManager = client.GetGrain <IAccountOrchestrator>("AccountOrchestrator"); //activate Orchestrator accountManager.CreateAccountOrchestrator(); return new WebHostBuilder() .UseKestrel() .ConfigureServices(services => { services.AddSingleton <StatelessServiceContext>(serviceContext); services.AddSingleton <IClusterClient>(client); }) .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup <Startup>() .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None) .UseUrls(url) .Build(); })) }); }
private static ClientBuilder ConfigureCluster(this ClientBuilder clientBuilder, ClusterInfoOption clusterInfo, TimeSpan responseTimeout, TimeSpan responseTimeoutWithDebugger) { clientBuilder.Configure <ClientMessagingOptions>(options => { options.ResponseTimeout = responseTimeout; options.ResponseTimeoutWithDebugger = responseTimeoutWithDebugger; }).Configure <ClusterOptions>(options => { options.ClusterId = clusterInfo.ClusterId; options.ServiceId = clusterInfo.ServiceId; }); return(clientBuilder); }
private IClusterClient BuildClusterClient(GrainRouteValues routeValues, DownstreamContext context) { var clientOptions = routeValues.ClientOptions; IClientBuilder build = new ClientBuilder(); build.Configure <ClusterOptions>(opt => { opt.ClusterId = clientOptions.ClusterId; opt.ServiceId = clientOptions.ServiceId; }); build = this.UseServiceDiscovery(build, context); var client = build.Build(); return(this.ConnectClient(routeValues.GrainName, client)); }
public IUdfsClusterClient Build( UdfsClusterClientOptions clusterClientOptions ) { var clientBuilder = new ClientBuilder(); // Local or distribute cluster if (clusterClientOptions.Membership.Enabled) { clientBuilder.UseAdoNetClustering( options => { options.Invariant = clusterClientOptions.Membership.Provider; options.ConnectionString = clusterClientOptions.Membership.ConnectionString; } ); } else { clientBuilder.UseLocalhostClustering(); } //.ConfigureLogging(logging => logging.AddConsole()) //.UsePerfCounterEnvironmentStatistics() //.Configure<ClientMessagingOptions>(options => options.ResponseTimeout = TimeSpan.FromSeconds(30)) clientBuilder.Configure <ClusterOptions>( options => { options.ClusterId = clusterClientOptions.ClusterService.ClusterId; options.ServiceId = clusterClientOptions.ClusterService.ServiceId; } ); // Interfaces foreach (var item in clusterClientOptions.GrainAssemblies) { clientBuilder.ConfigureApplicationParts(parts => parts.AddApplicationPart(item).WithReferences()); } // Build client return(new UdfsClusterClient(clusterClientOptions, clientBuilder.Build())); }
public static IClusterClient Get(OrleansClientConnectionOptions options) { var serviceName = new Uri(options.FabricUrl); var builder = new ClientBuilder(); builder.Configure <ClusterOptions>(opt => { opt.ServiceId = serviceName.ToString(); opt.ClusterId = options.ClusterId; }); builder.UseAzureStorageClustering(opt => opt.ConnectionString = options.TableStorageConnectionString); builder.ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(IMyFirstGrain).Assembly)); builder.ConfigureLogging(logging => logging.AddDebug()); var client = builder.Build(); return(client); }
public static IClusterClient Get(string fabricUrl, string azureStorageConnectionString) { var serviceName = new Uri(fabricUrl); var builder = new ClientBuilder(); builder.Configure <ClusterOptions>(options => { options.ServiceId = serviceName.ToString(); options.ClusterId = "development"; }); builder.UseAzureStorageClustering(options => options.ConnectionString = azureStorageConnectionString); builder.ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(IMyFirstGrain).Assembly)); builder.ConfigureLogging(logging => logging.AddDebug()); var client = builder.Build(); return(client); }
private static async Task Run(string[] args) { var serviceName = new Uri("fabric:/StatelessCalculatorApp/StatelessCalculatorService"); var builder = new ClientBuilder(); builder.Configure <ClusterOptions>(options => { options.ServiceId = serviceName.ToString(); options.ClusterId = "development"; }); // TODO: Pick a clustering provider and configure it here. builder.UseAzureStorageClustering(options => options.ConnectionString = "UseDevelopmentStorage=true"); // Add the application assemblies. builder.ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(ICalculatorGrain).Assembly)); // Optional: configure logging. builder.ConfigureLogging(logging => logging.AddDebug()); // Create the client and connect to the cluster. var client = builder.Build(); await client.Connect(); double result; if (args.Length < 1) { Console.WriteLine( $"Usage: {Assembly.GetExecutingAssembly()} <operation> [operand]\n\tOperations: get, set, add, subtract, multiple, divide"); return; } var value = args.Length > 1 ? double.Parse(args[1]) : 0; var calculator = client.GetGrain <ICalculatorGrain>(Guid.Empty); var observer = new CalculatorObserver(); var observerReference = await client.CreateObjectReference <ICalculatorObserver>(observer); var cancellationTokenSource = new CancellationTokenSource(); var subscriptionTask = StaySubscribed(calculator, observerReference, cancellationTokenSource.Token); switch (args[0].ToLower()) { case "stress": result = await StressTest(client); break; case "add": case "+": result = await calculator.Add(value); break; case "subtract": case "-": result = await calculator.Subtract(value); break; case "multiply": case "*": result = await calculator.Multiply(value); break; case "divide": case "/": result = await calculator.Divide(value); break; case "set": result = await calculator.Set(value); break; case "get": default: result = await calculator.Get(); break; } Console.WriteLine(result); Console.WriteLine("Listening for updates to calculations. Press any key to exit."); Console.ReadKey(); cancellationTokenSource.Cancel(); await subscriptionTask; }
public static ClientPeerBuilder AddPeers(this ClientBuilder self) { self.AddMiddleware <ClientPeerMiddleware>(); self.Configure(s => s.AddSingleton <PeerCollection>()); return(new ClientPeerBuilder(self)); }
private static async Task Run(string[] args) { var serviceName = new Uri("fabric:/StatelessCalculatorApp/StatelessCalculatorService"); var builder = new ClientBuilder(); builder.Configure <ClusterOptions>(options => { options.ServiceId = Guid.Empty; options.ClusterId = "dev"; }); // Use Service Fabric for managing cluster membership. builder.UseServiceFabricClustering(serviceName); // Add the application assemblies. builder.ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(ICalculatorGrain).Assembly)); // Optional: configure logging. builder.ConfigureLogging(logging => logging.AddDebug()); builder.ConfigureServices( services => { // Some deployments require a custom FabricClient, eg so that cluster endpoints and certificates can be configured. // A pre-configured FabricClient can be injected. var fabricClient = new FabricClient(); services.AddSingleton(fabricClient); }); // Create the client and connect to the cluster. var client = builder.Build(); await client.Connect(); double result; if (args.Length < 1) { Console.WriteLine( $"Usage: {Assembly.GetExecutingAssembly()} <operation> [operand]\n\tOperations: get, set, add, subtract, multiple, divide"); return; } var value = args.Length > 1 ? double.Parse(args[1]) : 0; var calculator = client.GetGrain <ICalculatorGrain>(Guid.Empty); var observer = new CalculatorObserver(); var observerReference = await client.CreateObjectReference <ICalculatorObserver>(observer); var cancellationTokenSource = new CancellationTokenSource(); var subscriptionTask = StaySubscribed(calculator, observerReference, cancellationTokenSource.Token); switch (args[0].ToLower()) { case "stress": result = await StressTest(client); break; case "add": case "+": result = await calculator.Add(value); break; case "subtract": case "-": result = await calculator.Subtract(value); break; case "multiply": case "*": result = await calculator.Multiply(value); break; case "divide": case "/": result = await calculator.Divide(value); break; case "set": result = await calculator.Set(value); break; case "get": default: result = await calculator.Get(); break; } Console.WriteLine(result); Console.WriteLine("Listening for updates to calculations. Press any key to exit."); Console.ReadKey(); cancellationTokenSource.Cancel(); await subscriptionTask; }