Springboot中启动netty的几种方法

目录

1.CommandLineRunner

2.在Application中使用

3.在netty的启动类中使用

4.ApplicationListener

5.PostConstruct

1.CommandLineRunner

        CommandLineRunner这个接口是springboot提供用来实现数据预加载,当springboot项目启动的时候,便可以通过该接口跟随springboot执行代码逻辑 ,且在整个生命周期中 只会执行一次。

2.在Application中使用

@SpringBootApplication

@ComponentScan(basePackages = {"com.ait.cloud", "org.n3r.idworker"})

public class CloudApplication implements CommandLineRunner {

    //将springutils交由spring进行管理

    @Bean

    public SpringUtil getSpringUtil(){

        return new SpringUtil();

    }

    public static void main(String[] args) {

        SpringApplication.run(CloudApplication.class, args);

    }

    @Autowired

    private NettyServer nettyServer;

    @Override

    public void run(String... args) throws Exception {

        this.nettyServer.start();

    }

}

如上代码是springboot的启动器实现了CommandLineRunner接口 ,并将该接口中的run方法重写

@Component

@Slf4j

public class NettyServer {

    private static NettyServer nettyServer;

    @PostConstruct

    public void init(){

        nettyServer = this;

    }

    @Value("${netty.port}")

    private  int port;

    private static class SingletionNettyServer{

        static final NettyServer instance = new NettyServer();

    }

    public static NettyServer getInstance(){

        return SingletionNettyServer.instance;

    }

    private EventLoopGroup mainGroup;

    private EventLoopGroup subGroup;

    private ServerBootstrap server;

    private ChannelFuture future;

    public NettyServer(){

        mainGroup = new NioEventLoopGroup();

        subGroup = new NioEventLoopGroup();

        server = new ServerBootstrap();

        server.group(mainGroup,subGroup)

                .channel(NioServerSocketChannel.class)

                .childHandler(new NettyServerInitializer());

    }

    public void start(){

        log.info("通道配置");

        this.future = server.bind(nettyServer.port);

        log.info("netty server 启动完毕,启动端口为:"+nettyServer.port);

    }

}

        将netty的服务端启动类交由spring进行管理 ,可以在application中直接注入 ,调用是西安写好的启动方法便可以将netty服务进行启动。

        启动项目可见 控制台已提示启动成功

2019-09-19 10:35:39.290  INFO 6752 --- [  restartedMain] c.a.c.modules.netty.sever.NettyServer    : 通道配置

2019-09-19 10:35:41.008  INFO 6752 --- [  restartedMain] c.a.c.modules.netty.sever.NettyServer    : netty server 启动完毕,启动端口为:10086

2019-09-19 10:35:41.710  INFO 6752 --- [  restartedMain] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource

2019-09-19 10:35:42.213  INFO 6752 --- [  restartedMain] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited

2019-09-19 10:35:43.205  INFO 6752 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'

2019-09-19 10:35:43.774  INFO 6752 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729

2019-09-19 10:35:43.847  INFO 6752 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8086 (http) with context path '/api'

2019-09-19 10:35:43.852  INFO 6752 --- [  restartedMain] com.ait.cloud.CloudApplication           : Started CloudApplication in 17.151 seconds (JVM running for 20.083)

3.在netty的启动类中使用

@Component

@Slf4j

public class NettyServer implements CommandLineRunner {

    private static NettyServer nettyServer;

    @PostConstruct

    public void init(){

        nettyServer = this;

    }

    @Value("${netty.port}")

    private  int port;

    private static class SingletionNettyServer{

        static final NettyServer instance = new NettyServer();

    }

    public static NettyServer getInstance(){

        return SingletionNettyServer.instance;

    }

    private EventLoopGroup mainGroup;

    private EventLoopGroup subGroup;

    private ServerBootstrap server;

    private ChannelFuture future;

    public NettyServer(){

        mainGroup = new NioEventLoopGroup();

        subGroup = new NioEventLoopGroup();

        server = new ServerBootstrap();

        server.group(mainGroup,subGroup)

                .channel(NioServerSocketChannel.class)

                .childHandler(new NettyServerInitializer());

    }

    public void start(){

        log.info("通道配置");

        this.future = server.bind(nettyServer.port);

        log.info("netty server 启动完毕,启动端口为:"+nettyServer.port);

    }

    @Override

    public void run(String... args) throws Exception {

        this.start();

    }

}

如上 直接将netty的启动类是实现CommandLineRunner并且复写run方法

启动项目

2019-09-19 13:30:30.532  INFO 6112 --- [  restartedMain] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited

2019-09-19 13:30:31.944  INFO 6112 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'

2019-09-19 13:30:32.603  INFO 6112 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729

2019-09-19 13:30:32.680  INFO 6112 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8086 (http) with context path '/api'

2019-09-19 13:30:32.685  INFO 6112 --- [  restartedMain] com.ait.cloud.CloudApplication           : Started CloudApplication in 8.264 seconds (JVM running for 10.749)

2019-09-19 13:30:32.687  INFO 6112 --- [  restartedMain] c.a.c.modules.netty.sever.NettyServer    : 通道配置

2019-09-19 13:30:33.985  INFO 6112 --- [  restartedMain] c.a.c.modules.netty.sever.NettyServer    : netty server 启动完毕,启动端口为:10086

        除此之外 CommandLineRunner的实现类上还可以通过**@Order**这个注解来定义方法的启动顺序

例如 我们新建一个类来实现接口 并将@order注解加上 设置为1

@Slf4j

@Component

