Ejemplo n.º 1
0
        /// <summary>
        /// Adds the ServiceRegistry.
        /// </summary>
        /// <param name="services">The service collection.</param>
        /// <param name="configuration">The configuration instance.</param>
        /// <returns></returns>
        public static IServiceRegistryBuilder AddServiceRegistry(this IServiceCollection services, IConfiguration configuration)
        {
            var options = new ServiceRegistryOptions();

            configuration.Bind(options);

            return(services.AddServiceRegistry(options));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds the ServiceRegistry.
        /// </summary>
        /// <param name="services">The service collection.</param>
        /// <param name="setupAction">The setup action.</param>
        /// <returns></returns>
        public static IServiceRegistryBuilder AddServiceRegistry(this IServiceCollection services, Action <ServiceRegistryOptions> setupAction = null)
        {
            var options = new ServiceRegistryOptions();

            setupAction?.Invoke(options);

            return(services.AddServiceRegistry(options));
        }
        public RegistrationTokenProvider(IDataProtectionProvider protector, ServiceRegistryOptions options, ILogger <RegistrationTokenProvider> logger)
        {
            _protector = (protector ?? throw new ArgumentNullException(nameof(protector))).CreateProtector("RegistrationTokenProvider");
            _logger    = logger ?? throw new ArgumentNullException(nameof(logger));

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _tokenLifespan = options.RegisterTokenLifespan;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 注销服务
        /// </summary>
        /// <param name="serviceRegistryOption">服务注册配置信息</param>
        public void Deregister(ServiceRegistryOptions serviceRegistryOption)
        {
            // 1. 创建 Consul 客户端连接
            var consulClient = new ConsulClient(config =>
            {
                // 1.1 配置 Consul 服务端地址
                config.Address = new Uri(serviceRegistryOption.Address);
            });

            // 2. 注销服务
            consulClient.Agent.ServiceDeregister(serviceRegistryOption.Id);

            // 3. 关闭连接
            consulClient.Dispose();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 注册服务
        /// </summary>
        /// <param name="serviceRegistryOption">服务注册配置信息</param>
        public void Register(ServiceRegistryOptions serviceRegistryOption)
        {
            // 1. 创建 Consul 客户端连接
            var consulClient = new ConsulClient(config =>
            {
                // 1.1 配置 Consul 服务端地址
                config.Address = new Uri(serviceRegistryOption.RegistryAddress);
            });

            // 2. 获取服务内部地址
            // TODO...

            // 3. 创建 Consul 服务注册对象
            var registration = new AgentServiceRegistration()
            {
                ID      = serviceRegistryOption.Id,
                Name    = serviceRegistryOption.Name,
                Address = serviceRegistryOption.Address,
                Port    = serviceRegistryOption.Port,
                Tags    = serviceRegistryOption.Tags,
                Check   = new AgentServiceCheck()
                {
                    // 3.1 Consul 健康检查超时时间
                    Timeout = TimeSpan.FromSeconds(serviceRegistryOption.Timeout),
                    // 3.2 服务停止指定时间后执行注销
                    DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(serviceRegistryOption.DeregisterAfterServiceStop),
                    // 3.3 Consul 健康检查地址
                    HTTP = serviceRegistryOption.HealthCheckAddress,
                    // 3.3.1 跳过证书验证 --> 未安装有效证书时,可以通过此参数跳过证书校验
                    TLSSkipVerify = true,
                    // 3.4 Consul 健康检查间隔时间
                    Interval = TimeSpan.FromSeconds(serviceRegistryOption.HealthCheckInterval),
                }
            };

            // 4. 注册服务
            consulClient.Agent.ServiceRegister(registration).Wait();

            // 5. 关闭连接
            consulClient.Dispose();
        }
Ejemplo n.º 6
0
 public ConsulServiceRegistry(IOptions <ServiceRegistryOptions> options)
 {
     this.serviceRegistryOptions = options.Value;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="CachingServiceStore{T}"/> class.
 /// </summary>
 /// <param name="inner">The inner store.</param>
 /// <param name="cache">The cache instance.</param>
 /// <param name="options">The options.</param>
 public CachingServiceStore(T inner, ICache <Service> cache, ServiceRegistryOptions options)
 {
     _inner   = inner ?? throw new ArgumentNullException(nameof(inner));
     _cache   = cache ?? throw new ArgumentNullException(nameof(cache));
     _options = options ?? throw new ArgumentNullException(nameof(options));
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Adds the ServiceRegistry.
        /// </summary>
        /// <param name="services">The service collection.</param>
        /// <param name="options">The service registry options.</param>
        /// <returns></returns>
        public static IServiceRegistryBuilder AddServiceRegistry(this IServiceCollection services, ServiceRegistryOptions options)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            options.Validate();

            services.AddSingleton(options);
            services.AddScoped <IRegistrationTokenProvider, RegistrationTokenProvider>();
            services.AddScoped <IServiceRegistry, ServiceRegistry>();
            services.AddTransient <RegisterEndpoint>();
            services.AddTransient <ServiceEndpoint>();

            return(new ServiceRegistryBuilder(services));
        }