/zfoo

💡极致性能的Java服务器框架,RPC,游戏服务器框架,web应用服务器框架。(Extreme fast enterprise Java server framework, can be RPC, game server framework, web server framework.)

Primary LanguageJavaApache License 2.0Apache-2.0

👉 为性能而生的万能服务器框架👈



Ⅰ. zfoo简介🚩

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

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

  • 普通java项目,spring项目,分布式项目,容器项目, 不停机无差别热更新代码 hotswap
  • Excel配置自动映射, 单服和分布式Excel热更新方案 storage
  • MongoDB的自动映射框架 orm
  • 事件总线 event
  • 时间任务调度 scheduler
  • 内置在程序里的轻量级cpu,内存,硬盘,网络监控, 无需代码和额外工具,解放运维生产力 monitor

Ⅱ. 背景和适用项目

  • 性能需求极高的项目,如网站和游戏服务器框架,单服滚服,全球服,直播聊天,IM系统,实时推送
  • 节省研发成本的项目,如想节省,开发,部署,运维成本
  • 适合作为 Godot,Unity,Cocos,Webgl,H5 的后端基础框架,网络通信协议支持 tcp udp websocket http
  • 语言支持 Java Javascript C# Lua GDScript,可以轻易实现跨平台
  • 喜欢 KISS法则 的项目 ,简单的配置,优雅的代码

Ⅲ. 完整的工程案例和视频教程

Ⅳ. 问题

Ⅴ. 安装和使用

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框架,支持 tcp udp websocket http

// 服务提供者,只需要在方法上加个注解,则自动注册接口
@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的自动映射框架,使用 caffeine 设计了二级缓存,充分释放数据库压力

// 无需自己写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 onMyNoticeEvent(MyNoticeEvent 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;
}

Ⅵ. 提交规范👏

  • 欢迎喜欢这个项目的人来一起维护这个项目,提交代码的时候注意下面规范
  • Java项目格式化代码的方式采用IntelliJ Idea默认的格式化
  • 代码提交的说明(commit message)按照下面给出的些常用格式
feat[module]: 新增某一项功能
perf[module]: 优化了模块代码或者优化了什么功能
fix[module]: 修改了什么bug
test[module]: 测试了什么东西
del[module]: 删除了某些功能或者无用代码
ref[module]: 重命名或者重构了模块
doc[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.

Ⅷ. Stargazers over time

Stargazers over time