6 Star 18 Fork 18

祝健 / zfoo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Ⅰ. zfoo简介

  • 性能炸裂,天生异步,Actor设计思想,无锁化设计,基于Spring的MVC式用法的万能RPC框架
  • 极致序列化,原生集成的目前二进制序列化和反序列化速度最快的 zfoo protocol 作为网络通讯协议
  • 高可拓展性,单台服务器部署,集群部署,注册中心加集群部署,网关加集群部署,随意搭配

完善的工作开发流程,完整的线上解决方案

  • 普通java项目,spring项目,spring boot项目,一行代码无差别热更新 hotswap
  • Excel配置自动映射和Excel热更新方案 storage
  • 轻量级cpu,内存,硬盘,网络监控, 拒绝复杂的监控部署 monitor
  • mongodb的自动映射框架 orm
  • 事件总线 event
  • 时间任务调度 scheduler

Ⅱ. 背景和适用项目

  • 性能需求极高的项目,如游戏服务器,单服滚服,全球服
  • 节省研发成本的项目,如想节省,开发,部署,运维成本
  • 喜欢 KISS法则 的项目 ,简单的配置,优雅的代码。
用Java做网络游戏涉及到比较复杂的交互场景的时候,远程请求非常多,这时候就需要一个高性能的rpc框架。
Java生态的rpc框架非常多,但是很少有rpc框架能够契合到游戏项目的rpc框架,下面列了一些:
    dubbo,代码重量级,异步调用繁琐,没有网关,无法直接和客户端交互;
    grpc,代码重量级,使用复杂,部署复杂,没有网关,很难自定义;
    akka,用Scala语言,学习成本极高,直接放弃;
所以就写了一个万能的rpc框架,代码轻量级,可以方便的自定义,自带网关的,天生异步,工具链完整。
经过后续的不断优化,还做出了一个目前Java速度最快的序列化框架,然后速度就直接起飞了。
后来发现其实不仅仅能用来做游戏,只要有对性能有极致需求的rpc场景都能使用的上这个框架。

Ⅲ. 完整的工程案例

  • zapp 图片分享聊天网站,难度5星
  • tank-game-server 进击的坦克(The Fight of Tanks),难度2星

Ⅳ. 问题

  • 每个工程目录的test文件夹下都有标准的demo展示和注释说明,可以直接运行
  • 问题讨论群QQ: 876280300

Ⅴ. 安装和使用

1. 环境要求

JDK 11+,可以在 OpenJDKOracle JDK 无缝切换

如果你的机器没有安装JDK 11+,最快速的安装方法是在Idea中的Project Structure,Platform Settings,SDKs中直接下载

2. protocol 目前性能最好的Java序列化和反序列化库

// zfoo协议注册,只能初始化一次
ProtocolManager.initProtocol(Set.of(ComplexObject.class, ObjectA.class, ObjectB.class));

// 序列化
ProtocolManager.write(byteBuf, complexObject);

// 反序列化
var packet = ProtocolManager.read(byteBuf);

3. net 目前速度最快的RPC框架

// 服务提供者,只需要在方法上加个注解,则自动注册接口
@PacketReceiver
public void atUserInfoAsk(Session session, UserInfoAsk ask) {
}

// 消费者,同步请求远程用户信息,会阻塞当前的线程,慎重考虑使用同步请求
var userInfoAsk = UserInfoAsk.valueOf(userId);
var answer = NetContext.getCosumer().syncAsk(userInfoAsk, UserInfoAnswer.class, userId).packet();

// 消费者,异步请求远程用户信息,不会柱塞当前的线程,异步请求成功过后依然会在userId指定的线程执行逻辑
NetContext.getCosumer()
                    .asyncAsk(userInfoAsk, UserInfoAnswer.class, userId)
                    .whenComplete(sm -> {
                        // do something
                    );

4. hotswap 热更新代码,不需要停止服务器,不需要额外的任何配置,一行代码开启热更新

// 传入需要更新的class文件
HotSwapUtils.hotswapClass(bytes);

5. orm 基于mongodb的自动映射框架

// 无需自己写sql和任何配置,直接通过注解定义在数据库中定义一张表
@EntityCache(cacheStrategy = "tenThousand", persister = @Persister("time30s"))
public class UserEntity implements IEntity<Long> {
    @Id
    private long id;
    private String name;
}

// 更新数据库的数据
entityCaches.update(userEntity);

6. event 事件总线解耦不同模块,提高代码的质量

// 接收一个事件,只需要在需要接收事件的方法上加一个注解就会自动监听这个事件
@EventReceiver
public void onNoticeEvent(NoticeEvent event) {
    // do something
}

// 抛出一个事件
EventBus.syncSubmit(MyNoticeEvent.valueOf("同步事件"));
EventBus.asyncSubmit(MyNoticeEvent.valueOf("异步事件"));

7. scheduler 基于cron表达式的定时任务调度框架

@Scheduler(cron = "0/1 * * * * ?")
public void cronSchedulerPerSecond() {
    // do something
}

8. storage excel和Java类自动映射框架,无需代码和任何工具只需要定义一个和excel对应的类,直接解析excel

@Resource
public class StudentResource {
    @Id
    private int id;
    @Index
    private String name;
    private int age;
}

Ⅵ. 为什么快

  • 使用目前性能最好的 zfoo protocol 作为网关和RPC消息的序列化和反序列化协议
  • 无锁化设计和优雅的线程池设计,用户的请求通过网关总能保证请求在同一台服务器的同一条线程去执行,所以就不需要用锁保证并发
  • rpc调用天生异步支持,并且保证rpc异步调用结束过后在同一条线程去执行,类似于actor的设计思想,特别适合对性能有极高需求的场景,如直播聊天,游戏等等。
  • 数据库使用高性能分布式数据库 mongodb ,在其之上使用 caffeine 设计了 zfoo orm 二级缓存,充分释放数据库压力
  • 服务器热更新和监控无需代码和额外工具,直接内置在程序里,解放运维生产力
  • 使用MVC设计模式,规范开发,保证代码质量,高效执行

Ⅶ. 提交规范

  • Java项目格式化代码的方式采用IntelliJ Idea默认的格式化
  • 所有的接口的参数和方法返回的参数都默认是非空的,如果参数可空,需要加上org.springframework.lang.Nullable注解
  • 代码提交的时候需要固定格式,如给net增加了一个功能,feat[net]: 功能描述,下面给出了一些常用的格式
feat[module]: 新增某一项功能
perf[module]: 优化了模块代码或者优化了什么功能
fix[module]: 修改了什么bug
test[module]: 测试了什么东西
doc[module]: 增加了什么文档
del[module]: 删除了某些功能或者无用代码

Ⅷ. License

zfoo使用 Apache License Version 2.0

Copyright (C) 2020 The zfoo Authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

超高性能的基于Java的RPC框架 展开 收起
Java 等 6 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/yardans/zfoo.git
git@gitee.com:yardans/zfoo.git
yardans
zfoo
zfoo
main

搜索帮助