分布式服務框架Dubbo總結-0x01 簡介

Dubbo是什么


Dubbo是一個分布式服務框架,致力于提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。。

為什么要用Dubbo


當網站變大,業務量飛速增長,需要對應用進行拆分,服務化,以提高項目的可維護性以及開發效率。但是當服務的數量多到一定程度時,服務之間的依賴,地址的配置與管理就變得復雜。服務之間的均衡負載問題也顯得尤為突出,這種時候,正需要dubbo來解決

dubbo常見配置解析

官方配置參考手冊

要點

Paste_Image.png
  • Provider: 暴露服務的服務提供方。
  • Consumer: 調用遠程服務的服務消費方。
  • Registry: 服務注冊與發現的注冊中心。

    注冊中心官方推薦采用zooKeeper,各種注冊中心策略點這里

  • Monitor: 統計服務的調用次調和調用時間的監控中心。
  • Container: 服務運行容器。

調用關系說明:

  1. 服務容器負責啟動,加載,運行服務提供者。
  2. 服務提供者在啟動時,向注冊中心注冊自己提供的服務。
  3. 服務消費者在啟動時,向注冊中心訂閱自己所需的服務。
  4. 注冊中心返回服務提供者地址列表給消費者,如果有變更,注冊中心將基于長連接推送變更數據給消費者。
  5. 服務消費者,從提供者地址列表中,基于軟負載均衡算法,選一臺提供者進行調用,如果調用失敗,再選另一臺調用。
  6. 服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鐘發送一次統計數據到監控中心。

代碼

Demo點我下載

Paste_Image.png

一共分為3大塊

  • 服務提供者API接口
  • 服務提供端
  • 服務消費端

服務提供者API接口

IProviderAPI.java

public interface IProviderAPI {
     String dosomething(String json,long logId);
}

服務消費者

Consumer.java

@Service
public class Consumer {
  private Long count=0L;
  @Autowired
  private IProviderAPI providerAPI;

//  @Scheduled(cron = "0-59 * * * * ?")//定時
@Scheduled(fixedDelay = 1000 * 5 , initialDelay = 1000)//心跳更新,5000毫秒執行一次,延遲1秒后才執行
  public void doSomething(){
      System.out.println(providerAPI.dosomething("now is "+count,count++));
  }
}

消費者Runner(用于啟動spring容器,加載dubbo,spring配置)

ConsumerRunner.java

public class ConsumerRunner {
    public static void main(String[] args) throws IOException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                new String[]{"consumer.xml", "spring.xml", "dubbo.xml"});
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

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-3.0.xsd
         http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
    <dubbo:reference id="providerImpl" check="false" timeout="25000" retries="0"
                     interface="com.m1.api.IProviderAPI" />
   
</beans>


服務提供者

ProviderImpl.java

@Service("providerImpl")
public class ProviderImpl implements IProviderAPI {
    public String dosomething(String json, long logId) {
        System.out.println(json);
        return "i finish doing "+json +" where logid=="+logId;
    }
}

提供者Runner(用于啟動spring容器,加載dubbo,spring配置)

ProviderRunner.java

public class ProviderRunner {
    public static void main(String[] args) throws IOException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                new String[] {
                        "provider.xml",
                        "spring.xml",
                        "dubbo.xml" });
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

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">

        <!-- 聲明需要暴露的服務接口 -->
        <dubbo:service interface="com.m1.api.IProviderAPI" ref="providerImpl" />

</beans>

dubbo.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-3.0.xsd
         http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
    
    <dubbo:application name="dubbo-test"/>
    <dubbo:registry address="zookeeper://yourIP:yourPort" />
    <dubbo:protocol name="dubbo" port="21101" />
</beans>

spring.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:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com.m1" /><!--自動掃描-->

    <!--開啟這個配置 啟用定時 -->
    <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
    <task:scheduler id="qbScheduler" pool-size="10"/>
</beans>

先運行ProviderRunner.java,后運行ConsumerRunner.java,每隔5秒鐘可以看到控制臺有不同的輸出,從zookeeper運行日志,也能發現訪問的ip

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,869評論 18 139
  • 這個方法存在一個弊端,就是當表視圖存在導航條的時候,表視圖會被導航條遮蓋。所以需要添加如下代碼
    靜花寒閱讀 1,506評論 3 7
  • 沒有情緒 還是思維太豐富 低沉的耳鳴 是還在沉睡 偶爾 的清醒了 發現了生命的真諦 但為什么 一切都是夢幻一般 又...
    冰辛閱讀 383評論 1 0