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

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

推荐阅读更多精彩内容