dubbo配置及使用

一. Dubbo是什麼?
Dubbo是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。簡單的說,dubbo就是個服務框架,如果沒有分佈式的需求,其實是不需要用的,只有在分佈式的時候,纔有dubbo這樣的分佈式服務框架的需求,並且本質上是個服務調用的東東,說白了Dubbo就是個遠程服務調用的分佈式框架(告別Web Service模式中的WSdl,以服務者與消費者的方式在dubbo上註冊)
其核心部分包含:
1. 遠程通訊: 提供對多種基於長連接的NIO框架抽象封裝,包括多種線程模型,序列化,以及“請求-響應”模式的信息交換方式。
2. 集羣容錯: 提供基於接口方法的透明遠程過程調用,包括多協議支持,以及軟負載均衡,失敗容錯,地址路由,動態配置等集羣支持。
3. 自動發現: 基於註冊中心目錄服務,使服務消費方能動態的查找服務提供方,使地址透明,使服務提供方可以平滑增加或減少機器。

二. Dubbo能做什麼?
1.透明化的遠程方法調用,就像調用本地方法一樣調用遠程方法,只需簡單配置,沒有任何API侵入。      
2.軟負載均衡及容錯機制,可在內網替代F5等硬件負載均衡器,降低成本,減少單點。
3. 服務自動註冊與發現,不再需要寫死服務提供方地址,註冊中心基於接口名查詢服務提供者的IP地址,並且能夠平滑添加或刪除服務提供者。

三. Dubbo的架構
節點角色說明:
Provider: 暴露服務的服務提供方。
Consumer: 調用遠程服務的服務消費方。
Registry: 服務註冊與發現的註冊中心。
Monitor: 統計服務的調用次調和調用時間的監控中心。
Container: 服務運行容器。


dubbo配置及使用

package com.unj.dubbotest.provider;
  
import java.util.List;
  
public interface DemoService {
  
    String sayHello(String name);
	
    public List getUsers();  
  
} 
實現類

package com.unj.dubbotest.provider;  
  
import java.util.ArrayList;  
import java.util.LinkedList;  
import java.util.List;    
  
public class DemoServiceImpl implements DemoService{  
      
     public String sayHello(String name) {  
            return "Hello " + name;  
     }  
     public List getUsers() {  
         List list = new ArrayList();  
         User u1 = new User();  
         u1.setName("jack");  
         u1.setAge(20);  
         u1.setSex("男"); 
         list.add(u1);  
         return list;  
     }  
}  
配置dubbo-provider.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">
	   
	<bean id="demoService" class="com.unj.dubbotest.provider.DemoServiceImpl" />

    <!-- 提供方應用信息,用於計算依賴關係 -->
    <dubbo:application name="xixi_provider"  />
    <!-- 使用zookeeper廣播註冊中心暴露服務地址 -->
    <dubbo:registry protocol="zookeeper" address="${zookeeper.address}" />
    <!-- 增加監控 -->
    <dubbo:monitor  protocol="registry" />
    <!-- 端口 -->
    <dubbo:protocol name="dubbo" port="20880"/>	
    
    <dubbo:service interface="com.unj.dubbotest.provider.DemoService" ref="demoService" version="1.0" timeout="5000"/>  

</beans>

配置dubbo-consumer.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="hehe_consumer" />  
	<!-- 使用zookeeper廣播註冊中心暴露服務地址 -->
	<dubbo:registry protocol="zookeeper" address="${zookeeper.address}"/>
	<dubbo:consumer timeout="30000" check="false"/>

	<dubbo:reference id="demoService" interface="com.unj.dubbotest.provider.DemoService" version="1.0"/>   
</beans>

參考文章:http://www.cnblogs.com/lilixin/p/5724976.html

http://blog.csdn.net/xlgen157387/article/details/51865289

本地調試

	<dubbo:reference id="demoService" interface="com.unj.dubbotest.provider.DemoService" url="dubbo://localhost:20880" version="1.0.0"/>


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