[摘要]
Voldemort:高性能、高扩展性、不错的一致性Key-Value集群方案,目前被LinkIn使用,与Dynamo思想类似,参见http://project-voldemort.com
Objot:面向服务的轻量级web开发库,参见http://objot.com
[目标]
搭建一个WAP游戏开发框架,利用烂服务器集群提供高性能,尽量保证游戏数据的一致性,并可根据负载情况随时进行扩展,同时能够通过配置来调节各服务器利用率。
尽可能减少网络传输,单服务器既包含WEB也包含DB节点。
[技术选型]
1、游戏中不需要复杂的查询,因此抛弃了SQL数据库而使用Key-Value存储;通过对各种数据库的了解,发现Voldemort更适合,能保证目标。采用其Server Routing模式,不将WEB与DB在物理上划开。
2、Spring过于臃肿,故采用更加轻量级的Objot,同时Objot基于Java5,配置更简洁。
3、采用JSTL + JSP处理页面数据。
4、采用Tomcat作为WEB容器。
[实现](以单数据库节点为例)
1、在Servlet中启动Voldemort服务器
cluster.xml //Voldemort集群配置文件
<cluster>
<name>vortex</name>
<server>
<id>0</id>
<host>localhost</host>
<http-port>8081</http-port>
<socket-port>6666</socket-port>
<partitions>0, 1</partitions>
</server>
</cluster>
server.properties //Voldemort节点服务器配置
# The ID of *this* particular cluster node
node.id=0
max.threads=100
############### DB options ######################
#不需要HTTP服务,Server Routing Mode必须开启socket
http.enable=false
socket.enable=true
# 采用BDB存储
bdb.write.transactions=false
bdb.flush.transactions=false
bdb.cache.size=256M
#NIO connector settings.
enable.nio.connector=false
storage.configs=voldemort.store.bdb.BdbStorageConfiguration, voldemort.store.readonly.ReadOnlyStorageConfiguration
stores.xml //存储方式配置
<stores>
<store>
<name>vortex</name>
<persistence>bdb</persistence>
<routing>server</routing>
<replication-factor>1</replication-factor>
<required-reads>1</required-reads>
<required-writes>1</required-writes>
<key-serializer>
<type>identity</type>
</key-serializer>
<value-serializer>
<type>identity</type>
</value-serializer>
</store>
</stores>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
...
<servlet>
<servlet-name>vortex-servlet</servlet-name>
<servlet-class>vortex.VortexServlet</servlet-class>
<!-- Voldemort家目录:)-->
<init-param>
<param-name>voldemort-home</param-name>
<param-value>/home/nkadun/vortex/voldemort</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>vortex-servlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
...
</web-app>
VortexServlet.java
public class VortexServlet implements Servlet {
...
private final String VOLDEMORT_HOME_KEY = "voldemort-home";
private VoldemortServer server;
private StoreRepository respository;
private ServiceHandler handler;
@Override
public void destroy() {
logger.info("Shutdown voldemort server...");
if(server != null)
server.stop();
logger.info("Shutdown voldemort server successfully.");
}
@Override
public void init(ServletConfig c) throws ServletException {
config = c;
handler = new ServiceHandler();
try {
logger.info("Starting voldemort server...");
String voldemortHome = c.getInitParameter(VOLDEMORT_HOME_KEY);
server = new VoldemortServer(VoldemortConfig.loadFromVoldemortHome(voldemortHome));
//启动Voldemort服务器
server.start();
respository = ((StorageService)server.getService(ServiceType.STORAGE)).getStoreRepository();
logger.info("Voldemort server started.");
handler.init(respository);
}
catch(ConfigurationException e) {
logger.info("Error occurred:", e);
throw e;
}
catch(Exception e) {
logger.error("Error occurred:", e);
throw new ServletException(e);
}
}
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
try {
String page = handler.handle(request, response);
forward(page, request, response);
}
catch(Exception e) {
throw new ServletException(e);
}
}
...
}