dubbo中的缓存

/ Dubbo / 没有评论 / 684浏览

dubbo在客户端调用时可以对结果进行缓存从而加快热门数据的访问速度,并且dubbo提供了声明式缓存,也就是配置个参数就可以了。


在dubbo中有3种类型的缓存下面我们看一下它们的区别。


下面我们来验证一下dubbo中的缓存功能。我们已lur缓存为例,下面为具体的代码。

HelloWorldImpl:

/**
 * 接口实现类
 */
@Service
public class HelloWorldImpl implements HelloWorld {
    public String getHelloWorld() {
        System.out.println(new SimpleDateFormat("yyyy-mm-dd HH🇲🇲ss").format(new Date()));
        return "Hello World Dubbo";
    }

}

因为我们要测试缓存功能,所以我们在实现类中输出了时间戳,这样方便我们验证缓存是否有效。

provider.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方信息-->
    <dubbo:application name="jilinwula"/>

    <!-- 使用zookeeper暴露服务地址 -->
    <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper"/>

    <!-- 暴露服务端口 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 声明需要暴露的服务接⼝ -->
    <dubbo:service interface="com.jilinwula.api.HelloWorld" ref="helloWorldImpl"/>

    <!-- component-scan自动扫描注解 -->
    <context:component-scan base-package="com.jilinwula.*"/>

</beans>

因为dubbo中的缓存功能是在调用方配置的,所以服务提供者的配置不用添加任何缓存设置。

consumer.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="jilinwula"/>

    <!-- 使用zookeeper查找服务地址 -->
    <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper"/>

    <!-- Dubbo自动生成远程服务代理 -->

    <dubbo:reference id="helloWorld" interface="com.jilinwula.api.HelloWorld" cache="lru"/>

</beans>

我们在配置文件中添加了cache属性,并指定了缓存类型为lru类型。下面我们测试一下,看一下缓存效果。

Consumer:

/**
 * 服务调用方
 */
public class Consumer {

  public static void main(String[] args) throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
        new String[]{"consumer.xml"});
    context.start();

    HelloWorld helloWorld = context.getBean("helloWorld", HelloWorld.class);

    while (true) {
      System.out.println(helloWorld.getHelloWorld());
      Thread.sleep(1000);
    }

  }
}

服务提供者日志输出:

2018-07-31 21:23:57:882DEBUG [ProductBossService,org.springframework.beans.factory.support.AbstractBeanFactory,251]: Returning cached instance of singleton bean 'lifecycleProcessor'
2018-24-31 21:24:12
2018-07-31 21:24:34:723 WARN [ProductBossService,com.alibaba.dubbo.remoting.transport.AbstractServer,200]:  [DUBBO] All clients has discontected from /192.168.0.4:20880. You can graceful shutdown now., dubbo version: 2.6.0, current host: 127.0.0.1

服务调用者日志输出:

2018-07-31 21:24:12:420 INFO [ProductBossService,com.alibaba.dubbo.config.ReferenceConfig,425]:  [DUBBO] Refer dubbo service com.jilinwula.api.HelloWorld from url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=jilinwula&cache=lru&check=false&dubbo=2.6.0&generic=false&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=2343®ister.ip=192.168.0.4&remote.timestamp=1533043436848&side=consumer×tamp=1533043451164, dubbo version: 2.6.0, current host: 192.168.0.4
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
2018-07-31 21:24:34:711 INFO [ProductBossService,com.alibaba.dubbo.config.AbstractConfig$1,80]:  [DUBBO] Run shutdown hook now., dubbo version: 2.6.0, current host: 192.168.0.4

我们看日志输出,虽然调用者调用了多次接口方法,但服务提供者时间戳只输出了一次,这就说明缓存已经生效了,否则服务提供者时间戳会输出的次数会和服务调用者调用接口的次数相同,上述的次数明显不同。下面我们将consumer.xml文件中的cache参数去掉在看一下日志输出。

consumer.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="jilinwula"/>

    <!-- 使用zookeeper查找服务地址 -->
    <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper"/>

    <!-- Dubbo自动生成远程服务代理 -->

    <dubbo:reference id="helloWorld" interface="com.jilinwula.api.HelloWorld"/>

</beans>

服务提供者日志输出:

2018-07-31 21:37:46:155DEBUG [ProductBossService,org.springframework.beans.factory.support.AbstractBeanFactory,251]: Returning cached instance of singleton bean 'lifecycleProcessor'
2018-37-31 21:37:54
2018-37-31 21:37:55
2018-37-31 21:37:56
2018-37-31 21:37:57
2018-07-31 21:37:58:100 WARN [ProductBossService,com.alibaba.dubbo.remoting.transport.AbstractServer,200]:  [DUBBO] All clients has discontected from /192.168.0.4:20880. You can graceful shutdown now., dubbo version: 2.6.0, current host: 127.0.0.1

服务调用者日志输出:

2018-07-31 21:37:53:702 INFO [ProductBossService,com.alibaba.dubbo.config.ReferenceConfig,425]:  [DUBBO] Refer dubbo service com.jilinwula.api.HelloWorld from url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=jilinwula&check=false&dubbo=2.6.0&generic=false&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=2374®ister.ip=192.168.0.4&remote.timestamp=1533044264820&side=consumer×tamp=1533044272398, dubbo version: 2.6.0, current host: 192.168.0.4
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
Hello World Dubbo
2018-07-31 21:37:58:076 INFO [ProductBossService,com.alibaba.dubbo.config.AbstractConfig$1,80]:  [DUBBO] Run shutdown hook now., dubbo version: 2.6.0, current host: 192.168.0.4

