Dubbo入門實例

現在用到了分佈式框架Dubbo,隨筆謝謝Dubbo入門的實例

解釋:註冊中心,服務註冊的地方,通俗的說就是服務所在的位置

我這裏的是在192.168.2.168上面

需要用到的jar包

wKioL1fRBvrQVLeHAABAaWD_o6I131.png-wh_50

這是客服端和服務端都需要的jar包,我們新建Maven工程。

項目結構圖:

wKiom1fRB__jF9qzAAA8rLV5NqI380.png-wh_50

服務端:

    一個接口(接口中的方法在實現時方法名開始不能以get開頭,莫名報錯):

    

    public interface UserService {
	public void daoGet();
}

    

    實現類(這裏必須要實現Seriealizable接口,否則會出現一個錯誤):

    

public class UserServiceImpl implements UserService, Serializable {

	public void daoGet() {
		System.out.println("This is UserServiceImpl Method");
	}

}


    Dao層實現類:

    

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中的相關屬性,列出來只是爲了方便理解客戶端引用服務端:
 <groupId>per.lx</groupId>
  <artifactId>dubbo-Service</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>dubbo-Service</name>

我的服務端pom裏面沒有導入的包,我的MAVEN倉庫出了點錯誤,所有用導包的方式。

啓動Service主函數:

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();
	}
}

---------------------------------------------------------------------------

-------------------服務端寫完了,客戶端更簡單了----------------------------

---------------------------------------------------------------------------


客服端需要的jar包和項目結構與服務端一致,不過有一點很重要,需要在客戶端的pom.xml中加入以下代碼方便對Service的引用:

<dependency>
   	<groupId>per.lx</groupId>
   	<artifactId>dubbo-Service</artifactId>
   	<version>0.0.1-SNAPSHOT</version>
   </dependency>

這樣是完成了對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" />
    <!-- 這是註冊中心地址 -->
    <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>


客戶端完成服務端方法:

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();
	}
}


主函數:啓動客戶端的方法:
public class AppTest {
	public static void main(String[] args) throws Exception{
		ConsumerThd thd=new ConsumerThd();
		thd.daoService();
		System.in.read();
	}
}
---------------------------------------------------------------------
到這裏會發現在服務器的Console打印下面出來了我們在服務端打印的信息,到這裏,Dubbo入門基本就完了。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章