// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddAutoMapper(typeof(MappingProfiles)); services.AddControllers(); services.AddSingleton <IConnectionMultiplexer>(c => { var configuration = ConfigurationOptions.Parse(Configuration .GetConnectionString("Redis"), true); return(ConnectionMultiplexer.Connect(configuration)); }); var consulSettings = new ConsulSettings(); new ConfigureFromConfigurationOptions <ConsulSettings>(Configuration.GetSection("ConsulConfig")).Configure(consulSettings); // services.AddSingleton(consulSettings); services.AddSingleton <IConsulSettings>(consulSettings); services.AddConsulConfig(consulSettings); services.AddApplicationServices(); // services.AddIdentityServices(_config); services.AddSwaggerDocumentation(); services.AddCors(opt => { opt.AddPolicy("CorsPolicy", policy => { policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin(); //.WithOrigins("http://localhost:4200"); }); }); }
public void ConfigureConsul(IConfigurationRoot config, ConsulSettings settings) { // Shorthand to use the default Consul setup, but with a different port // number retrieved from the application configuration settings.Port = config.GetValue <int>("Consul.Port"); // Configure the underlying ConsulClientConfiguration object settings.Configure = _ => { // This line is an equivalent to "settings.Port = #" as shown above _.Address = $"http://localhost:{config["Consul.Port"]}".ToUri(); _.Token = config["Consul.Token"]; }; // ConsulDotNet allows you to further configure the HttpClient // that it uses internallly to talk to Consul settings.ClientOverride = httpClient => { httpClient.Timeout = 1.Seconds(); }; // ConsulDotNet also allows you to configure the HttpClientHandler // that it uses internally to talk to Consul settings.HandlerOverride = httpClientHandler => { httpClientHandler.AutomaticDecompression = DecompressionMethods.None; }; }
/// <summary> /// Enables service discovery using consul to resolve the correct url for a remote RequestDTO /// </summary> public ConsulFeature(ConsulSettings settings = null) { Settings = new ConsulFeatureSettings(); if (settings != null) { settings.Invoke(Settings); } }
public ContainerOptions() { Database = new DatabaseSettings(); Token = new TokenSettings(); Caching = new CachingSettings(); CORS = new CorsSettings(); Consul = new ConsulSettings(); }
public async Task successfully_unregister_on_shutdown() { _runtime.Dispose(); using (var settings = new ConsulSettings()) { var nodes = await settings.Client.KV.List(ConsulNodeDiscovery.TRANSPORTNODE_PREFIX); nodes.StatusCode.ShouldBe(HttpStatusCode.NotFound); nodes.Response.ShouldBeNull(); } }
public async Task successfully_registers_itself_as_an_active_node() { theNodeDiscovery.LocalNode.ShouldNotBeNull(); var peers = await theNodeDiscovery.FindPeers(); peers.Single().ServiceName.ShouldBe("ConsulTestApp"); using (var settings = new ConsulSettings()) { var nodes = await settings.Client.KV.List(ConsulNodeDiscovery.TRANSPORTNODE_PREFIX); nodes.Response.Length.ShouldBe(1); } }
public override void Configure(Container container) { ConsulSettings consulSettings = new ConsulSettings(settings => { settings.ConsulAgentUrl = "http://192.168.200.235:8500"; settings.IncludeDefaultServiceHealth = false; settings.SetDefaultGateway(baseUri => new JsonServiceClient(baseUri) { UserName = "******" }); settings.AddTags("urlprefix-/NetCoreApi.ServiceStack"); //settings.AddServiceCheck(host => //{ // return new HealthCheck(ServiceHealth.Ok, "working normally"); //}, //// default check once per minute //intervalInSeconds: 60, //// deregisters the service if health is critical after x minutes, null = disabled by default //deregisterIfCriticalAfterInMinutes: 30 //); }); var appHost = new HostConfig { // 禁用Metadata Soap11 Soap12页面 EnableFeatures = Feature.All.Remove(Feature.Metadata | Feature.Soap11 | Feature.Soap12), // 修改默认的metadata页面未swagger-ui页面 DefaultRedirectPath = "/swagger-ui/", // 禁用调试模式 DebugMode = true, // the url:port that other services will use to access this one //WebHostUrl = "http://192.168.20.133:5123", // optional //ApiVersion = "1.0", HandlerFactoryPath = "/" }; SetConfig(appHost); Plugins.Add(new ConsulFeature(consulSettings)); // 启用swagger插件 Plugins.Add(new SwaggerFeature()); // 未捕获的异常在此处统一处理 UncaughtExceptionHandlers.Add(UncaughtExceptionHandlersDel); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var consulSettings = new ConsulSettings(); new ConfigureFromConfigurationOptions <ConsulSettings>(Configuration.GetSection("ConsulConfig")).Configure(consulSettings); // services.AddSingleton(consulSettings); services.AddSingleton <IConsulSettings>(consulSettings); services.AddConsulConfig(consulSettings); services.AddCors(opt => { opt.AddPolicy("CorsPolicy", policy => { policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin(); //WithOrigins("http://localhost:4200"); }); }); services.AddControllers(); }
/// <summary> /// Enables service discovery using consul to resolve the correct url for a remote RequestDTO /// </summary> public ConsulFeature(ConsulSettings settings = null) { Settings = new ConsulFeatureSettings(); settings?.Invoke(Settings); }
public ConsulNodeDiscovery(ConsulSettings settings, IChannelGraph channels, BusSettings envSettings) : base(settings, channels, envSettings) { }
protected ConsulService(ConsulSettings settings, IChannelGraph channels, BusSettings envSettings) { _channels = channels; client = settings.Client; MachineName = envSettings.MachineName; }
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) { services.Configure <MongoSettings>( configuration.GetSection(nameof(MongoSettings))); services.AddSingleton <IMongoSettings>(sp => sp.GetRequiredService <IOptions <MongoSettings> >().Value); services.Configure <RabbitMqConfiguration>(configuration.GetSection("RabbitMq")); services.AddAutoMapper(typeof(MappingProfiles)); // services.Configure<ConsulSettings>( // configuration.GetSection(nameof(ConsulSettings))); // services.AddSingleton<IConsulSettings>(sp => // sp.GetRequiredService<IOptions<ConsulSettings>>().Value); var consulSettings = new ConsulSettings(); new ConfigureFromConfigurationOptions <ConsulSettings>(configuration.GetSection("ConsulConfig")).Configure(consulSettings); // services.AddSingleton(consulSettings); services.AddSingleton <IConsulSettings>(consulSettings); services.AddConsulConfig(consulSettings); // Creating a `LoggerProviderCollection` lets Serilog optionally write // events through other dynamically-added MEL ILoggerProviders. // var providers = new LoggerProviderCollection(); // Log.Logger = new LoggerConfiguration() // .ReadFrom.Configuration(configuration) // .Enrich.WithProperty("ApplicationName", typeof(DependencyInjection).Assembly.GetName().Name) // // .Enrich.WithProperty("Environment", env.EnvironmentName) // .WriteTo.Providers(providers) // .CreateLogger(); // services.AddSingleton(providers); // services.AddSingleton<ILoggerFactory>(sc => // { // var providerCollection = sc.GetService<LoggerProviderCollection>(); // var factory = new SerilogLoggerFactory(null, true, providerCollection); // foreach (var provider in sc.GetServices<ILoggerProvider>()) // factory.AddProvider(provider); // return factory; // }); // services.AddLogging(); services.AddScoped <IProductRepository, ProductRepository>(); services.AddScoped <IProductTypeRepository, ProductTypeRepository>(); services.AddScoped <IProductBrandRepository, ProductBrandRepository>(); services.AddScoped <IImageRepository, ImageRepository>(); services.AddSingleton <ILoggerManager, LoggerManager>(); services.AddScoped <MongoContextSeed>(); services.AddScoped <MongoContext>(); services.AddTransient <IProductUpdateSender, ProductUpdateSender>(); return(services); }
public ConsulSubscriptionRepository(ConsulSettings settings, IChannelGraph channels, BusSettings envSettings) : base(settings, channels, envSettings) { }
public ConsulSubscriptionRepository(ConsulSettings settings, IChannelGraph channels, MessagingSettings envSettings) : base(settings, channels, envSettings) { _settings = settings; }
public static IServiceCollection AddConsulConfig(this IServiceCollection services, ConsulSettings consulSetting) { // var consulSetting = services.GetRequiredService<IConsulSettings>(); services.AddSingleton <IConsulClient, ConsulClient>(p => new ConsulClient(consulConfig => { consulConfig.Address = new Uri(consulSetting.ConsulAddresss); })); return(services); }
public static IApplicationBuilder UseConsul(this IApplicationBuilder app, IHostApplicationLifetime lifetime, ConsulSettings consulSettings) { var consulClient = new ConsulClient(config => { config.Address = new Uri(consulSettings.ConsulAddress); }); var registration = new AgentServiceRegistration() { ID = Guid.NewGuid().ToString(), Name = consulSettings.ServiceName, // 服务名 Address = consulSettings.ServiceIP, // 服务绑定IP Port = consulSettings.ServicePort, // 服务绑定端口 Check = new AgentServiceCheck() { DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5), //服务启动多久后注册 Interval = TimeSpan.FromSeconds(60), //健康检查时间间隔 HTTP = consulSettings.ServiceHealthCheck, //健康检查地址 Timeout = TimeSpan.FromSeconds(5) } }; // 服务注册 consulClient.Agent.ServiceRegister(registration).ConfigureAwait(false).GetAwaiter().GetResult(); // 应用程序终止时,服务取消注册 lifetime.ApplicationStopping.Register(() => { consulClient.Agent.ServiceDeregister(registration.ID).ConfigureAwait(false).GetAwaiter().GetResult(); }); return(app); }