Netty, SpringBoot, MyBatis搭建游戏服务器 - 1

Netty, SpringBoot, MyBatis搭建游戏服务器 - 1


各框架简介

Netty是步的、事件驱动的网络应用程序框架和工具,,很适合做游戏服务器。

使用SpringBoot可以抛弃传统Spirng框架的繁琐配置和依赖版本管理,更方便地打包与部署。这里我们主要是使用SpringBoot的依赖注入功能。

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。因为我们的数据库可能因为业务需求不断地变化,所以我们可以用MyBaits的逆向工程实时根据数据库的表生成实体类、Mapper接口,Mapper XML。或者利用注解注解写SQL语句查询。


目的

我们要使用Netty构建一个游戏服务端,她可以接受客户端的信息,并根据业务逻辑做出响应。
Netty, SpringBoot, MyBatis搭建游戏服务器 - 1_第1张图片

Spring负责依赖注入,管理对象(游戏业务对象另外由缓存模块管理),使得项目代码直之间解耦合。

MyBatis负责依据业务需求持久化各类数据。


依赖配置

项目使用Maven来管理依赖,依赖结构初步想法是

  • mmorpg
    • gameserver
      • mysql
      • 缓存模块
      • 游戏配置管理模块
    • gameclient

下面是当前版本项目的pom文件

总项目定义文件


<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.0modelVersion>

    <groupId>com.wan37groupId>
    <artifactId>mmorpgartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>pompackaging>

    <name>mmorpgname>
    <description>Demo project for Spring Bootdescription>

    <modules>
        <module>gameservermodule>
        <module>mysqlmodule>
    modules>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.5.RELEASEversion>
        <relativePath/> 
    parent>

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

    <dependencies>
        
            
            
        

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
            <optional>trueoptional>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

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

    <repositories>
        <repository>
            <id>spring-snapshotsid>
            <name>Spring Snapshotsname>
            <url>https://repo.spring.io/snapshoturl>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
        repository>
        <repository>
            <id>spring-milestonesid>
            <name>Spring Milestonesname>
            <url>https://repo.spring.io/milestoneurl>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
    repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshotsid>
            <name>Spring Snapshotsname>
            <url>https://repo.spring.io/snapshoturl>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
        pluginRepository>
        <pluginRepository>
            <id>spring-milestonesid>
            <name>Spring Milestonesname>
            <url>https://repo.spring.io/milestoneurl>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        pluginRepository>
    pluginRepositories>
project>

持久化模块定义文件


<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">
    <parent>
        <artifactId>mmorpgartifactId>
        <groupId>com.wan37groupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>mysqlartifactId>
    <packaging>jarpackaging>


    <dependencies>

        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.2version>
        dependency>


        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.26version>
        dependency>


        
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelperartifactId>
            <version>4.1.6version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.1.43version>
        dependency>

        
        
            
            
            
        

    dependencies>

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

            
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.2version>
                <configuration>
                    <configurationFile>src\main\resources\mybatis\generatorConfig.xmlconfigurationFile>
                    
                    <verbose>trueverbose>
                    
                    <overwrite>trueoverwrite>
                configuration>
                <executions>
                    <execution>
                        <id>GenerateMyBatis Artifactsid>
                        <goals>
                            <goal>generategoal>
                        goals>
                    execution>
                executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generatorgroupId>
                        <artifactId>mybatis-generator-coreartifactId>
                        <version>1.3.2version>
                    dependency>
                dependencies>
            plugin>

        plugins>
    build>

project>

服务端模块


<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.0modelVersion>


    <parent>
        <artifactId>mmorpgartifactId>
        <groupId>com.wan37groupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>



    <artifactId>gameserverartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>gameservername>
    <description>A game server project for Spring Bootdescription>


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

    <dependencies>
        
        <dependency>
            <groupId>io.nettygroupId>
            <artifactId>netty-allartifactId>
            <version>4.1.29.Finalversion>
        dependency>

        
        <dependency>
            <groupId>com.wan37groupId>
            <artifactId>mysqlartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>

    dependencies>

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

project>


Spring和myBatis的配置文件

gameserver模块中的application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/game?useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

