[transfer] Dubbo declarative cache

Cache is widely used in order to improve the speed of data access. Dubbo is no exception. It provides declarative caching to reduce the user's workload of caching.

1, Caching policy in Dubbo

  • lru deletes redundant cache based on the principle of least recent use, and keeps the hottest data cached.
  • threadlocal current thread cache, such as a page rendering, uses many portals. Each portal needs to check user information. Through thread cache, this redundant access can be reduced.
  • jcache andJSR107 Integration, can bridge a variety of cache implementations.

2, Provider

The server includes interface and Implementation

Interface:

  1. package com.tgb.cacheService;  
  2.   
  3. /** 
  4.  * Server cache interface
  5.  * @author xx 
  6.  * 
  7.  */  
  8. public interface CacheService {  
  9.     String findCache(String id);  
  10. }  


Realization:

 

  1. package com.tgb.cacheService;  
  2.   
  3. import java.util.concurrent.atomic.AtomicInteger;  
  4.   
  5. /** 
  6.  * Implementation of server cache interface
  7.  * @author xx 
  8.  * 
  9.  */  
  10. public class CacheServiceImpl implements CacheService {  
  11.   
  12.     private final AtomicInteger i = new AtomicInteger();  
  13.       
  14.     public String findCache(String id) throws Exception {  
  15.          return "request: " + id + ", response: " + i.getAndIncrement();  
  16.     }  
  17. }  


spring configuration file: CacheProvider.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://code.alibabatech.com/schema/dubbo  
  8.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  9.         ">       
  10.     <dubbo:application name="cache-provider" />  
  11.     <dubbo:registry  protocol="zookeeper"  address="192.168.24.140:2181"  />  
  12.     <dubbo:protocol name="dubbo" port="20880" />       
  13.     <dubbo:service interface="com.tgb.cacheService.CacheService" ref="cacheService" />       <!-- And local bean Same implementation services -->  
  14.     <bean id="cacheService" class="com.tgb.cacheService.CacheServiceImpl" />  
  15. </beans>  


Program entry:

  1. package com.tgb.main;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4. /** 
  5.  * Server portal
  6.  * @author xx 
  7.  * 
  8.  */  
  9. public class CacheProvider {  
  10.   
  11.     public static void main(String[] args) throws Exception{  
  12.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "CacheProvider.xml" });  
  13.         context.start();  
  14.         System.out.println("press any key to exit");  
  15.         System.in.read();  
  16.     }  
  17. }  


3, Consumer

The interface is the same as the server

spring configuration file: CacheConsumer.xml, configure cache

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://code.alibabatech.com/schema/dubbo  
  8.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  9.         ">       
  10.     <!-- The consumer application name is used to calculate the dependency. It is not a matching condition. It should not be the same as the provider  192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183-->  
  11.     <dubbo:application name="cache-consumer" />       <!-- Use multicast Broadcast registry exposure discovery service address -->  
  12.     <dubbo:registry  protocol="zookeeper"  address="192.168.24.140:2181" />         <!-- Generate remote service agent, which can be connected with local bean Same use demoService -->  
  13.     <dubbo:reference id="cacheService" interface="com.tgb.cacheService.CacheService" cache="true" />  
  14. </beans>  


Program entry:

  1. package com.tgb.main;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. import com.tgb.cacheService.CacheService;  
  6.   
  7. /**  
  8.  * Client portal
  9.  * @author xx  
  10.  *  
  11.  */  
  12. public class CacheConsumer {  
  13.   
  14.     public static void main(String[] args) throws Exception {  
  15.   
  16.             ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "CacheConsumer.xml" });  
  17.             context.start();  
  18.               
  19.             CacheService cacheService = (CacheService)context.getBean("cacheService");  
  20.   
  21.             //The test cache takes effect and multiple calls return the same result. (server side self growth return value)
  22.             String fix = null;  
  23.             for (int i = 0; i 5; i ++) {  
  24.                 String result = cacheService.findCache("0"); //request: 0, response: 1001  
  25.                 if (fix == null || fix.equals(result)) {  
  26.                     System.out.println("OK: " + result);  
  27.                 } else {  
  28.                     System.err.println("ERROR: " + result);  
  29.                 }  
  30.                 fix = result;  
  31.                 Thread.sleep(6000);  
  32.             }  
  33.               
  34.             //The default cache.size of LRU is 1000, execute 1001 times, there should be overflow, 1001 times after executing 1001 times*2=2002,therefore result For 2002  
  35.             for (int n = 0; n 1001; n ++) {  
  36.                 String pre = null;  
  37.                 for (int i = 0; i 10; i ++) {  
  38.                     String result = cacheService.findCache(String.valueOf(n));  
  39.                     if (pre != null && ! pre.equals(result)) {  
  40.                         System.err.println("ERROR: " + result);  
  41.                     }  
  42.                     pre = result;  
  43.                 }  
  44.             }  
  45.               
  46.             //The test LRU has a cache item removed at the beginning
  47.             String result = cacheService.findCache("0"); //request: 0, response: 2002  
  48.             if (fix != null && ! fix.equals(result)) {  
  49.                 System.out.println("OK: " + result);  
  50.             } else {  
  51.                 System.err.println("ERROR: " + result);  
  52.             }  
  53.         }  
  54.     }  


Three, test

First, start zookeeper, then provider and consumer. The execution results are as follows:

  1. OK: request: 0, response: 1003  
  2. OK: request: 0, response: 1003  
  3. OK: request: 0, response: 1003  
  4. OK: request: 0, response: 1003  
  5. OK: request: 0, response: 1003  
  6. OK: request: 0, response: 2004  


The response value on the server side changes, but if the response result is 1000, then after 1001 execution, the result is 2001. When the third loop in the execution portal is executed, the result value in the cache is the latest, and the last unused value has been removed.

Original: https://blog.csdn.net/ghiben1314/article/details/47752661

Tags: Java Dubbo xml Spring Zookeeper

Posted on Mon, 30 Mar 2020 22:12:31 -0700 by whatsthis