@Order(1)

public class test implements CommandLineRunner {

    @Override

    public void run(String... args) throws Exception {

        log.info("我是第一个启动的方法");

    }

}

之后我们将netty的启动类上加上@order注解并设置为2

@Component

@Slf4j

@Order(2)

public class NettyServer implements CommandLineRunner {

 @Override

    public void run(String... args) throws Exception {

        log.info("我是第二个启动的方法");

        this.start();

    }

之后我们启动项目 通过控制台可以看见 @Order注解中所配置的数字越小 其启动时候的优先级越高。

2019-09-19 13:42:50.933  INFO 2340 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8086 (http) with context path '/api'

2019-09-19 13:42:50.937  INFO 2340 --- [  restartedMain] com.ait.cloud.CloudApplication           : Started CloudApplication in 7.87 seconds (JVM running for 10.365)

2019-09-19 13:42:50.940  INFO 2340 --- [  restartedMain] com.ait.cloud.modules.netty.sever.test   : 我是第一个启动的方法

2019-09-19 13:42:50.940  INFO 2340 --- [  restartedMain] c.a.c.modules.netty.sever.NettyServer    : 我是第二个启动的方法

2019-09-19 13:42:50.940  INFO 2340 --- [  restartedMain] c.a.c.modules.netty.sever.NettyServer    : 通道配置

2019-09-19 13:42:52.245  INFO 2340 --- [  restartedMain] c.a.c.modules.netty.sever.NettyServer    : netty server 启动完毕,启动端口为:10086

4.ApplicationListener

@Component

public class NettyBooter implements ApplicationListener<ContextRefreshedEvent> {

    @Override

    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        if (contextRefreshedEvent.getApplicationContext().getParent() == null){

            try {

                //获取server实例 

                NettyServer.getInstance().start();

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    }

}

ContextRefreshedEvent

   ApplicationContext 被初始化或刷新时,该事件被发布。这也可以在 ConfigurableApplicationContext接口中使用 **refresh()**方法来发生。此处的初始化是指:所有的Bean被成功装载,后处理Bean被检测并激活,所有Singleton Bean 被预实例化,ApplicationContext容器已就绪可用。

2019-09-19 19:21:28.988  INFO 15368 --- [  restartedMain] c.a.c.modules.netty.sever.NettyServer    : 通道配置

2019-09-19 19:21:30.920  INFO 15368 --- [  restartedMain] c.a.c.modules.netty.sever.NettyServer    : netty server 启动完毕,启动端口为:10086

2019-09-19 19:21:30.959  INFO 15368 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8086 (http) with context path '/api'

2019-09-19 19:21:30.964  INFO 15368 --- [  restartedMain] com.ait.cloud.CloudApplication           : Started CloudApplication in 15.682 seconds (JVM running for 18.797)

5.PostConstruct

@PostConstruct

public void start(){

    log.info("通道配置");

    this.future = server.bind(nettyServer.port);

    log.info("netty server 启动完毕,启动端口为:"+nettyServer.port);

}

        将server中的启动方法添加@PostConstruct注解 便可以在项目启动的时候进行方法的调用。

2019-09-19 19:27:30.580  INFO 15868 --- [  restartedMain] c.a.c.modules.netty.sever.NettyServer    : 通道配置

2019-09-19 19:27:31.853  INFO 15868 --- [  restartedMain] c.a.c.modules.netty.sever.NettyServer    : netty server 启动完毕,启动端口为:10086

2019-09-19 19:27:32.294  INFO 15868 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'

2019-09-19 19:27:33.026  INFO 15868 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729

2019-09-19 19:27:33.101  INFO 15868 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8086 (http) with context path '/api'

2019-09-19 19:27:33.105  INFO 15868 --- [  restartedMain] com.ait.cloud.CloudApplication           : Started CloudApplication in 9.988 seconds (JVM running for 14.559)

  • 23
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的 Spring Boot 使用 Netty 的示例: 1. 首先,添加以下依赖到 pom.xml 文件: ```xml <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.48.Final</version> </dependency> ``` 2. 创建一个 Netty 服务器类,并实现 ChannelInboundHandlerAdapter 接口: ```java @Component @ChannelHandler.Sharable public class NettyServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 处理客户端请求 ByteBuf byteBuf = (ByteBuf) msg; byte[] bytes = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(bytes); String request = new String(bytes, "UTF-8"); System.out.println("收到客户端请求:" + request); // 响应客户端请求 String response = "Hello, " + request; ByteBuf responseBuf = Unpooled.copiedBuffer(response.getBytes()); ctx.write(responseBuf); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } } ``` 3. 创建 Netty 服务器启动类,并使用 @PostConstruct 注解启动 Netty 服务器: ```java @Component public class NettyServer { private final NettyServerHandler nettyServerHandler; @Autowired public NettyServer(NettyServerHandler nettyServerHandler) { this.nettyServerHandler = nettyServerHandler; } @PostConstruct public void start() throws InterruptedException { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(nettyServerHandler); } }); ChannelFuture future = serverBootstrap.bind(8080).sync(); if (future.isSuccess()) { System.out.println("Netty 服务器启动成功"); } } } ``` 以上示例,我们创建了一个 Netty 服务器,监听 8080 端口。当客户端连接到服务器时,服务器会收到客户端请求,并响应客户端请求。 注意:如果你的 Spring Boot 应用部署在 Tomcat 或者 Jetty 容器,则需要在启动方法上添加 @Bean 注解,以确保正确启动 Netty 服务器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

棉花糖老丫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值