mybatis:
  mapper-locations: classpath*:mapper/*.xml
  check-config-location: true
  type-aliases-package: com.wan37.mysql.pojo.entity
  
debug: true

MyBitis配置类(分页助手不知是否能用到)

package com.wan37.gameServer.config;

import com.github.pagehelper.PageHelper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;


/**
 * @author 钱伟健 gonefutre
 * @date 2017/9/12 22:27.
 * @E-mail [email protected]
 */

/*
 * 注册MyBatis分页插件PageHelper
 */

@Configuration
@MapperScan(basePackages = {"com.wan37"})
public class MybatisConfig {

    @Bean
    public PageHelper pageHelper() {
        System.out.println("MyBatisConfiguration.pageHelper()");
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }
}

MyBatis逆向工程文件



<generatorConfiguration >
    
    
    
    <classPathEntry location="C:\project\mmorpg\mysql\mysql-connector-java-5.1.47.jar"/>
    
    <context id="msql" targetRuntime="MyBatis3" >

        
        
        
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
        
        
        
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />


        
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="supperssDate" value="false"/>
        commentGenerator>
        
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/game"
                        userId="root"
                        password="123456" />
        
        <javaTypeResolver>
            
            <property name="forceBigDecimals" value="false"/>
        javaTypeResolver>
        
        <javaModelGenerator targetPackage="com.wan37.mysql.pojo.entity" targetProject="src/main/java">
            
            <property name="enableSubPackages" value="true"/>
            
            <property name="trimStrings" value="true"/>
        javaModelGenerator>

        
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            
            <property name="enableSubPackages" value="true"/>
        sqlMapGenerator>
        
        <javaClientGenerator targetPackage="com.wan37.mysql.pojo.mapper" targetProject="src/main/java"
                             type="XMLMAPPER" >
            
            <property name="enableSubPackages" value="true"/>
        javaClientGenerator>
        
        
        
        
        <table tableName="game_role"/>
        <table tableName="player"/>
        <table tableName="player_roles"/>

    context>
generatorConfiguration>

整合Netty

我使用的办法是把Netty服务的Handler,启动类都都变成由Spring管理的类。通过在类定义上加@Component注解,SpringBoot启动时会自动扫描注解并将Bean注入到容器中。使用 @PostConstruct修饰启动方法来启动Netty服务。

import com.wan37.gameServer.server.handler.ServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;


/**
 * @author gonefuture  [email protected]
 * time 2018/9/10 11:36
 * @version 1.00
 * Description: 游戏服务端
 */
@Slf4j
@Component
@ChannelHandler.Sharable
public class GameServer {

    @Resource
    private ServerHandler serverHandler;

    //绑定端口
    private void bind(int port) throws Exception {
        // 逻辑线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        // 工作线程
        EventLoopGroup workGroup = new NioEventLoopGroup();

        ServerBootstrap bootstrap = new ServerBootstrap(); // 启动器
        bootstrap.group(bossGroup, workGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024) // 最大客户端连接数为1024
                //是否启用心跳保活机制
                .childOption(ChannelOption.SO_KEEPALIVE, true)
        .childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                // 这里添加业务处理handler
                ch.pipeline().addLast( serverHandler);
            }
        });

        try {
            ChannelFuture future = bootstrap.bind(port).sync();
            if (future.isSuccess()) {
                System.out.println("Server starts success at port:"+port);
            }
            future.channel().closeFuture().sync();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        int port = 8000;
        new GameServer().bind(port);
    }


    @PostConstruct
    public void start() {
        int port = 8000;
        try {
          bind(port);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

期间遇到的各种问题

整合的时候遇到过很多问题

  1. 整合Netty时,由于使用new关键字创建Handler实例,导致在Handle中装配的服务实例为空,不能使用。
  2. MyBatis配置类写在了mysql模块而没有写在gameserver模块,导致mapper文件扫描不到。
  3. 逆向工程时产生了同名的mapper xml文件未及时删除,导致出现mapper result map 重复的错误。

结语

当前只整合了持久化模块,接下来还要编写请求分发的部分和整合缓存模块。

你可能感兴趣的:(游戏开发)