本例子分为客户端和服务端,
其中客户端核心类为:
App ? ? ? ? ? ? ? ? 客户端运行Main主类
DemoService ?接口(公共部分包名——类名和服务端相同)
User ? ? ? ? ? ? ? ?VO对象,必须实现序列化,供VO接口使用(公共部分包名——类名和服务端相同)
?
服务端核心类为:
App ? ? ? ? ? ? ? ? ? ? ?客户端运行Main主类
DemoService ? ? ? ?接口 (公共部分包名——类名和客户端相同)
DemoServiceImpl ?接口实现类,提供业务方法供客户端调用
User ? ? ? ? ? ? ? ? ? ? VO对象,必须实现序列化,供VO接口使用(公共部分包名——类名和客户端相同)
?
?
具体代码如下:
?
?
?服务端:
?
?
客户端:
运行的Main主类
package cn.com.dubbo.dubboclient;
?
import java.io.IOException;
import java.util.List;
?
import org.springframework.context.support.ClassPathXmlApplicationContext;
?
import cn.com.dubbo.dubbotest.DemoService;
?
/**
?<dependency>
? ? ? ? ? ? <groupId>com.alibaba</groupId>
? ? ? ? ? ? <artifactId>dubbo</artifactId>
? ? ? ? ? ? <version>2.5.3</version>
? ? ? ? </dependency>
? ? ? ??
? ? ? ? ?<dependency>
? ? ? ? ? ? <groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
? ? ? ? </dependency>
??
? ? ? <dependency>
? ? ? <groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
? ? ? </dependency>
?*/
public class App?
{
? ? public static void main( String[] args ) throws IOException
? ? {
? ? 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.out.println(demoService.hehe()); ?
? ? ? ? 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: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="multicast://224.5.6.7:1234" /> --> ?
? ? <dubbo:registry address="zookeeper://192.168.1.111:2181" /> ?
??
? ? <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService --> ?
? ? <dubbo:reference id="demoService" ?
? ? ? ? interface="cn.com.dubbo.dubbotest.DemoService" /> ?
??
</beans> ?
?
服务端:
?
运行的Main主类
package cn.com.dubbo.dubbotest;
?
import java.io.IOException;
?
import org.springframework.context.support.ClassPathXmlApplicationContext;
?
/**
?<dependency>
? ? ? ? ? ? <groupId>com.alibaba</groupId>
? ? ? ? ? ? <artifactId>dubbo</artifactId>
? ? ? ? ? ? <version>2.5.3</version>
? ? ? ? </dependency>
? ? ? ??
? ? ? ? ?<dependency>
? ? ? ? ? ? <groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
? ? ? ? </dependency>
??
? ? ? <dependency>
? ? ? <groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
? ? ? </dependency>
?*/
public class App?
{
? ? public static void main( String[] args ) throws IOException
? ? {
? ? ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}); ?
? ? ? ? ?context.start(); ?
? ??
? ? ? ? ?System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟 ?
? ? }
}
?
DemoServiceImpl 接口实现类
package cn.com.dubbo.dubbotest;
?
import java.util.ArrayList;
import java.util.List;
?
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("男"); ?
? ? ? ? ??
? ? ? ? User u2 = new User(); ?
? ? ? ? u2.setName("tom"); ?
? ? ? ? u2.setAge(21); ?
? ? ? ? u2.setSex("女"); ?
? ? ? ? ??
? ? ? ? User u3 = new User(); ?
? ? ? ? u3.setName("rose"); ?
? ? ? ? u3.setAge(19); ?
? ? ? ? u3.setSex("女"); ?
? ? ? ? ??
? ? ? ? list.add(u1); ?
? ? ? ? list.add(u2); ?
? ? ? ? list.add(u3); ?
? ? ? ? return list; ?
? ? } ?
} ?
配置文件:
<?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="xixi_provider" ?/> ?
? ?
? ? <!-- 使用multicast广播注册中心暴露服务地址 ??
? ? <dubbo:registry address="multicast://224.5.6.7:1234" />--> ?
? ??
? ? <!-- 使用zookeeper注册中心暴露服务地址 --> ?
? ? <dubbo:registry address="zookeeper://192.168.1.111:2181" /> ??
? ??
? ? <!-- 用dubbo协议在20880端口暴露服务 --> ?
? ? <dubbo:protocol name="dubbo" port="20880" /> ?
? ?
? ? <!-- 声明需要暴露的服务接口 --> ?
? ? <dubbo:service interface="cn.com.dubbo.dubbotest.DemoService" ref="demoService" /> ?
??
? ? <!-- 和本地bean一样实现服务 --> ?
? ? <bean id="demoService" class="cn.com.dubbo.dubbotest.DemoServiceImpl" /> ?
? ? ??
</beans> ?
?
?
?
公共部分代码:
UserVO类
package cn.com.dubbo.dubbotest;
?
import java.io.Serializable;
?
//java.lang.IllegalStateException: Serialized class cn.com.dubbo.dubbotest.User must implement java.io.Serializable
public class User implements Serializable{
?
private String name;
private int age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
?
?
}
?
接口类:
package cn.com.dubbo.dubbotest;
?
import java.util.List; ?
?
public interface DemoService { ?
??
? ? String sayHello(String name); ?
??
? ? public List getUsers(); ?
??
} ?
?
?
运行结果验证
?
?
错误解决方案:
缺少VO类
?
原文:http://gaojingsong.iteye.com/blog/2305960