1 Star 1 Fork 2

wangjun0710 / Surging 微服务框架使用入门

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 7.57 KB
一键复制 编辑 原始数据 按行查看 历史
billyang 提交于 2018-04-13 14:31 . Update README.md

SurgingDemo

base surging

前言:

Surging 分布式微服务框架适合做什么

  1. 企业级互联网架构平台;
  2. 传统大型项目,伸缩性很强的项目,可应对突发的流量暴增(比如双十一订单暴增);
  3. 移动互联网项目,比如为了因对突发的因营销等带来的流量暴增,评论暴增等等情况;

欢迎补充...

本示例项目是基于 Surging https://github.com/dotnetcore/surging

旨在描述如何在 surging 的基础上运行dapper完成一个增删改的例子

Surging缓存拦截

1.在sqlserver中建立Test 数据库

运行下面脚本,生成user表

test.db

2.运行Surging Demo

clone代码 git clone https://github.com/billyang/SurgingDemo.git

因为本示例项目没有从nuget 引用,直接从 surging 项目引用,没有拷贝一份放在自己的解决方案,

假设目录结构如下:

D:\git\surging
D:\git\SurgingDemo

最终保持SurgingSurgingDemo在同一个目录

这样做的好处:

  • 是和 surging 保持最新代码
  • 是方便学习surging和调试,毕竟你想使用surging、理解surging才是踏出第一步

Surging.ApiGateway 提供了服务管理以及网关统一访问入口。 目前开发还不完善,如果现在要用于正式开发建议自己要部分重写 ApiGateway,加入权限验证。相信等到1.0版本作者也会把数据监控、流量控制、数据安全、分流控制、身份认证等管理功能加入,当然这些功能并不会影响正常使用。

本示例服务注册中心使用 consul,因为调试简单,只需 consul agent -dev 即可开启consul

在 windows 中启动:
发布网关 1. ApiGateway dotnet run Surging.ApiGateway
启用服务 2. Server   dotnet Bill.Demo.Services.Server.dll
发布客户端(本示例使用 web mvc) 3. Bill.Demo.Web dotnet run Bill.Demo.Web

假设你已经把SurgingDemo已运行起来了,即可对根据Dapper对User进行增删改查 dapper

介绍一下本示例各项目的职责

Bill.Demo.Core 用户定义数据模型

Bill.Demo.DapperCore (Dapper仓储,其中仓储需继承 UserRepository: Surging.Core.CPlatform.Ioc.BaseRepository)

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);
            }
        }
   }

Bill.Demo.IModuleServices (和Surging项目一样,定义模块服务接口以及领域模型)

       Task<UserDto> GetUserById(Int64 id);
        
        Task<Boolean> UpdateUser(UserDto user);

        Task<Boolean> DeleteUser(Int64 userId);

Bill.Demo.ModuleServices (和Surging项目一样,实现模块服务)

如:

        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);
        }

Bill.Demo.Services.Server 服务

Bill.Demo.Web 客户端

        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);
        }
1
https://gitee.com/wangjun0710/SurgingDemo.git
git@gitee.com:wangjun0710/SurgingDemo.git
wangjun0710
SurgingDemo
Surging 微服务框架使用入门
master

搜索帮助