1 Star 12 Fork 1

PeakLee / spring-boot-netty-starter

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 20.64 KB
一键复制 编辑 原始数据 按行查看 历史
Peaklee 提交于 2023-04-19 10:35 . 2022.1.0-RELEASE

spring-boot-netty-starter

暂无内容 暂无内容

暂无内容 暂无内容 暂无内容 暂无内容

暂无内容

文档主页 Document

什么是spring-boot-netty-starter?

spring-boot-netty-starter是一款spring-boot和netty的整合框架,对于不太了解netty的同学,能够实现快速开发基于netty的socket服务器.其中实现了大量的注解和过程拦截器,能够无缝对接spring-boot进行快速开发,光说不练假把式,接下来就让我们开始吧!

快速开始

第一步:

  • 添加对应的maven依赖
    <dependency>
        <groupId>site.peaklee.framework</groupId>
        <artifactId>spring-boot-netty-starter</artifactId>
        <version>${Last-version}</version>
    </dependency>

第二步:

  • 创建应用程序主类:
import org.springframework.boot.autoconfigure.SpringBootApplication;
import site.peakleeframework.core.SpringSocketApplication;

@SpringBootApplication
public class MySocketServer {
    public static void main(String[] args) {
        SpringSocketApplication.run(MySocketServer.class, args);
    }
}

第三步:

  • 创建一个handler或者消息接收器,来处理具体的业务逻辑
import site.peakleeframework.annotation.Handler;
import site.peakleeframework.handler.adapter.AdapterSessionInboundTypeHandler;
import site.peakleeframework.session.impl.Session;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Handler(1)
public class MessageHandler extends AdapterSessionInboundTypeHandler<String> {
    @Override
    public void onMessage(Session session, String msg) {
        log.info("message received:{}",msg);
        //todo 业务方法实现
    }
    
}
  • AdapterSessionInboundTypeHandler类中实现了Session对象的管理,直接调用即可,且无需向nettyinitChannel进行注册,只需要加入注解@Handler即可自动注册,数字1表示注册的顺序,即handler的处理顺序
  • 或者直接实现接口BeforeSocketReadMessage,该接口的消息回调优先级大于AdapterSessionInboundTypeHandler
import site.peakleeframework.core.spi.BeforeSocketReadMessage;
import site.peakleeframework.session.impl.Session;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MessageListener implements BeforeSocketReadMessage {

    @Override
    public void beforeReadMessage(Session ctx, Object msg) {
        log.info("message received:{}", msg);
        //todo 业务方法实现
    }
}

第四步:

  • 在idea工具中直接启动即可,默认端口是7080,默认编解码器是String类型,启动完成后会返回一个SocketConfigurableContext上下文对象,随后让我们一起来发现它的用途吧
  • 如你所见实现一个简单的Socket程序只需要简单的四步即可完成.
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/lee_coding/spring-boot-netty-starter.git
git@gitee.com:lee_coding/spring-boot-netty-starter.git
lee_coding
spring-boot-netty-starter
spring-boot-netty-starter
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891