日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Dubbo快速启动示例

發(fā)布時間:2025/7/14 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Dubbo快速启动示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • Dubbo 采用全 Spring 配置方式,透明化接入應(yīng)用,對應(yīng)用沒有任何 API 侵入,只需用 Spring 加載 Dubbo 的配置即可,Dubbo 基于 Spring 的 Schema 擴展 進行加載。
  • 如果不想使用 Spring 配置,可以通過 API 的方式 進行調(diào)用。

服務(wù)提供者

定義服務(wù)接口

  • DemoService.java
package org.apache.dubbo.demo;public interface DemoService {String sayHello(String name); } 復制代碼

在服務(wù)提供方實現(xiàn)接口

  • DemoServiceImpl.java
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 配置聲明暴露服務(wù)

  • 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"><!-- 提供方應(yīng)用信息,用于計算依賴關(guān)系 --><dubbo:application name="hello-world-app" /><!-- 使用multicast廣播注冊中心暴露服務(wù)地址 --><dubbo:registry address="multicast://224.5.6.7:1234" /><!-- 用dubbo協(xié)議在20880端口暴露服務(wù) --><dubbo:protocol name="dubbo" port="20880" /><!-- 聲明需要暴露的服務(wù)接口 --><dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService" /><!-- 和本地bean一樣實現(xiàn)服務(wù) --><bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl" /> </beans> 復制代碼

加載 Spring 配置

  • Provider.java
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(); // 按任意鍵退出} } 復制代碼

服務(wù)消費者

通過 Spring 配置引用遠程服務(wù)

  • consumer.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"><!-- 消費方應(yīng)用名,用于計算依賴關(guān)系,不是匹配條件,不要與提供方一樣 --><dubbo:application name="consumer-of-helloworld-app" /><!-- 使用multicast廣播注冊中心暴露發(fā)現(xiàn)服務(wù)地址 --><dubbo:registry address="multicast://224.5.6.7:1234" /><!-- 生成遠程服務(wù)代理,可以和本地bean一樣使用demoService --><dubbo:reference id="demoService" interface="org.apache.dubbo.demo.DemoService" /> </beans> 復制代碼

加載Spring配置,并調(diào)用遠程服務(wù)

  • Consumer.java
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"); // 獲取遠程服務(wù)代理String hello = demoService.sayHello("world"); // 執(zhí)行遠程方法System.out.println( hello ); // 顯示調(diào)用結(jié)果} } 復制代碼

注:

  • 該接口需單獨打包,在服務(wù)提供方和消費方共享 DemoService.java
  • 對服務(wù)消費方隱藏實現(xiàn) DemoServiceImpl.java provider.xml
  • 也可以使用 IoC 注入 Consumer.java

轉(zhuǎn)載于:https://juejin.im/post/5cbc29b2518825324664e19f

總結(jié)

以上是生活随笔為你收集整理的Dubbo快速启动示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。