缓存的应用非常广泛,为了提高数据访问的速度。Dubbo也不例外,它提供了声明式缓存,以减少用户加缓存的工作量。

一、Dubbo中缓存策略

  • lru 基于最近最少使用原则删除多余缓存,保持最热的数据被缓存。
  • threadlocal 当前线程缓存,比如一个页面渲染,用到很多portal,每个portal都要去查用户信息,通过线程缓存,可以减少这种多余访问。
  • jcache 与JSR107集成,可以桥接各种缓存实现。

二、Provider

服务端包含接口和实现

接口:

  1. package com.tgb.cacheService;
  2. /**
  3. * 服务端 缓存 接口
  4. * @author xx
  5. *
  6. */
  7. public interface CacheService {
  8. String findCache(String id);
  9. }

实现:

  1. package com.tgb.cacheService;
  2. import java.util.concurrent.atomic.AtomicInteger;
  3. /**
  4. * 服务端 缓存 接口实现
  5. * @author xx
  6. *
  7. */
  8. public class CacheServiceImpl implements CacheService {
  9. private final AtomicInteger i = new AtomicInteger();
  10. public String findCache(String id) throws Exception {
  11. return "request: " + id + ", response: " + i.getAndIncrement();
  12. }
  13. }

spring配置文件: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" />       <!-- 和本地bean一样实现服务 -->
  14. <bean id="cacheService" class="com.tgb.cacheService.CacheServiceImpl" />
  15. </beans>

程序入口:

  1. package com.tgb.main;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. /**
  4. * 服务端入口
  5. * @author xx
  6. *
  7. */
  8. public class CacheProvider {
  9. public static void main(String[] args) throws Exception{
  10. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "CacheProvider.xml" });
  11. context.start();
  12. System.out.println("按任意键退出");
  13. System.in.read();
  14. }
  15. }

三、Consumer

接口同服务端

spring配置文件:CacheConsumer.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. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样  192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183-->
  11. <dubbo:application name="cache-consumer" />       <!-- 使用multicast广播注册中心暴露发现服务地址 -->
  12. <dubbo:registry  protocol="zookeeper"  address="192.168.24.140:2181" />         <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
  13. <dubbo:reference id="cacheService" interface="com.tgb.cacheService.CacheService" cache="true" />
  14. </beans>

程序入口:

  1. package com.tgb.main;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. import com.tgb.cacheService.CacheService;
  4. /**
  5. * 客户端入口
  6. * @author xx
  7. *
  8. */
  9. public class CacheConsumer {
  10. public static void main(String[] args) throws Exception {
  11. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "CacheConsumer.xml" });
  12. context.start();
  13. CacheService cacheService = (CacheService)context.getBean("cacheService");
  14. // 测试缓存生效,多次调用返回同样的结果。(服务器端自增长返回值)
  15. String fix = null;
  16. for (int i = 0; i < 5; i ++) {
  17. String result = cacheService.findCache("0"); //request: 0, response: 1001
  18. if (fix == null || fix.equals(result)) {
  19. System.out.println("OK: " + result);
  20. } else {
  21. System.err.println("ERROR: " + result);
  22. }
  23. fix = result;
  24. Thread.sleep(6000);
  25. }
  26. // LRU的缺省cache.size为1000,执行1001次,应有溢出,执行了1001次后1001*2=2002,所以result为2002
  27. for (int n = 0; n < 1001; n ++) {
  28. String pre = null;
  29. for (int i = 0; i < 10; i ++) {
  30. String result = cacheService.findCache(String.valueOf(n));
  31. if (pre != null && ! pre.equals(result)) {
  32. System.err.println("ERROR: " + result);
  33. }
  34. pre = result;
  35. }
  36. }
  37. // 测试LRU有移除最开始的一个缓存项
  38. String result = cacheService.findCache("0"); //request: 0, response: 2002
  39. if (fix != null && ! fix.equals(result)) {
  40. System.out.println("OK: " + result);
  41. } else {
  42. System.err.println("ERROR: " + result);
  43. }
  44. }
  45. }

三、测试

首先要启动zookeeper,然后依次启动provider和consumer,执行结果如下:

  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

服务器端的response值是变化的,但是如果response结果是1000,那么在执行了1001次后,结果为2001,到执行入口中第三个循环的时候缓存中result值是最新的,最近最久不使用的已经被移除了。

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

