예제 #1
0
 public ConsulServiceEndpoint(IConsulBuilder consulBuilder, IOptionsSnapshot <ServerIdentifier> server,
                              ILogger <ConsulServiceEndpoint> logger)
 {
     ConsulBuilder    = consulBuilder;
     Logger           = logger;
     ServerIdentifier = server.Value;
 }
예제 #2
0
        /// <summary>
        /// 应用程序退出,注销当前服务
        /// </summary>
        /// <param name="builder">Consul构建器</param>
        /// <returns>Consul构建器</returns>
        public static IConsulBuilder OnApplicationStopDeregister(this IConsulBuilder builder)
        {
            var lifttime = builder.Services.GetService <IHostApplicationLifetime>();

            lifttime.ApplicationStopped.Register(() =>
            {
                builder.Deregister();
            });
            return(builder);
        }
예제 #3
0
 /// <summary>
 /// 注册服务到Consul服务中心
 /// </summary>
 public static IConsulBuilder Register(this IConsulBuilder builder)
 {
     try
     {
         builder.Client.Config.Address = new Uri(builder.NodeInfo.ServerAddress);
         var info = builder.NodeInfo.Convert(builder.Builder);
         builder.Client.Agent.ServiceRegister(info).Wait();
         return(builder);
     }
     catch (Exception exe)
     {
         throw new Exception("无法连接到Consul服务器", exe);
     }
 }
예제 #4
0
        private static Action <WebHostBuilderContext, IServiceCollection> ConfigureServices(
            NethiumConfig nethiumConfig, IConsulHandler consulHandler, IConsulBuilder consulBuilder
            ) =>
        (context, collection) =>
        {
            var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(context.Configuration["Nethium:ServerSecretKey"] ?? throw new ArgumentNullException("Nethium:ServerSecretKey", "ServerSecretKey not specified in config")));

            var tokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey         = signingKey,

                RequireExpirationTime = true,
                ValidateLifetime      = true,

                ValidateAudience = false,
                ValidateActor    = false,
                ValidateIssuer   = false
            };

            collection.AddSingleton(
                new JwtHeader(new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256)));

            collection.AddHttpContextAccessor();
            collection.AddHttpClient();
            collection.Configure <ServerIdentifier>(context.Configuration.GetSection("Nethium"));
            collection.Configure <ConfigurationOptions>(o =>
            {
                o.Prefix = nethiumConfig.Prefix !;
                o.ConfigurationPrefix = nethiumConfig.ConfigurationPrefix;
                o.Separator           = nethiumConfig.Separator !;
                o.Watch      = nethiumConfig.Watch;
                o.AutoReload = nethiumConfig.AutoReload ?? false;
            });
            collection.AddSingleton(consulHandler);
            collection.AddSingleton(consulBuilder);
            collection.AddScoped <IAuthHandler, StubAuthHandler>();
            collection.AddScoped <ISwaggerStubHandler, SwaggerStubHandler>();
            collection.AddScoped <IConsulKvEndpoint, ConsulKvEndpoint>();
            collection.AddScoped <IConsulServiceEndpoint, ConsulServiceEndpoint>();
            collection.AddScoped <IServicesLocator, ConsulServicesLocator>();
            collection.AddScoped(typeof(IServiceFactory <>), typeof(SwaggerServiceFactory <>));
            collection.AddScoped(typeof(IServiceChooseStrategy <>), typeof(NearestStrategy <>));
            collection.AddHostedService <NethiumHostedService>();
            collection.AddOpenApiDocument();
            collection
            .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options => { options.TokenValidationParameters = tokenValidationParameters; });
        };
예제 #5
0
 private static Action <WebHostBuilderContext, IConfigurationBuilder> ConfigureAppConfiguration(
     ConfigurationOptions configurationOptions,
     NethiumConfig nethiumConfig,
     IConsulHandler consulHandler,
     IConsulBuilder consulBuilder
     ) =>
 (context, configurationBuilder) =>
 {
     var consulKvEndpoint = new ConsulKvEndpoint(consulBuilder,
                                                 nethiumConfig.LoggerFactory?.CreateLogger <ConsulKvEndpoint>())
     {
         WatchExceptionHandler = WatchExceptionHandler
     };
     configurationBuilder
     .AddJsonFile("nethium.json", true, true)
     .AddEnvironmentVariables("NETHIUM_")
     .Add(new ConsulConfigurationSource(configurationOptions, consulHandler, consulKvEndpoint));
 };
예제 #6
0
 /// <summary>
 /// 注册到服务中心并注册注销事件,默认会调用健康检查,你需要指定对应的请求url
 /// </summary>
 /// <param name="builder">Consul构建器</param>
 /// <returns></returns>
 public static IConsulBuilder UseConsul(this IConsulBuilder builder)
 {
     return(builder.Register().OnApplicationStopDeregister());
 }
예제 #7
0
 /// <summary>
 /// 从服务中心注销当前服务
 /// </summary>
 /// <param name="builder">Consul构建器</param>
 /// <returns>Consul构建器</returns>
 public static IConsulBuilder Deregister(this IConsulBuilder builder)
 {
     builder.Client.Config.Address = new Uri(builder.NodeInfo.ServerAddress);
     builder.Client.Agent.ServiceDeregister(builder.NodeInfo.Id).Wait();
     return(builder);
 }
예제 #8
0
 public ConsulKvEndpoint(IConsulBuilder consulBuilder, ILogger <ConsulKvEndpoint>?logger)
 {
     ConsulBuilder = consulBuilder;
     Logger        = logger;
 }