赞
踩
由于工作原因一直没有接触过高并发的项目,近期根据网上的教程整理了一个关于高并发的demo。Demo只在开发环境下使用,未上生产环境。第一次写文章,有啥子不好的地方请纠正
功能:商品抢购
使用场景:商城类型网站或者小程序等
开发工具:Visual Studio 2019、sql server2017、Jmeter、Visual Studio core、Nginx、RabbitMQ
框架:.net 5+ef core+swagger
数据库:sql server
缓存框架:redis 5.0 ----集群
消息队列:RabbitMQ
负载均衡:Nginx
消息总线框架:CAP
测试工具:Jmeter
参考文章:https://www.cnblogs.com/yaopengfei/category/1239348.html
Jmeter安装教程:https://blog.csdn.net/wust_lh/article/details/86095924
用户表,产品表,秒杀产品表,订单表,支付记录表(不使用),秒杀时间模型表(不使用):
用户表(User)
产品表(Product)
秒杀产品表(SeckillProduct)
订单表(Order),后期引入Redis NuGet包时名称会冲突建议不要用Order来命名
准备工作到这里差不多就完成了
VS2019创建新项目->选择ASP.NET Core Web应用程序->然后选择ASP.NET Core Web API(选择.net core 和ASP.NET Core 5.0 )
添加NuGet包: Microsoft.EntityFrameworkCore、Microsoft.EntityFrameworkCore.SqlServer、Swashbuckle.AspNetCore、EntityFramework、EFCore.BulkExtensions(如有遗漏请补充)
配置数据库:
appsettings.json添加数据库配置:
"DBConnection": {
"ConnectionStrings": { "Dbconn": "server=DESKTOP-236E652\\MSSQLSERVER2;database=MessageQueuing;Trusted_Connection=true" }
//"ConnectionStrings": { "Dbconn": "server=DESKTOP-236E652\\MSSQLSERVER2;database=Demo;uid=sa;pwd=sa123456" }
},
新建项目DBHelper,Common,Models,然后把实体类建好。
在Common项目中新建类DbHelperContext.cs(数据库连接类),继承DbContext,
/// <summary> /// 数据库连接类 /// </summary> public class DbHelperContext:DbContext { public DbHelperContext(DbContextOptions<DbHelperContext> options) : base(options) { } public DbSet<Order> Order { get; set; } public DbSet<PayRecord> PayRecord { get; set; } public DbSet<Product> Product { get; set; } public DbSet<SeckillProduct> SeckillProduct { get; set; } public DbSet<seckillTimeModel> seckillTimeModel { get; set; } public DbSet<User> User { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { } }
到Startup.cs注册数据库连接类
services.AddDbContext<DbHelperContext>(option =>
{
option.UseSqlServer(Configuration[string.Join(":", new string[] { "DBConnection", "ConnectionStrings", "Dbconn" })]);
});
接下来配置路由
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
开始配置Swagger: 添加NuGe包:IGeekFan.AspNetCore.Knife4jUI,这个版本下的Swagger ui页面看着舒服。在Common新建MiddlewareExtensions文件夹,然后再文件夹里添加SwaggerExtensions类,配置Swagger的基础信息,如标题,版本,路径
public static void AddSwaggerExtensions(this IServiceCollection services) { services.AddSwaggerGen(x => { x.SwaggerDoc("v1", new OpenApiInfo { Title = "MessageQueuing", Version = "v1" }); x.OrderActionsBy(o => o.RelativePath); x.CustomOperationIds(apiDesc => { var controllerAction = apiDesc.ActionDescriptor as ControllerActionDescriptor; if (controllerAction == null) return null; return controllerAction.ControllerName + "-" + controllerAction.ActionName; }); var basePath = Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath; var xmlFile = System.AppDomain.CurrentDomain.FriendlyName + ".xml"; var xmlPath = Path.Combine(basePath, "MessageQueuing.xml"); x.IncludeXmlComments(xmlPath, true); }); }
然后到Startup.cs注册SwaggerExtensions类
//说明文档配置
services.AddSwaggerExtensions();
MiddlewareExtensions文件夹,继续添加SwaggerMiddlewareExtensions类,配置ui页面
public static void UseSwaggerMiddlewareExtensions(this IApplicationBuilder app)
{
app.UseSwagger();
app.UseKnife4UI(x =>
{
x.SwaggerEndpoint("/swagger/v1/swagger.json", "MessageQueuing v1");
});
}
依然到Startup.cs注册SwaggerMiddlewareExtensions类
//文档说明
app.UseSwaggerMiddlewareExtensions();
到这,项目基本的配置就完成了接下来开始进行接口开发和测试
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。