【转】Dubbo声明式缓存的更多相关文章

  1. Dubbo声明式缓存

    为了进一步提高消费者对用户的响应速度,减轻提供者的压力,Dubbo提供了基于结果的声明式缓存.该缓存是基于消费者端的,所以使用很简单,只需修改消费者配置文件,与提供者无关 一.创建消费者07-cons ...

  2. Spring 3.1新特性之三:Spring对声明式缓存的支持

    一.概述: Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如EHCache 或者 OSCache),而是一个对缓 ...

  3. dubbo之结果缓存

    结果缓存,用于加速热门数据的访问速度,Dubbo提供声明式缓存,以减少用户加缓存的工作量. lru 基于最近最少使用原则删除多余缓存,保持最热的数据被缓存. threadlocal 当前线程缓存,比如 ...

  4. Spring Cloud Eureka 分布式开发之服务注册中心、负载均衡、声明式服务调用实现

    介绍 本示例主要介绍 Spring Cloud 系列中的 Eureka,使你能快速上手负载均衡.声明式服务.服务注册中心等 Eureka Server Eureka 是 Netflix 的子模块,它是 ...

  5. spring cloud深入学习(四)-----eureka源码解析、ribbon解析、声明式调用feign

    基本概念 1.Registe 一一服务注册当eureka Client向Eureka Server注册时,Eureka Client提供自身的元数据,比如IP地址.端口.运行状况指标的Uri.主页地址 ...

  6. spring声明式事务管理总结

    事务配置 首先在/WEB-INF/applicationContext.xml添加以下内容: <!-- 配置事务管理器 --> <bean id="transactionM ...

  7. PHP系统声明式事务处理

    转自:http://www.jianshu.com/p/34261804bc45 1.数据库事务 事务(Transaction)是并发控制的基本单位.所谓的事务,它是一个操作序列,这些操作要么都执行, ...

  8. Spring声明式事务配置管理方法

    环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add ...

  9. Spring声明式事务配置管理方法(转)

    项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add libra ...

随机推荐

  1. JVM内存回收区域+对象存活的判断+引用类型+垃圾回收线程

    此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 注意:本文主要参考自<深入理解Java虚拟机(第二版)> 说明:查看本文之前,推荐先知道JVM ...

  2. 知物由学 | AI时代,那些黑客正在如何打磨他们的“利器”?(一)

    本文由  网易云发布. “知物由学”是网易云易盾打造的一个品牌栏目,词语出自汉·王充<论衡·实知>.人,能力有高下之分,学习才知道事物的道理,而后才有智慧,不去求问就不会知道.“知物由学” ...

  3. linux系统下安装Jenkins

    1.首先准备java环境,安装JDK 2.部署jenkins wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redha ...

  4. Weekly Contest 130

    1029. Binary Prefix Divisible By 5 Given an array A of 0s and 1s, consider N_i: the i-th subarray fr ...

  5. Docker registry 私有仓库镜像查询、删除、上传、下载 shell

    #Docker官方私有仓库registry #官方只提供了API接口,不方便使用,就写了个shell #docker-registry安装配置http://www.cnblogs.com/elvi/p ...

  6. 四,mysql优化——sql语句优化之索引二

    1,在什么列适合添加索引 (1)较频繁的作为查询条件字段应该添加索引 select * from emp where empid = 2; (2)唯一性太差的字段不适合添加索引,即时频繁作为查询条件. ...

  7. Python 去除列表中重复的元素

    Python 去除列表中重复的元素 来自比较容易记忆的是用内置的set l1 = ['b','c','d','b','c','a','a'] l2 = list(set(l1)) print l2 还 ...

  8. 《JAVA与模式》之不变模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述不变(Immutable)模式的: 一个对象的状态在对象被创建之后就不再变化,这就是所谓的不变模式. 不变模式的结构 不变模式可增强对象的 ...

  9. 纯css实现不固定行数的文本在一个容器内垂直居中

    项目中要实现的效果如图: html代码 及 css代码: <!DOCTYPE html> <html> <head> <meta charset=" ...

  10. matplotlib基本使用(矩形图、饼图、热力图、3D图)

    使用matplotlib画简单的图形: #-*- coding:utf-8 -*- from numpy.random import randn import matplotlib.pyplot as ...