dubbo入门使用
Posted yinz163diudiu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dubbo入门使用相关的知识,希望对你有一定的参考价值。
主要参考dubbo官网demo
此处采用zookeeper注册中心进行服务协调管理
真个项目结构如下所示:
dcommon : 主要用于定义服务接口, 为dconsumer,dprovider所依赖
dparent: maven父工程
dconsumer: 服务消费者
dprovider: 服务提供者
首先需要在dconsumer和dprovider工程pom文件中加入相关dubbo及zookeeper客户端依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> </dependency> <!-- 此处zk客户端使用curator(从 2.3.0 版本开始支持可选 curator 实现) 客户端, 也可选择使用zkclient 客户端(从 2.2.0 版本开始缺省为 zkclient 实现) --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency>
dcommon服务接口定义如下,DemoService.java:
package com.yinz.service; public interface DemoService { String sayHello(String word); }
dprovider服务实现如下,DemoServiceImpl.java:
package com.yinz.service.impl; import com.yinz.service.DemoService; public class DemoServiceImpl implements DemoService{ @Override public String sayHello(String word) { return "hello : " + word; } }
xml配置如下,application.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="demo-provider"/> <!-- 延迟暴露服务,spring容器初始化成功后再暴露服务,防止当服务器实现类中有 applicationContext.getBean() 的调用时出错, 强烈建议全部采用 IoC 注入的方式使用 Spring的Bean。--> <dubbo:provider delay="-1"/> <!-- register="false" 禁用注册 subscribe="false" 禁用订阅 --> <dubbo:registry address="zookeeper://47.98.11.199:2181"/> <!-- 配置dubbo协议线程模型 --> <dubbo:protocol name="dubbo" port="20880" accesslog="true" dispatcher="message" threadpool="fixed" threads="100"/> <!--cluster="failover" 为默认配置,此处可省略 --> <dubbo:service interface="com.yinz.service.DemoService" ref="demoService" retries="2" cluster="failover" loadbalance="roundrobin"/> <bean id="demoService" class="com.yinz.service.impl.DemoServiceImpl"/> </beans>
加载spring配置代码如下:
package com.yinz.provider; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] {"classpath:application.xml"}); context.start(); // press any key to exit System.in.read(); } }
dconsumer,服务消费端配置引用远程服务,application.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="demo-consumer"/> <dubbo:registry address="zookeeper://47.98.11.199:2181"/> <dubbo:protocol name="dubbo" port="20880"/> <dubbo:reference interface="com.yinz.service.DemoService" id="demoService" async="true"/> </beans>
加载spring配置并调用远程服务
package com.yinz.consumer; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yinz.service.CallbackListener; import com.yinz.service.CallbackService; import com.yinz.service.DemoService; public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[]{"classpath:application.xml"}); context.start(); // obtain proxy object for remote invocation DemoService demoService = (DemoService) context.getBean("demoService"); String hello = demoService.sayHello("world"); System.out.println(hello); System.in.read(); } }
此外dubbo支持容错、负载均衡等高级特性,可参考dubbo官方文档介绍
另外附上dubbo管理控制台官网安装教程
以上是关于dubbo入门使用的主要内容,如果未能解决你的问题,请参考以下文章
dubbo远程调用(rpc)-->快速入门+管理控制台+整合Springboot开发