从零开始手写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

相关文章

springboot rocketmq配置生产者和消息者的步骤

《springbootrocketmq配置生产者和消息者的步骤》本文介绍了如何在SpringBoot中集成RocketMQ,包括添加依赖、配置application.yml、创建生产者和消费者,并展... 目录1. 添加依赖2. 配置application.yml3. 创建生产者4. 创建消费者5. 使用在

Spring Retry 实现乐观锁重试实践记录

《SpringRetry实现乐观锁重试实践记录》本文介绍了在秒杀商品SKU表中使用乐观锁和MybatisPlus配置乐观锁的方法,并分析了测试环境和生产环境的隔离级别对乐观锁的影响,通过简单验证,... 目录一、场景分析 二、简单验证 2.1、可重复读 2.2、读已提交 三、最佳实践 3.1、配置重试模板

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

Java中有什么工具可以进行代码反编译详解

《Java中有什么工具可以进行代码反编译详解》:本文主要介绍Java中有什么工具可以进行代码反编译的相关资,料,包括JD-GUI、CFR、Procyon、Fernflower、Javap、Byte... 目录1.JD-GUI2.CFR3.Procyon Decompiler4.Fernflower5.Jav

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线

在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题记录

《在SpringBoot中使用异步线程时的HttpServletRequest复用问题记录》文章讨论了在SpringBoot中使用异步线程时,由于HttpServletRequest复用导致... 目录一、问题描述:异步线程操作导致请求复用时 Cookie 解析失败1. 场景背景2. 问题根源二、问题详细分

Java对象和JSON字符串之间的转换方法(全网最清晰)

《Java对象和JSON字符串之间的转换方法(全网最清晰)》:本文主要介绍如何在Java中使用Jackson库将对象转换为JSON字符串,并提供了一个简单的工具类示例,该工具类支持基本的转换功能,... 目录前言1. 引入 Jackson 依赖2. 创建 jsON 工具类3. 使用示例转换 Java 对象为

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer