背景:
之前一直只考虑用JavaSe进行游戏服务器开发,目前项目使用了Spring,发现还是非常好的,好处如下:
好处1:依赖注入非常方便,我们只使用Spring最基本的功能即可,这样子就算是有一些模块不使用Spring管理也是非常方便的,因为我现在已经能轻松控制住Spring容器的声明周期。
好处2: 模块之间就像搭建积木即可,又相互配合。 我想支持web也是非常轻松。
好处3: 这样子再去整合Mybatis、或者其它的一些MQ、ES之类的中间件,就太简单了。
1.Application.java
- package com.example.springbootgame.application;
-
- import org.springframework.context.ApplicationContext;
-
- public class Application {
- private static ApplicationContext applicationContext;
-
- public static ApplicationContext getApplicationContext() {
- return applicationContext;
- }
-
- public static void setApplicationContext(ApplicationContext applicationContext) {
- Application.applicationContext = applicationContext;
- }
-
- public static
T getBean(Class requiredType ) { - if (applicationContext == null) {
- return null;
- }
- return applicationContext.getBean(requiredType);
- }
-
- }
2.RedisConfig.java
- package com.example.springbootgame.config;
-
- import org.springframework.stereotype.Component;
-
- @Component
- public class RedisConfig {
- public int port = 6379;
- }
3.LoginHandler.java
- package com.example.springbootgame.handler;
-
- import com.example.springbootgame.config.RedisConfig;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
-
- @Controller
- public class LoginHandler {
- @Autowired
- RedisConfig redisConfig;
-
- /**
- * 玩家登录
- */
- public void onCSLogin(){
- System.out.println(redisConfig.port);
- }
- }
4.GameServer.java
- package com.example.springbootgame;
-
- import com.example.springbootgame.application.Application;
- import com.example.springbootgame.handler.LoginHandler;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ConfigurableApplicationContext;
-
- import java.io.IOException;
-
- @SpringBootApplication
- @Slf4j
- public class GameServer {
- public static void registerShutdownHook() {
- Runtime.getRuntime().addShutdownHook(new Thread(() -> {
- log.info("gs shutdown in {}", Thread.currentThread().getName());
-
- // 测试bean的获取
- LoginHandler loginHandler = Application.getBean(LoginHandler.class);
- loginHandler.onCSLogin();
-
- // 关闭Spring容器
- ApplicationContext applicationContext = Application.getApplicationContext();
- if (applicationContext != null) {
- ConfigurableApplicationContext cac = (ConfigurableApplicationContext) applicationContext;
- cac.close();
- }
-
- }, "ShutdownHook-GameServer-Thread"));
- }
-
- public static void main(String[] args) {
- registerShutdownHook();
-
- // 启动Spring容器
- ApplicationContext applicationContext = SpringApplication.run(GameServer.class, args);
- Application.setApplicationContext(applicationContext);
-
-
- // 初始化各个模块,如:进行handler的扫描
-
- // 阻塞关服
- try {
- System.in.read();
- } catch (IOException e) {
- log.error("exception", e);
- }
- }
-
- }
-
- . ____ _ __ _ _
- /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
- ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
- \\/ ___)| |_)| | | | | || (_| | ) ) ) )
- ' |____| .__|_| |_|_| |_\__, | / / / /
- =========|_|==============|___/=/_/_/_/
- :: Spring Boot :: (v2.7.17)
-
- 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)
- 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"
- 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)
- a
- 2023-11-18 14:54:19.189 INFO 4288 --- [meServer-Thread] com.example.springbootgame.GameServer : gs shutdown in ShutdownHook-GameServer-Thread
- 6379