美文网首页Dubbo
dubbo 使用学习七(结果缓存)

dubbo 使用学习七(结果缓存)

作者: 安琪拉_4b7e | 来源:发表于2017-11-09 11:31 被阅读0次

dubbo 我们提供给了结果缓存功能,只要进行简单的配置就能实现结果缓存功能!

一、服务提供者

1、服务提供者接口

[java]view plaincopyprint?

packagecom.test.dubboser;

publicinterfaceCacheService {

String findCache(String id);

}

package com.test.dubboser;

public interface CacheService {

String findCache(String id);

}

2、服务提供者接口实现类

[java]view plaincopyprint?

packagecom.test.dubboser;

importjava.util.concurrent.atomic.AtomicInteger;

publicclassCacheServiceImpimplementsCacheService{

privatefinalAtomicInteger i =newAtomicInteger();

publicString findCache(String id) {

// TODO Auto-generated method stub

String result ="request: "+ id +", response: "+ i.getAndIncrement();

System.out.println(result);

returnresult;

}

}

package com.test.dubboser;

import java.util.concurrent.atomic.AtomicInteger;

public class CacheServiceImp implements CacheService{

private final AtomicInteger i = new AtomicInteger();

public String findCache(String id) {

// TODO Auto-generated method stub

String result = "request: " + id + ", response: " + i.getAndIncrement();

System.out.println(result);

return result;

}

}

3、服务端配置文件

[html]view plaincopyprint?

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

">

ref="demoService"/>

ref="demoService2"/>

ref="cacheService"/>

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

">

ref="demoService"/>

ref="demoService2"/>

ref="cacheService"/>

二、服务消费者

1、服务消费者配置文件

[html]view plaincopyprint?

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

">

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配置结果缓存还是很简单的……

2、客户端代码

[java]view plaincopyprint?

packagecom.test.dubbocli;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

importcom.test.dubboser.CacheService;

importcom.test.dubboser.ServiceDemo;

importcom.test.dubboser.ServiceDemo2;

publicclassMain {

publicstaticvoidmain(String[] args)throwsInterruptedException {

run();

}

publicstaticvoidrun()throwsInterruptedException{

ClassPathXmlApplicationContext context =newClassPathXmlApplicationContext(newString[] {"applicationConsumer.xml"});

context.start();

//ServiceDemo demoServer = (ServiceDemo) context.getBean("demoServicemy");

//ServiceDemo2 demoServer2 = (ServiceDemo2) context.getBean("demoServicemy2");

CacheService cacheService=(CacheService)context.getBean("cacheService");

/*ServiceDemo demoServer3 = (ServiceDemo) context.getBean("demoServicemy3");*/

/*String str=demoServer.say("java ---->>>");

String str2=demoServer2.say("java ---->>>");*/

String test=null;

for(inti=0;i<10;i++){

String caches=cacheService.findCache("0");

if(test==null||test.equals(caches)){

System.out.println("i="+i +" ok:"+caches);

}else{

System.err.println("i="+ i +" ERROR: "+ caches);

}

test= caches;

Thread.sleep(500);

}

String caches=cacheService.findCache("1");

System.out.println("last:"+caches);

/*String str3=demoServer3.say("java ---->>>");*/

/*System.err.println("res: "+str);

System.err.println("res: "+str2);*/

//System.err.println("cache res"+caches);

//System.err.println("res: "+str3);

}

}

package com.test.dubbocli;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.dubboser.CacheService;

import com.test.dubboser.ServiceDemo;

import com.test.dubboser.ServiceDemo2;

public class Main {

public static void main(String[] args) throws InterruptedException {

run();

}

public static void run() throws InterruptedException{

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });

context.start();

//ServiceDemo demoServer = (ServiceDemo) context.getBean("demoServicemy");

//ServiceDemo2 demoServer2 = (ServiceDemo2) context.getBean("demoServicemy2");

CacheService cacheService=(CacheService)context.getBean("cacheService");

/*ServiceDemo demoServer3 = (ServiceDemo) context.getBean("demoServicemy3");*/

/*String str=demoServer.say("java ---->>>");

String str2=demoServer2.say("java ---->>>");*/

String test=null;

for(int i=0;i<10;i++){

String caches=cacheService.findCache("0");

if(test==null||test.equals(caches)){

System.out.println("i="+i +" ok:"+caches);

}else{

System.err.println("i=" + i + " ERROR: " + caches);

}

test= caches;

Thread.sleep(500);

}

String caches=cacheService.findCache("1");

System.out.println("last:"+caches);

/*String str3=demoServer3.say("java ---->>>");*/

/*System.err.println("res: "+str);

System.err.println("res: "+str2);*/

//System.err.println("cache res"+caches);

//System.err.println("res: "+str3);

}

}

3、服务端接口实现代码

[java]view plaincopyprint?

packagecom.test.dubboser;

