一.编写客户端和服务器端共用接口类
1.登录接口类
public interface LoginService {
public User login(String name, String psw);
}
2.注册接口类
public interface RegisterService {
public User createUser(String name, String psw);
}
二服务器端
1.登录接口实现
public class LoginServiceImpl implements LoginService{
public User login(String name, String psw){
User user = null;
if(name.equals(“penngo”) && psw.equals(“123”)){
user = new User();
user.setId(1);
user.setName(“penngo”);
}
return user;
}
}
2.注册接口实现
public class RegisterServiceImpl implements RegisterService{
public User createUser(String name, String psw){
User user = new User();
user.setId(2);
user.setName(name);
user.setPassword(psw);
return user;
}
}
3.服务器端利用spring发布服务dubbo-penngo-provider.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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd”>
<!– 提供方应用信息,用于计算依赖关系 –>
<dubbo:application name=”provider-penngo-app” />
<!– 使用zookeeper广播注册中心暴露服务地址 –>
<dubbo:registry address=”zookeeper://127.0.0.1:2181″ />
<!– <dubbo:registry address=”multicast://224.5.6.7:1234″ /> –>
<!– 用dubbo协议在20880端口暴露服务 –>
<dubbo:protocol name=”dubbo” port=”20880″ />
<!– 和本地bean一样实现服务 –>
<bean id=”loginService” class=”com.penngo.dubbo.serviceimpl.LoginServiceImpl” />
<bean id=”registerService” class=”com.penngo.dubbo.serviceimpl.RegisterServiceImpl” />
<!– 声明需要暴露的服务接口 –>
<dubbo:service interface=”com.penngo.dubbo.service.LoginService” ref=”loginService” />
<dubbo:service interface=”com.penngo.dubbo.service.RegisterService” ref=”registerService” />
</beans>
4.服务启动类,发布服务
public class Server {
public static void main(String[] args) throws Exception{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {“dubbo-penngo-provider.xml”});
context.start();
System.in.read(); // 按任意键退出
}
}
客户端调用服务很简单,只需一个xml文件和一个java调用类
客户端利用spring配置可用远程服务器
<?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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd”>
<dubbo:application name=”consumer-penngo-app” />
<dubbo:registry address=”zookeeper://127.0.0.1:2181″ />
<!– 使用multicast广播注册中心暴露发现服务地址 –>
<!– <dubbo:registry address=”multicast://224.5.6.7:1234″ /> –>
<!– 生成远程服务代理, –>
<dubbo:reference id=”loginService” interface=”com.penngo.dubbo.service.LoginService” />
<dubbo:reference id=”registerService” interface=”com.penngo.dubbo.service.RegisterService” />
</beans>
客户端调用
public class Client {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {“dubbo-penngo-consumer.xml”});
context.start();
LoginService loginService = (LoginService)context.getBean(“loginService”); // 获取远程服务代理
User user = loginService.login(“penngo”, “123”);
System.out.println( user.getId() + ” ” + user.getName()); // 显示调用结果
RegisterService registerService = (RegisterService)context.getBean(“registerService”);
User user2 = registerService.createUser(“penngo”, “123”);
System.out.println( user2.getId() + ” ” + user2.getName()); // 显示调用结果
}
}
转载请注明来源网站:blog.ytso.com谢谢!
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/7019.html