[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集合框架完美结合。

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

相关推荐
PandaQue4 小时前
《怪物猎人:荒野》游戏可以键鼠直连吗
游戏
白狐欧莱雅8 小时前
使用python中的pygame简单实现飞机大战游戏
经验分享·python·游戏·pygame
豆本-豆豆奶9 小时前
用 Python 写了一个天天酷跑(附源码)
开发语言·python·游戏·pygame·零基础教程
Leoysq12 小时前
【UGUI】实现点击注册按钮跳转游戏场景
游戏·unity·游戏引擎·ugui
zsc_1181 天前
(C++回溯算法)微信小程序“开局托儿所”游戏
c++·算法·游戏
中云DDoS CC防护蔡蔡1 天前
棋牌游戏防ddos攻击,高防IP好用吗?
运维·服务器·游戏·网络安全·ddos
dangoxiba1 天前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第十八集补充:制作空洞骑士独有的EventSystem和InputModule
游戏·unity·c#·游戏引擎·playmaker
ToDesk_Daas2 天前
为什么越来越多人开始用云电脑?网友道出了真相
科技·游戏·电脑
虞书欣的62 天前
Python小游戏22——吃豆豆小游戏
python·算法·游戏·编辑器·pygame
魔法自动机2 天前
Unity3D学习FPS游戏(9)武器音效添加、创建敌人模型和血条
android·学习·游戏