SmartRoute是基于Dotnet Core设计的可运行在linux和windows下的服务通讯组件,其设计理念是去中心化和零配置即可实现服务通讯集群。SmartRoute是通过消息订阅的机制实现服务与服务之间的通讯,它可以让广播网段内所有服务器上的应用自动构建通讯集群; 而通讯集群完全是SmartRoute自动构建并不需要进行任何配置或安装中间服务。通过这种全新的通讯开发方式可以让开发者更轻松和简单地构建基于服务的集群通讯应用。
分布式业务号生成服务:https://github.com/IKende/SmartRoute.BNR
分布式锁服务https://github.com/IKende/SmartRoute.DLocks
消息路由中心:https://github.com/IKende/SmartRoute.MRC
public class Program : IUserService
{
public static void Main(string[] args)
{
INode node = SmartRoute.NodeFactory.Default;
node.Loger.Type = LogType.ALL;
node.AddLogHandler(new SmartRoute.ConsoleLogHandler(LogType.ALL));
node.Open();
node.RegisterService<IUserService>(new Program());
while (true)
{
Console.Write(node.GetClusterInfo());
System.Threading.Thread.Sleep(1000);
}
}
public void ChangePWD(string name, string oldpwd, string newpwd)
{
Console.WriteLine("ChangePWD {0}/{1}/{2}", name, oldpwd, newpwd);
}
public DateTime Register(string name, string email)
{
Console.WriteLine("register {0}/{1}", name, email);
return DateTime.Now;
}
}
通过任意一个节点获取整个集群节点的状态信息,主要包括:网络IO,网络流量,消息处理量和当前每秒相应并发量的信息.
node.GetClusterInfo()
注册接口服务
public class Program : IUserService
{
public static void Main(string[] args)
{
INode node = SmartRoute.NodeFactory.Default;
node.Loger.Type = LogType.ALL;
node.AddLogHandler(new SmartRoute.ConsoleLogHandler(LogType.ALL));
node.Open();
SwitchSubscriber rmiserver = new SwitchSubscriber(node);
rmiserver.Register<IUserService>(new Program());
System.Threading.Thread.Sleep(-1);
}
public void ChangePWD(string name, string oldpwd, string newpwd)
{
Console.WriteLine("ChangePWD {0}/{1}/{2}", name, oldpwd, newpwd);
}
public DateTime Register(string name, string email)
{
Console.WriteLine("register {0}/{1}", name, email);
return DateTime.Now;
}
}
调用接口服务
INode node = NodeFactory.Default;
node.Loger.Type = LogType.ALL;
node.AddLogHandler(new ConsoleLogHandler(LogType.ALL));
node.Open();
SwitchSubscriber rmiserver = new SwitchSubscriber(node);
mUserService = new UserService(rmiserver);
DateTime result = mUserService.Register("henry" + i, "[email protected]");
通过SwitchSubscriber实现多节点订阅负载处理
IList<Employee> item = mSwitchSubscriber.PublishToServicee<IList<Employee>>("henry", Employee.GetEmployee());
由于很多场景需要支持同步返回消息处理,因此加入些功能的支持
Employee result = ken.Publish<Employee>("henry", Employee.GetEmployee());
public class Program
{
static long mCount;
public static void Main(string[] args)
{
INode node = NodeFactory.Default;
node.Open();
EventSubscriber henry = node.Register<EventSubscriber>("henry");
henry.Register<User>(OnUser);
henry.Register<Employee>(OnEmployees);
Console.Read();
}
private static void OnEmployees(Message msg, Employee emp)
{
System.Threading.Interlocked.Increment(ref mCount);
msg.Reply(Employee.GetEmployee());
}
private static void OnUser(Message msg, User user)
{
System.Threading.Interlocked.Increment(ref mCount);
msg.Reply(new User { Name = "henry" });
}
}
public class Program
{
static long mCount;
public static void Main(string[] args)
{
INode node = NodeFactory.Default;
node.Open();
EventSubscriber ken = node.Register<EventSubscriber>("ken");
ken.Register<User>(OnUser);
ken.Register<Employee>(OnEmployees);
node.SubscriberRegisted = (n, s) =>
{
if (s.Name == "henry")
{
ken.Publish("henry", Employee.GetEmployee());
}
};
while (true)
{
Console.WriteLine(mCount);
System.Threading.Thread.Sleep(1000);
}
Console.Read();
}
private static void OnEmployees(Message msg, Employee emp)
{
System.Threading.Interlocked.Increment(ref mCount);
msg.Reply(Employee.GetEmployee());
}
private static void OnUser(Message msg, User user)
{
msg.Reply(new User { Name = "key" });
System.Threading.Interlocked.Increment(ref mCount);
}
}