如果一个分布式系统中,一个服务要调用多个服务,那么,那个地方消耗的时间大,必须要服务时钟同步才可,时间同步非常重要。
方法1:ntp服务器:
如果运维比较靠谱,那么linux下使用ntp服务进行同步是最佳选择,java客户端调用ntp服务时间例子见: http://lshh83.iteye.com/blog/546405。
方法2:数据库服务器
服务都使用数据库的时间来作为时间,这种情况数据查询频繁,实时行不行。
方法3:redis服务器
使用redis服务器的time方法获得对应的时间,使用方法如下:
/** * 获得当前时间 * @return */ public static List<String> time(){ Jedis client = null; List<String> resultList = null; try { client = sentinelPool.getResource(); resultList = client.time(); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage(), e); } finally { if (client != null) client.close(); } return resultList; }
经测试,部署在青云服务器上,使用vpn连接,单个请求大概消耗3到4毫秒,不太需要时间太精确的情况下可用。