dubbo教程系列2
生活随笔
收集整理的這篇文章主要介紹了
dubbo教程系列2
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
快速啟動(dòng)
Dubbo采用全Spring配置方式,透明化接入應(yīng)用,對(duì)應(yīng)用沒(méi)有任何API侵入,只需用Spring加載Dubbo的配置即可,Dubbo基于Spring的Schema擴(kuò)展進(jìn)行加載。
服務(wù)提供者
定義服務(wù)接口: (該接口需單獨(dú)打包,在服務(wù)提供方和消費(fèi)方共享)
DemoService.java package com.alibaba.dubbo.demo;public interface DemoService {String sayHello(String name);}在服務(wù)提供方實(shí)現(xiàn)接口:(對(duì)服務(wù)消費(fèi)方隱藏實(shí)現(xiàn))
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配置聲明暴露服務(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://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"><!-- 提供方應(yīng)用信息,用于計(jì)算依賴關(guān)系 --><dubbo:application name="hello-world-app" /><!-- 使用multicast廣播注冊(cè)中心暴露服務(wù)地址 --><dubbo:registry address="multicast://224.5.6.7:1234" /><!-- 用dubbo協(xié)議在20880端口暴露服務(wù) --><dubbo:protocol name="dubbo" port="20880" /><!-- 聲明需要暴露的服務(wù)接口 --><dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" /><!-- 和本地bean一樣實(shí)現(xiàn)服務(wù) --><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(); // 按任意鍵退出}}服務(wù)消費(fèi)者
通過(guò)Spring配置引用遠(yuǎn)程服務(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://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"><!-- 消費(fèi)方應(yīng)用名,用于計(jì)算依賴關(guān)系,不是匹配條件,不要與提供方一樣 --><dubbo:application name="consumer-of-helloworld-app" /><!-- 使用multicast廣播注冊(cè)中心暴露發(fā)現(xiàn)服務(wù)地址 --><dubbo:registry address="multicast://224.5.6.7:1234" /><!-- 生成遠(yuǎn)程服務(wù)代理,可以和本地bean一樣使用demoService --><dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" /></beans>加載Spring配置,并調(diào)用遠(yuǎn)程服務(wù):(也可以使用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"); // 獲取遠(yuǎn)程服務(wù)代理String hello = demoService.sayHello("world"); // 執(zhí)行遠(yuǎn)程方法System.out.println( hello ); // 顯示調(diào)用結(jié)果}}總結(jié)
以上是生活随笔為你收集整理的dubbo教程系列2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 前端两个经典bug
- 下一篇: javaWeb项目部署至tomcat下c