基礎篇--簡單的Dubbo提供者provider與消費者consumer

一、提供方Provider

1.接口


[java] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. public interface DemoService {  

  2.   

  3.     String sayHello(String name);  

  4.   

  5.     public List getUsers();  

  6.   

  7. }  


2.實現類



[java] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. public class DemoServiceImpl implements DemoService {  

  2.   

  3.     public String sayHello(String name) {  

  4.         return "Hello " + name;  

  5.     }  

  6.   

  7.     public List getUsers() {  

  8.         List list = new ArrayList();  

  9.         User u1 = new User();  

  10.         u1.setName("jack");  

  11.         u1.setAge(20);  

  12.         u1.setSex("m");  

  13.   

  14.         User u2 = new User();  

  15.         u2.setName("tom");  

  16.         u2.setAge(21);  

  17.         u2.setSex("m");  

  18.   

  19.         User u3 = new User();  

  20.         u3.setName("rose");  

  21.         u3.setAge(19);  

  22.         u3.setSex("w");  

  23.   

  24.         list.add(u1);  

  25.         list.add(u2);  

  26.         list.add(u3);  

  27.         return list;  

  28.     }  

  29. }  

3.model類



[java] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. /** 

  2.  * 實現序列化 

  3.  * @author Administrator 

  4.  * 

  5.  */  

  6. public class User implements Serializable {  

  7.     private static final long serialVersionUID = 1L;  

  8.     private int age;  

  9.     private String name;  

  10.     private String sex;  

  11.   

  12.     public User() {  

  13.         super();  

  14.     }  

  15.   

  16.     public User(int age, String name, String sex) {  

  17.         super();  

  18.         this.age = age;  

  19.         this.name = name;  

  20.         this.sex = sex;  

  21.     }  

  22.   

  23.     public int getAge() {  

  24.         return age;  

  25.     }  

  26.   

  27.     public void setAge(int age) {  

  28.         this.age = age;  

  29.     }  

  30.   

  31.     public String getName() {  

  32.         return name;  

  33.     }  

  34.   

  35.     public void setName(String name) {  

  36.         this.name = name;  

  37.     }  

  38.   

  39.     public String getSex() {  

  40.         return sex;  

  41.     }  

  42.   

  43.     public void setSex(String sex) {  

  44.         this.sex = sex;  

  45.     }  

  46.   

  47. }  

4.applicationContenxt.xml配置

注意:提供方需要指定端口,不同提供方需要使用不同端口,不然會有端口衝突,使用的端口需要在防火牆iptables中配置允許通過

消費方不用指定端口


[html] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <beans xmlns="http://www.springframework.org/schema/beans"  

  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  

  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  

  5.         http://www.springframework.org/schema/beans/spring-beans.xsd  

  6.         http://code.alibabatech.com/schema/dubbo  

  7.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  

  8.         ">  

  9.   

  10.     <!-- 提供方應用信息,用於計算依賴關係 -->  

  11.     <dubbo:application name="demo_provider" />  

  12.   

  13.     <!-- 使用zookeeper註冊中心暴露服務地址 -->  

  14.     <dubbo:registry address="zookeeper://192.168.1.121:2181" />  

  15.   

  16.     <!-- 用dubbo協議在20880端口暴露服務 -->  

  17.     <dubbo:protocol name="dubbo" port="20880" />  

  18.   

  19.     <!-- 聲明需要暴露的服務接口 -->  

  20.     <dubbo:service interface="com.unj.dubbotest.provider.DemoService"  

  21.         ref="demoService" />  

  22.           

  23.           

  24.     <!-- 具體的實現bean,用來注入-->  

  25.     <bean id="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />  

  26.   

  27. </beans>  

5.提供方Provider測試方法



[java] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. public class Provider {  

  2.   

  3.     public static void main(String[] args) throws Exception {  

  4.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  

  5.                 new String[] { "applicationContext.xml" });  

  6.         context.start();  

  7.         System.in.read(); // 爲保證服務一直開着,利用輸入流的阻塞來模擬  

  8.     }  

  9. }  


運行結果:




6.在Dubbo管理控制檯上查看發佈的提供方



二、消費方Consume

1.引用提供方的接口

2.消費方applictionContenxt.xml配置


[html] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <beans xmlns="http://www.springframework.org/schema/beans"  

  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  

  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  

  5.         http://www.springframework.org/schema/beans/spring-beans.xsd  

  6.         http://code.alibabatech.com/schema/dubbo  

  7.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  

  8.         ">  

  9.   

  10.     <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方一樣 -->  

  11.     <dubbo:application name="hehe_consumer" />  

  12.   

  13.     <!-- 使用zookeeper註冊中心暴露服務地址 -->  

  14.     <dubbo:registry address="zookeeper://192.168.1.121:2181" />  

  15.   

  16.     <!-- 生成遠程服務代理,可以像使用本地bean一樣使用demoService -->  

  17.     <dubbo:reference id="demoService"  

  18.         interface="com.unj.dubbotest.provider.DemoService" />  

  19.   

  20. </beans>  

3.消費方調用提供方



[java] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. public class Consumer {  

  2.   

  3.     public static void main(String[] args) throws Exception {  

  4.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  

  5.                 new String[] { "applicationContext.xml" });  

  6.         context.start();  

  7.   

  8.         DemoService demoService = (DemoService) context.getBean("demoService");  

  9.         String hello = demoService.sayHello("tom");  

  10.         System.out.println(hello);  

  11.   

  12.         List list = demoService.getUsers();  

  13.         if (list != null && list.size() > 0) {  

  14.             for (int i = 0; i < list.size(); i++) {  

  15.                 System.out.println(list.get(i));  

  16.             }  

  17.         }  

  18.         System.in.read();  

  19.     }  

  20.   

  21. }  


控制檯信息輸出




dubbo管理控制檯查看



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