您的位置:首页 > 其它

基础篇--简单的Dubbo提供者provider与消费者consumer

2016-05-30 14:54 567 查看
一、提供方Provider1.接口
[java] view plain copy




public interface DemoService {

String sayHello(String name);

public List getUsers();

}

2.实现类

[java] view plain copy




public class DemoServiceImpl implements DemoService {

public String sayHello(String name) {

return "Hello " + name;

}

public List getUsers() {

List list = new ArrayList();

User u1 = new User();

u1.setName("jack");

u1.setAge(20);

u1.setSex("m");

User u2 = new User();

u2.setName("tom");

u2.setAge(21);

u2.setSex("m");

User u3 = new User();

u3.setName("rose");

u3.setAge(19);

u3.setSex("w");

list.add(u1);

list.add(u2);

list.add(u3);

return list;

}

}

3.model类

[java] view plain copy




/**

* 实现序列化

* @author Administrator

*

*/

public class User implements Serializable {

private static final long serialVersionUID = 1L;

private int age;

private String name;

private String sex;

public User() {

super();

}

public User(int age, String name, String sex) {

super();

this.age = age;

this.name = name;

this.sex = sex;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

}

4.applicationContenxt.xml配置
注意:提供方需要指定端口,不同提供方需要使用不同端口,不然会有端口冲突,使用的端口需要在防火墙iptables中配置允许通过消费方不用指定端口
[html] view plain copy




<?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" />

<!-- 使用zookeeper注册中心暴露服务地址 -->

<dubbo:registry address="zookeeper://192.168.1.121:2181" />

<!-- 用dubbo协议在20880端口暴露服务 -->

<dubbo:protocol name="dubbo" port="20880" />

<!-- 声明需要暴露的服务接口 -->

<dubbo:service interface="com.unj.dubbotest.provider.DemoService"

ref="demoService" />

<!-- 具体的实现bean,用来注入-->

<bean id="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />

</beans>

5.提供方Provider测试方法

[java] view plain copy




public class Provider {

public static void main(String[] args) throws Exception {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

new String[] { "applicationContext.xml" });

context.start();

System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟

}

}

运行结果:



6.在Dubbo管理控制台上查看发布的提供方



二、消费方Consume1.引用提供方的接口2.消费方applictionContenxt.xml配置
[html] view plain copy




<?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="hehe_consumer" />

<!-- 使用zookeeper注册中心暴露服务地址 -->

<dubbo:registry address="zookeeper://192.168.1.121:2181" />

<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->

<dubbo:reference id="demoService"

interface="com.unj.dubbotest.provider.DemoService" />

</beans>

3.消费方调用提供方

[java] view plain copy




public class Consumer {

public static void main(String[] args) throws Exception {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

new String[] { "applicationContext.xml" });

context.start();

DemoService demoService = (DemoService) context.getBean("demoService");

String hello = demoService.sayHello("tom");

System.out.println(hello);

List list = demoService.getUsers();

if (list != null && list.size() > 0) {

for (int i = 0; i < list.size(); i++) {

System.out.println(list.get(i));

}

}

System.in.read();

}

}

控制台信息输出



dubbo管理控制台查看
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Dubbo