生活随笔
收集整理的這篇文章主要介紹了
Dubbo快速启动示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- Dubbo 采用全 Spring 配置方式,透明化接入應用,對應用沒有任何 API 侵入,只需用 Spring 加載 Dubbo 的配置即可,Dubbo 基于 Spring 的 Schema 擴展 進行加載。
- 如果不想使用 Spring 配置,可以通過 API 的方式 進行調用。
服務提供者
定義服務接口
package org.apache.dubbo.demo;public interface DemoService {String sayHello(String name);
}
復制代碼在服務提供方實現接口
package org.apache.dubbo.demo.provider;import org.apache.dubbo.demo.DemoService;public class DemoServiceImpl implements DemoService {public String sayHello(String name) {
return "Hello " + name;}
}
復制代碼用 Spring 配置聲明暴露服務
<?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=
"hello-world-app" /><!-- 使用multicast廣播注冊中心暴露服務地址 --><dubbo:registry address=
"multicast://224.5.6.7:1234" /><!-- 用dubbo協議在20880端口暴露服務 --><dubbo:protocol name=
"dubbo" port=
"20880" /><!-- 聲明需要暴露的服務接口 --><dubbo:service interface=
"org.apache.dubbo.demo.DemoService" ref=
"demoService" /><!-- 和本地bean一樣實現服務 --><bean id=
"demoService" class=
"org.apache.dubbo.demo.provider.DemoServiceImpl" />
</beans>
復制代碼加載 Spring 配置
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
"http://10.20.160.198/wiki/display/dubbo/provider.xml"});context.start();System.in.read(); // 按任意鍵退出}
}
復制代碼服務消費者
通過 Spring 配置引用遠程服務
<?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=
"consumer-of-helloworld-app" /><!-- 使用multicast廣播注冊中心暴露發現服務地址 --><dubbo:registry address=
"multicast://224.5.6.7:1234" /><!-- 生成遠程服務代理,可以和本地bean一樣使用demoService --><dubbo:reference id=
"demoService" interface=
"org.apache.dubbo.demo.DemoService" />
</beans>
復制代碼加載Spring配置,并調用遠程服務
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.dubbo.demo.DemoService;public class Consumer {public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
"http://10.20.160.198/wiki/display/dubbo/consumer.xml"});context.start();DemoService demoService = (DemoService)context.getBean(
"demoService"); // 獲取遠程服務代理String hello = demoService.sayHello(
"world"); // 執行遠程方法System.out.println( hello ); // 顯示調用結果}
}
復制代碼注:
-
- 該接口需單獨打包,在服務提供方和消費方共享 DemoService.java
-
- 對服務消費方隱藏實現 DemoServiceImpl.java provider.xml
-
- 也可以使用 IoC 注入 Consumer.java
轉載于:https://juejin.im/post/5cbc29b2518825324664e19f
總結
以上是生活随笔為你收集整理的Dubbo快速启动示例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。