Java 杂谈微服务架构和实践Java技术升华

游戏服务器架构:数据回写

2018-07-04  本文已影响32人  聪明的奇瑞

描述

设计方案

数据回写

代码案例

public interface IFlushTimer extends Runnable {
    int delay();
}
public class Player implements IFlushTimer {
    private int id;
    private String name;
    private ScheduledFuture<?> db_sf = null;

    public Player(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public int delay() {
        return 5;
    }

    @Override
    public void run() {
        this.flush();
    }

    /**
     * 启动定时回写任务
     */
    public void startup() {
        db_sf = DBFlushTimer.add(this);
    }

    /**
     * 模拟数据库回写
     */
    private void flush() {
        System.out.println("id:" + this.id + "回写中...");
        // 模拟回写操作延迟
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 清除定时回写并回写数据
     */
    public void clear() {
        synchronized (db_sf) {
            db_sf.cancel(false);
            db_sf = null;
        }
        runClose();
    }

    /**
     * 回写数据并执行其他操作
     */
    public void runClose() {
        this.flush();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public class DBFlushTimer {
    private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    public static ScheduledFuture add(IFlushTimer timer){
        ScheduledFuture scheduledFuture = scheduler.scheduleWithFixedDelay(timer,(long)timer.delay(),(long)timer.delay(),TimeUnit.SECONDS);
        return scheduledFuture;
    }
}

public class PlayerService {

    public static final ConcurrentHashMap<Integer, Player> playerId2Player = new ConcurrentHashMap<Integer, Player>();

    /**
     * 开服初始化,模拟从数据库加载玩家信息
     */
    public static void init() {
        Player p1 = new Player(1, "p1");
        Player p2 = new Player(2, "p2");
        PlayerService.playerId2Player.put(p1.getId(), p1);
        PlayerService.playerId2Player.put(p2.getId(), p2);

        // 启动定时回写
        p1.startup();
        p2.startup();
    }

    /**
     * 停服回写所有数据
     */
    public static void flushToDB(){
        System.out.println("flush player data to db .....");
        for (Player player : playerId2Player.values()){
            player.clear();
        }
        System.out.println("flush tong data end .....");
    }
}
public class GameServer {
    public static void main(String[] args)throws Exception {
        init();
        TimeUnit.SECONDS.sleep(12);
        stop();
    }

    public static void init(){
        System.out.println("init game server .....");
        PlayerService.init();
    }
    // 关服
    public static void stop(){
        System.out.println("stop game server .....");
        PlayerService.flushToDB();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读