【.NET Core項目實戰-統一認證平臺】第四章 網關篇-資料庫存儲配置(2)

来源:https://www.cnblogs.com/jackcao/archive/2018/11/13/9950305.html
-Advertisement-
Play Games

" 【.NET Core項目實戰 統一認證平臺】開篇及目錄索引 " 上篇文章我們介紹瞭如何擴展Ocelot網關,並實現資料庫存儲,然後測試了網關的路由功能,一切都是那麼順利,但是有一個問題未解決,就是如果網關配置信息發生變更時如何生效?以及我使用其他資料庫存儲如何快速實現?本篇就這兩個問題展開講解, ...


【.NET Core項目實戰-統一認證平臺】開篇及目錄索引

上篇文章我們介紹瞭如何擴展Ocelot網關,並實現資料庫存儲,然後測試了網關的路由功能,一切都是那麼順利,但是有一個問題未解決,就是如果網關配置信息發生變更時如何生效?以及我使用其他資料庫存儲如何快速實現?本篇就這兩個問題展開講解,用到的文檔及源碼將會在GitHub上開源,每篇的源代碼我將用分支的方式管理,本篇使用的分支為course2
附文檔及源碼下載地址:[https://github.com/jinyancao/CtrAuthPlatform/tree/course2]

一、實現動態更新路由

上一篇我們實現了網關的配置信息從資料庫中提取,項目發佈時可以把我們已有的網關配置都設置好並啟動,但是正式項目運行時,網關配置信息隨時都有可能發生變更,那如何在不影響項目使用的基礎上來更新配置信息呢?這篇我將介紹2種方式來實現網關的動態更新,一是後臺服務定期提取最新的網關配置信息更新網關配置,二是網關對外提供安全介面,由我們需要更新時,調用此介面進行更新,下麵就這兩種方式,我們來看下如何實現。

1、定時服務方式

網關的靈活性是設計時必須考慮的,實現定時服務的方式我們需要配置是否開啟和更新周期,所以我們需要擴展配置類AhphOcelotConfiguration,增加是否啟用服務和更新周期2個欄位。

namespace Ctr.AhphOcelot.Configuration
{
    /// <summary>
    /// 金焰的世界
    /// 2018-11-11
    /// 自定義配置信息
    /// </summary>
    public class AhphOcelotConfiguration
    {
        /// <summary>
        /// 資料庫連接字元串,使用不同資料庫時自行修改,預設實現了SQLSERVER
        /// </summary>
        public string DbConnectionStrings { get; set; }

        /// <summary>
        /// 金焰的世界
        /// 2018-11-12
        /// 是否啟用定時器,預設不啟動
        /// </summary>
        public bool EnableTimer { get; set; } = false;

        /// <summary>
        /// 金焰的世界
        /// 2018-11.12
        /// 定時器周期,單位(毫秒),預設30分鐘自動更新一次
        /// </summary>
        public int TimerDelay { get; set; } = 30*60*1000;
    }
}

配置文件定義完成,那如何完成後臺任務隨著項目啟動而一起啟動呢?IHostedService介面瞭解一下,我們可以通過實現這個介面,來完成我們後臺任務,然後通過Ioc容器註入即可。

新建DbConfigurationPoller類,實現IHostedService介面,詳細代碼如下。

using Microsoft.Extensions.Hosting;
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.Repository;
using Ocelot.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ctr.AhphOcelot.Configuration
{
    /// <summary>
    /// 金焰的世界
    /// 2018-11-12
    /// 資料庫配置信息更新策略
    /// </summary>
    public class DbConfigurationPoller : IHostedService, IDisposable
    {
        private readonly IOcelotLogger _logger;
        private readonly IFileConfigurationRepository _repo;
        private readonly AhphOcelotConfiguration _option;
        private Timer _timer;
        private bool _polling;
        private readonly IInternalConfigurationRepository _internalConfigRepo;
        private readonly IInternalConfigurationCreator _internalConfigCreator;
        public DbConfigurationPoller(IOcelotLoggerFactory factory,
            IFileConfigurationRepository repo,
            IInternalConfigurationRepository internalConfigRepo,
            IInternalConfigurationCreator internalConfigCreator, 
            AhphOcelotConfiguration option)
        {
            _internalConfigRepo = internalConfigRepo;
            _internalConfigCreator = internalConfigCreator;
            _logger = factory.CreateLogger<DbConfigurationPoller>();
            _repo = repo;
            _option = option;
        }

        public void Dispose()
        {
            _timer?.Dispose();
        }

        public Task StartAsync(CancellationToken cancellationToken)
        {
            if (_option.EnableTimer)
            {//判斷是否啟用自動更新
                _logger.LogInformation($"{nameof(DbConfigurationPoller)} is starting.");
                _timer = new Timer(async x =>
                {
                    if (_polling)
                    {
                        return;
                    }
                    _polling = true;
                    await Poll();
                    _polling = false;
                }, null, _option.TimerDelay, _option.TimerDelay);
            }
            return Task.CompletedTask;
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            if (_option.EnableTimer)
            {//判斷是否啟用自動更新
                _logger.LogInformation($"{nameof(DbConfigurationPoller)} is stopping.");
                _timer?.Change(Timeout.Infinite, 0);
            }
            return Task.CompletedTask;
        }

        private async Task Poll()
       {
            _logger.LogInformation("Started polling");

            var fileConfig = await _repo.Get();

            if (fileConfig.IsError)
            {
                _logger.LogWarning($"error geting file config, errors are {string.Join(",", fileConfig.Errors.Select(x => x.Message))}");
                return;
            }
            else
            {
                var config = await _internalConfigCreator.Create(fileConfig.Data);
                if (!config.IsError)
                {
                    _internalConfigRepo.AddOrReplace(config.Data);
                }
            }
            _logger.LogInformation("Finished polling");
        }
    }
}

項目代碼很清晰,就是項目啟動時,判斷配置文件是否開啟定時任務,如果開啟就根據啟動定時任務去從資料庫中提取最新的配置信息,然後更新到內部配置並生效,停止時關閉並釋放定時器,然後再註冊後臺服務。

//註冊後端服務
builder.Services.AddHostedService<DbConfigurationPoller>();

現在我們啟動網關項目和測試服務項目,配置網關啟用定時器,代碼如下。

public void ConfigureServices(IServiceCollection services)
{
    services.AddOcelot().AddAhphOcelot(option=>
    {
       option.DbConnectionStrings = "Server=.;Database=Ctr_AuthPlatform;User ID=sa;Password=bl123456;";
       option.EnableTimer = true; //啟用定時任務
       option.TimerDelay = 10*000;//周期10秒
    });
}

啟動後使用網關地址訪問http://localhost:7777/ctr/values,可以得到正確地址。

然後我們在資料庫執行網關路由修改命令,等10秒後再刷新頁面,發現原來的路由失效,新的路由成功。

UPDATE AhphReRoute SET UpstreamPathTemplate='/cjy/values' where ReRouteId=1


看到這個結果是不是很激動,只要稍微改造下我們的網關項目就實現了網關配置信息的自動更新功能,剩下的就是根據我們項目後臺管理界面配置好具體的網關信息即可。

2、介面更新的方式

對於良好的網關設計,我們應該是可以隨時控制網關啟用哪種配置信息,這時我們就需要把網關的更新以介面的形式對外進行暴露,然後後臺管理界面在我們配置好網關相關信息後,主動發起配置更新,並記錄操作日誌。

我們再回顧下Ocelot源碼,看是否幫我們實現了這個介面,查找法Ctrl+F搜索看有哪些地方註入了IFileConfigurationRepository這個介面,驚喜的發現有個FileConfigurationController控制器已經實現了網關配置信息預覽和更新的相關方法,查看源碼可以發現代碼很簡單,跟我們之前寫的更新方式一模一樣,那我們如何使用這個方法呢?

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Ocelot.Configuration.File;
using Ocelot.Configuration.Setter;

namespace Ocelot.Configuration
{
    using Repository;

    [Authorize]
    [Route("configuration")]
    public class FileConfigurationController : Controller
    {
        private readonly IFileConfigurationRepository _repo;
        private readonly IFileConfigurationSetter _setter;
        private readonly IServiceProvider _provider;

        public FileConfigurationController(IFileConfigurationRepository repo, IFileConfigurationSetter setter, IServiceProvider provider)
        {
            _repo = repo;
            _setter = setter;
            _provider = provider;
        }

        [HttpGet]
        public async Task<IActionResult> Get()
        {
            var response = await _repo.Get();

            if(response.IsError)
            {
                return new BadRequestObjectResult(response.Errors);
            }

            return new OkObjectResult(response.Data);
        }

        [HttpPost]
        public async Task<IActionResult> Post([FromBody]FileConfiguration fileConfiguration)
        {
            try
            {
                var response = await _setter.Set(fileConfiguration);

                if (response.IsError)
                {
                    return new BadRequestObjectResult(response.Errors);
                }

                return new OkObjectResult(fileConfiguration);
            }
            catch(Exception e)
            {
                return new BadRequestObjectResult($"{e.Message}:{e.StackTrace}");
            }
        }
    }
}

從源碼中可以發現控制器中增加了授權訪問,防止非法請求來修改網關配置,Ocelot源碼經過升級後,把不同的功能進行模塊化,進一步增強項目的可配置性,減少冗餘,管理源碼被移到了Ocelot.Administration里,詳細的源碼也就5個文件組成,代碼比較簡單,就不單獨講解了,就是配置管理介面地址,並使用IdentityServcer4進行認證,正好也符合我們我們項目的技術路線,為了把網關配置介面和網關使用介面區分,我們需要配置不同的Scope進行區分,由於本篇使用的IdentityServer4會在後續篇幅有完整介紹,本篇就直接列出實現代碼,不做詳細的介紹。現在開始改造我們的網關代碼,來集成後臺管理介面,然後測試通過授權介面更改配置信息且立即生效。

public void ConfigureServices(IServiceCollection services)
{
    Action<IdentityServerAuthenticationOptions> options = o =>
    {
        o.Authority = "http://localhost:6611"; //IdentityServer地址
        o.RequireHttpsMetadata = false;
        o.ApiName = "gateway_admin"; //網關管理的名稱,對應的為客戶端授權的scope
    };
    services.AddOcelot().AddAhphOcelot(option =>
    {
        option.DbConnectionStrings = "Server=.;Database=Ctr_AuthPlatform;User ID=sa;Password=bl123456;";
        //option.EnableTimer = true;//啟用定時任務
        //option.TimerDelay = 10 * 000;//周期10秒
    }).AddAdministration("/CtrOcelot", options);
}

註意,由於Ocelot.Administration擴展使用的是OcelotMiddlewareConfigurationDelegate中間件配置委托,所以我們擴展中間件AhphOcelotMiddlewareExtensions需要增加擴展代碼來應用此委托。

private static async Task<IInternalConfiguration> CreateConfiguration(IApplicationBuilder builder)
{
    //提取文件配置信息
    var fileConfig = await builder.ApplicationServices.GetService<IFileConfigurationRepository>().Get();
    var internalConfigCreator = builder.ApplicationServices.GetService<IInternalConfigurationCreator>();
    var internalConfig = await internalConfigCreator.Create(fileConfig.Data);
    //如果配置文件錯誤直接拋出異常
    if (internalConfig.IsError)
    {
        ThrowToStopOcelotStarting(internalConfig);
    }
    //配置信息緩存,這塊需要註意實現方式,因為後期我們需要改造下滿足分散式架構,這篇不做講解
    var internalConfigRepo = builder.ApplicationServices.GetService<IInternalConfigurationRepository>();
    internalConfigRepo.AddOrReplace(internalConfig.Data);
    //獲取中間件配置委托(2018-11-12新增)
    var configurations = builder.ApplicationServices.GetServices<OcelotMiddlewareConfigurationDelegate>();
    foreach (var configuration in configurations)
    {
        await configuration(builder);
    }
    return GetOcelotConfigAndReturn(internalConfigRepo);
}

新建IdeitityServer認證服務,並配置服務埠6666,並添加二個測試客戶端,一個設置訪問scope為gateway_admin,另外一個設置為其他,來分別測試認證效果。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IdentityServer4.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Ctr.AuthPlatform.TestIds4
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentityServer()
                .AddDeveloperSigningCredential()
                .AddInMemoryApiResources(Config.GetApiResources())
                .AddInMemoryClients(Config.GetClients());
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseIdentityServer();
        }
    }

    public class Config
    {
        // scopes define the API resources in your system
        public static IEnumerable<ApiResource> GetApiResources()
        {
            return new List<ApiResource>
            {
                new ApiResource("api1", "My API"),
                new ApiResource("gateway_admin", "My admin API")
            };
        }

        // clients want to access resources (aka scopes)
        public static IEnumerable<Client> GetClients()
        {
            // client credentials client
            return new List<Client>
            {
                new Client
                {
                    ClientId = "client1",
                    AllowedGrantTypes = GrantTypes.ClientCredentials,

                    ClientSecrets =
                    {
                        new Secret("secret1".Sha256())
                    },
                    AllowedScopes = { "api1" }
                },
                new Client
                {
                    ClientId = "client2",
                    AllowedGrantTypes = GrantTypes.ClientCredentials,

                    ClientSecrets =
                    {
                        new Secret("secret2".Sha256())
                    },
                    AllowedScopes = { "gateway_admin" }
                }
            };
        }
    }
}

