您的位置:首页 > 其它

Dubbo入门案例

2015-08-12 15:26 381 查看
有关Dubbo的相关介绍, 请参考官方文档,地址:http://dubbo.io/

下面主要介绍如何用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: