dubbo 使用學(xué)習(xí)七(結(jié)果緩存)

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市启绰,隨后出現(xiàn)的幾起案子昂儒,更是在濱河造成了極大的恐慌,老刑警劉巖委可,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件渊跋,死亡現(xiàn)場離奇詭異,居然都是意外死亡着倾,警方通過查閱死者的電腦和手機(jī)拾酝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來卡者,“玉大人蒿囤,你說我怎么就攤上這事』⒄#” “怎么了蟋软?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀的道長嗽桩。 經(jīng)常有香客問我岳守,道長,這世上最難降的妖魔是什么碌冶? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任湿痢,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘譬重。我一直安慰自己拒逮,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布臀规。 她就那樣靜靜地躺著滩援,像睡著了一般。 火紅的嫁衣襯著肌膚如雪塔嬉。 梳的紋絲不亂的頭發(fā)上玩徊,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天,我揣著相機(jī)與錄音谨究,去河邊找鬼恩袱。 笑死,一個(gè)胖子當(dāng)著我的面吹牛胶哲,可吹牛的內(nèi)容都是我干的畔塔。 我是一名探鬼主播,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼鸯屿,長吁一口氣:“原來是場噩夢啊……” “哼澈吨!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起寄摆,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤棚辽,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后冰肴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡榔组,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年熙尉,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片搓扯。...
    茶點(diǎn)故事閱讀 37,997評論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡检痰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出锨推,到底是詐尸還是另有隱情铅歼,我是刑警寧澤,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布换可,位于F島的核電站椎椰,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏沾鳄。R本人自食惡果不足惜慨飘,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧瓤的,春花似錦休弃、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至稽坤,卻和暖如春丈甸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背慎皱。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工老虫, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人茫多。 一個(gè)月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓祈匙,卻偏偏與公主長得像,于是被迫代替她去往敵國和親天揖。 傳聞我的和親對象是個(gè)殘疾皇子夺欲,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容