配置好認證伺服器後,我們使用PostMan來測試介面調用,首先使用有許可權的client2客戶端,獲取access_token,然後使用此access_token訪問網關配置介面。

訪問http://localhost:7777/CtrOcelot/configuration可以得到我們資料庫配置的結果。

我們再使用POST的方式修改配置信息,使用PostMan測試如下,請求後返回狀態200(成功),然後測試修改前和修改後路由地址,發現立即生效,可以分別訪問http://localhost:7777/cjy/valueshttp://localhost:7777/cjy/values驗證即可。然後使用client1獲取access_token,請求配置地址,提示401未授權,為預期結果,達到我們最終目的。

到此,我們網關就實現了2個方式更新配置信息,大家可以根據實際項目的情況從中選擇適合自己的一種方式使用即可。

二、實現其他資料庫擴展(以MySql為例)

我們實際項目應用過程中,經常會根據不同的項目類型選擇不同的資料庫,這時網關也要配合項目需求來適應不同資料庫的切換,本節就以mysql為例講解下我們的擴展網關怎麼實現資料庫的切換及應用,如果有其他資料庫使用需求可以根據本節內容進行擴展。

【.NET Core項目實戰-統一認證平臺】第三章 網關篇-資料庫存儲配置信息(1)中介紹了網關的資料庫初步設計,裡面有我的設計的概念模型,現在使用mysql資料庫,直接生成mysql的物理模型,然後生成資料庫腳本,詳細的生成方式請見上一篇,一秒搞定。是不是有點小激動,原來可以這麼方便。

