成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

如何在springboot中整合netty

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何在springboot中整合netty,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元靈壽做網(wǎng)站,已為上家服務(wù),為靈壽各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792

1. 新建一個(gè)springboot項(xiàng)目,在pom文件中添加netty依賴:

<dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-all</artifactId>
      <version>5.0.0.Alpha1</version>
    </dependency>

2.新建netty服務(wù)
其實(shí)可以復(fù)制上一篇文章的netty的三個(gè)服務(wù)類,做一些稍微的修改就行了;這里為了方便演示,且修都是改好了的,就直接貼出來了;

DiscardServer類:

@Component
public class DiscardServer {
  @Resource
  private ChildChannelHandler childChannelHandler;
  public void run(int port) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    System.out.println("準(zhǔn)備運(yùn)行端口:" + port);
    try {
      ServerBootstrap bootstrap = new ServerBootstrap();
      bootstrap.group(bossGroup, workerGroup)
          .channel(NioServerSocketChannel.class)
          .option(ChannelOption.SO_BACKLOG, 128)
          .childHandler(childChannelHandler);
      //綁定端口,同步等待成功
      ChannelFuture f = bootstrap.bind(port).sync();
      //等待服務(wù)監(jiān)聽端口關(guān)閉
      f.channel().closeFuture().sync();
    } finally {
      //退出,釋放線程資源
      workerGroup.shutdownGracefully();
      bossGroup.shutdownGracefully();
    }
  }
}

ChildChannelHandler類

@Component
public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
  @Resource
  private DiscardServerHandler discardServerHandler;

  public void initChannel(SocketChannel socketChannel) throws Exception {
    socketChannel.pipeline().addLast(discardServerHandler);
  }
}

3.DiscardServerHandler類

特別注意DiscardServerHandler類上需要加@Sharable注解,如果不加的話會報(bào)錯(cuò);

@Component
@Sharable
public class DiscardServerHandler extends ChannelHandlerAdapter {
  @Resource
  private BaseService baseService;
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) {

    try {
      ByteBuf in = (ByteBuf) msg;
      System.out.println("傳輸內(nèi)容是");
      System.out.println(in.toString(CharsetUtil.UTF_8));
      //這里調(diào)用service服務(wù)
      baseService.test();
    } finally {
      ReferenceCountUtil.release(msg);
    }
  }
  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    // 出現(xiàn)異常就關(guān)閉
    cause.printStackTrace();
    ctx.close();
  }
}

3.netty調(diào)用所需的服務(wù)類

1.BaseService接口

public interface BaseService {
  /**
   * 測試接口
   */
  void test();
}

2.接口實(shí)現(xiàn)類BaseServiceImpl:

@Service
public class BaseServiceImpl implements BaseService {
  @Override
  public void test() {
    System.out.println("調(diào)用service服務(wù)");
  }
}

4 springboot啟動類

由于main方法是靜態(tài)方法,netty服務(wù)啟動類不是靜態(tài)類,在main方法里面需要用new的方式啟動;

也可以將netty服務(wù)啟動類改為靜態(tài)類,然后調(diào)用其他非靜態(tài)的類時(shí)就得用new方法來構(gòu)造其他類了;

我也百度到了幾篇文章說實(shí)現(xiàn)CommandLineRunner接口,所以我用了springboot啟動類實(shí)現(xiàn)CommandLineRunner接口的run方法,然后在run方法里啟動netty服務(wù)

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
  @Resource
  private DiscardServer discardServer;

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

  @Override
  public void run(String... args) throws Exception {
    discardServer.run(8080);
  }
}

5.測試

寫一個(gè)能發(fā)送數(shù)據(jù)的socket就可以了;

發(fā)送的數(shù)據(jù)為:

public static void main(String[] args){
    try {
      Socket socket=new Socket("localhost",8080);
      OutputStream outputStream = socket.getOutputStream();
      PrintWriter printWriter=new PrintWriter(outputStream);
      printWriter.write("$tmb00035ET3318/08/22 11:5804029.94,027.25,20.00,20.00$");
      printWriter.flush();
      socket.shutdownOutput();
      socket.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

我的測試結(jié)果:

傳輸內(nèi)容是
$tmb00035ET3318/08/22 11:5804029.94,027.25,20.00,20.00$
aaaaa

到這里,netty與springboot的整合就完成了;

我在整合過程中遇到的問題

我使用springboot結(jié)合netty的流程

springboot啟動類中啟動netty啟動類(DiscardServer),netty啟動類(DiscardServer)再調(diào)用初始化channel類(ChildChannelHandler),然后初始化channel類再調(diào)用(DiscardServerHandler)類;然后DiscardServerHandler類再調(diào)用service服務(wù);如下示例圖:

如何在springboot中整合netty

問題:

  • springboot啟動類我并沒有實(shí)現(xiàn)CommandLineRunner接口,直接在main方法通過new的方式啟動netty服務(wù)

  • 我實(shí)現(xiàn)了CommandLineRunner接口,但是我在run方法中用的new的方式啟動的netty服務(wù)或者我在run方法使用注入的方式啟動netty,但是在其他某個(gè)地方調(diào)用另一個(gè)類使用了new的方式;

  • DiscardServerHandler類上為標(biāo)記@Sharable類,會報(bào)錯(cuò)誤;

以上總結(jié)起來的問題就是我在springboot整合netty的過程中有其中一處的調(diào)用其他類時(shí)使用的方式是new構(gòu)造的,這樣雖然springboot能啟動,netty也能啟動,但是netty服務(wù)中使用new構(gòu)造的那個(gè)類中無法依賴注入,會報(bào)空指針異常;

舉個(gè)栗子:在圖中的過程中,我在ChildChannelHandler類中通過new的方式調(diào)用DiscardServerHandler類,其他的過程都是使用注入的方式調(diào)用,就會出現(xiàn)上邊的問題;

在遇到空指針的時(shí)候,我把spring托管的bean打印了出來,所有的類都在spring的托管中,但是就是無法注入,我也一直沒有明白怎么回事,最后用了一個(gè)極端的方法,就是在調(diào)用服務(wù)時(shí),獲取spring的上下文,然后再根據(jù)名字來獲取bean,你谷歌或百度:非托管類調(diào)用spring托管類,就能找到很多文章了;雖然用這個(gè)方式能解決上述的問題,但總是不好的;

最后的解決辦法:所以類之間的調(diào)用都使用spring的依賴注入,別用new的方式來調(diào)用或者靜態(tài)方法的方式調(diào)用

上述就是小編為大家分享的如何在springboot中整合netty了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞標(biāo)題:如何在springboot中整合netty
分享網(wǎng)址:http://jinyejixie.com/article24/iisdje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、域名注冊搜索引擎優(yōu)化、建站公司、電子商務(wù)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)
哈密市| 洛川县| 荔波县| 河源市| 余江县| 阳山县| 宜阳县| 安徽省| 大宁县| 佳木斯市| 建瓯市| 磴口县| 普宁市| 平山县| 五指山市| 安平县| 旌德县| 瑞金市| 繁昌县| 漯河市| 双桥区| 怀化市| 永城市| 嵊州市| 临江市| 建湖县| 鄂托克旗| 惠来县| 泰来县| 泗阳县| 安平县| 台南县| 中牟县| 鄂托克前旗| 商城县| 深水埗区| 镇巴县| 扎鲁特旗| 丹寨县| 柘荣县| 周至县|