1.Provider搭建核心:3件事
1.1 需要告知Provider(服務提供者)闽瓢,Registry(注冊中心)(之前已經(jīng)安裝好zookeeper作為注冊中心了)在哪里
1.2 告訴Provider使用哪種協(xié)議(Dubbo,RMI,Hession)
1.3 Provider告訴Registry接癌,Provider的哪一個服務接口需要發(fā)布
2.注意事項
2.1 在使用dubbo的時候,接口和實現(xiàn)類需要分開放到不同的項目
原因:consumer(消費者)需要遠程調(diào)用接口的時候扣讼,需要去依賴接口缺猛,如果接口和實現(xiàn)類都在一個項目,那么就直接可以看到實現(xiàn)類了椭符,能看到具體的調(diào)用實現(xiàn)就不是RPC調(diào)用了
創(chuàng)建接口項目荔燎,聲明服務接口
Provider就是接口項目的實現(xiàn)類項目
Provider就相當于以前單個項目的service的實現(xiàn)類
3.具體步驟
3.1 新建一個Maven項目作為父項目,管理子模塊之間的依賴
dubboparent的pom.xml
<?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.sccl</groupId>
<artifactId>dubbo-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-service</module>
<module>dubbo-service-impl</module>
</modules>
</project>
3.2 新建一個Maven Project座享,作為父項目的子模塊婉商,里面只有接口(dubbo-service)
??3.2.1 為什么這么做?
??RPC框架,不希望Consumer知道具體實現(xiàn).如果實現(xiàn)類和接口在同一個項目中,Consumer依賴這個項目時,就會知道實現(xiàn)類具體實現(xiàn).
??和建立父項目的方式相同,項目建立后渣叛,把子模塊項目的打包方式改為jar
dubbo-service的pom.xml
<?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">
<parent>
<artifactId>dubbo-parent</artifactId>
<groupId>com.sccl</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-service</artifactId>
<packaging>jar</packaging>
</project>
3.3 新建一個Maven Project丈秩,作為父項目的子模塊,里面寫接口的實現(xiàn)類(dubbo-service-impl)
??和建立上一個子模塊的方式相同淳衙,打包方式也改為jar
dubbo-service-impl的pom.xml里面進行配置
1 依賴接口
2 依賴dubbo,去掉老版本spring
3 依賴新版本spring
4 依賴zookeeper客戶端工具zkClient
<?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">
<parent>
<!--父工程只相當于是管理依賴的蘑秽,src包可以刪掉-->
<artifactId>dubbo-parent</artifactId>
<groupId>com.sccl</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-service-impl</artifactId>
<packaging>jar</packaging>
<dependencies>
<!--實現(xiàn)類項目依賴接口項目-->
<dependency>
<groupId>com.sccl</groupId>
<artifactId>dubbo-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<!--導入dubbo的jar包,dubbo此時依賴的spring版本是2.5.6 SEC03版本的太低了箫攀,
需要剔除這個低版本-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<!--剔除2.5.6 SEC03 版本的spring-->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--springmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- 訪問zookeeper的客戶端jar -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
</project>
3.4 在dubbo-service-impl項目中肠牲,新建實現(xiàn)類,并實現(xiàn)接口方法.
3.5 新建配置文件dubbo-provider.xml,并配置
??3.5.1 <dubbo:application/>
給provider起名,在monitor或管理工具中區(qū)別是哪個provider(服務提供者)
??3.5.2 <dubbo:registry/>
配置注冊中心
????1.address
:注冊中心的ip和端口
????2.protocol
:使用哪種注冊中心
??3.5.3 <dubbo:protocol/>
配置協(xié)議
????1.name
:使用什么協(xié)議
????2.port
:consumer invoke(調(diào)用
) provider時的端口號
??3.5.4 <dubbo:service/>
注冊接口
????1.ref
引用接口實現(xiàn)類<bean>
的id
值
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:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-xmlns:全名是xml namespace,也即是為當前的這個xml指定命名空間匠童。
xmlns:xsi:是指當前xml所要遵循的標簽規(guī)范.
xsi:schemaLocation:指定的命名空間對應的驗證文件埂材,用來定義xml schema的地址,
也就是xml書寫時需要遵循的語法汤求,用于聲明了目標命名空間的模式文檔俏险。。兩部分組成扬绪,
前面部分就是命名空間的名字竖独,后面是xsd(xmlschema)的地址,
也是就表示把定義這個命名空間的schema文件給引用進來,
好讓eclipse這類型工具能夠解析和驗證你的xml文件是否符合語法規(guī)范挤牛。
等同于莹痢。用于聲明了目標命名空間的模式文檔。
在xsi:schemaLocation后面配置的字符串都是成對的墓赴,前面的是命名空間的URI竞膳,后面是xsd文件的URI;
小結:給引入的xml文檔聲明一個命名空間诫硕,并指定xml文件遵循的規(guī)范坦辟,
以及指定xml文件的位置
-->
<!-給當前Provider自定義一個名稱-->
<dubbo:application name="dubbo-service"/>
<!-配置注冊中心 暴露注冊服務地址-->
<dubbo:registry address="192.168.0.106:2181" protocol="zookeeper"/>
<!-provider 使用"dubbo"協(xié)議在20880端口暴露服務-->
<dubbo:protocol name="dubbo" port="20880"/>
<!-provider到注冊中心注冊功能接口-->
<dubbo:service interface="com.sccl.service.DemoService" ref="demoServiceImpl"/>
<bean id="demoServiceImpl" class="com.sccl.service.impl.DemoServiceImpl"/>
</beans>
3.6 啟動容器
??3.6.1 通過spring方式啟動
??dubbo-provider.xml位置沒有要求
//測試服務提供者搭建是否成功
public class Test {
public static void main(String[] args) throws Exception{
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("dubbo-provider.xml");
ac.start();
System.out.println("啟動成功");
System.in.read(); //按任意鍵退出,接收到控制臺的輸入后就關閉
}
}
??3.6.2 使用dubbo提供的方式啟動(推薦使用這種方式)
??此時要求dubbo-provider.xml必須放入類路徑下/META-INF/spring/*.xml
// 測試服務提供者搭建是否成功
public class Test {
public static void main(String[] args) throws Exception{
//官方推薦的方式
//強制要求:配置文件必須放在/META-INF/spring/*.xml
Main.main(args);
}
}
啟動步驟:先啟動注冊中心章办,再啟動provider服務