一、背景
dubbo是個什么?
首先要說的是,網上有很多高大上的回答,可自行百度,這里只說一些非常狹隘的東西:
dubbo是一個分布式服務框架,我們一般用它進行遠程方法調用。(分布式、遠程方法調用下面有注釋)
ok,狹隘的東西回答完畢(下面注釋也是狹隘的)~~~
分布式:將一個功能分成多個小模塊,交由不同服務器處理,整合得到最終結果。
遠程方法調用:RMI,可像本地調用一樣調用其它系統的功能
二、適用場景
供應商各子系統間的相互調用
三、服務端配置
1、實現提供遠程方法調用的類
接口類
public interface ICompanyService { /** * 檢測公司是否存在:根據公司名稱檢測CRM系統中公司是否存在 */ boolean checkCompanyExist(String companyName) throws Exception; }
實現類
public class CompanyServiceImpl implements ICompanyService { /** * 檢測公司是否存在:根據公司名稱檢測CRM系統中公司是否存在 */ @Override public boolean checkCompanyExist(String companyName) throws Exception { return false; } }
2、在pom.xml增加dubbo所需jar包
<!-- Dubbo Jar包引入 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.9</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- zookeeper 引入 --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.5</version> <exclusions> <exclusion> <artifactId>jmxtools</artifactId> <groupId>com.sun.jdmk</groupId> </exclusion> <exclusion> <artifactId>jmxri</artifactId> <groupId>com.sun.jmx</groupId> </exclusion> <exclusion> <artifactId>jms</artifactId> <groupId>javax.jms</groupId> </exclusion> </exclusions> </dependency>
3、增加dubbo配置文件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.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- dubbo服務器端配置文件 --> <!-- 提供方應用信息,用於計算依賴關系 --> <dubbo:application name="dubbo-crm-service" /> <!-- 使用zookeeper注冊中心,暴露服務地址 --> <dubbo:registry protocol="zookeeper" address="${zookeeper.url}" /> <!-- 用dubbo協議在20883端口暴露服務 --> <dubbo:protocol name="dubbo" port="20883" /> <!-- 聲明需要暴露的服務接口及接口實現 --> <!-- 公司自助注冊接口 ,用戶驗證、權限接口 --> <dubbo:service interface="com.glodon.crm.dubbo.service.ICompanyService" ref="crmCompanyService" /> <bean id="crmCompanyService" class="com.glodon.crm.dubbo.service.impl.CompanyServiceImpl" /> </beans>
4、在web.xml中加載dubbo的配置文件
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml,classpath*:dubbo.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
5、導出遠程方法調用的接口為jar包
eclipse中選中接口文件,右鍵 - Export - 選中java下的JAR file - 選擇保存路徑,輸入jar包名稱,點擊Finish
四、客戶端配置
1、在pom.xml增加dubbo所需jar包,和服務端提供的接口jar包(略)
2、增加dubbo配置文件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.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- dubbo服務器端配置文件 --> <!-- 消費方應用信息,用於計算依賴關系 --> <dubbo:application name="gcxx_consumer" /> <!-- 使用zookeeper注冊中心,獲取服務地址 --> <dubbo:registry protocol="zookeeper" address="${zookeeper.url}" /> <!-- dubbo端口 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 生成遠程服務代理,可以像使用本地bean一樣使用companyService --> <dubbo:reference id="companyService" interface="com.glodon.crm.dubbo.service.ICompanyService" check="false"/> </beans>
3、在web.xml中加載dubbo的配置文件(略)
4、在程序中調用遠程方法,要注意spring注入的遠程接口類的對象名要和上方dubbo:reference的id相同
public class Test { @Autowired ICompanyService companyService; public void test(){ try { companyService.checkCompanyExist("test"); } catch (Exception e) { e.printStackTrace(); } } }
五、直連服務提供方
開發時會碰到開發期間多個服務提供方中只有一個是自己要調的,而上述配置中dubbo的調用近乎隨機,不能調到自己想調的服務方
這時,調整dubbo配置文件為ip直連即可
<!-- 生成遠程服務代理,可以像使用本地bean一樣使用companyService --> <dubbo:reference id="companyService" interface="com.glodon.crm.dubbo.service.ICompanyService" check="false" url="172.16.231.230:20883"/>
