Dubbo入门案例
2015-08-12 15:26
381 查看
有关Dubbo的相关介绍, 请参考官方文档,地址:http://dubbo.io/
下面主要介绍如何用maven搭建一个dubbo的demo,Dubbo简单的来说就是由,服务提供者,服务消费者和注册中心构成
上面这张图是从官网上截的图,哈哈
闲话不多说,直接上代码
1) 抽取出公共的代码:如domain,和到需要暴露的接口等
服务提供方:
服务消费方:
先运行 provider,启动服务,在运行 ConSumer
源代码下载:dubbo-demo
下面主要介绍如何用maven搭建一个dubbo的demo,Dubbo简单的来说就是由,服务提供者,服务消费者和注册中心构成
上面这张图是从官网上截的图,哈哈
闲话不多说,直接上代码
1) 抽取出公共的代码:如domain,和到需要暴露的接口等
public class User implements Serializable{ private String name; private int age; private String sex; public User(String name, int age, String sex) { this.name = name; this.age = age; this.sex = sex; } public User() { } //省略getter/setter
public interface DemoService { String sayHello(String name); List getUsers(); }
服务提供方:
import java.util.ArrayList; import java.util.List; import com.baofoo.dubbotest.domian.User; import com.baofoo.dubbotest.server.DemoService; public class DemoServiceImpl implements DemoService { public List getUsers() { List<User> list =new ArrayList<User>(); User u1=new User("AAA", 12, "M"); User u2=new User("BBB", 13, "F"); User u3=new User("CCC", 12, "M"); list.add(u1); list.add(u2); list.add(u3); return list; } public String sayHello(String name) { return "Hello "+ name; } }apploicationContext.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:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 具体的实体bean --> <bean id="demoService" class="com.baofoo.server.impl.DemoServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="demo_provider"/> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://10.0.20.175:2181" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.baofoo.dubbotest.server.DemoService" ref="demoService"></dubbo:service> </beans>
public class Provider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml"); context.start(); System.in.read();//// 为保证服务一直开着,利用输入流的阻塞来模拟 } }
服务消费方:
<?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:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 通过Spring配置引用远程服务 --> <!-- 消息方应用名,用于计算依赖关系,不匹配条件,不要与提供方一样 --> <dubbo:application name="demo_consume"/> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://10.0.20.175:2181" /> <!-- 生成远程代理,可以像使用本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.baofoo.dubbotest.server.DemoService" /> </beans>
public class ConSumer { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext-customer.xml"); context.start(); DemoService demoService = (DemoService) context.getBean("demoService"); String hello = demoService.sayHello("AAA"); System.out.println(hello); List<User> list = demoService.getUsers(); for (User u : list) { System.out.println(u); } System.in.read(); } }
先运行 provider,启动服务,在运行 ConSumer
源代码下载:dubbo-demo
相关文章推荐
- 【Java】打印所有的水仙花数
- 9S12汇编指令【HCS12】
- 每个Android开发者都应该了解的资源列表
- 比较好用的dns列表
- js深拷贝和浅拷贝----尊重原创,转自:http://www.cnblogs.com/yichengbo/archive/2014/07/10/3835882.html
- hdu5372(2015多校7)--Segment Game(树状数组)
- HBase性能优化
- MapReduce原理
- leetCode #204 Count Primes
- linux的内核、内核态和用户态的区别
- AllJoyn
- windows下mysql日志开启与查询
- Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签
- 硬件十万个为什么——运放篇(二)带反馈运放电路的稳定性标准
- Qt 常用类(18)——QStandardItemModel
- Android WebView与JavaScript交互实现Web App
- 深入JavaScript(4)立即调用的函数表达式
- Java 注解
- hdu 5375 Gray code (dp)
- java 泛型