由于服务调用者调用接口的次数和服务提供者时间戳的次数相同,所以说明dubbo在服务调用时没有使用缓存。


下面我们修改一下接口的实现使接口每次返回的数据都是不同的,来在一次验证dubbo中的缓存功能。

HelloWorldImpl:

/**
 * 接口实现类
 */
@Service
public class HelloWorldImpl implements HelloWorld {

  public String getHelloWorld() {
    System.out.println(new SimpleDateFormat("yyyy-mm-dd HH🇲🇲ss").format(new Date()));
    return String.format("Hello World Dubbo Time: %s", System.currentTimeMillis());
  }

}

我们在接口返回的时候添加了时间戳,以此来演示接口每次返回不同的数据。现在我们不添加缓存参数来看一下日志输出。

服务提供者日志输出:

2018-07-31 22:08:32:403 INFO [ProductBossService,com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1,120]:  [DUBBO] disconected from /192.168.0.4:60401,url:dubbo://192.168.0.4:20880/com.jilinwula.api.HelloWorld?anyhost=true&application=jilinwula&bind.ip=192.168.0.4&bind.port=20880&channel.readonly.sent=true&codec=dubbo&dubbo=2.6.0&generic=false&heartbeat=60000&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=2469&side=provider×tamp=1533046081794, dubbo version: 2.6.0, current host: 127.0.0.1
2018-08-31 22:08:40
2018-08-31 22:08:41
2018-08-31 22:08:42
2018-08-31 22:08:43
2018-07-31 22:08:43:658 WARN [ProductBossService,com.alibaba.dubbo.remoting.transport.AbstractServer,200]:  [DUBBO] All clients has discontected from /192.168.0.4:20880. You can graceful shutdown now., dubbo version: 2.6.0, current host: 127.0.0.1

服务调用者日志输出:

2018-07-31 22:08:40:312 INFO [ProductBossService,com.alibaba.dubbo.config.ReferenceConfig,425]:  [DUBBO] Refer dubbo service com.jilinwula.api.HelloWorld from url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=jilinwula&check=false&dubbo=2.6.0&generic=false&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=2476®ister.ip=192.168.0.4&remote.timestamp=1533046081794&side=consumer×tamp=1533046118922, dubbo version: 2.6.0, current host: 192.168.0.4
Hello World Dubbo Time: 1533046120558
Hello World Dubbo Time: 1533046121583
Hello World Dubbo Time: 1533046122591
Hello World Dubbo Time: 1533046123601
2018-07-31 22:08:43:639 INFO [ProductBossService,com.alibaba.dubbo.config.AbstractConfig$1,80]:  [DUBBO] Run shutdown hook now., dubbo version: 2.6.0, current host: 192.168.0.4

我们看服务调用者日志接口返回的信息是不同的。下面我们将缓存参数添加上在看一下日志输出。

服务提供者日志输出:

2018-07-31 22:23:12:487 INFO [ProductBossService,com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1,120]:  [DUBBO] disconected from /192.168.0.4:60592,url:dubbo://192.168.0.4:20880/com.jilinwula.api.HelloWorld?anyhost=true&application=jilinwula&bind.ip=192.168.0.4&bind.port=20880&channel.readonly.sent=true&codec=dubbo&dubbo=2.6.0&generic=false&heartbeat=60000&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=2501&side=provider×tamp=1533046977714, dubbo version: 2.6.0, current host: 127.0.0.1
2018-23-31 22:23:23
2018-07-31 22:23:27:470 WARN [ProductBossService,com.alibaba.dubbo.remoting.transport.AbstractServer,200]:  [DUBBO] All clients has discontected from /192.168.0.4:20880. You can graceful shutdown now., dubbo version: 2.6.0, current host: 127.0.0.1

服务调用者日志输出:

2018-07-31 22:23:23:419 INFO [ProductBossService,com.alibaba.dubbo.config.ReferenceConfig,425]:  [DUBBO] Refer dubbo service com.jilinwula.api.HelloWorld from url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=jilinwula&cache=lru&check=false&dubbo=2.6.0&generic=false&interface=com.jilinwula.api.HelloWorld&methods=getHelloWorld&pid=2507®ister.ip=192.168.0.4&remote.timestamp=1533046977714&side=consumer×tamp=1533047001913, dubbo version: 2.6.0, current host: 192.168.0.4
Hello World Dubbo Time: 1533047003743
Hello World Dubbo Time: 1533047003743
Hello World Dubbo Time: 1533047003743
Hello World Dubbo Time: 1533047003743
2018-07-31 22:23:27:458 INFO [ProductBossService,com.alibaba.dubbo.config.AbstractConfig$1,80]:  [DUBBO] Run shutdown hook now., dubbo version: 2.6.0, current host: 192.168.0.4

我们看服务调用者日志服务一共调用了4次但这4次接口返回的信息居然是一样的,这是因为我们配置了缓存,所以无论接口调用多少次,返回的结果均是接口第一次调用的结果,也就是把接口第一次返回的结果缓存了。


这就是dubbo中lur类型的缓存的功能,有关其它缓存类型在后续的文章中在做介绍。