當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Dubbo(三)之Spring zookeeper集成
生活随笔
收集整理的這篇文章主要介紹了
Dubbo(三)之Spring zookeeper集成
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載自??Dubbo快速開始
Dubbo 采用全 Spring 配置方式,透明化接入應用,對應用沒有任何 API 侵入,只需用 Spring 加載 Dubbo 的配置即可,Dubbo 基于?Spring 的 Schema 擴展?進行加載。
如果不想使用 Spring 配置,可以通過?API 的方式?進行調用。
服務提供者
完整安裝步驟,請參見:示例提供者安裝
安裝:
git clone https://github.com/apache/dubbo.git cd dubbo/dubbo-demo/dubbo-demo-xml 運行 dubbo-demo-xml-provider中的org.apache.dubbo.demo.provider.Application 如果使用Intellij Idea 請加上-Djava.net.preferIPv4Stack=true配置:
resources/spring/dubbo-provider.xml 修改其中的dubbo:registry,替換成真實的注冊中心地址,推薦使用zookeeper,如: <dubbo:registry address="zookeeper://127.0.0.1:2181"/>定義服務接口
DemoService.java?1:
package org.apache.dubbo.demo;public interface DemoService {String sayHello(String name); }在服務提供方實現接口
DemoServiceImpl.java?2:
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 配置聲明暴露服務
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="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 配置
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[]{"META-INF/spring/dubbo-demo-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://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配置,并調用遠程服務
Consumer.java?3:
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[] {"META-INF/spring/dubbo-demo-consumer.xml"});context.start();DemoService demoService = (DemoService)context.getBean("demoService"); // 獲取遠程服務代理String hello = demoService.sayHello("world"); // 執行遠程方法System.out.println( hello ); // 顯示調用結果} }該接口需單獨打包,在服務提供方和消費方共享???
對服務消費方隱藏實現???
也可以使用 IoC 注入???
總結
以上是生活随笔為你收集整理的Dubbo(三)之Spring zookeeper集成的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 变强最好的方法就是单人四排变强最好的方法
- 下一篇: SpringBoot使用日志