从零开始手写mmo游戏从框架到爆炸(五)— 集成springboot

本文主要是介绍从零开始手写mmo游戏从框架到爆炸(五)— 集成springboot,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        算了,放弃挣扎了,笔者写了一些代码,发现还是绕不过springboot,或者说自己来进行依赖管理最后肯定会爆炸的。同时方便后续接入第三方框架,咱们还是老老实实的接入springboot框架吧。

父pom引入parent依赖:

    <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.3.RELEASE</version><relativePath/></parent>

eternity-server增加依赖:

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.loveprogrammer</groupId><artifactId>eternity-core</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>

修改eternity-server启动类:

package com.loveprogrammer;import com.loveprogrammer.base.factory.ServerBootstrapFactory;
import com.loveprogrammer.base.factory.ServerChannelFactory;
import com.loveprogrammer.exception.ServerException;
import com.loveprogrammer.netty.simple.SocketServer;
import com.loveprogrammer.netty.start.BasicServer;
import com.loveprogrammer.netty.start.IServer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!**/
@SpringBootApplication
public class EternityServerMain
{// 为了保证使用时,不需要每次都去创建logger 对象,我们声明静态常量public static final Logger LOGGER = LoggerFactory.getLogger(EternityServerMain.class);public static void main( String[] args ){LOGGER.info( "Hello World!" );// 最基本的启动方法
//        try {
//            LOGGER.info("开始启动Socket服务器...");
//            new SocketServer().run();
//        } catch (Exception e) {
//            LOGGER.error( "服务器启动失败",e);
//        }// 工厂模式启动方法
//        try {
//            Channel channel = ServerChannelFactory.createAcceptorChannel();
//            channel.closeFuture().sync();
//        } catch (Exception e) {
//            LOGGER.error( "服务器启动失败",e);
//        }SpringApplication.run(EternityServerMain.class, args);System.out.println("(♥◠‿◠)ノ゙  springboot启动成功   ლ(´ڡ`ლ)゙  \n");// 启动类启动try {IServer server = new BasicServer();server.start();} catch (Exception e) {LOGGER.error( "服务器启动失败",e);}}
}

 增加配置文件 application.yml:

server:port: 8083
logging:level:com.loveprogrammer: debugorg.springframework: warnio.netty: debug

启动日志如下:

