共計(jì) 3063 個(gè)字符,預(yù)計(jì)需要花費(fèi) 8 分鐘才能閱讀完成。
這篇文章主要講解了“基于 dubbo protocol 的 demo 怎么實(shí)現(xiàn)”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著丸趣 TV 小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“基于 dubbo protocol 的 demo 怎么實(shí)現(xiàn)”吧!
說(shuō)明:本文基于 redhat6 做的 demo,jdk 等各種環(huán)境都已經(jīng)就緒了。建議 java 開(kāi)發(fā)者了解 linux 系統(tǒng)。
demo 項(xiàng)目如圖:
運(yùn)行結(jié)果如圖:
2、新建接口服務(wù):com.enson.demo.service.DubboService
代碼:
package com.enson.demo.service;
public interface DubboService {public String printWord(String word);
}
3、實(shí)現(xiàn)接口:com.enson.demo.service.impl.DubboServiceImpl
代碼:
package com.enson.demo.service.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.enson.demo.service.DubboService;
public class DubboServiceImpl implements DubboService{
@Override
public String printWord(String word) {String outWord = new SimpleDateFormat( [yyyy-MM-dd HH:mm:ss ] )
.format(new Date()) + word;
System.err.println(outWord);
return outWord;
}
4、創(chuàng)建 provider.xml
?xml version= 1.0
5、創(chuàng)建 test.Provider 類,作為服務(wù)注冊(cè)的入口:
package test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {provider.xml});
context.start();
System.in.read(); // 按任意鍵退出}
以上完成了服務(wù)提供者的編寫(xiě)。
6、創(chuàng)建 com.enson.custom.action.DubboAction
package com.enson.custom.action;
import com.enson.demo.service.DubboService;
public class DubboAction {
public DubboService dubboService ;
//Spring 方法注入
public void setDubboService(DubboService dubboService) {
this.dubboService = dubboService;
public String printWord(){
System.out.println( action printWord 被執(zhí)行!return this.dubboService.printWord(Hello World // 執(zhí)行遠(yuǎn)程方法}
7、創(chuàng)建 cstom.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 !-- 消費(fèi)方應(yīng)用名,用于計(jì)算依賴關(guān)系,不是匹配條件,不要與提供方一樣 -- dubbo:application name= dubbo-custom-app / !-- 使用 zookeeper 注冊(cè)中心暴露發(fā)現(xiàn)服務(wù)地址 -- dubbo:registry address= zookeeper://127.0.0.1:2181 / !-- 生成遠(yuǎn)程服務(wù)代理,可以和本地 bean 一樣使用 dubboService -- dubbo:reference id= dubboService interface= com.enson.demo.service.DubboService / !-- 普通的 spring bean 的定義 -- bean id= dubboAction >
8、創(chuàng)建消費(fèi)者類:test.Custompackage test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.enson.custom.action.DubboAction; public class Custom {public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { consumer.xml }); context.start(); DubboAction dubboAction = (DubboAction) context.getBean( dubboAction // 獲取遠(yuǎn)程服務(wù)代理 String word = dubboAction.printWord();// 執(zhí)行遠(yuǎn)程方法 System.out.println(word); // 顯示調(diào)用結(jié)果 }測(cè)試:
1、啟動(dòng) zookeeper。
2、執(zhí)行 Provider。Run as java application
3、執(zhí)行 Custom。Run as java application
查看控制臺(tái)即可。
感謝各位的閱讀,以上就是“基于 dubbo protocol 的 demo 怎么實(shí)現(xiàn)”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì) 基于 dubbo protocol 的 demo 怎么實(shí)現(xiàn)這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!