Giter Site home page Giter Site logo

snowman89 / grain Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dianbaer/grain

0.0 0.0 0.0 12.99 MB

grain is a RPC framework.It contains many to many relational distributed locks and so on.Select components on demand,Don't kidnap developers.

License: MIT License

Java 51.16% Batchfile 0.14% HTML 1.40% JavaScript 47.30%

grain's Introduction

grain

Build Status Codacy Badge License

grain是一个极简、颗粒化的RPC框架,同时包含多对多关系的分布式锁等组件,按需选择组件,不绑架开发者。

grain架构图

架构图

grain本着极简、颗粒化的原则,就算一步一步开发到distributedlock(多对多关系的分布式锁),也仅仅使用了三个外部jar包。

开发者可根据项目情况按需选择组件,例如:

1、如果你想使用distributedlock(多对多关系的分布式锁),那需要选择他依赖的六个组件和三个外部jar包。

2、如果你想使用rpc(远程对象访问),那需要选择他依赖的五个组件和三个外部jar包。

3、如果你想使用threadmsg(系统多线程模型,线程消息通讯),那需要选择他依赖的三个组件,不需要任何外部jar包。

4、如果你想使用threadkeylock(支持锁类型单键值与双键值的多线程锁),那仅仅只需要他依赖的一个组件和他本身。

https://github.com/dianbaer/grain

组件介绍

1、grain-log(日志接口)

如果想打印日志,需要实现此日志接口并将实现的类的对象传递到各个组件初始化函数中。

grain-log-如何使用


2、grain-msg(通用消息)

可进行消息的注册、监听、派发等功能。(当前线程)

grain-msg-如何使用


3、grain-thread(系统多线程模型)

grain最核心的组件,支撑起上层很多组件包含threadmsg(线程消息通讯)、rpc(远程对象访问)、distributedlock(多对多关系的分布式锁)。

grain-thread支持创建多线程池,业务轮询精准注入指定线程ID,任意消息(例如:msg、tcp、websocket等)精准注入指定线程ID。

grain-thread-如何使用


4、grain-threadmsg(线程消息通讯)

系统多线程之间的通讯,业务线程跳转都依赖此组件。

grain-threadmsg-如何使用


5、grain-tcp(TCP客户端与服务器)

通过grain-tcp可创建TCP客户端与服务器进行通讯,客户端支持断线重连。通讯消息序列化与反序列化,粘包问题等都已内部解决。

grain-tcp-如何使用

例子:grain-tcp-clienttest、grain-tcp-servertest直接运行main函数即可


6、grain-rpc(远程对象访问)

通过grain-rpc可以创建RPC客户端与服务器进行远程对象访问。多线程阻塞,唤醒等复杂的多线程业务都已内部解决。

简单例子:

1、创建消息包
RPCTestC.Builder builder = RPCTestC.newBuilder();
builder.setName("RPC你好啊");
TcpPacket pt = new TcpPacket(TestTCode.TEST_RPC_C, builder.build());
2、远程调用
TcpPacket ptReturn = WaitLockManager.lock(session, pt);

grain-rpc-如何使用

例子:grain-rpc-clienttest、grain-rpc-servertest直接运行main函数即可


7、grain-distributedlock(多对多关系的分布式锁)

去中心化思路,通过grain-distributedlock可以创建分布式锁服务器与锁客户端。
grain-distributedlock不同类型互不影响,相同类型不同键值互不影响。仅仅当类型与键值都相等时会进行分布式阻塞。
锁客户端与锁服务器的双向线程阻塞,服务器匹配、类型键值与线程ID的匹配都已内部解决。

简单例子:

// 获取锁
int lockId = DistributedLockClient.getLock("111", "user");
if (lockId == 0) {
	return;
}
/*********** 执行分布式锁业务逻辑 *********/
System.out.println("分布式锁id为:" + lockId);
/*********** 执行分布式锁业务逻辑 *********/
// 释放锁
DistributedLockClient.unLock("111", "user", lockId);

grain-distributedlock-如何使用

例子:grain-distributedlock-clienttest、grain-distributedlock-servertest直接运行main函数即可


8、grain-config(通用配置加载管理)

grain-config可以进行配置的加载,管理等工作。支持模板类文件与json文件。

grain-config-如何使用


9、grain-threadkeylock(支持锁类型单键值与双键值的多线程锁)

在多线程业务中,支持锁类型的单键值与双键值,并且支持锁函数

简单例子1(锁函数):当类型为TEST1,键值为111同时调用函数时,会进行锁定。

public String lockFunction(Object... params) {}
String str = (String) KeyLockManager.lockMethod("111", TEST1, (params) -> lockFunction(params), new Object[] { "222", 111 });

简单例子2(锁函数):当类型为TEST1,键值为111或222同时调用函数时,会进行锁定。

String str = (String) KeyLockManager.lockMethod("111", "222", TEST1, (params) -> lockFunction(params), new Object[] { "222", 111 });

grain-threadkeylock-如何使用


10、grain-reds、grain-mongodb、grain-mariadb(持久与缓存)

更简洁的操作redis、mongodb、mariadb。

grain-reds-如何使用

grain-mongodb-如何使用

grain-mariadb-如何使用


11、grain-websocket(websocket服务器创建)

将grain-websocket包引入web工程,可以创建websocket服务器。(业务基于容器线程,例如tomcat线程)

grain-websocket-如何使用

例子:grain-websocket-test


12、grain-threadwebsocket(websocket服务器创建)

将grain-threadwebsocket包引入web工程,可以创建websocket服务器。(业务分发至系统多线程模型grain-thread,可以精准指派某业务归属线程ID)

grain-threadwebsocket-如何使用

例子:grain-threadwebsocket-test


13、grain-httpserver(创建http服务器)

基于servlet的http服务器,简化http请求

grain-httpserver-如何使用

例子:grain-httpserver-test


打版本

ant

依赖

java8

grain's People

Contributors

dianbaer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.