15:11:17.181 [main] INFO com.loveprogrammer.EternityServerMain - Hello World!.   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::        (v2.3.3.RELEASE)2024-02-05 15:11:17.476  INFO 12648 --- [           main] com.loveprogrammer.EternityServerMain    : Starting EternityServerMain on DESKTOP-0VA8RF8 with PID 12648 (F:\gitee\game\eternity-online\eternity-server\target\classes started by admin in F:\gitee\game\eternity-online)
2024-02-05 15:11:17.477 DEBUG 12648 --- [           main] com.loveprogrammer.EternityServerMain    : Running with Spring Boot v2.3.3.RELEASE, Spring v5.2.8.RELEASE
2024-02-05 15:11:17.477  INFO 12648 --- [           main] com.loveprogrammer.EternityServerMain    : No active profile set, falling back to default profiles: default
2024-02-05 15:11:17.947  INFO 12648 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-02-05 15:11:17.947  INFO 12648 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2024-02-05 15:11:18.102  INFO 12648 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-02-05 15:11:18.292  INFO 12648 --- [           main] com.loveprogrammer.EternityServerMain    : Started EternityServerMain in 1.047 seconds (JVM running for 1.408)
(♥◠‿◠)ノ゙  springboot启动成功   ლ(´ڡ`ლ)゙  2024-02-05 15:11:18.295  INFO 12648 --- [           main] c.loveprogrammer.base.bean.ServerConfig  : **************Server INFO******************
2024-02-05 15:11:18.295  INFO 12648 --- [           main] c.loveprogrammer.base.bean.ServerConfig  : protocolType  : TCP
2024-02-05 15:11:18.295  INFO 12648 --- [           main] c.loveprogrammer.base.bean.ServerConfig  : port          : 8088
2024-02-05 15:11:18.295  INFO 12648 --- [           main] c.loveprogrammer.base.bean.ServerConfig  : channelType   : NIO
2024-02-05 15:11:18.295  INFO 12648 --- [           main] c.loveprogrammer.base.bean.ServerConfig  : **************Server INFO******************
2024-02-05 15:11:18.298 DEBUG 12648 --- [           main] i.n.u.i.logging.InternalLoggerFactory    : Using SLF4J as the default logging framework
2024-02-05 15:11:18.301 DEBUG 12648 --- [           main] i.n.channel.MultithreadEventLoopGroup    : -Dio.netty.eventLoopThreads: 16
2024-02-05 15:11:18.306 DEBUG 12648 --- [           main] i.n.util.internal.PlatformDependent0     : java.nio.Buffer.address: available
2024-02-05 15:11:18.306 DEBUG 12648 --- [           main] i.n.util.internal.PlatformDependent0     : sun.misc.Unsafe.theUnsafe: available
2024-02-05 15:11:18.306 DEBUG 12648 --- [           main] i.n.util.internal.PlatformDependent0     : sun.misc.Unsafe.copyMemory: available
2024-02-05 15:11:18.307 DEBUG 12648 --- [           main] i.n.util.internal.PlatformDependent0     : direct buffer constructor: available
2024-02-05 15:11:18.307 DEBUG 12648 --- [           main] i.n.util.internal.PlatformDependent0     : java.nio.Bits.unaligned: available, true
2024-02-05 15:11:18.307 DEBUG 12648 --- [           main] i.n.util.internal.PlatformDependent0     : java.nio.DirectByteBuffer.<init>(long, int): available
2024-02-05 15:11:18.307 DEBUG 12648 --- [           main] io.netty.util.internal.Cleaner0          : java.nio.ByteBuffer.cleaner(): available
2024-02-05 15:11:18.308 DEBUG 12648 --- [           main] i.netty.util.internal.PlatformDependent  : Platform: Windows
2024-02-05 15:11:18.308 DEBUG 12648 --- [           main] i.netty.util.internal.PlatformDependent  : Java version: 8
2024-02-05 15:11:18.308 DEBUG 12648 --- [           main] i.netty.util.internal.PlatformDependent  : -Dio.netty.noUnsafe: false
2024-02-05 15:11:18.308 DEBUG 12648 --- [           main] i.netty.util.internal.PlatformDependent  : sun.misc.Unsafe: available
2024-02-05 15:11:18.308 DEBUG 12648 --- [           main] i.netty.util.internal.PlatformDependent  : -Dio.netty.noJavassist: false
2024-02-05 15:11:18.308 DEBUG 12648 --- [           main] i.netty.util.internal.PlatformDependent  : Javassist: unavailable
2024-02-05 15:11:18.309 DEBUG 12648 --- [           main] i.netty.util.internal.PlatformDependent  : You don't have Javassist in your class path or you don't have enough permission to load dynamically generated classes.  Please check the configuration for better performance.
2024-02-05 15:11:18.309 DEBUG 12648 --- [           main] i.netty.util.internal.PlatformDependent  : -Dio.netty.tmpdir: C:\Users\admin\AppData\Local\Temp (java.io.tmpdir)
2024-02-05 15:11:18.309 DEBUG 12648 --- [           main] i.netty.util.internal.PlatformDependent  : -Dio.netty.bitMode: 64 (sun.arch.data.model)
2024-02-05 15:11:18.309 DEBUG 12648 --- [           main] i.netty.util.internal.PlatformDependent  : -Dio.netty.noPreferDirect: false
2024-02-05 15:11:18.309 DEBUG 12648 --- [           main] i.netty.util.internal.PlatformDependent  : io.netty.maxDirectMemory: 3806855168 bytes
2024-02-05 15:11:18.315 DEBUG 12648 --- [           main] io.netty.channel.nio.NioEventLoop        : -Dio.netty.noKeySetOptimization: false
2024-02-05 15:11:18.315 DEBUG 12648 --- [           main] io.netty.channel.nio.NioEventLoop        : -Dio.netty.selectorAutoRebuildThreshold: 512
2024-02-05 15:11:18.317 DEBUG 12648 --- [           main] i.netty.util.internal.PlatformDependent  : org.jctools-core.MpscChunkedArrayQueue: available
2024-02-05 15:11:18.334  INFO 12648 --- [           main] c.l.base.factory.ServerChannelFactory    : 创建Server...
2024-02-05 15:11:18.337 DEBUG 12648 --- [           main] io.netty.channel.DefaultChannelId        : -Dio.netty.processId: 12648 (auto-detected)
2024-02-05 15:11:18.338 DEBUG 12648 --- [           main] io.netty.util.NetUtil                    : -Djava.net.preferIPv4Stack: false
2024-02-05 15:11:18.338 DEBUG 12648 --- [           main] io.netty.util.NetUtil                    : -Djava.net.preferIPv6Addresses: false
2024-02-05 15:11:18.373 DEBUG 12648 --- [           main] io.netty.util.NetUtil                    : Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1)
2024-02-05 15:11:18.374 DEBUG 12648 --- [           main] io.netty.util.NetUtil                    : \proc\sys\net\core\somaxconn: 200 (non-existent)
2024-02-05 15:11:18.411 DEBUG 12648 --- [           main] io.netty.channel.DefaultChannelId        : -Dio.netty.machineId: b4:2e:99:ff:fe:84:c1:cf (auto-detected)
2024-02-05 15:11:18.412 DEBUG 12648 --- [           main] i.netty.util.internal.ThreadLocalRandom  : -Dio.netty.initialSeedUniquifier: 0xc160f6aba52ef4a2
2024-02-05 15:11:18.417 DEBUG 12648 --- [           main] io.netty.util.ResourceLeakDetector       : -Dio.netty.leakDetection.level: simple
2024-02-05 15:11:18.417 DEBUG 12648 --- [           main] io.netty.util.ResourceLeakDetector       : -Dio.netty.leakDetection.maxRecords: 4
2024-02-05 15:11:18.421 DEBUG 12648 --- [           main] io.netty.buffer.PooledByteBufAllocator   : -Dio.netty.allocator.numHeapArenas: 16
2024-02-05 15:11:18.421 DEBUG 12648 --- [           main] io.netty.buffer.PooledByteBufAllocator   : -Dio.netty.allocator.numDirectArenas: 16
2024-02-05 15:11:18.421 DEBUG 12648 --- [           main] io.netty.buffer.PooledByteBufAllocator   : -Dio.netty.allocator.pageSize: 8192
2024-02-05 15:11:18.421 DEBUG 12648 --- [           main] io.netty.buffer.PooledByteBufAllocator   : -Dio.netty.allocator.maxOrder: 11
2024-02-05 15:11:18.421 DEBUG 12648 --- [           main] io.netty.buffer.PooledByteBufAllocator   : -Dio.netty.allocator.chunkSize: 16777216
2024-02-05 15:11:18.421 DEBUG 12648 --- [           main] io.netty.buffer.PooledByteBufAllocator   : -Dio.netty.allocator.tinyCacheSize: 512
2024-02-05 15:11:18.421 DEBUG 12648 --- [           main] io.netty.buffer.PooledByteBufAllocator   : -Dio.netty.allocator.smallCacheSize: 256
2024-02-05 15:11:18.422 DEBUG 12648 --- [           main] io.netty.buffer.PooledByteBufAllocator   : -Dio.netty.allocator.normalCacheSize: 64
2024-02-05 15:11:18.422 DEBUG 12648 --- [           main] io.netty.buffer.PooledByteBufAllocator   : -Dio.netty.allocator.maxCachedBufferCapacity: 32768
2024-02-05 15:11:18.422 DEBUG 12648 --- [           main] io.netty.buffer.PooledByteBufAllocator   : -Dio.netty.allocator.cacheTrimInterval: 8192
2024-02-05 15:11:18.426 DEBUG 12648 --- [           main] io.netty.buffer.ByteBufUtil              : -Dio.netty.allocator.type: pooled
2024-02-05 15:11:18.426 DEBUG 12648 --- [           main] io.netty.buffer.ByteBufUtil              : -Dio.netty.threadLocalDirectBufferSize: 65536
2024-02-05 15:11:18.426 DEBUG 12648 --- [           main] io.netty.buffer.ByteBufUtil              : -Dio.netty.maxThreadLocalCharBufferSize: 16384Process finished with exit code -1

有了spring,我们开始下一步,增加消息分发功能

下一章:从零开始手写mmo游戏从框架到爆炸(六)— 消息处理工厂-CSDN博客

 全部源码详见:

gitee : eternity-online: 多人在线mmo游戏 - Gitee.com

分支:step-05

这篇关于从零开始手写mmo游戏从框架到爆炸(五)— 集成springboot的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/684746

相关文章

Java如何用乘号来重复字符串的功能

《Java如何用乘号来重复字符串的功能》:本文主要介绍Java使用乘号来重复字符串的功能,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java乘号来重复字符串的功能1、利用循环2、使用StringBuilder3、采用 Java 11 引入的String.rep

SpringBoot中HTTP连接池的配置与优化

《SpringBoot中HTTP连接池的配置与优化》这篇文章主要为大家详细介绍了SpringBoot中HTTP连接池的配置与优化的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、HTTP连接池的核心价值二、Spring Boot集成方案方案1:Apache HttpCl

Spring Boot项目打包和运行的操作方法

《SpringBoot项目打包和运行的操作方法》SpringBoot应用内嵌了Web服务器,所以基于SpringBoot开发的web应用也可以独立运行,无须部署到其他Web服务器中,下面以打包dem... 目录一、打包为JAR包并运行1.打包为可执行的 JAR 包2.运行 JAR 包二、打包为WAR包并运行

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

Spring Boot 常用注解整理(最全收藏版)

《SpringBoot常用注解整理(最全收藏版)》本文系统整理了常用的Spring/SpringBoot注解,按照功能分类进行介绍,每个注解都会涵盖其含义、提供来源、应用场景以及代码示例,帮助开发... 目录Spring & Spring Boot 常用注解整理一、Spring Boot 核心注解二、Spr

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

详解如何在SpringBoot控制器中处理用户数据

《详解如何在SpringBoot控制器中处理用户数据》在SpringBoot应用开发中,控制器(Controller)扮演着至关重要的角色,它负责接收用户请求、处理数据并返回响应,本文将深入浅出地讲解... 目录一、获取请求参数1.1 获取查询参数1.2 获取路径参数二、处理表单提交2.1 处理表单数据三、

java变量内存中存储的使用方式

《java变量内存中存储的使用方式》:本文主要介绍java变量内存中存储的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍2、变量的定义3、 变量的类型4、 变量的作用域5、 内存中的存储方式总结1、介绍在 Java 中,变量是用于存储程序中数据

如何合理管控Java语言的异常

《如何合理管控Java语言的异常》:本文主要介绍如何合理管控Java语言的异常问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍2、Thorwable类3、Error4、Exception类4.1、检查异常4.2、运行时异常5、处理方式5.1. 捕获异常

Spring Boot集成SLF4j从基础到高级实践(最新推荐)

《SpringBoot集成SLF4j从基础到高级实践(最新推荐)》SLF4j(SimpleLoggingFacadeforJava)是一个日志门面(Facade),不是具体的日志实现,这篇文章主要介... 目录一、日志框架概述与SLF4j简介1.1 为什么需要日志框架1.2 主流日志框架对比1.3 SLF4