high-availability 在WEB项目中的使用方法在非WEB项目中的使用方法 介绍
high-availability 保障服务的持续高可用、高性能及负载均衡。
高可用: 服务多副本
高性能: 超时限制
负载均衡: 环形队列
已经实现的功能:
一句话说明白这个项目的原理:
后端有多个一模一样的搜索,还有多个一模一样的REdis,中间层的业务逻辑不直接调用搜索和REdis,而是通过这个组件来调用
在WEB项目中的使用方法
1. 编译依赖:
git clone http://git.oschina.net/ysc/high-availability.git cd high-availability mvn install
2. 在 pom.xml 中指定以下依赖:
<dependency> <groupId>org.apdplat.service</groupId> <artifactId>high-availability</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
3. 在 src/main/resources 目录下新建目录 conf, 然后在 conf 目录下新建文件 conf.txt, 加入如下配置项:
#search api depends on these servers search.api.server.urls=http://192.168.0.100:8080/search.jsp, http://192.168.0.101:8080/search.jsp #timeout config to guarantee response time search.api.timeout.seconds=1 #whether output status to log in every unavailable urls valid process status.log.enabled=true #unavailable schedule initial delay seconds unavailable.schedule.initial.delay.seconds=60 #unavailable schedule period seconds unavailable.schedule.period.seconds=60 #redis servers redis.servers=192.168.0.102:6379:b01cbe1209a545a7cdb, 192.168.0.103:6379:b01cbe1209a545a7cdb redis.pool.blockWhenExhausted=false redis.pool.jmxenabled=false redis.pool.lifo=true redis.pool.maxIdle=500 redis.pool.maxTotal=500 redis.pool.maxWaitMillis=500 redis.pool.minevictableIdleTimeMillis=1800000 redis.pool.minIdle=0 redis.pool.numTestsPerevictionRun=3 redis.pool.softMinevictableIdleTimeMillis=1800000 redis.pool.testOnBorrow=true redis.pool.testWhileIdle=true redis.pool.timeBetweenevictionRunsMillis=-1 redis.pool.readTimeoutMillis=2000
4. 在 web.xml 中配置一个监听器:
<listener> <listener-class>org.apdplat.data.visualization.container.HighAvailabilityListener </listener-class> </listener>
5. 监听器 HighAvailabilityListener.java 类的代码如下:
public class HighAvailabilityListener implements servletcontextlistener { private static final Logger LOGGER = LoggerFactory.getLogger(HighAvailabilityListener.class); @Override public void contextinitialized(ServletContextEvent sce) { String conf = sce.getServletContext().getRealPath("/WEB-INF/classes/conf/"); LOGGER.info("启动搜索服务, 监控配置目录: {}", conf); ConfWatcher.startWatch(conf); sce.getServletContext().setAttribute("SearchAPI", new SearchAPIImpl()); } @Override public void contextDestroyed(ServletContextEvent sce) { LOGGER.info("停止搜索服务"); SearchAPI searchAPI = (SearchAPI)sce.getServletContext().getAttribute("SearchAPI"); if(searchAPI != null){ searchAPI.close(); } LOGGER.info("停止监控配置目录"); ConfWatcher.stopWatch(); } }
SearchAPI searchAPI = (SearchAPI) application.getAttribute("SearchAPI"); if(searchAPI == null) { out.println("搜索服务未启动"); return; } String keyword = request.getParameter("kw") == null ? "CCTV-1" : request.getParameter("kw"); int topN = 5; try{ topN = Integer.parseInt(request.getParameter("topN")); }catch (Exception e){ // } String result = null; long start = System.currentTimeMillis(); result = searchAPI.search(keyword, topN); String cost = Utils.getTimeDes(System.currentTimeMillis()-start); // 如果想知道搜索服务的状态 String status = searchAPI.getStatus().replace("\n", "<br/>").replace("\t", " ");
String DETECT_KEY = "redis_ha_detector"; Jedis jedis = null; try{ jedis = JedisAPI.getInstance().getJedis(); // 如果所有REdis服务都不可用, 则返回null if(jedis != null) { jedis.set(DETECT_KEY, String.valueOf(System.currentTimeMillis())); System.out.println(jedis.get(DETECT_KEY)); System.out.println("REdis服务状态:\n" + JedisAPI.getInstance().getStatus()); } } finally { if(jedis != null) { // 返回连接用完后必须要关闭, 调用close方法 jedis.close();} }
在非WEB项目中的使用方法
1. 编译依赖:
git clone http://git.oschina.net/ysc/high-availability.git cd high-availability mvn install
2. 在 pom.xml 中指定以下依赖:
<dependency> <groupId>org.apdplat.service</groupId> <artifactId>high-availability</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
3. 新建目录 conf, 然后在 conf 目录下新建文件 conf.txt, 加入如下配置项:
#search api depends on these servers search.api.server.urls=http://192.168.0.100:8080/search.jsp, http://192.168.0.101:8080/search.jsp #timeout config to guarantee response time search.api.timeout.seconds=1 #whether output status to log in every unavailable urls valid process status.log.enabled=true #unavailable schedule initial delay seconds unavailable.schedule.initial.delay.seconds=60 #unavailable schedule period seconds unavailable.schedule.period.seconds=60 #redis servers redis.servers=192.168.0.102:6379:b01cbe1209a545a7cdb, 192.168.0.103:6379:b01cbe1209a545a7cdb redis.pool.blockWhenExhausted=false redis.pool.jmxenabled=false redis.pool.lifo=true redis.pool.maxIdle=500 redis.pool.maxTotal=500 redis.pool.maxWaitMillis=500 redis.pool.minevictableIdleTimeMillis=1800000 redis.pool.minIdle=0 redis.pool.numTestsPerevictionRun=3 redis.pool.softMinevictableIdleTimeMillis=1800000 redis.pool.testOnBorrow=true redis.pool.testWhileIdle=true redis.pool.timeBetweenevictionRunsMillis=-1 redis.pool.readTimeoutMillis=2000
4. 将conf目录加入classpath:
java -cp conf:xxx-1.0-SNAPSHOT-jar-with-dependencies.jar
Path conf = Paths.get(ConfWatcher.class.getResource("/conf/").getPath()); LOGGER.info("启动搜索服务, 监控配置目录: {}", conf); ConfWatcher.startWatch(conf); SearchAPI searchAPI = new SearchAPIImpl();
String keyword = "CCTV-1"; int topN = 5; String result = null; long start = System.currentTimeMillis(); result = searchAPI.search(keyword, topN); String cost = Utils.getTimeDes(System.currentTimeMillis()-start); // 如果想知道搜索服务的状态 String status = searchAPI.getStatus();
String DETECT_KEY = "redis_ha_detector"; Jedis jedis = null; try{ jedis = JedisAPI.getInstance().getJedis(); // 如果所有REdis服务都不可用, 则返回null if(jedis != null) { jedis.set(DETECT_KEY, String.valueOf(System.currentTimeMillis())); System.out.println(jedis.get(DETECT_KEY)); System.out.println("REdis服务状态:\n" + JedisAPI.getInstance().getStatus()); } } finally { if(jedis != null){ // 返回连接用完后必须要关闭, 调用close方法 jedis.close(); } }
high-availability 在WEB项目中的使用方法在非WEB项目中的使用方法 官网
http://git.oschina.net/ysc/high-availability
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。