1 Star 0 Fork 128

王旭阳/springboot-cli

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

基于netty的 protobuf服务器

简介

附件doc中已包含相应的文件及文档,也可以自行到官网下载最新版 参考官网中文文档

环境

# 环境 版本 说明
1 JDK openJdk 11.0.8 建议JDK11及以上

项目结构

 ├──channel          管道
 ├──config           服务核心配置
 ├──handler          消息处理器
 ├──procotol         协议文件
 ├──server           服务配置
 ├──store            频道存储
 ├──utils            工具包
 ├──NettyClientApplication.java   主启动类

使用教程

1. 下载解压文件

官网下载地址

2. 编写proto文件

message.proto

// 声明使用proto3
syntax = "proto3";
// 包名
option java_package = "com.netty.client.procotol";
// 类名
option java_outer_classname = "MessageBuf";
 
// 消息整合,便于netty导入解码器
message Message {
  // 包类型
  PackType packType = 1;
  // 根据包类型多选1
  oneof Pack{
      LoginRequest loginRequest = 2;
	  LoginResponse loginResponse = 3;
	  MessageRequest messageRequest = 4;
	  MessageResponse messageResponse = 5;
  }
  // 包类型枚举
  enum PackType {
     LOGIN_REQ = 0;
	 LOGIN_RESP = 1;
	 MESSAGE_REQ = 2;
	 MESSAGE_RESP = 3;
  }
}
 
// 登录请求,包含用户名
message LoginRequest {
  string username = 1;
  string password = 2;
}

// 登录响应
message LoginResponse {
  int32 code = 1;
  string message = 2;
}
 
// 消息请求
message MessageRequest {
  int32 messageId = 1;
  int32 type = 2;
  string data = 3;
}

// 消息响应
message MessageResponse {
  int32 messageId = 1;
  int32 code = 2;
  string message = 3;
}

3 命令行执行

# --java_out 输出路径、message.proto 要执行的文件
protoc.exe --java_out=E:\lqd\protoc-3.20.1-rc-1-win64\bin message.proto

4 netty引入协议文件

ChannelInit.java 中添加解码器

.addLast(new ProtobufDecoder(MessageBuf.Message.getDefaultInstance()))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiaozhi0802/springboot-cli.git
git@gitee.com:xiaozhi0802/springboot-cli.git
xiaozhi0802
springboot-cli
springboot-cli
master

搜索帮助