[SpringDataMongodb开发游戏服务器实战]

背景:

xdb其实足够完美了,现在回想一下,觉得有点复杂,我们不应该绑定语言,最好有自己的架构思路。

七号堡垒作为成功的商业项目,告诉我:其实数据是多读少写的,有修改的时候直接改库也是没问题的,这样子有个好处就是rpc获取到的数据足够的准确。

跨服这块结合redis,我们可以轻松获取其它服的数据。

华珺的Player数据管理思路,将所有的数据都存放Role上,这样子获取是十分的简单。

因此,我决定写一个简单而又足够上线的框架,思路:

技术栈: SpringBoot+netty+mongo+pb 。

是按照模块划分成多线程的,也就是: 尽量单进程多线程去完成任务。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jn</groupId>
    <artifactId>mongodemo</artifactId>
    <version>1.0</version>
    <name>mongodemo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

MongoConfig.java

package com.jn.framework.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

@Configuration
public class MongoConfig {

    @SuppressWarnings("all")
    @Bean
    public MongoTemplate mongoTemplate(MongoDatabaseFactory mongoDatabaseFactory, MongoMappingContext mongoMappingContext) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDatabaseFactory);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
        //去掉_class字段
        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
        return new MongoTemplate(mongoDatabaseFactory, mappingConverter);
    }
}

SpringBeanUtil.java

package com.jn.framework.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 普通的Java类中获取spring中的bean通用类
 */
@Component
public class SpringBeanUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringBeanUtil.applicationContext == null) {
            SpringBeanUtil.applicationContext = applicationContext;
        }
    }


    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

}

IEntity.java

package com.jn.framework;

public interface IEntity {
    void save();
}

IProcedure.java

package com.jn.framework;

public interface IProcedure {
    void call();
}

PlayerEntity.java

package com.jn.mongodemo.account.entity;

import com.jn.framework.IEntity;
import com.jn.framework.utils.SpringBeanUtil;
import com.jn.mongodemo.bag.entity.BagEntityRepository;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

@Data
@Document("PlayerEntity")
public class PlayerEntity implements IEntity {
    @Id
    @Indexed
    private Long roleId;

    @Override
    public void save() {
        SpringBeanUtil.getBean(PlayerEntityRepository.class).save(this);
    }
}

PlayerEntityRepository.java

package com.jn.mongodemo.account.entity;

import org.springframework.data.mongodb.repository.MongoRepository;

public interface PlayerEntityRepository extends MongoRepository<PlayerEntity, Long> {

}

PLogin.java

package com.jn.mongodemo.account.procedure;

import com.jn.framework.IProcedure;
import com.jn.framework.utils.SpringBeanUtil;
import com.jn.mongodemo.account.entity.PlayerEntity;
import com.jn.mongodemo.account.entity.PlayerEntityRepository;
import com.jn.mongodemo.bag.entity.BagEntity;
import com.jn.mongodemo.bag.entity.BagEntityRepository;
import com.jn.mongodemo.role.Role;

public class PLogin implements IProcedure {
    private long roleId;

    public PLogin(long roleId) {
        this.roleId = roleId;
    }

    @Override
    public void call() {

        // 加载玩家信息
        PlayerEntityRepository playerEntityRepository = SpringBeanUtil.getBean(PlayerEntityRepository.class);
        PlayerEntity playerEntity = playerEntityRepository.findById(roleId).orElse(null);
        if (playerEntity == null) {
            playerEntity = new PlayerEntity();
            playerEntity.setRoleId(roleId);
            playerEntityRepository.save(playerEntity);
        }
        Role.inst().setPlayerEntity(playerEntity);

        // 背包信息
        BagEntityRepository bagEntityRepository = SpringBeanUtil.getBean(BagEntityRepository.class);
        BagEntity bagEntity = bagEntityRepository.findById(roleId).orElse(null);
        if (bagEntity == null) {
            bagEntity = new BagEntity();
            bagEntity.setRoleId(roleId);
            bagEntity.setNum(100);
            bagEntityRepository.save(bagEntity);
        }
        Role.inst().setBagEntity(bagEntity);

        // 测试修改
        Role.inst().getBagEntity().setNum(Role.inst().getBagEntity().getNum() + 100);
        Integer num = Role.inst().getBagEntity().getItemMap().getOrDefault(1, 0);
        Role.inst().getBagEntity().getItemMap().put(1, num+1);

        Role.inst().getBagEntity().save();
    }
}

AccountMsgHandler.java

package com.jn.mongodemo.account;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jn.mongodemo.account.procedure.PLogin;

@RestController
@RequestMapping("/account")
public class AccountMsgHandler {

    /*
    http://localhost:8080/account/login?roleId=3
     */
    @RequestMapping("/login")
    public void login(Long roleId) {
        new PLogin(roleId).call();
    }
}

BagEntity.java

package com.jn.mongodemo.bag.entity;

import com.jn.framework.IEntity;
import com.jn.framework.utils.SpringBeanUtil;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.HashMap;
import java.util.Map;

@Data
@Document("BagEntity")
public class BagEntity implements IEntity {
    @Id
    @Indexed
    private Long roleId;

    private Integer num;

    private Map<Integer, Integer> itemMap = new HashMap<>();

    @Override
    public void save() {
        SpringBeanUtil.getBean(BagEntityRepository.class).save(this);
    }
}

BagEntityRepository.java

package com.jn.mongodemo.bag.entity;

import org.springframework.data.mongodb.repository.MongoRepository;

public interface BagEntityRepository extends MongoRepository<BagEntity, Long> {

}

Role.java

package com.jn.mongodemo.role;

import com.jn.mongodemo.account.entity.PlayerEntity;
import com.jn.mongodemo.bag.entity.BagEntity;
import lombok.Data;

@Data
public class Role {
    private static Role instance = new Role();

    public static Role inst() {
        return instance;
    }

    private PlayerEntity playerEntity;
    private BagEntity bagEntity;
}

MongodemoApplication.java

package com.jn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MongodemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MongodemoApplication.class, args);
    }

}

application.yml

spring:
  data:
    mongodb:
      host: 127.0.0.1
      port: 27017
      database: mongodemo

效果:

总结:

可见游戏服使用mongo是十分合适的,轻松面对复杂的数据结构,并且和java集合框架完美结合。

其实,修改数据这块,我们可以登录时是: 同步记载,保存时,可以异步。

相关推荐
m0_7482517221 小时前
Android webview 打开本地H5项目(Cocos游戏以及Unity游戏)
android·游戏·unity
gantengsheng1 天前
基于51单片机和OLED12864的小游戏《贪吃蛇》
单片机·嵌入式硬件·游戏·51单片机
264玫瑰资源库1 天前
从零开始C++棋牌游戏开发之第三篇:游戏的界面布局设计
开发语言·c++·python·游戏·pygame·源代码管理
264玫瑰资源库1 天前
从零开始C++游戏开发之第七篇:游戏状态机与回合管理
开发语言·c++·游戏
windwind20001 天前
游戏关卡设计方法的杂感
游戏·关卡设计
白乐天_n2 天前
腾讯游戏安全移动赛题Tencent2016A
安全·游戏
这是我582 天前
C++打小怪游戏
c++·其他·游戏·visual studio·小怪·大型·怪物
tealcwu2 天前
【游戏设计原理】21 - 解谜游戏的设计
游戏·游戏策划
清梦20202 天前
经典问题---跳跃游戏II(贪心算法)
算法·游戏·贪心算法
tealcwu2 天前
【游戏设计原理】22 - 石头剪刀布
游戏·游戏策划