high-availability保障服务的持续高可用、高性能及负载均衡。
高可用:服务多副本
高性能:超时限制
负载均衡:环形队列
已经实现的功能:
1.HTTP调用方式的搜索服务
2.REDIS访问
一句话说明白这个项目的原理:
后端有多个一模一样的搜索,还有多个一模一样的REDIS,中间层的业务逻辑不直接调用搜索和REDIS,而是通过这个组件来调用
在WEB项目中的使用方法1.编译依赖:
git cloe https://git.oschia.et/ysc/high-availability.git cd high-availability mv istall2.在pom.xml中指定以下依赖:
<depedecy> <groupId>org.apdplat.service</groupId> <artifactId>high-availability</artifactId> <versio>1.0-SNAPSHOT</versio></depedecy>3.在src/mai/resources目录下新建目录cof,然后在cof目录下新建文件cof.txt,加入如下配置项:
#search api depeds o these serverssearch.api.server.urls=https://192.168.0.100:8080/search.jsp, https://192.168.0.101:8080/search.jsp #timeout cofig to guaratee respose timesearch.api.timeout.secods=1 #whether output status to log i every uavailable urls valid processstatus.log.eabled=true#uavailable schedule iitial delay secodsuavailable.schedule.iitial.delay.secods=60 #uavailable schedule period secodsuavailable.schedule.period.secods=60 #redis serversredis.servers=192.168.0.102:6379:b01cbe1209a545a7cdb, 192.168.0.103:6379:b01cbe1209a545a7cdbredis.pool.blockWheExhausted=false redis.pool.jmxEabled=false redis.pool.lifo=true redis.pool.maxIdle=500 redis.pool.maxTotal=500 redis.pool.maxWaitMillis=500 redis.pool.miEvictableIdleTimeMillis=1800000 redis.pool.miIdle=0 redis.pool.umTestsPerEvictioRu=3 redis.pool.softMiEvictableIdleTimeMillis=1800000 redis.pool.testOBorrow=true redis.pool.testWhileIdle=true redis.pool.timeBetweeEvictioRusMillis=-1 redis.pool.readTimeoutMillis=20004.在web.xml中配置一个监听器:
<listeer> <listeer-class>org.apdplat.data.visualizatio.cotaier.HighAvailabilityListeer </listeer-class> </listeer>5.监听器HighAvailabilityListeer.java类的代码如下:
public class HighAvailabilityListeer implemets ServletCotextListeer { private static fial Logger LOGGER = LoggerFactory.getLogger(HighAvailabilityListeer.class); @Override public void cotextIitialized(ServletCotextEvet sce) { Strig cof = sce.getServletCotext().getRealPath("/WEB-INF/classes/cof/"); LOGGER.ifo("启动搜索服务, 监控配置目录: {}", cof); CofWatcher.startWatch(cof); sce.getServletCotext().setAttribute("SearchAPI", ew SearchAPIImpl()); } @Override public void cotextDestroyed(ServletCotextEvet sce) { LOGGER.ifo("停止搜索服务"); SearchAPI searchAPI = (SearchAPI)sce.getServletCotext().getAttribute("SearchAPI"); if(searchAPI != ull){ searchAPI.close(); } LOGGER.ifo("停止监控配置目录"); CofWatcher.stopWatch(); } }6.在jsp中调用搜索服务:
SearchAPI searchAPI = (SearchAPI) applicatio.getAttribute("SearchAPI"); if(searchAPI == ull) { out.pritl("搜索服务未启动"); retur;}Strig keyword = request.getParameter("kw") == ull ? "CCTV-1" : request.getParameter("kw");it topN = 5; try{ topN = Iteger.parseIt(request.getParameter("topN")); }catch (Exceptio e){ // }Strig result = ull;log start = System.curretTimeMillis();result = searchAPI.search(keyword, topN);Strig cost = Utils.getTimeDes(System.curretTimeMillis()-start); // 如果想知道搜索服务的状态 Strig status = searchAPI.getStatus().replace("\", "<br/>").replace("\t", "&bsp; &bsp; &bsp; &bsp; ");7.获取REDIS连接:
Strig DETECT_KEY = "redis_ha_detector";Jedis jedis = ull; try{ jedis = JedisAPI.getIstace().getJedis(); // 如果所有REDIS服务都不可用, 则返回ull if(jedis != ull) { jedis.set(DETECT_KEY, Strig.valueOf(System.curretTimeMillis())); System.out.pritl(jedis.get(DETECT_KEY)); System.out.pritl("REDIS服务状态:\" + JedisAPI.getIstace().getStatus()); } } fially { if(jedis != ull) { // 返回连接用完后必须要关闭, 调用close方法 jedis.close();} }在非WEB项目中的使用方法1.编译依赖:
git cloe https://git.oschia.et/ysc/high-availability.git cd high-availability mv istall2.在pom.xml中指定以下依赖:
<depedecy> <groupId>org.apdplat.service</groupId> <artifactId>high-availability</artifactId> <versio>1.0-SNAPSHOT</versio></depedecy>3.新建目录cof,然后在cof目录下新建文件cof.txt,加入如下配置项:
#search api depeds o these servers search.api.server.urls=https://192.168.0.100:8080/search.jsp, https://192.168.0.101:8080/search.jsp #timeout cofig to guaratee respose time search.api.timeout.secods=1 #whether output status to log i every uavailable urls valid process status.log.eabled=true #uavailable schedule iitial delay secods uavailable.schedule.iitial.delay.secods=60 #uavailable schedule period secods uavailable.schedule.period.secods=60 #redis servers redis.servers=192.168.0.102:6379:b01cbe1209a545a7cdb, 192.168.0.103:6379:b01cbe1209a545a7cdbredis.pool.blockWheExhausted=false redis.pool.jmxEabled=false redis.pool.lifo=true redis.pool.maxIdle=500 redis.pool.maxTotal=500 redis.pool.maxWaitMillis=500 redis.pool.miEvictableIdleTimeMillis=1800000 redis.pool.miIdle=0 redis.pool.umTestsPerEvictioRu=3 redis.pool.softMiEvictableIdleTimeMillis=1800000 redis.pool.testOBorrow=true redis.pool.testWhileIdle=true redis.pool.timeBetweeEvictioRusMillis=-1 redis.pool.readTimeoutMillis=20004.将cof目录加入classpath:
java -cp cof:xxx-1.0-SNAPSHOT-jar-with-depedecies.jar5.启动搜索服务并监控配置目录的代码如下:
Path cof = Paths.get(CofWatcher.class.getResource("/cof/").getPath());LOGGER.ifo("启动搜索服务, 监控配置目录: {}", cof);CofWatcher.startWatch(cof);SearchAPI searchAPI = ew SearchAPIImpl();6.调用搜索服务:
Strig keyword = "CCTV-1"; it topN = 5;Strig result = ull; log start = System.curretTimeMillis();result = searchAPI.search(keyword, topN);Strig cost = Utils.getTimeDes(System.curretTimeMillis()-start); // 如果想知道搜索服务的状态 Strig status = searchAPI.getStatus();7.获取REDIS连接:
Strig DETECT_KEY = "redis_ha_detector";Jedis jedis = ull; try{ jedis = JedisAPI.getIstace().getJedis(); // 如果所有REDIS服务都不可用, 则返回ull if(jedis != ull) { jedis.set(DETECT_KEY, Strig.valueOf(System.curretTimeMillis())); System.out.pritl(jedis.get(DETECT_KEY)); System.out.pritl("REDIS服务状态:\" + JedisAPI.getIstace().getStatus()); }} fially { if(jedis != ull){ // 返回连接用完后必须要关闭, 调用close方法 jedis.close(); }}
评论