• 使用SpringBoot进行游戏服务器开发


    背景:

    之前一直只考虑用JavaSe进行游戏服务器开发,目前项目使用了Spring,发现还是非常好的,好处如下:

            好处1:依赖注入非常方便,我们只使用Spring最基本的功能即可,这样子就算是有一些模块不使用Spring管理也是非常方便的,因为我现在已经能轻松控制住Spring容器的声明周期。

            好处2: 模块之间就像搭建积木即可,又相互配合。 我想支持web也是非常轻松。

            好处3: 这样子再去整合Mybatis、或者其它的一些MQ、ES之类的中间件,就太简单了。

    1.Application.java

    1. package com.example.springbootgame.application;
    2. import org.springframework.context.ApplicationContext;
    3. public class Application {
    4. private static ApplicationContext applicationContext;
    5. public static ApplicationContext getApplicationContext() {
    6. return applicationContext;
    7. }
    8. public static void setApplicationContext(ApplicationContext applicationContext) {
    9. Application.applicationContext = applicationContext;
    10. }
    11. public static T getBean(Class requiredType) {
    12. if (applicationContext == null) {
    13. return null;
    14. }
    15. return applicationContext.getBean(requiredType);
    16. }
    17. }

    2.RedisConfig.java

    1. package com.example.springbootgame.config;
    2. import org.springframework.stereotype.Component;
    3. @Component
    4. public class RedisConfig {
    5. public int port = 6379;
    6. }

    3.LoginHandler.java

    1. package com.example.springbootgame.handler;
    2. import com.example.springbootgame.config.RedisConfig;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.stereotype.Controller;
    5. @Controller
    6. public class LoginHandler {
    7. @Autowired
    8. RedisConfig redisConfig;
    9. /**
    10. * 玩家登录
    11. */
    12. public void onCSLogin(){
    13. System.out.println(redisConfig.port);
    14. }
    15. }

    4.GameServer.java

    1. package com.example.springbootgame;
    2. import com.example.springbootgame.application.Application;
    3. import com.example.springbootgame.handler.LoginHandler;
    4. import lombok.extern.slf4j.Slf4j;
    5. import org.springframework.boot.SpringApplication;
    6. import org.springframework.boot.autoconfigure.SpringBootApplication;
    7. import org.springframework.context.ApplicationContext;
    8. import org.springframework.context.ConfigurableApplicationContext;
    9. import java.io.IOException;
    10. @SpringBootApplication
    11. @Slf4j
    12. public class GameServer {
    13. public static void registerShutdownHook() {
    14. Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    15. log.info("gs shutdown in {}", Thread.currentThread().getName());
    16. // 测试bean的获取
    17. LoginHandler loginHandler = Application.getBean(LoginHandler.class);
    18. loginHandler.onCSLogin();
    19. // 关闭Spring容器
    20. ApplicationContext applicationContext = Application.getApplicationContext();
    21. if (applicationContext != null) {
    22. ConfigurableApplicationContext cac = (ConfigurableApplicationContext) applicationContext;
    23. cac.close();
    24. }
    25. }, "ShutdownHook-GameServer-Thread"));
    26. }
    27. public static void main(String[] args) {
    28. registerShutdownHook();
    29. // 启动Spring容器
    30. ApplicationContext applicationContext = SpringApplication.run(GameServer.class, args);
    31. Application.setApplicationContext(applicationContext);
    32. // 初始化各个模块,如:进行handler的扫描
    33. // 阻塞关服
    34. try {
    35. System.in.read();
    36. } catch (IOException e) {
    37. log.error("exception", e);
    38. }
    39. }
    40. }
    1. . ____ _ __ _ _
    2. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
    3. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    4. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
    5. ' |____| .__|_| |_|_| |_\__, | / / / /
    6. =========|_|==============|___/=/_/_/_/
    7. :: Spring Boot :: (v2.7.17)
    8. 2023-11-18 14:54:16.886 INFO 4288 --- [ main] com.example.springbootgame.GameServer : Starting GameServer using Java 11.0.11 on DESKTOP-JTMBOEI with PID 4288 (D:\2_test_java\SpringBootGame\target\classes started by Administrator in D:\2_test_java\SpringBootGame)
    9. 2023-11-18 14:54:16.889 INFO 4288 --- [ main] com.example.springbootgame.GameServer : No active profile set, falling back to 1 default profile: "default"
    10. 2023-11-18 14:54:17.477 INFO 4288 --- [ main] com.example.springbootgame.GameServer : Started GameServer in 0.924 seconds (JVM running for 1.979)
    11. a
    12. 2023-11-18 14:54:19.189 INFO 4288 --- [meServer-Thread] com.example.springbootgame.GameServer : gs shutdown in ShutdownHook-GameServer-Thread
    13. 6379

  • 相关阅读:
    万字长文搞懂ShuffleNet v2模型
    OpenCV自学笔记二十三:K近邻算法
    nvm的下载,安装与使用详解
    MobileViT
    蓝桥杯 超级胶水 答疑
    [python] Multiprocessing.Pool使用-多线程并发执行代码
    10.Xaml ListBox控件
    【二分图】 二分图上匹配问题 和 匈牙利算法正确性说明
    37页数字乡村振兴智慧农业整体规划建设方案
    【无标题】
  • 原文地址:https://blog.csdn.net/themagickeyjianan/article/details/134478400