新建MySqlFileConfigurationRepository實現IFileConfigurationRepository介面,需要NuGet中添加MySql.Data.EntityFrameworkCore

using Ctr.AhphOcelot.Configuration;
using Ctr.AhphOcelot.Model;
using Dapper;
using MySql.Data.MySqlClient;
using Ocelot.Configuration.File;
using Ocelot.Configuration.Repository;
using Ocelot.Responses;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Ctr.AhphOcelot.DataBase.MySql
{
    /// <summary>
    /// 金焰的世界
    /// 2018-11-12
    /// 使用MySql來實現配置文件倉儲介面
    /// </summary>
    public class MySqlFileConfigurationRepository : IFileConfigurationRepository
    {
        private readonly AhphOcelotConfiguration _option;
        public MySqlFileConfigurationRepository(AhphOcelotConfiguration option)
        {
            _option = option;
        }

        /// <summary>
        /// 從資料庫中獲取配置信息
        /// </summary>
        /// <returns></returns>
        public async Task<Response<FileConfiguration>> Get()
        {
            #region 提取配置信息
            var file = new FileConfiguration();
            //提取預設啟用的路由配置信息
            string glbsql = "select * from AhphGlobalConfiguration where IsDefault=1 and InfoStatus=1";
            //提取全局配置信息
            using (var connection = new MySqlConnection(_option.DbConnectionStrings))
            {
                var result = await connection.QueryFirstOrDefaultAsync<AhphGlobalConfiguration>(glbsql);
                if (result != null)
                {
                    var glb = new FileGlobalConfiguration();
                    //賦值全局信息
                    glb.BaseUrl = result.BaseUrl;
                    glb.DownstreamScheme = result.DownstreamScheme;
                    glb.RequestIdKey = result.RequestIdKey;
                    if (!String.IsNullOrEmpty(result.HttpHandlerOptions))
                    {
                        glb.HttpHandlerOptions = result.HttpHandlerOptions.ToObject<FileHttpHandlerOptions>();
                    }
                    if (!String.IsNullOrEmpty(result.LoadBalancerOptions))
                    {
                        glb.LoadBalancerOptions = result.LoadBalancerOptions.ToObject<FileLoadBalancerOptions>();
                    }
                    if (!String.IsNullOrEmpty(result.QoSOptions))
                    {
                        glb.QoSOptions = result.QoSOptions.ToObject<FileQoSOptions>();
                    }
                    if (!String.IsNullOrEmpty(result.ServiceDiscoveryProvider))
                    {
                        glb.ServiceDiscoveryProvider = result.ServiceDiscoveryProvider.ToObject<FileServiceDiscoveryProvider>();
                    }
                    file.GlobalConfiguration = glb;

                    //提取所有路由信息
                    string routesql = "select T2.* from AhphConfigReRoutes T1 inner join AhphReRoute T2 on T1.ReRouteId=T2.ReRouteId where AhphId=@AhphId and InfoStatus=1";
                    var routeresult = (await connection.QueryAsync<AhphReRoute>(routesql, new { result.AhphId }))?.AsList();
                    if (routeresult != null && routeresult.Count > 0)
                    {
                        var reroutelist = new List<FileReRoute>();
                        foreach (var model in routeresult)
                        {
                            var m = new FileReRoute();
                            if (!String.IsNullOrEmpty(model.AuthenticationOptions))
                            {
                                m.AuthenticationOptions = model.AuthenticationOptions.ToObject<FileAuthenticationOptions>();
                            }
                            if (!String.IsNullOrEmpty(model.CacheOptions))
                            {
                                m.FileCacheOptions = model.CacheOptions.ToObject<FileCacheOptions>();
                            }
                            if (!String.IsNullOrEmpty(model.DelegatingHandlers))
                            {
                                m.DelegatingHandlers = model.DelegatingHandlers.ToObject<List<string>>();
                            }
                            if (!String.IsNullOrEmpty(model.LoadBalancerOptions))
                            {
                                m.LoadBalancerOptions = model.LoadBalancerOptions.ToObject<FileLoadBalancerOptions>();
                            }
                            if (!String.IsNullOrEmpty(model.QoSOptions))
                            {
                                m.QoSOptions = model.QoSOptions.ToObject<FileQoSOptions>();
                            }
                            if (!String.IsNullOrEmpty(model.DownstreamHostAndPorts))
                            {
                                m.DownstreamHostAndPorts = model.DownstreamHostAndPorts.ToObject<List<FileHostAndPort>>();
                            }
                            //開始賦值
                            m.DownstreamPathTemplate = model.DownstreamPathTemplate;
                            m.DownstreamScheme = model.DownstreamScheme;
                            m.Key = model.RequestIdKey;
                            m.Priority = model.Priority ?? 0;
                            m.RequestIdKey = model.RequestIdKey;
                            m.ServiceName = model.ServiceName;
                            m.UpstreamHost = model.UpstreamHost;
                            m.UpstreamHttpMethod = model.UpstreamHttpMethod?.ToObject<List<string>>();
                            m.UpstreamPathTemplate = model.UpstreamPathTemplate;
                            reroutelist.Add(m);
                        }
                        file.ReRoutes = reroutelist;
                    }
                }
                else
                {
                    throw new Exception("未監測到任何可用的配置信息");
                }
            }
            #endregion
            if (file.ReRoutes == null || file.ReRoutes.Count == 0)
            {
                return new OkResponse<FileConfiguration>(null);
            }
            return new OkResponse<FileConfiguration>(file);
        }

        //由於資料庫存儲可不實現Set介面直接返回
        public async Task<Response> Set(FileConfiguration fileConfiguration)
        {
            return new OkResponse();
        }
    }
}

