dubbo 我們提供給了結(jié)果緩存功能爪瓜,只要進(jìn)行簡單的配置就能實(shí)現(xiàn)結(jié)果緩存功能备燃!
一成箫、服務(wù)提供者
1酱床、服務(wù)提供者接口
[java]view plaincopyprint?
packagecom.test.dubboser;
publicinterfaceCacheService?{
String?findCache(String?id);
}
package com.test.dubboser;
public interface CacheService {
String findCache(String id);
}
2屉栓、服務(wù)提供者接口實(shí)現(xiàn)類
[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舷蒲、服務(wù)端配置文件
[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"/>
二、服務(wù)消費(fèi)者
1友多、服務(wù)消費(fèi)者配置文件
[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配置結(jié)果緩存還是很簡單的……
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、服務(wù)端接口實(shí)現(xiàn)代碼
[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;
}
}
第一次啟動(dòng)服務(wù)提供者域滥,消費(fèi)端運(yùn)行結(jié)果:
[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
服務(wù)端沒有重新啟動(dòng)纵柿,而客戶端再次訪問結(jié)果:
[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:?
愿意了解或者源碼的朋友直接求求交流分享技術(shù):2042849237
更多詳細(xì)源碼參考來源:http://minglisoft.cn/technology