jimu 是一个基于.Net Core 2.0 简单易用的微服务框架,使用了大量的开源库(如 DotNetty, consul.net, Flurl.Http, Json.net, Log4net, Quartz.net ... ), 支持分布式、高并发和负载均衡, 实现了服务治理(如服务注册、发现、健康检测 ...)和 RPC 调用。
jimu 在持续迭代开发中,很多功能还在排期(如可视化监控和管理工具,热更新,服务熔断、限流和降级 ...),如非高手,不建议上生产环境。
jimu(积木),正如其中文名,希望用她来开发项目像搭积木一样简单快速可控,使项目安全可靠稳定,整体架构可拓展、高并发、分布式。
更多详情,查看 Wiki
创建一个基于 .Net Core 2.0 的类库项目,并添加 jimu 依赖
Install-Package Jimu
添加服务
注意引用空间: using Jimu;
[JimuServiceRoute("api/{Service}")] // RPC 调用路径
public class UserService : IJimuService
{
[JimuService(CreatedBy = "grissom")] // 指定服务的元数据, 该服务调用路径为 api/user/getname?id=
public string GetName(string id)
{
return $"user id {id}, name enjoy!";
}
}
创建一个基于 .Net Core 2.0 的控制台项目, 并添加 jimu.server 依赖
Install-Package Jimu.Server
在 Main 函数中添加服务器启动代码
注意引用空间: using Jimu.Server;
static void Main(string[] args)
{
var hostBuilder = new ServiceHostServerBuilder(new Autofac.ContainerBuilder())
.UseLog4netLogger()
.LoadServices("QuickStart.Services")
.UseDotNettyForTransfer("127.0.0.1", 8001)
.UseInServerForDiscovery()
;
using (var host = hostBuilder.Build())
{
host.Run();
Console.ReadLine();
}
}
创建一个基于 .Net Core 2.0 的 Asp.Net Core Web 应用程序(可选择 API 项目模版),并添加 jimu.client 依赖
Install-Package Jimu.Client
修改 Startup.cs 类的代码, 以便添加对 jimu 的支持
using Jimu.Client;
using Jimu.Client.ApiGateway;
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.AddMvc();
services.UseJimu();
}
// 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.UseMvc();
var host = new ServiceHostClientBuilder(new Autofac.ContainerBuilder())
.UseLog4netLogger()
.UsePollingAddressSelector()
.UseDotNettyForTransfer()
.UseInServerForDiscovery(new Jimu.DotNettyAddress("127.0.0.1", 8001))
.Build();
app.UseJimu(host);
host.Run();
}
}
然后在浏览器访问: http://localhost:58156/api/user/getname?id=666
请下载 jimu 源码, 或者下载项目 jimu.demo
项目暂时由我独自开发和管理,问题请提交 issues
项目的更多资料正在断断续续地整理, 可关注我的 博客园
联系我请发邮件: [email protected]
-
JimuLog4netOptions: Log4net 日志组件
{ "JimuLog4netOptions":{ "EnableConsoleLog":true, "EnableFileLog":true, "FileLogPath":"log", "FileLogLevel":"Debug,Info,Warn,Error", "ConsoleLogLevel":"Debug,Info,Warn,Error", "UseInService": true } }
-
JimuNLogOptions: NLog 组件
-
JwtAuthorizationOptions: Jwt 授权
{ "JwtAuthorizationOptions":{ "ServerIp": "192.168.10.195", "ServerPort": 8001, "Protocol": "Http", //传输协议:Http,Netty "SecretKey": "123456",//生成token 的钥匙 "ValidateLifetime": true, "ExpireTimeSpan": "1.1:1:0",//token 有效时长: day.hour:minute:second "ValidateIssuer": false,// "ValidIssuer": "", "ValidateAudience": false, "ValidAudience": "", "TokenEndpointPath": "/api/v1/token",//获取 token 的路径 } }
-
ConsulOptions: 使用 Consul 作为服务发现组件
{ "ConsulOptions":{ "Ip": "127.0.0.1",//consul ip "Port": 8500,// consul port "ServiceGroups": "MyService",//服务注册所属的组别 "ServerAddress": "192.168.10.195:8001",//服务宿主的地址 } }
-
-
TransportOptions: 服务调用的传输组件
{ "TransportOptions":{ "Ip":"192.168.10.195",//服务宿主ip "Port": 8001, //服务宿主端口 "Protocol":"Netty" //传输协议: Netty, Http } }
-
ServiceOptions: 服务配置
{ "ServiceOptions":{ "Path": "",//服务dll所在路径,默认当前目录 "LoadFilePattern": "IService.dll,Service.dll",//需要加载的服务dll,支持统配符:*.dll,*.txt "WatchFilePattern": "",//监视文件修改,实现热加载,e.g.:IService.dll,Service.dll,支持通配符:*.dll,*.json "EnableWatch": false//是否启动热加载 } }
-
JimuLog4netOptions: Log4net 日志组件
{ "JimuLog4netOptions":{ "EnableConsoleLog":true, "EnableFileLog":true, "FileLogPath":"log", "FileLogLevel":"Debug,Info,Warn,Error", "ConsoleLogLevel":"Debug,Info,Warn,Error" } }
-
JimuNLogOptions: NLog 组件
- JwtAuthorizationOptions: Jwt 授权,支持在客户端实现 jwt 授权,服务端不需要配置
{
"JwtAuthorizationOptions":{
"ServerIp": "192.168.10.195",
"ServerPort": 8001,
"Protocol": "Http", //传输协议:Http,Netty
"SecretKey": "123456",//生成token 的钥匙
"ValidateLifetime": true,
"ExpireTimeSpan": "1.1:1:0",//token 有效时长: day.hour:minute:second
"ValidateIssuer": false,//
"ValidIssuer": "",
"ValidateAudience": false,
"ValidAudience": "",
"TokenEndpointPath": "/api/v1/token",//获取 token 的路径
}
}
- ConsulOptions: 使用 Consul 作为服务发现组件
{
"ConsulOptions":{
"Ip": "127.0.0.1",//consul ip
"Port": 8500,// consul port
"ServiceGroups": "MyService",//服务注册所属的组别
}
}
-
DiscoveryOptions:客户端会缓存已发现的服务,设定刷新频率
{ "DiscoveryOptions":{ "UpdateJobIntervalMinute":1//单位分钟,1分钟刷新一次 } }
-
FaultTolerantOptions:服务调用时的容错机制
{ "FaultTolerantOptions":{ "RetryTimes":0 //服务调用失败重试次数 } }
-
HealthCheckOptions: 根据已发现服务的ip,port 定时进行服务器心跳监测(客户端主动连接)
{ "HealthCheckOptions":{ "IntervalMinute":1 //心跳监测时间间隔,单位分钟 } }
-
LoadBalanceOptions
{ "LoadBalanceOptions":{ "LoadBalance":"Polling" //负载均衡算法: Polling - 轮询 } }
-
ServiceProxyOptions
{ "ServiceProxyOptions":{ "AssemblyNames":["IOrderServices.dll","IUserServices.dll"]//代理接口dll配置 } }
-
TokenGetterOptions
{ "TokenGetterOptions":{ "GetType":"HttpHeader" //从http header 获取, Request.Headers["Authorization"] } }
-
TransportOptions: 服务调用的传输组件
{ "TransportOptions":{ "Protocol":"Netty,Http" //传输协议: Netty, Http } }