實現代碼後如何擴展到我們的網關里呢?只需要在註入時增加一個擴展即可。在ServiceCollectionExtensions類中增加如下代碼。

/// <summary>
/// 擴展使用Mysql存儲。
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IOcelotBuilder UseMySql(this IOcelotBuilder builder)
{
    builder.Services.AddSingleton<IFileConfigurationRepository, MySqlFileConfigurationRepository>();
    return builder;
}

然後修改網關註入代碼。

public void ConfigureServices(IServiceCollection services)
{
    Action<IdentityServerAuthenticationOptions> options = o =>
    {
        o.Authority = "http://localhost:6611"; //IdentityServer地址
        o.RequireHttpsMetadata = false;
        o.ApiName = "gateway_admin"; //網關管理的名稱,對應的為客戶端授權的scope
    };
    services.AddOcelot().AddAhphOcelot(option =>
                                       {
                                           option.DbConnectionStrings = "Server=localhost;Database=Ctr_AuthPlatform;User ID=root;Password=bl123456;";
                                           //option.EnableTimer = true;//啟用定時任務
                                           //option.TimerDelay = 10 * 000;//周期10秒
                                       })
        .UseMySql()
        .AddAdministration("/CtrOcelot", options);
}

最後把mysql資料庫插入sqlserver一樣的路由測試信息,然後啟動測試,可以得到我們預期的結果。為了方便大家測試,附mysql插入測試數據腳本如下。

