【轉】Dubbo聲明式緩存

原文:https://blog.csdn.net/ggibenben1314/article/details/47752661html

緩存的應用很是普遍,爲了提升數據訪問的速度。Dubbo也不例外,它提供了聲明式緩存,以減小用戶加緩存的工做量。java

1、Dubbo中緩存策略spring

  • lru 基於最近最少使用原則刪除多餘緩存,保持最熱的數據被緩存。
  • threadlocal 當前線程緩存,好比一個頁面渲染,用到不少portal,每一個portal都要去查用戶信息,經過線程緩存,能夠減小這種多餘訪問。
  • jcache 與JSR107集成,能夠橋接各類緩存實現。

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,配置緩存

<?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  
        ">       
    <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方同樣  192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183-->  
    <dubbo:application name="cache-consumer" />       <!-- 使用multicast廣播註冊中心暴露發現服務地址 -->  
    <dubbo:registry  protocol="zookeeper"  address="192.168.24.140:2181" />         <!-- 生成遠程服務代理,能夠和本地bean同樣使用demoService -->  
    <dubbo:reference id="cacheService" interface="com.tgb.cacheService.CacheService" cache="true" />  
</beans>  

  

程序入口:

 

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值是最新的,最近最久不使用的已經被移除了。

相關文章
相關標籤/搜索