Beispiel #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            string connStr = Configuration.GetValue("MysqlUser", GlobalObject.DefaultConfigValue);

            services.AddDbContext <UserContext>(opt =>
            {
                opt.UseMySQL(connStr);
            });
            var serviceDiscoveryConfig    = Configuration.GetSection(GlobalObject.Namespace_ServiceDiscovery);
            var strServiceDiscoveryConfig = serviceDiscoveryConfig.GetValue(GlobalObject.Namespace_DefaultKey, GlobalObject.DefaultConfigValue);
            ServiceDiscoveryOptions objServiceDiscovery = JsonConvert.DeserializeObject <ServiceDiscoveryOptions>(strServiceDiscoveryConfig);

            services.Configure <ServiceDiscoveryOptions>(opt =>
            {
                opt.Consul      = objServiceDiscovery.Consul;
                opt.ServiceName = objServiceDiscovery.ServiceName;
            });
            services.AddSingleton <IConsulClient>(p => new ConsulClient(cfg =>
            {
                var serviceConfiguration = p.GetRequiredService <IOptions <ServiceDiscoveryOptions> >().Value;

                if (!string.IsNullOrEmpty(serviceConfiguration.Consul.HttpEndpoint))
                {
                    // if not configured, the client will use the default value "127.0.0.1:8500"
                    cfg.Address = new Uri(serviceConfiguration.Consul.HttpEndpoint);
                }
            }));
            services.AddSingleton <IHostedService, HostedService>();


            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(opt =>
            {
                opt.RequireHttpsMetadata = false;
                opt.Audience             = "user_api";
                opt.Authority            = "http://localhost";
            });

            var capDiscoveryConfig           = Configuration.GetSection(GlobalObject.Namespace_CAPDiscovery);
            var strCAPDiscoveryConfig        = capDiscoveryConfig.GetValue(GlobalObject.Namespace_DefaultKey, GlobalObject.DefaultConfigValue);
            DiscoveryOptions objCAPDiscovery = JsonConvert.DeserializeObject <DiscoveryOptions>(strCAPDiscoveryConfig);

            services.AddCap(opt =>
            {
                opt.UseEntityFramework <UserContext>().UseRabbitMQ("localhost").UseDashboard();
                //注册到consul
                opt.UseDiscovery(d =>
                {
                    d.DiscoveryServerHostName = objCAPDiscovery.DiscoveryServerHostName;
                    d.DiscoveryServerPort     = objCAPDiscovery.DiscoveryServerPort;
                    d.CurrentNodeHostName     = objCAPDiscovery.CurrentNodeHostName;
                    d.CurrentNodePort         = objCAPDiscovery.CurrentNodePort;
                    d.NodeId   = objCAPDiscovery.NodeId;
                    d.NodeName = objCAPDiscovery.NodeName;
                });
            });

            services.AddMvc(opt => opt.EnableEndpointRouting = false);
        }
Beispiel #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <ProjectContext>(opt =>
            {
                opt.UseMySQL(Configuration.GetValue("MysqlProject", GlobalObject.DefaultConfigValue),
                             builder => builder.MigrationsAssembly(typeof(Startup).Assembly.GetName().Name));
            });
            services.AddScoped <IRecommend, RecommendService>()
            .AddScoped <IProjectQueries, ProjectQueriesService>(sp => {
                return(new ProjectQueriesService(Configuration.GetValue("MysqlProject", GlobalObject.DefaultConfigValue)));
            });
            Assembly[] assemblies =
            {
                Assembly.Load(new AssemblyName("Project.Api")),
                Assembly.Load(new AssemblyName("Project.Domain")),
                Assembly.Load(new AssemblyName("Project.Infrastructure"))
            };
            services.AddMediatR(assemblies);

            //services.Configure<ServiceDiscoveryOptions>(Configuration.GetSection("ServiceDiscovery"));
            var serviceDiscoveryConfig    = Configuration.GetSection(GlobalObject.Namespace_ServiceDiscovery);
            var strServiceDiscoveryConfig = serviceDiscoveryConfig.GetValue(GlobalObject.Namespace_DefaultKey, GlobalObject.DefaultConfigValue);
            ServiceDiscoveryOptions objServiceDiscovery = JsonConvert.DeserializeObject <ServiceDiscoveryOptions>(strServiceDiscoveryConfig);

            services.Configure <ServiceDiscoveryOptions>(opt =>
            {
                opt.Consul      = objServiceDiscovery.Consul;
                opt.ServiceName = objServiceDiscovery.ServiceName;
            });
            services.AddSingleton <IConsulClient>(p => new ConsulClient(cfg =>
            {
                var serviceConfiguration = p.GetRequiredService <IOptions <ServiceDiscoveryOptions> >().Value;

                if (!string.IsNullOrEmpty(serviceConfiguration.Consul.HttpEndpoint))
                {
                    // if not configured, the client will use the default value "127.0.0.1:8500"
                    cfg.Address = new Uri(serviceConfiguration.Consul.HttpEndpoint);
                }
            }));
            services.AddSingleton <IHostedService, HostedService>();
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(opt =>
            {
                opt.RequireHttpsMetadata = false;
                opt.Audience             = "project_api";
                opt.Authority            = "http://localhost";
            });
            services.AddScoped <IProjectRepository, ProjectRepository>();
            var capDiscoveryConfig    = Configuration.GetSection(GlobalObject.Namespace_CAPDiscovery);
            var strCAPDiscoveryConfig = capDiscoveryConfig.GetValue(GlobalObject.Namespace_DefaultKey, GlobalObject.DefaultConfigValue);

            DotNetCore.CAP.Dashboard.NodeDiscovery.DiscoveryOptions objCAPDiscovery = JsonConvert.DeserializeObject <DotNetCore.CAP.Dashboard.NodeDiscovery.DiscoveryOptions>(strCAPDiscoveryConfig);
            services.AddCap(opt =>
            {
                opt.UseEntityFramework <ProjectContext>().UseRabbitMQ("localhost").UseDashboard();
                //注册到consul
                opt.UseDiscovery(d =>
                {
                    d.DiscoveryServerHostName = objCAPDiscovery.DiscoveryServerHostName;
                    d.DiscoveryServerPort     = objCAPDiscovery.DiscoveryServerPort;
                    d.CurrentNodeHostName     = objCAPDiscovery.CurrentNodeHostName;
                    d.CurrentNodePort         = objCAPDiscovery.CurrentNodePort;
                    d.NodeId   = objCAPDiscovery.NodeId;
                    d.NodeName = objCAPDiscovery.NodeName;
                });
            });
            services.AddMvc(opt => opt.EnableEndpointRouting = false);
        }
Beispiel #3
0
 public ConsulNodeDiscoveryProvider(ILoggerFactory logger, DiscoveryOptions options)
 {
     _logger  = logger.CreateLogger <ConsulNodeDiscoveryProvider>();
     _options = options;
 }