『網際網路架構』軟體架構-掌握dubbo常規應用(上)(40)
回顧上次的文章,講了分散式的歷史和發展的背景,通過一個場景一個公司的專案想換成分散式的專案,但是真正的要把一個系統改成成為一個分散式系統的時候,過程並非那麼容易,需要整個公司一起來配合,包括業務部門和技術部門,才能把系統做好,其實單體變成分散式的不僅僅是技術發生了改變,公司的運作流程和系統的版本釋出,都已經做了改變。如果一個系統要更改成分散式系統,一定要把相關的干係人都召集到一起開個會,評估下。技術人員只需要做到對業務的波動降低到最低,開發成本一定要可控,評估好,不要做成一個爛尾的專案。分散式架構是把雙刃劍,必須根據公司的實際情況,確實需要這種橫向擴充套件能力的時候,迫切的需要提升效能,其他沒有除了更改分散式這種好辦法了(資料庫優化,程式碼優化做盡了)。如果垂直架構可以搞定的話,不建議搞成分散式架構,垂直業務也分開了,業務進行垂直拆分了。
(一)Dubbo
- 歷史
直到2011年10月27日,阿里巴巴開源了自己的SOA服務化治理方案的核心框架Dubbo,服務治理和SOA的設計理念開始逐漸在國內軟體行業中落地,並被廣泛應用。
Dubbo作為阿里巴巴內部的SOA服務化治理方案的核心框架,在2012年時已經每天為2000+個服務提供3,000,000,000+次訪問量支援,並被廣泛應用於阿里巴巴集團的各成員站點。Dubbo自2011年開源後,已被許多非阿里系公司使用,其中既有當當網、網易考拉、國美電器等網際網路公司,也有中國人壽、青島海爾等傳統企業。
- 官網
http://dubbo.apache.org/zh-cn/
-
介紹
>dubbo 阿里開源的一個SOA服務治理框架,從目前來看把它稱作是一個RCP遠端呼叫框架更為貼切。單從RPC框架來說,功能較完善,支援多種傳輸和序列化方案。所以想必大家已經知道他的核心功能了:就是遠端呼叫。
-
簡單的例項演示
> idea新建立一個java專案,原始碼:原始碼資料夾/『網際網路架構』軟體架構-掌握dubbo常規應用(40),專案名稱:dubbo-study
1.pom檔案
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.idig8</groupId> <artifactId>dubbo-study</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>dubbo-study Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>https://idig8.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> </dependencies> <build> <finalName>dubbo-study</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
2.provider
可以通過小廣播的形式,也可以通過redis 和zookeeper,後面會重點說協議的。先看如何編寫。
<?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="demo-provider"/> <dubbo:registry address="multicast://224.5.6.8:1235"/> <!--<dubbo:registry protocol="redis" address="192.168.0.147:6379" check="true"/>--> <dubbo:protocol name="dubbo" port="20880"/> <dubbo:service interface="com.idig8.service.DemoService" ref="demoService"> <dubbo:method name="sayHello" timeout="2000"/> </dubbo:service> <dubbo:service interface="com.idig8.service.UserService" ref="userServiceImpl"/> <bean id="userServiceImpl" class="com.idig8.service.UserServiceImpl"/> <bean id="demoService" class="com.idig8.service.DemoServiceImpl"/> </beans>
3.consumber.xml
可以通過小廣播的形式,也可以通過redis 和zookeeper,後面會重點說協議的。先看如何編寫。消費者的小廣播multicast://224.5.6.8:1235 一定要跟生產者保持一致。
<?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="demo-consumer"/> <dubbo:registry address="multicast://224.5.6.8:1235"/> <!--<dubbo:registry protocol="redis" address="192.168.0.147:6379" check="true"/>--> <dubbo:referenceid="demoService" timeout="2000" interface="com.idig8.service.DemoService"/> </beans>
4.Proivider.java
package com.idig8.provider; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "config/spring-dubbo-provider.xml"); context.start(); System.out.println("dubbo multicast 服務啟動成功 "); System.in.read(); // press any key to exit } }
5.Consumber.java
package com.idig8.consumer; import com.idig8.service.DemoService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "config/spring-dubbo-consumer.xml"); context.start(); DemoService demoService = (DemoService) context.getBean("demoService"); // obtain proxy object for remote invocation String hello = demoService.sayHello("world"); // execute remote invocation System.out.println(hello); // show the result while (true) { byte[] b = new byte[1024]; int szie = System.in.read(b); if (new String(b, 0, szie).trim().equals("send")) { System.out.println(demoService.sayHello("hanmeimei")); } } } }
開發pom.xml > dubbo-provider.xml > Provider.java> spring-dubbo-consumer.xml > Consumer.java
6.執行順序,執行Provider.java,執行Consumer.java
- dubbo架構簡要講解
1.Provider(提供者)繫結指定埠並啟動服務(20880)。
2.提供者連線註冊中心,併發本機IP、埠、應用資訊和提供服務資訊傳送至註冊中心儲存。
3.Consumer(消費者),連線註冊中心 ,併發送應用資訊、所求服務資訊至註冊中心。
4.註冊中心根據消費者所求服務資訊匹配對應的提供者列表傳送至Consumer 應用快取。配置檔案裡面的interface來匹配。
5.Consumer 在發起遠端呼叫時基於快取的消費者列表擇其一發起呼叫。快取,是虛線,效能提升,註冊中心掛了不影響。
6.Provider 狀態變更會實時通知註冊中心、在由註冊中心實時推送至Consumer。
7.一定要記住,真正的呼叫跟註冊中心沒關係,而是cosumber直接呼叫的provider。
*這麼設計的意義:
1.Consumer 與Provider 解偶,雙方都可以橫向增減節點數。
2.註冊中心對本身可做對等叢集,可動態增減節點,並且任意一臺宕掉後,將自動切換到另一臺。
3.去中心化,雙方不直接依懶註冊中心,即使註冊中心全部宕機短時間內也不會影響服務的呼叫。
4.服務提供者無狀態,任意一臺宕掉後,不影響使用。
PS:搞明白Registry,Consumber,Porvider這3方一定要搞明白他們是如何呼叫的,通過架構圖瞭解虛線和實現的關係。呼叫的順序,如果註冊中心掛了,不用影響Registry 和Consumber的通訊,但是本身dubbo有自檢機制會爆連線不到註冊中心,但是不影響呼叫。
>>原創文章,歡迎轉載。轉載請註明:轉載自IT人故事會,謝謝!
>>原文連結地址:上一篇:已是最新文章