原文:https://blog.csdn.net/ggibenben1314/article/details/47752661html
緩存的應用很是普遍,爲了提升數據訪問的速度。Dubbo也不例外,它提供了聲明式緩存,以減小用戶加緩存的工做量。java
1、Dubbo中緩存策略spring
2、Provider
緩存
服務端包含接口和實現服務器
接口:app
package com.tgb.cacheService; /** * 服務端 緩存 接口 * @author xx * */ public interface CacheService { String findCache(String id); }
實現:ide
package com.tgb.cacheService; import java.util.concurrent.atomic.AtomicInteger; /** * 服務端 緩存 接口實現 * @author xx * */ public class CacheServiceImpl implements CacheService { private final AtomicInteger i = new AtomicInteger(); public String findCache(String id) throws Exception { return "request: " + id + ", response: " + i.getAndIncrement(); } }
spring配置文件:CacheProvider.xml測試
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <dubbo:application name="cache-provider" /> <dubbo:registry protocol="zookeeper" address="192.168.24.140:2181" /> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service interface="com.tgb.cacheService.CacheService" ref="cacheService" /> <!-- 和本地bean同樣實現服務 --> <bean id="cacheService" class="com.tgb.cacheService.CacheServiceImpl" /> </beans>
程序入口:atom
package com.tgb.main; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 服務端入口 * @author xx * */ public class CacheProvider { public static void main(String[] args) throws Exception{ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "CacheProvider.xml" }); context.start(); System.out.println("按任意鍵退出"); System.in.read(); } }
3、Consumerspa
接口同服務端
spring配置文件:CacheConsumer.xml,配置緩存
程序入口:
package com.tgb.main; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tgb.cacheService.CacheService; /** * 客戶端入口 * @author xx * */ public class CacheConsumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "CacheConsumer.xml" }); context.start(); CacheService cacheService = (CacheService)context.getBean("cacheService"); // 測試緩存生效,屢次調用返回一樣的結果。(服務器端自增加返回值) String fix = null; for (int i = 0; i < 5; i ++) { String result = cacheService.findCache("0"); //request: 0, response: 1001 if (fix == null || fix.equals(result)) { System.out.println("OK: " + result); } else { System.err.println("ERROR: " + result); } fix = result; Thread.sleep(6000); } // LRU的缺省cache.size爲1000,執行1001次,應有溢出,執行了1001次後1001*2=2002,因此result爲2002 for (int n = 0; n < 1001; n ++) { String pre = null; for (int i = 0; i < 10; i ++) { String result = cacheService.findCache(String.valueOf(n)); if (pre != null && ! pre.equals(result)) { System.err.println("ERROR: " + result); } pre = result; } } // 測試LRU有移除最開始的一個緩存項 String result = cacheService.findCache("0"); //request: 0, response: 2002 if (fix != null && ! fix.equals(result)) { System.out.println("OK: " + result); } else { System.err.println("ERROR: " + result); } } }
3、測試
首先要啓動zookeeper,而後依次啓動provider和consumer,執行結果以下:
OK: request: 0, response: 1003 OK: request: 0, response: 1003 OK: request: 0, response: 1003 OK: request: 0, response: 1003 OK: request: 0, response: 1003 OK: request: 0, response: 2004
服務器端的response值是變化的,可是若是response結果是1000,那麼在執行了1001次後,結果爲2001,到執行入口中第三個循環的時候緩存中result值是最新的,最近最久不使用的已經被移除了。