# DeZhenCoreTemplate **Repository Path**: BigHammerWang/DeZhenCoreTemplate ## Basic Information - **Project Name**: DeZhenCoreTemplate - **Description**: 12345678910 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-05-19 - **Last Updated**: 2023-12-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 框架简介 ### 1、基于.net core 3.1原生管道机制,不做任何修改,只做上层应用的封装,兼容所有原生.net core解决方案; ### 2、插件式组件,不强依赖任何中间件,用则引用,不用则卸载,不会对程序的启动有任何影响 ### 3、通过特性对对象进行管理,简洁明了 ## 使用简介 ### 1、对象的管理 ####    对象的注册:只需在类上声明特性[Service]即可,该特新拥有两个参数分别为ServiceLifetime和Identifier,分别代表注册对象的生命周期和对象标识(标识用于同一接口多实现的分别注册,在后面[Autowired]中会用到) ``` /// /// 默认生命周期为单例 /// [Service] public class UserServiceImpl:IUserService { } /// /// 同一接口的三种实现,传入不同的Identifier /// [Service(ServiceLifetime.Singleton,"WeChat")] public class WeChatLoginServiceImpl:ILoginService { } [Service(ServiceLifetime.Singleton,"Web")] public class WebLoginServiceImpl:ILoginService { } [Service(ServiceLifetime.Singleton,"DingDing")] public class DingDingLoginServiceImpl:ILoginService { } ``` ####    对象的注入:在调用的类的构造函数里注入IAutowiredPlatform且调用Autowired(this)方法,即可使用[Autowired]特性获取所注入的服务 ``` public class DemoController { /// /// 直接通过[Autowired]即可注入对象,无需写臃肿的构造函数代码或IOC容器获取代码,简洁明了 /// [Autowired] private IUserService _userService; /// /// 同一接口的不通实现可以传入不同服务的Identifier即可获取不同的实现 /// [Autowired("Web")] private ILoginService _webLoginService; [Autowired("DingDing")] private ILoginService _dingTalkbLoginService; [Autowired("WeChat")] private ILoginService _weChatLoginService; public DemoController(IAutowiredPlatform autowiredService) { autowiredService.Autowired(this); } } ``` ### 2、读取配置文件 ####    在调用的类的构造函数里注入IAutowiredPlatform且调用Autowired(this)方法,即可使用[Value]特性获读取配置文件 ``` public class UserController { /// /// 通过特性即可读取配置文件,不用写臃肿的AppConfig.GetXXXX()方法 /// [Value("Redis")] private RedisConfig Config; public DemoController(IAutowiredPlatform autowiredService) { autowiredService.Autowired(this); } } ``` ### 3、数据库操作 ####    Nuget中引用Dezhen.Core.SqlSugar包,创建的类继承BaseRepository,传入configId,即可使用SqlSugarScopeProvider对象进行CRUD操作 ``` AppSettings中配置 "DBConfig": [ { "ConfigId": "1", "DbType": "SqlServer", "ConnectionString": "Data Source=XX.XX.XX.XX;Initial Catalog=Test;User ID=sa;Password=123456;TrustServerCertificate=true" } ] [Service] public class UserRepository:BaseRepository { public UserRepository(IAutowiredPlatform autowiredService) : base("1") { autowiredService.Autowired(this); } public async Task> GetUserInfoFromDataBaseAsync() { return await SqlClient.Queryable().ToListAsync(); } } ``` ### 4、Redis操作 ####    Nuget中引用Dezhen.Core.Redis包,注入IRedisPlatform即可进行redis操作(当IRedisPlatform提供的方法不满足现有业务的需求时, 可以使用其中IDatabase和IConnectionMultiplexer两个StackExchange.Redis提供的原生接口对redis进行操作) ``` AppSettings中配置 "Redis": { "ConnectString": "xx.xx.xx.xx:6379,password=123456", //链接字符串 "Db": 0, //库 "Sentinel": { //哨兵模式 "Enable": false, // true:开启哨兵模式 false: 关闭哨兵模式 "Ip": [], "Password": "123456", //密码 "ServiceName": "mymaster" //哨兵模式下主机名称 } }, [Service] public class UserRepository { /// /// 通过特性[Autowired] 获取IRedisPlatform操作redis /// [Autowired] private IRedisPlatform _redis; public UserRepository(IAutowiredPlatform autowiredService) { autowiredService.Autowired(this); } public async Task GetUserInfoFromRedisAsync(String key) { return await _redisPlatform.GetAsync(key); } } ``` ### 4、微服务负载均衡调用 ####    Nuget中引用Dezhen.Core.Consul包,添加配置,即可注册到Consul ``` AppSettings中配置 "Consul": { "ConsulIP": "1.117.220.221", //注册中心IP "ConsulPort": "8500", //注册中心端口 "RibbonPattern": "RoundRobin", //轮训方式 Random:随机 RoundRobin:负载均衡 WeightRandom:加权随机 WeightRoundRobin:加权轮旋 CustormRobbin :自定义 "ServiceName": "Template", //服务名称 "Weight": 1, //权重 "GatewayAddress": "http://1.117.220.221:8002" //需要对外时对外提供的调用地址,不填则默认读取StartConfig }, ``` ####   需要添加如下controller用于和consul心跳检测 ``` [Route("api/[controller]/[action]")] public class ConsulController : BaseApiController { [HttpGet] public String Health() { return "OK"; } } ``` ####   微服务调用(调用地址不再为固定的http://ip:port/api/...而是采用ServiceName,即:http://ServiceName/api/... IConsulHttpClient中请求方法会根据ServiceName和配置文件中配置好的算法进行调用,已内置负载均衡,权重等算法) ``` [Route("api/[controller]/[action]")] public class DemoController : BaseApiController { /// /// 注入该接口 /// [Autowired] private IConsulHttpClient _consulHttpClient; /// /// 演示负载均衡,返回信息是请求的不同的实例 /// /// [HttpGet] public async Task ShwoRoundRobinDemo() { String serviceName = "Demo"; return HttpResult.Success(await _consulHttpClient.GetAsync($"http://{serviceName}/api/demo/GetSelfAddress")); } } ``` ## 不在更新,后续请移步https://gitee.com/bighammercloud/bighammercoretemplate