#插入全局測試信息
insert into AhphGlobalConfiguration(GatewayName,RequestIdKey,IsDefault,InfoStatus)
values('測試網關','test_gateway',1,1);

#插入路由分類測試信息
insert into AhphReRoutesItem(ItemName,InfoStatus) values('測試分類',1);

#插入路由測試信息 
insert into AhphReRoute values(1,1,'/ctr/values','[ "GET" ]','','http','/api/Values','[{"Host": "localhost","Port": 9000 }]','','','','','','','',0,1);

#插入網關關聯表
insert into AhphConfigReRoutes values(1,1,1);

如果想擴展其他資料庫實現,直接參照此源碼即可。

三、回顧與預告

本篇我們介紹了2種動態更新配置文件的方法,實現訪問不同,各有利弊,正式使用時可以就實際情況選擇即可,都能達到我們的預期目標,也介紹了Ocelot擴展組件的使用和IdentityServer4的基礎入門信息。然後又擴展了我們mysql資料庫的存儲方式,增加到了我們網關的擴展里,隨時可以根據項目實際情況進行切換。

網關的存儲篇已經全部介紹完畢,有興趣的同學可以在此基礎上繼續拓展其他需求,下一篇我們將介紹使用redis來重寫Ocelot里的所有緩存,為我們後續的網關應用打下基礎。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1、一行代碼實現1-100的和 2、如何在一個函數內修改全局變數的值 3、字典如何刪除鍵和合併兩個字典 4、說一說對python的GIL的理解 GIL是python的全局解釋器鎖,在一個進程中如果有多個線程執行,其中一個線程在執行的時候會霸占python解釋器(加鎖即GIL),那麼其他線程就不能執行 ...
  • WIN7系統,VC2010下。 程式A靜態鏈接B.dll動態庫。 B.dll中導出3個類: (1) 基類 class AFX_EXT_CLASS base { public: base(){}; virtual ~base(){}; virtual int getX() = 0; protected ...
  • 一 、約束 python中約束有兩種 第一種,通過拋異常進行約束,這種是子類不按我要求的來,我就給你拋異常(推薦) 操作:提取一個父類. 在父類中給出一個方法。但在方法中不給出任何代碼,直接拋異常 1 # 貼吧 2 # 項目經理(級別高一點兒) 3 class Base: 4 def login(s ...
  • 2018年11月12日20:51:35 一、基礎知識: 1、JVM、JRE和JDK的區別: JVM(Java Virtual Machine):java虛擬機,用於保證java的跨平臺的特性。 java語言是跨平臺,jvm不是跨平臺的。 JRE(Java Runtime Environment):j ...
  • 按位與&: 只要對應的二個二進位都為1時,結果位就為1 按位或|:只要對應的二個二進位有一個為1時,結果位就為1 按位異或^:0⊕0=0,1⊕0=1,0⊕1=1,1⊕1=0(同為0,異為1) 按位非:取反操作符 ...
  • 在定義一個類的時候,有時我們需要獲取一個類的屬性值,而這個屬性值需要經過類中的其他屬性運算來獲得的。那麼很容易,只要我們在類中定義一個方法,並且通過調用方法可以獲取到那個需要運算的屬性值。那麼,問題來了,當有一天需求變了,你需要反向操作你之前實現的類,你需要通過傳入那個需要運算得來的值來獲取參與運算 ...
  • 基礎類型 原始類型:id必須要傳,否則報錯。 包裝類型:id可以不傳,後臺接受到null。 list&set 簡單類型 前臺 form表單 ajax 後臺 複雜類型 如 :(略)同json格式對象 數組 前臺 form表單 ajax 後臺 map 前臺 form ajax 後臺 pojo簡單屬性 前 ...
  • 一、NumPy簡介: NumPy是python一個包。它是一個由多為數組對象和用於處理數字的常式集合組成的庫。 二、Ndarray對象 NumPy中的數組類稱為ndarray,ndarray是一系列同類型數據的集合,以0下標為開始進行集合的索引。 ndarray對象用於存放同類型元素的多維數組。 n ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...