日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

dubbo教程系列2

發布時間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dubbo教程系列2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

快速啟動

Dubbo采用全Spring配置方式,透明化接入應用,對應用沒有任何API侵入,只需用Spring加載Dubbo的配置即可,Dubbo基于Spring的Schema擴展進行加載。

服務提供者

定義服務接口: (該接口需單獨打包,在服務提供方和消費方共享)

DemoService.java package com.alibaba.dubbo.demo;public interface DemoService {String sayHello(String name);}

在服務提供方實現接口:(對服務消費方隱藏實現)

DemoServiceImpl.java package com.alibaba.dubbo.demo.provider;import com.alibaba.dubbo.demo.DemoService;public class DemoServiceImpl implements DemoService {public String sayHello(String name) {return "Hello " + name;}}

用Spring配置聲明暴露服務:

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://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: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="com.alibaba.dubbo.demo.DemoService" ref="demoService" /><!-- 和本地bean一樣實現服務 --><bean id="demoService" class="com.alibaba.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(); // 按任意鍵退出}}

服務消費者

通過Spring配置引用遠程服務:

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://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:application name="consumer-of-helloworld-app" /><!-- 使用multicast廣播注冊中心暴露發現服務地址 --><dubbo:registry address="multicast://224.5.6.7:1234" /><!-- 生成遠程服務代理,可以和本地bean一樣使用demoService --><dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" /></beans>

加載Spring配置,并調用遠程服務:(也可以使用IoC注入)

Consumer.java

import org.springframework.context.support.ClassPathXmlApplicationContext; import com.alibaba.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 ); // 顯示調用結果}}

總結

以上是生活随笔為你收集整理的dubbo教程系列2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。