您好,登錄后才能下訂單哦!
現(xiàn)在用到了分布式框架Dubbo,隨筆謝謝Dubbo入門的實(shí)例
解釋:注冊(cè)中心,服務(wù)注冊(cè)的地方,通俗的說(shuō)就是服務(wù)所在的位置
我這里的是在192.168.2.168上面
需要用到的jar包
這是客服端和服務(wù)端都需要的jar包,我們新建Maven工程。
項(xiàng)目結(jié)構(gòu)圖:
服務(wù)端:
一個(gè)接口(接口中的方法在實(shí)現(xiàn)時(shí)方法名開(kāi)始不能以get開(kāi)頭,莫名報(bào)錯(cuò)):
public interface UserService { public void daoGet(); }
實(shí)現(xiàn)類(這里必須要實(shí)現(xiàn)Seriealizable接口,否則會(huì)出現(xiàn)一個(gè)錯(cuò)誤):
public class UserServiceImpl implements UserService, Serializable { public void daoGet() { System.out.println("This is UserServiceImpl Method"); } }
Dao層實(shí)現(xiàn)類:
public class UserDao { public void testDao() throws Exception { System.out.println("This is testDao Method"); } }
配置文件applicationPrvider.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 "> <dubbo:application name="hello-world" /> <dubbo:registry address="zookeeper://192.168.2.168:2181" /> <!-- Service interface Concurrent Control --> <dubbo:service interface="per.lx.service.UserService"ref="daoService" executes="10" /> <!-- designate implementation --> <bean id="daoService" class="per.lx.service.UserServiceImpl" /> </beans> 在pom.xml中的相關(guān)屬性,列出來(lái)只是為了方便理解客戶端引用服務(wù)端: <groupId>per.lx</groupId> <artifactId>dubbo-Service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dubbo-Service</name>
我的服務(wù)端pom里面沒(méi)有導(dǎo)入的包,我的MAVEN倉(cāng)庫(kù)出了點(diǎn)錯(cuò)誤,所有用導(dǎo)包的方式。
啟動(dòng)Service主函數(shù):
public class Main { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"applicationProvider.xml"}); System.out.println("kaishi"); ctx.start(); System.out.println("任意鍵退出!_____by____lx"); System.in.read(); } }
---------------------------------------------------------------------------
-------------------服務(wù)端寫(xiě)完了,客戶端更簡(jiǎn)單了----------------------------
---------------------------------------------------------------------------
客服端需要的jar包和項(xiàng)目結(jié)構(gòu)與服務(wù)端一致,不過(guò)有一點(diǎn)很重要,需要在客戶端的pom.xml中加入以下代碼方便對(duì)Service的引用:
<dependency> <groupId>per.lx</groupId> <artifactId>dubbo-Service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
這樣是完成了對(duì)Service的引用
客戶端的配置文件applicationConsumer.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 "> <!-- consumer application name --> <dubbo:application name="Client-Test" /> <!-- 這是注冊(cè)中心地址 --> <dubbo:registry address="zookeeper://192.168.2.168:2181"/> <dubbo:consumer timeout="5000" /> <!-- which service to consume? --> <dubbo:reference id="daoService" interface="per.lx.service.UserService"/> </beans>
客戶端完成服務(wù)端方法:
public class ConsumerThd { public void daoService(){ ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext( new String[] {"applicationConsumer.xml"}); context.start(); System.out.println("Client Success"); UserService userService = (UserService) context.getBean("daoService"); userService.daoGet(); } } 主函數(shù):?jiǎn)?dòng)客戶端的方法: public class AppTest { public static void main(String[] args) throws Exception{ ConsumerThd thd=new ConsumerThd(); thd.daoService(); System.in.read(); } } --------------------------------------------------------------------- 到這里會(huì)發(fā)現(xiàn)在服務(wù)器的Console打印下面出來(lái)了我們?cè)诜?wù)端打印的信息,到這里,Dubbo入門基本就完了。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。