使用dubbo呼叫服務
阿新 • • 發佈:2018-12-12
建立服務提供者
1.建立服務介面並編寫實現類
2.匯入jar包,這裡使用maven匯入
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>io.netty</groupId >
<artifactId>netty-all</artifactId>
<version>4.0.23.Final</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
<dependency >
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
3.編寫服務配置檔案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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方應用資訊,用於計算依賴關係 -->
<dubbo:application name="provider" />
<!--以下是三種提供服務的方式-->
<!-- ------------------------------------------------------------------------------------------------------------------ -->
<!-- 使用multicast廣播註冊中心暴露服務地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 本地模式,即服務方與消費方在同一臺電腦上 -->
<!-- <dubbo:registry address="N/A" /> -->
<!-- zookeeper註冊服務,推薦使用 -->
<!-- <dubbo:registry address="zookeeper://192.168.127.99:2181?backup=192.168.127.99:2182,192.168.127.99:2183" /> -->
<dubbo:registry protocol="zookeeper" address="192.168.127.99:2181,192.168.127.99:2182,192.168.127.99:2183" />
<!-- ------------------------------------------------------------------------------------------------------------------ -->
<!-- 用dubbo協議在20880埠暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 宣告需要暴露的服務介面 -->
<dubbo:service interface="cn.demoService.DemoService"
ref="demoService" />
<!-- 和本地bean一樣實現服務 -->
<bean id="demoService" class="cn.demoService.impl.DemoServiceImpl" />
</beans>
專案結構如下
建立測試服務類
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("provider.xml");
try {
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
消費方
1.建立與服務方相同的介面,包名也應該相同
2.建立消費方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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方一樣 -->
<dubbo:application name="providerss" />
<!-- 使用multicast廣播註冊中心暴露發現服務地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- <dubbo:registry address="zookeeper://192.168.127.99:2181?backup=192.168.127.99:2182,192.168.127.99:2183" /> -->
<!--zookeeper叢集-->
<dubbo:registry protocol="zookeeper" address="192.168.127.99:2181,192.168.127.99:2182,192.168.127.99:2183" />
<!--zookeeper不叢集-->
<!-- <dubbo:registry address="zookeeper://192.168.127.99:2181" /> -->
<!-- 生成遠端服務代理,可以和本地bean一樣使用demoService -->
<!--使用本地-->
<!-- <dubbo:reference id="demoService" interface="cn.customer.DemoService" -->
<!-- url="127.0.0.1:20880" /> -->
<!--使用zookeeper或廣播提供服務-->
<dubbo:reference id="demoService1" check="false"
interface="cn.demoService.DemoService" />
</beans>
專案路徑
建立測試消費類
public class TestMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("customer.xml");
ac.start();
DemoService demoService = (DemoService) ac.getBean("demoService1");
String hello = demoService.getUser("world"); // 執行遠端方法
System.out.println(hello); // 顯示呼叫結果
}
}