Posted 鍏ㄦ爤娴嬭瘯绗旇
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
Dubbo鏄竴娆鹃珮鎬ц兘銆佽交閲忕骇鐨勫紑婧怞ava RPC妗嗘灦銆?br>
寰堝鏈嬪弸鎯崇粌涔燿ubbo鎺ュ彛娴嬭瘯锛屼絾鏄嫤浜庢病鏈夋帴鍙h€屾斁寮冿紝涓嬮潰鎴戝氨甯﹀ぇ瀹跺紑鍙戜竴涓畝鍗曠殑dubbo鎺ュ彛銆?/p>
鎺ュ彛闇€姹?/strong>
瀹㈡埛绔緭鍏ncleyong锛堝綋鐒讹紝涔熷彲浠ヨ緭鍏ュ叾瀹冨瓧绗︿覆锛夛紝鏈嶅姟绔繑鍥瀐ello uncleyong
寮€鍙戠幆澧?/strong>
jdk + idea + maven + zookeeper
jdk瀹夎锛歨ttps://www.cnblogs.com/uncleyong/p/10732951.html
idea瀹夎锛歨ttps://www.cnblogs.com/uncleyong/p/10971923.html
maven瀹夎锛歨ttps://www.cnblogs.com/uncleyong/p/10743181.html
zookeeper瀹夎锛歨ttps://www.cnblogs.com/uncleyong/p/10737119.html
common寮€鍙?/strong>
idea涓垱寤烘ā鍧梔ubbo-common
瀛樻斁鍏叡鐨勫疄浣撶被銆佹帴鍙?/p>
package com.uncleyong.dubbotest.service;
public interface SayHelloToClient {
public String sayHello(String name);
}
鐒跺悗mvn install鎵撳寘锛屼緵provider鍙奵onsumer鍦╬om鏂囦欢涓紩鍖呫€€
provider寮€鍙?/strong>
idea涓垱寤烘ā鍧梔ubbo_provider
鍒涘缓瀹炵幇绫?/p>
package com.uncleyong.dubbotest.service.impl;
import com.uncleyong.dubbotest.service.SayHelloToClient;
public class SayHelloToClientImpl implements SayHelloToClient {
public String sayHello(String name){
System.out.println("from client 锛? + name);
return "hello, " + name;
}
}
閰嶇疆鏂囦欢锛宲rovider.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-provider"/>
<!-- 浣跨敤zookeeper骞挎挱娉ㄥ唽涓績鏆撮湶鏈嶅姟鍦板潃 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 鐢╠ubbo鍗忚鍦?0880绔彛鏆撮湶鏈嶅姟 -->
<dubbo:protocol name="dubbo" port="20888"/>
<!-- 澹版槑闇€瑕佹毚闇茬殑鏈嶅姟鎺ュ彛 -->
<dubbo:service interface="com.uncleyong.dubbotest.service.SayHelloToClient" ref="sayhellotoclient"/>
<!-- 鍜屾湰鍦癰ean涓€鏍峰疄鐜版湇鍔?-->
<bean id="sayhellotoclient" class="com.uncleyong.dubbotest.service.impl.SayHelloToClientImpl"/>
</beans>
鍒涘缓涓昏繍琛屾枃浠讹紝ProviderMain
package com.uncleyong.dubbotest.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ProviderMain {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"provider.xml"});
context.start();
System.out.println("娉ㄥ唽鎴愬姛锛屽鎯抽€€鍑猴紝鎸変换鎰忛敭閫€鍑?);
System.in.read(); // 鎸変换鎰忛敭閫€鍑?/span>
}
}
consumer寮€鍙?/strong>
idea涓垱寤烘ā鍧梔ubbo_consumer
涓昏繍琛屾枃浠?/p>
package com.uncleyong.dubbotest.main;
import com.uncleyong.dubbotest.service.SayHelloToClient;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Scanner;
public class ConsumerMain {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"consumer.xml"});
context.start();
// 鑾峰彇杩滅▼鏈嶅姟浠g悊
SayHelloToClient say = (SayHelloToClient) context.getBean("sayhellotoclient");
// 鎵ц杩滅▼鏂规硶
String res = say.sayHello("UncleYong");
// 鏄剧ず璋冪敤缁撴灉
System.out.println(res);
new Scanner(System.in).next();
}
}
閰嶇疆鏂囦欢锛宑onsumer.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-consumer"/>
<!-- 浣跨敤zookeeper骞挎挱娉ㄥ唽涓績鏆撮湶鍙戠幇鏈嶅姟鍦板潃 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 鐢熸垚杩滅▼鏈嶅姟浠g悊锛屽彲浠ュ拰鏈湴bean涓€鏍蜂娇鐢╠emoService -->
<dubbo:reference id="sayhellotoclient" interface="com.uncleyong.dubbotest.service.SayHelloToClient"/>
</beans>
杩愯缁撴灉
鍏堝惎鍔▃ookeeper锛岃繘鍏ookeeper鐨刡in鐩綍锛岀偣鍑汇€恴kServer.cmd銆?/p>
鍚姩provider
鏈嶅姟娉ㄥ唽鎴愬姛
鍚姩consumer锛屽彲浠ョ湅鍒拌緭鍑轰簡缁撴灉
鍦╬rovider绔紝涔熷彲浠ョ湅鍒板鎴风鍙戣繃鏉ョ殑娑堟伅
鑷虫锛屽紑鍙戝畬鎴愶紝涓嬩竴绡囷紝鍒嗕韩鐢╦meter娴嬭瘯dubbo鎺ュ彛銆?/p>
鎶涚爾寮曠帀锛屾杩庝氦娴侊紱
澹版槑锛氬皝闈㈠浘鐗囨潵婧愪簬缃戠粶锛屽鏈変镜鏉冿紝璇疯仈绯诲垹闄ゃ€?/span>
END
鏄熸爣|鍦ㄧ湅|鐣欒█|杞彂鍒嗕韩锛岄搧绮?/strong>
闀挎寜浜岀淮鐮佸叧娉紝绗竴鏃堕棿鑾峰彇骞茶揣
鐐瑰嚮銆?/span>闃呰鍘熸枃銆?/span>
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章