base surging
- 企业级互联网架构平台;
- 传统大型项目,伸缩性很强的项目,可应对突发的流量暴增(比如双十一订单暴增);
- 移动互联网项目,比如为了因对突发的因营销等带来的流量暴增,评论暴增等等情况;
欢迎补充...
本示例项目是基于 Surging https://github.com/dotnetcore/surging
运行下面脚本,生成user表
clone代码 git clone https://github.com/billyang/SurgingDemo.git
D:\git\surging
D:\git\SurgingDemo
这样做的好处:
- 是和 surging 保持最新代码
- 是方便学习surging和调试,毕竟你想使用surging、理解surging才是踏出第一步
Surging.ApiGateway 提供了服务管理以及网关统一访问入口。 目前开发还不完善,如果现在要用于正式开发建议自己要部分重写 ApiGateway,加入权限验证。相信等到1.0版本作者也会把数据监控、流量控制、数据安全、分流控制、身份认证等管理功能加入,当然这些功能并不会影响正常使用。
本示例服务注册中心使用 consul,因为调试简单,只需 consul agent -dev 即可开启consul
假设你已经把SurgingDemo已运行起来了,即可对根据Dapper对User进行增删改查
public class UserRepository: BaseRepository, IBaseRepository<User>
{
/// <summary>
/// 创建一个用户
/// </summary>
/// <param name="entity">用户</param>
/// <param name="connectionString">链接字符串</param>
/// <returns></returns>
public Task<Boolean> CreateEntity(User entity, String connectionString = null)
{
using (IDbConnection conn = DataBaseConfig.GetSqlConnection(connectionString))
{
string insertSql = @"INSERT INTO dbo.auth_User
( TenantId ,
Name ,
Password ,
SecurityStamp ,
FullName ,
Surname ,
PhoneNumber ,
IsPhoneNumberConfirmed ,
EmailAddress ,
IsEmailConfirmed ,
EmailConfirmationCode ,
IsActive ,
PasswordResetCode ,
LastLoginTime ,
IsLockoutEnabled ,
AccessFailedCount ,
LockoutEndDateUtc
)
VALUES ( @tenantid ,
@name ,
@password ,
@securitystamp ,
@fullname ,
@surname ,
@phonenumber ,
@isphonenumberconfirmed ,
@emailaddress ,
@isemailconfirmed ,
@emailconfirmationcode ,
@isactive ,
@passwordresetcode ,
@lastlogintime ,
@islockoutenabled ,
@accessfailedcount ,
@lockoutenddateutc
);";
return Task.FromResult<Boolean>(conn.Execute(insertSql, entity) > 0);
}
}
}
Task<UserDto> GetUserById(Int64 id);
Task<Boolean> UpdateUser(UserDto user);
Task<Boolean> DeleteUser(Int64 userId);
如:
public async Task<UserDto> GetUserById(Int64 id)
{
var user = await _repository.GetEntityById(id);
return new UserDto()
{
Id = user.Id,
EmailAddress = user.EmailAddress,
Name = user.Name,
PhoneNumber = user.PhoneNumber,
Surname = user.Surname,
TenantId = user.TenantId,
FullName = user.FullName,
};
}
public async Task<Boolean> UpdateUser(UserDto user)
{
var entity = await _repository.GetEntityById(user.Id);
entity.Name = user.Name;
entity.Password = user.Password;
entity.FullName = user.FullName;
entity.Surname = user.Surname;
entity.EmailAddress = user.EmailAddress;
entity.PhoneNumber = user.PhoneNumber;
return await _repository.Update(entity);
}
public async Task<Boolean> DeleteUser(Int64 userId)
{
return await _repository.Delete(userId);
}
public async Task<IActionResult> Delete(Int64 id)
{
var service = ServiceLocator.GetService<IServiceProxyFactory>();
var userProxy = service.CreateProxy<IUserService>("User");
await userProxy.DeleteUser(id);
return RedirectToAction("User");
}
public async Task<JsonResult> GetUser(Int64 id)
{
var service = ServiceLocator.GetService<IServiceProxyFactory>();
var userProxy = service.CreateProxy<IUserService>("User");
var output= await userProxy.GetUserById(id);
return new JsonResult(output);
}
public async Task<JsonResult> Update(UserDto dto)
{
var service = ServiceLocator.GetService<IServiceProxyFactory>();
var userProxy = service.CreateProxy<IUserService>("User");
var output = await userProxy.UpdateUser(dto);
return new JsonResult(output);
}