dubbo集成spring


 

dubbo集成spring

1.項目依賴


<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.5</version>
</dependency>

<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.5</version>
<type>pom</type>
</dependency>

<dependency>
<groupId>cn.wjs</groupId>
<artifactId>dubbo-xml-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

2.服務提供者

 

resource/spring下添加dubbo-provider.xml:

通過 Spring 配置引用遠程服務


<dubbo:application metadata-type="remote" name="demo-provider"/>
<dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/>
<!-- zk注冊中心 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>

<dubbo:protocol name="dubbo"/>

<bean id="demoService" class="cn.wjs.DemoServiceImpl"/>
<!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
<dubbo:service interface="cn.wjs.DemoService" ref="demoService"/>
DemoServiceImpl: 
public class DemoServiceImpl implements DemoService {
private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);

public String sayHello(String name) {
logger.info("aaa");
return "Hello " + name;
}

}
public class Application {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml");
context.start();
System.in.read();
}
}

 

3.服務消費者

resource/spring目錄下添加dubbo-consumer.xml

通過 Spring 配置引用遠程服務

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
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="demo-consumer"/>

<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
<dubbo:reference id="demoService" check="false" interface="cn.wjs.DemoService"/>

</beans>

applicaiton: 

public class Application {
/**
* In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before
* launch the application
*/
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
context.start();
DemoService demoService = (DemoService) context.getBean("demoService"); // 獲取遠程服務代理
String hello = demoService.sayHello("world"); // 執行遠程方法
System.out.println(hello); // 顯示調用結果
}
}

 

4.啟動zookeeper注冊中心

zookeeper3.4.14/bin/zkServer.sh start  

5.分別啟動服務端和消費端

消費者啟動成功后輸出: hello world

 

github地址: 

服務端: dubbo-xml-provider

消費端: dubbo-xml-consumer

接口: dubbo-xml-interface

https://github.com/wujinsen/dubbo-learning

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM