Skip to content

Commit ffdc8c2

Browse files
author
Evan Hu
committed
添加注册中心
1 parent b69a4e5 commit ffdc8c2

File tree

6 files changed

+523
-0
lines changed

6 files changed

+523
-0
lines changed

game-cluster/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>game-project</artifactId>
7+
<groupId>info.xiaomo</groupId>
8+
<version>2019.1</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>game-cluster</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>info.xiaomo</groupId>
22+
<artifactId>game-core</artifactId>
23+
<version>2.0.7</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>info.xiaomo</groupId>
27+
<artifactId>game-protocol</artifactId>
28+
<version>2019.1</version>
29+
<scope>compile</scope>
30+
</dependency>
31+
</dependencies>
32+
33+
</project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package info.xiaomo.server.cluster;
2+
3+
import java.io.File;
4+
import java.util.Objects;
5+
import info.xiaomo.core.common.utils.FileUtil;
6+
import info.xiaomo.core.common.utils.SysUtil;
7+
import info.xiaomo.core.network.mina.config.MinaServerConfig;
8+
import info.xiaomo.core.persist.redis.jedis.JedisClusterConfig;
9+
import info.xiaomo.core.thread.ThreadPoolExecutorConfig;
10+
import info.xiaomo.server.cluster.server.ClusterServer;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
14+
/**
15+
* 启动类
16+
*
17+
* <p>服务器注册管理中心 <br>
18+
* 是否可用zookeeper替换
19+
*
20+
* @date 2017-03-31
21+
*/
22+
public class AppCluster {
23+
private static final Logger log = LoggerFactory.getLogger(AppCluster.class);
24+
25+
private static ClusterServer clusterServer;
26+
public static String path = "";
27+
28+
public static void main(String[] args) {
29+
File file = new File(System.getProperty("user.dir"));
30+
log.debug("user.dir: {}", System.getProperty("user.dir"));
31+
if ("target".equals(file.getName())) {
32+
path = file.getPath() + File.separatorChar + "config";
33+
} else {
34+
path = file.getPath() + File.separatorChar + "target" + File.separatorChar + "config";
35+
}
36+
log.info("配置路径为:" + path);
37+
JedisClusterConfig jedisClusterConfig =
38+
FileUtil.getConfigXML(path, "jedisclusterConfig.xml", JedisClusterConfig.class);
39+
if (jedisClusterConfig == null) {
40+
SysUtil.exit(AppCluster.class, null, "jedisclusterConfig");
41+
}
42+
ThreadPoolExecutorConfig threadExecutorConfig_http =
43+
FileUtil.getConfigXML(
44+
path, "threadExecutorConfig_http.xml", ThreadPoolExecutorConfig.class);
45+
if (threadExecutorConfig_http == null) {
46+
SysUtil.exit(AppCluster.class, null, "threadExecutorConfig_http");
47+
}
48+
ThreadPoolExecutorConfig threadExecutorConfig_tcp =
49+
FileUtil.getConfigXML(path, "threadExecutorConfig_tcp.xml", ThreadPoolExecutorConfig.class);
50+
if (threadExecutorConfig_tcp == null) {
51+
SysUtil.exit(AppCluster.class, null, "threadExecutorConfig_tcp");
52+
}
53+
MinaServerConfig minaServerConfig_http =
54+
FileUtil.getConfigXML(path, "minaServerConfig_http.xml", MinaServerConfig.class);
55+
if (minaServerConfig_http == null) {
56+
SysUtil.exit(AppCluster.class, null, "minaServerConfig_http");
57+
}
58+
MinaServerConfig minaServerConfig_tcp =
59+
FileUtil.getConfigXML(path, "minaServerConfig_tcp.xml", MinaServerConfig.class);
60+
if (minaServerConfig_tcp == null) {
61+
SysUtil.exit(AppCluster.class, null, "minaServerConfig_tcp");
62+
}
63+
// RedisManager redisManager = new RedisManager(jedisClusterConfig);
64+
65+
clusterServer =
66+
new ClusterServer(
67+
threadExecutorConfig_http,
68+
Objects.requireNonNull(minaServerConfig_http),
69+
threadExecutorConfig_tcp,
70+
Objects.requireNonNull(minaServerConfig_tcp));
71+
new Thread(clusterServer).start();
72+
}
73+
74+
public static ClusterServer getClusterServer() {
75+
return clusterServer;
76+
}
77+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package info.xiaomo.server.cluster.manager;
2+
3+
import java.util.Map;
4+
import java.util.Optional;
5+
import java.util.concurrent.ConcurrentHashMap;
6+
import info.xiaomo.core.server.ServerInfo;
7+
import info.xiaomo.core.server.ServerState;
8+
import info.xiaomo.core.server.ServerType;
9+
import info.xiaomo.server.cluster.server.ClusterTcpServer;
10+
import info.xiaomo.server.protocol.ServerMessage;
11+
import org.apache.mina.core.session.IoSession;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
/**
16+
* 服务器管理类
17+
*
18+
*
19+
* @date 2017-04-05
20+
*/
21+
public class ServerManager {
22+
private static final Logger LOGGER = LoggerFactory.getLogger(ServerManager.class);
23+
24+
private static final ServerManager instance = new ServerManager();
25+
/** 服务器列表 */
26+
final Map<ServerType, Map<Integer, ServerInfo>> servers = new ConcurrentHashMap<>();
27+
28+
public static ServerManager getInstance() {
29+
return instance;
30+
}
31+
32+
/**
33+
* 注册服务器信息
34+
*
35+
* @param serverInfo
36+
*/
37+
public ServerInfo registerServer(ServerMessage.ServerInfo serverInfo, IoSession session) {
38+
Map<Integer, ServerInfo> map = servers.get(ServerType.valueof(serverInfo.getType()));
39+
if (map == null) {
40+
map = new ConcurrentHashMap<>();
41+
servers.put(ServerType.valueof(serverInfo.getType()), map);
42+
}
43+
ServerInfo info = map.get(serverInfo.getId());
44+
if (info == null) {
45+
info = new ServerInfo();
46+
map.put(serverInfo.getId(), info);
47+
}
48+
info.setHttpPort(serverInfo.getHttpport());
49+
info.setId(serverInfo.getId());
50+
info.setIp(serverInfo.getIp());
51+
info.setMaxUserCount(serverInfo.getMaxUserCount());
52+
info.setName(serverInfo.getName());
53+
info.setOnline(serverInfo.getOnline());
54+
info.setPort(serverInfo.getPort());
55+
// info.setState(serverInfo.getState());
56+
info.setType(serverInfo.getType());
57+
info.setWwwip(serverInfo.getWwwip());
58+
info.onIoSessionConnect(session);
59+
info.setFreeMemory(serverInfo.getFreeMemory());
60+
info.setTotalMemory(serverInfo.getTotalMemory());
61+
info.setVersion(serverInfo.getVersion());
62+
63+
if (!session.containsAttribute(ClusterTcpServer.SERVER_INFO)) {
64+
session.setAttribute(ClusterTcpServer.SERVER_INFO, info);
65+
}
66+
return info;
67+
}
68+
69+
/**
70+
* 获取服务器列表
71+
*
72+
* @param serverType
73+
* @return
74+
*/
75+
public Map<Integer, ServerInfo> getServers(ServerType serverType) {
76+
return servers.get(serverType);
77+
}
78+
79+
/**
80+
* 获取服务器
81+
*
82+
*
83+
* @QQ 359135103 2017年7月13日 下午3:57:14
84+
* @param serverType
85+
* @param serverId
86+
* @return
87+
*/
88+
public ServerInfo getServer(ServerType serverType, int serverId) {
89+
Map<Integer, ServerInfo> map = getServers(serverType);
90+
if (map != null) {
91+
return map.get(serverId);
92+
}
93+
return null;
94+
}
95+
96+
public Map<ServerType, Map<Integer, ServerInfo>> getServers() {
97+
return servers;
98+
}
99+
100+
/**
101+
* 空闲服务器
102+
* <p>
103+
* 服务器状态监测,在线人数统计 <br>
104+
* 服务健康度检测(除了根据在线人数判断还可根据cpu内存等服务器检测设置优先级)
105+
* <p>
106+
* @param version 版本号 null 无版本要求
107+
* @return
108+
*/
109+
public ServerInfo getIdleGate(String version) {
110+
Map<Integer, ServerInfo> halls = getServers(ServerType.GATE);
111+
if (halls == null) {
112+
return null;
113+
}
114+
Optional<ServerInfo> findFirst = halls.values().stream()
115+
.filter(server -> server.getState() == ServerState.NORMAL.ordinal() && server.getSession() != null
116+
&& server.getSession().isConnected())
117+
.filter(server -> version == null || version.equals(server.getVersion())) //版本号检查
118+
.sorted((s1, s2) -> s1.getOnline() - s2.getOnline()).findFirst();
119+
if (findFirst.isPresent()) {
120+
return findFirst.get();
121+
}
122+
return null;
123+
}
124+
125+
/**
126+
* 移除服务器
127+
*
128+
* @param serverInfo
129+
*/
130+
public void removeServer(ServerInfo serverInfo) {
131+
LOGGER.info("服务器关闭:{}", serverInfo.toString());
132+
ServerType serverType = ServerType.valueof(serverInfo.getType());
133+
Map<Integer, ServerInfo> map = servers.get(serverType);
134+
if (map != null) {
135+
if (map.remove(serverInfo.getId()) != null) {
136+
LOGGER.info("服务器{}_{}已移除", serverType.toString(), serverInfo.getId());
137+
}
138+
}
139+
}
140+
141+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package info.xiaomo.server.cluster.server;
2+
3+
import info.xiaomo.core.common.handler.*;
4+
import info.xiaomo.core.network.mina.HttpServer;
5+
import info.xiaomo.core.network.mina.config.MinaServerConfig;
6+
import info.xiaomo.core.network.mina.handler.HttpServerIoHandler;
7+
import info.xiaomo.core.script.ScriptManager;
8+
import info.xiaomo.core.server.GameService;
9+
import info.xiaomo.core.thread.ThreadPoolExecutorConfig;
10+
import org.apache.mina.core.session.IoSession;
11+
import org.apache.mina.filter.FilterEvent;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
/**
16+
* http连接
17+
*
18+
*
19+
* @date 2017-03-31
20+
*/
21+
public class ClusterHttpServer extends GameService<MinaServerConfig> {
22+
23+
private static final Logger log = LoggerFactory.getLogger(ClusterHttpServer.class);
24+
25+
private final HttpServer httpServer;
26+
private final MinaServerConfig minaServerConfig;
27+
28+
public ClusterHttpServer(
29+
ThreadPoolExecutorConfig threadExecutorConfig, MinaServerConfig minaServerConfig) {
30+
super(threadExecutorConfig);
31+
this.minaServerConfig = minaServerConfig;
32+
this.httpServer = new HttpServer(minaServerConfig, new ClusterHttpServerHandler(this));
33+
}
34+
35+
@Override
36+
protected void running() {
37+
log.debug(" run ... ");
38+
httpServer.run();
39+
ScriptManager.getInstance().addIHandler(ReloadScriptHandler.class);
40+
ScriptManager.getInstance().addIHandler(ExitServerHandler.class);
41+
ScriptManager.getInstance().addIHandler(ReloadConfigHandler.class);
42+
ScriptManager.getInstance().addIHandler(JvmInfoHandler.class);
43+
ScriptManager.getInstance().addIHandler(GetFaviconHandler.class);
44+
ScriptManager.getInstance().addIHandler(ThreadInfoHandler.class);
45+
}
46+
47+
@Override
48+
protected void onShutdown() {
49+
super.onShutdown();
50+
log.debug(" stop ... ");
51+
httpServer.stop();
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return minaServerConfig.getName();
57+
}
58+
}
59+
60+
/**
61+
* 消息处理器
62+
*
63+
*
64+
* @date 2017-03-31
65+
*/
66+
class ClusterHttpServerHandler extends HttpServerIoHandler {
67+
68+
// private static final Logger log =
69+
// LoggerFactory.getLogger(ClusterHttpServerHandler.class);
70+
71+
private final GameService<MinaServerConfig> service;
72+
73+
public ClusterHttpServerHandler(GameService<MinaServerConfig> service) {
74+
this.service = service;
75+
}
76+
77+
@Override
78+
protected GameService<MinaServerConfig> getService() {
79+
return this.service;
80+
}
81+
82+
@Override
83+
public void event(IoSession ioSession, FilterEvent filterEvent) throws Exception {
84+
85+
}
86+
}

0 commit comments

Comments
 (0)