importjava.util.concurrent.atomic.AtomicInteger;

publicclassCacheServiceImpimplementsCacheService{

privatefinalAtomicInteger i =newAtomicInteger();

publicString findCache(String id) {

// TODO Auto-generated method stub

String result ="request: "+ id +", response: "+ i.getAndIncrement();

System.out.println(result);

returnresult;

}

}

package com.test.dubboser;

import java.util.concurrent.atomic.AtomicInteger;

public class CacheServiceImp implements CacheService{

private final AtomicInteger i = new AtomicInteger();

public String findCache(String id) {

// TODO Auto-generated method stub

String result = "request: " + id + ", response: " + i.getAndIncrement();

System.out.println(result);

return result;

}

}

第一次启动服务提供者,消费端运行结果:

[plain]view plaincopyprint?

i=0 ok:request: 0, response: 0

i=1 ok:request: 0, response: 0

i=2 ok:request: 0, response: 0

i=3 ok:request: 0, response: 0

i=4 ok:request: 0, response: 0

i=5 ok:request: 0, response: 0

i=6 ok:request: 0, response: 0

i=7 ok:request: 0, response: 0

i=8 ok:request: 0, response: 0

i=9 ok:request: 0, response: 0

last:request: 1, response: 1

i=0 ok:request: 0, response: 0

i=1 ok:request: 0, response: 0

i=2 ok:request: 0, response: 0

i=3 ok:request: 0, response: 0

i=4 ok:request: 0, response: 0

i=5 ok:request: 0, response: 0

i=6 ok:request: 0, response: 0

i=7 ok:request: 0, response: 0

i=8 ok:request: 0, response: 0

i=9 ok:request: 0, response: 0

last:request: 1, response: 1

服务端没有重新启动,而客户端再次访问结果:

[plain]view plaincopyprint?

i=0 ok:request: 0, response: 2

i=1 ok:request: 0, response: 2

i=2 ok:request: 0, response: 2

i=3 ok:request: 0, response: 2

i=4 ok:request: 0, response: 2

i=5 ok:request: 0, response: 2

i=6 ok:request: 0, response: 2

i=7 ok:request: 0, response: 2

i=8 ok:request: 0, response: 2

i=9 ok:request: 0, response: 2

last:request: 1, response: 3

i=0 ok:request: 0, response: 2

i=1 ok:request: 0, response: 2

i=2 ok:request: 0, response: 2

i=3 ok:request: 0, response: 2

i=4 ok:request: 0, response: 2

i=5 ok:request: 0, response: 2

i=6 ok:request: 0, response: 2

i=7 ok:request: 0, response: 2

i=8 ok:request: 0, response: 2

i=9 ok:request: 0, response: 2

last:request: 1, response: 

愿意了解或者源码的朋友直接求求交流分享技术:2042849237

更多详细源码参考来源:http://minglisoft.cn/technology

相关文章

  • dubbo 使用学习七(结果缓存)

    dubbo 我们提供给了结果缓存功能,只要进行简单的配置就能实现结果缓存功能! 一、服务提供者 1、服务提供者接口...

  • dubbo结果缓存机制

    此文已由作者赵计刚授权网易云社区发布。 欢迎访问网易云社区,了解更多网易技术产品运营经验。 dubbo提供了三种结...

  • dubbo盲点

    在 dubbo 中使用 Threadlocal 的相关问题 - 为程序员服务 dubbo缓存类型 lru基于最近最...

  • [目录]Dubbo 源码学习

    学习Dubbo的一些心得体会 Dubbo版本 2.5.3 Dubbo 简要介绍和使用 学习(一) Dubbo源码学...

  • Redis初探

    最近接触阿里的dubbo, 之前也没有遇到过集群的项目。突然想到缓存该如何处理,如果使用EHCache就是单点缓存...

  • 浏览器缓存策略

    浏览器每次发起请求时,先在本地缓存中查找结果以及缓存标识,根据缓存标识来判断是否使用本地缓存。如果缓存有效,则使用...

  • dubbo源码系列之filter的前生

    为什么说dubbo的声明式缓存不好用!!! dubbo源码系列之filter的今世 dubbo的filter类似于...

  • Dubbo SPI机制

    一、前言 学习dubbo源码时,Dubbo的SPI机制必须先了解,不然阅读Dubbo源码会非常困难 二、使用 1....

  • Dubbo提供者信息缓存

    dubbo本地缓存提供者信息 Dubbo调用服务方的地址并不是每次都到注册中心去获取,而是会对调用地址做本地缓存,...

  • dubbo积累:dubbo十层模型

    (一)前言 因为公司框架使用了dubbo作为RPC框架,所以,对于dubbo进行相应的学习,通过官网的学习,源码的...

网友评论

    本文标题:dubbo 使用学习七(结果缓存)

    本文链接:https://www.haomeiwen.com/subject/gzspmxtx.html