|
| 1 | +# 分布式服务框架之Dubbo(缓存策略) |
| 2 | + |
| 3 | +### Dubbo缓存介绍 |
| 4 | +针对结果缓存,用于加速热点数据访问速度,Dubbo提供声明式缓存,以减少用户加缓存工作量; |
| 5 | + |
| 6 | +### 缓存类型 |
| 7 | +#### ThreadLocal |
| 8 | +该缓存应用场景为: |
| 9 | +> - 比如一个请求需要多次访问服务,可以通过线程缓存,可以减少多余访问; |
| 10 | +> - 场景描述的核心内容是当前请求的上下文; |
| 11 | +
|
| 12 | +配置示例: |
| 13 | +```xml |
| 14 | +<dubbo:reference interface="com.demo.xx" cache="threadlocal" /> |
| 15 | +``` |
| 16 | + |
| 17 | +#### LRU缓存淘汰算法(Dubbo默认的缓存策略) |
| 18 | +该缓存应用场景为: |
| 19 | +> - LRU基于最近最少使用的原则删除多余缓存,保持最热的数据被缓存; |
| 20 | +> - 该类型的缓存是跨线程的; |
| 21 | +
|
| 22 | +##### LRU原理 |
| 23 | +LRU(Least recently used,最近最少使用)算法根据数据历史访问记录进行淘汰数据,其核心思想就是“如果数据最近被访问过,那么将来访问的几率也更高”; |
| 24 | + |
| 25 | +最常见的实现是使用一个链表保存缓存数据: |
| 26 | +1. 新数据插入链表头部; |
| 27 | +2. 每当缓存命中(即缓存数据被访问),则将数据移到链表头部; |
| 28 | +3. 当链表满的时候,将链表尾部数据丢弃; |
| 29 | + |
| 30 | +配置示例: |
| 31 | +```xml |
| 32 | +<dubbo:reference interface="com.demo.xx" cache="true" /> |
| 33 | +``` |
| 34 | + |
| 35 | +#### JCache |
| 36 | +Jcache与JSR107集成,可以桥接各种缓存实现; |
| 37 | + |
| 38 | +### Dubbo缓存实战 |
| 39 | +#### 1.配置Provider端 |
| 40 | +```xml |
| 41 | +<?xml version="1.0" encoding="UTF-8"?> |
| 42 | +<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 43 | + xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" |
| 44 | + xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" |
| 45 | + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
| 46 | + http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> |
| 47 | + <context:property-placeholder/> |
| 48 | + |
| 49 | + <dubbo:application name="cache-provider"/> |
| 50 | + |
| 51 | + <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/> |
| 52 | + |
| 53 | + <dubbo:protocol name="dubbo" port="20880"/> |
| 54 | + |
| 55 | + <bean id="cacheService" class="com.ipman.dubbo.cache.sample.service.impl.CacheServiceImpl"/> |
| 56 | + |
| 57 | + <dubbo:service interface="com.ipman.dubbo.cache.sample.service.CacheService" ref="cacheService"/> |
| 58 | +</beans> |
| 59 | +``` |
| 60 | + |
| 61 | +#### 2.配置Consumer端 |
| 62 | +```xml |
| 63 | +<?xml version="1.0" encoding="UTF-8"?> |
| 64 | +<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 65 | + xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" |
| 66 | + xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" |
| 67 | + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
| 68 | + http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> |
| 69 | + <context:property-placeholder/> |
| 70 | + |
| 71 | + <dubbo:application name="cache-consumer"/> |
| 72 | + |
| 73 | + <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/> |
| 74 | + |
| 75 | + <dubbo:reference id="cacheService" interface="com.ipman.dubbo.cache.sample.service.CacheService" cache="true"/> |
| 76 | +</beans> |
| 77 | +``` |
| 78 | + |
| 79 | +#### 3.测试LRU淘汰算法 |
| 80 | +```java |
| 81 | +public class CacheConsumer { |
| 82 | + |
| 83 | + @SneakyThrows |
| 84 | + public static void main(String[] args) { |
| 85 | + //启动消费者 |
| 86 | + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/cache-consumer.xml"); |
| 87 | + CacheService cacheService = (CacheService) context.getBean("cacheService"); |
| 88 | + |
| 89 | + //对比结果:True,命中缓存 |
| 90 | + String value = cacheService.findCache(0); |
| 91 | + System.out.println(value.equals(cacheService.findCache(0))); |
| 92 | + |
| 93 | + //默认使用LRU淘汰算法,默认缓存Size是1000,如果调用1001次缓存则失效 |
| 94 | + for (int i = 1; i <= 1001; i++) { |
| 95 | + cacheService.findCache(i); |
| 96 | + } |
| 97 | + |
| 98 | + //对比结果:Flase,缓存失效 |
| 99 | + System.out.println(value.equals(cacheService.findCache(0))); |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
0 commit comments