标签:
dubbo2.4.11源码编译 http://my.oschina.net/penngo/blog/494838
同样以两个简单的登录和注册服务为例,之前已经用thrift实现过,thrift版本的实现请参考Thrift的java和php http://my.oschina.net/penngo/blog/489311
客户端和服务器端共用接口类
登录接口类
package com.penngo.dubbo.service; public interface LoginService { public User login(String name, String psw); }
注册接口类
package com.penngo.dubbo.service; public interface RegisterService { public User createUser(String name, String psw); }
服务器端
登录接口实现
package com.penngo.dubbo.serviceimpl; import com.penngo.dubbo.service.LoginService; import com.penngo.dubbo.service.User; 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; } }
注册接口实现
package com.penngo.dubbo.serviceimpl; import com.penngo.dubbo.service.RegisterService; import com.penngo.dubbo.service.User; 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; } }
服务器端利用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>
服务启动类,发布服务
package com.penngo.dubbo.server; import org.springframework.context.support.ClassPathXmlApplicationContext; 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>
客户端调用
package com.penngo.dubbo.client; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.penngo.dubbo.service.LoginService; import com.penngo.dubbo.service.RegisterService; import com.penngo.dubbo.service.User; 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()); // 显示调用结果 } }
源码稍后提供下载
标签:
原文地址:http://my.oschina.net/penngo/blog/495613