美文网首页
netty和springboot入门案例

netty和springboot入门案例

作者: nextliving | 来源:发表于2019-03-08 16:55 被阅读3次

代码

本工程代码已经托管到github的netty-telnet-boot-demo

zoc7下载

zoc7下载地址https://www.emtec.com/zoc/index.html

创建netty工程

创建工程,工程名为netty-telnet-boot-demo。pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ms</groupId>
    <artifactId>netty-telnet-boot-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>netty-telnet-boot-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!-- 不引入spring boot默认的logback,使用log4j2,需要引入以下2个dependency -->
        <dependency>
                <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>


        <!-- netty nio编程 -->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.15.Final</version>
        </dependency>
        
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

netty服务器相关配置放在application-dev.properties中,内容如下:

tcp.port=8070
boss.thread.count=2
worker.thread.count=2
so.keepalive=true
so.backlog=100

这里tcp.port=8070是netty服务器的端口,等会需要使用zoc7连接这个端口。

启动

eclipse中Run As->Spring Boot App启动netty服务器。然后启动zoc7,点击Quick Connection,在弹出的对话框的Connect to部分填写localhost,在port部分填写8070,Connection type选择Telnet,然后点击connect。zoc7的界面会输出类似下面的信息:

Your channel key is /0:0:0:0:0:0:0:1:63800

同时eclipse的console中会输出类似下面的信息:

2019-03-08 16:47:49.350  INFO 3549 --- [ntLoopGroup-3-2] c.m.s.SomethingServerHandler             : Binded Channel Count is 1

在zoc7中输入hello world,eclipse的console中也会输出hello world

相关文章

网友评论

      本文标题:netty和springboot入门案例

      本文链接:https://www.haomeiwen.com/subject/muarpqtx.html