开发环境:JDK 1.6 tomcat 6.0
jar包 :spring3.1 dubbo-2.5.3 netty-3.10.5
开发
服务接口类 定义接口
服务实现类 定义实现接口的服务类
package com.yjm.provider; // 接口服务类 public interface SearchInfo { public String searchTest(String str); public String searchService(String str); } package com.yjm.providerimp; import com.yjm.provider.SearchInfo; //具体实现的服务 public class SearchInfoImp implements SearchInfo { public String searchService(String str) { return "dubbo test1 str" + str; } public String searchTest(String str) { return "dubbo test2 str" + str; } } package com.yjm.provider; // 接口服务类 public interface ServiceInfo { public String test1(String str2); public String helloDubbo(String str1); } package com.yjm.providerimp; import com.yjm.provider.ServiceInfo; //具体实现的服务 public class ServiceInfoImp implements ServiceInfo { public String helloDubbo(String str1) { System.out.println(str1); return "dubbo:modify" + str1; } public String test1(String str1) { System.out.println(str1); return ("dubbo test方法..modify") + str1; } }
启动服务类
package com.yjm.main; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class StartService { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "dubbo-provider.xml"); context.start(); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
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.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 使用multicast广播注册中心暴露服务地址 --> <!--<dubbo:registry address="multicast://192.168.101.243:1234" />--> <!-- 使用zookeeper注册中心暴露服务地址 --> <!--<dubbo:registry address="zookeeper://192.168.101.243:2181" />--> <!-- 用dubbo协议在20880端口暴露服务--> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="test-app" /> <dubbo:protocol name="dubbo" host="192.168.101.243" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.yjm.provider.ServiceInfo" ref="serviceinfo" registry="N/A"> </dubbo:service> <dubbo:service interface="com.yjm.provider.SearchInfo" ref="searchinfoimp" registry="N/A"></dubbo:service> <!-- 和本地bean一样实现服务 --> <bean id="serviceinfo" class="com.yjm.providerimp.ServiceInfoImp"></bean> <bean id="searchinfoimp" class="com.yjm.providerimp.SearchInfoImp"></bean> </beans>
客户端调用类
package com.yjm.main; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yjm.provider.SearchInfo; import com.yjm.provider.ServiceInfo; public class ProCustomerTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "dubbocustomer.xml"); ServiceInfo si = (ServiceInfo) context.getBean("serviceinfo"); String str = si.helloDubbo("hello"); String str1 = si.test1("tewt..sfsda"); System.out.println(str); System.out.println(str1); SearchInfo sei = (SearchInfo) context.getBean("searchinfo"); String str2 = sei.searchService("searchservice"); String str3 = sei.searchTest("test"); System.out.println(str2); System.out.println(str3); } }
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.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 使用zookeeper注册中心暴露服务地址 --> <!--<dubbo:registry address="zookeeper://115.28.189.59:2181" />--> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="test-app-customer" /> <dubbo:reference id="serviceinfo" interface="com.yjm.provider.ServiceInfo" url="dubbo://192.168.101.243:20880/"> </dubbo:reference> <dubbo:reference id="searchinfo" interface="com.yjm.provider.SearchInfo" url="dubbo://192.168.101.243:20880/"></dubbo:reference> </beans>
输出结果:
Feb 17, 2016 10:35:19 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org[email protected]ab50cd: startup date [Wed Feb 17 10:35:19 CST 2016]; root of context hierarchy Feb 17, 2016 10:35:19 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [dubbocustomer.xml] Feb 17, 2016 10:35:19 AM com.alibaba.dubbo.common.logger.jcl.JclLogger info 信息: using logger: com.alibaba.dubbo.common.logger.jcl.JclLoggerAdapter Feb 17, 2016 10:35:19 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.s[email protected]1d6776d: defining beans [test-app-customer,serviceinfo,searchinfo]; root of factory hierarchy Feb 17, 2016 10:35:21 AM com.alibaba.dubbo.common.logger.jcl.JclLogger info 信息: [DUBBO] Successed connect to server /192.168.101.243:20880 from NettyClient 192.168.101.243 using dubbo version 2.5.3, channel is NettyChannel [channel=[id: 0x83325a91, /192.168.101.243:3713 => /192.168.101.243:20880]], dubbo version: 2.5.3, current host: 192.168.101.243 Feb 17, 2016 10:35:21 AM com.alibaba.dubbo.common.logger.jcl.JclLogger info 信息: [DUBBO] Start NettyClient lxm-PC/192.168.101.243 connect to the server /192.168.101.243:20880, dubbo version: 2.5.3, current host: 192.168.101.243 Feb 17, 2016 10:35:21 AM com.alibaba.dubbo.common.logger.jcl.JclLogger info 信息: [DUBBO] Refer dubbo service com.yjm.provider.ServiceInfo from url dubbo://192.168.101.243:20880/com.yjm.provider.ServiceInfo?application=test-app-customer&dubbo=2.5.3&interface=com.yjm.provider.ServiceInfo&methods=test1,helloDubbo&pid=6288&side=consumer×tamp=1455676520551, dubbo version: 2.5.3, current host: 192.168.101.243 dubbo:modifyhello dubbo test方法..modifytewt..sfsda Feb 17, 2016 10:35:21 AM com.alibaba.dubbo.common.logger.jcl.JclLogger info 信息: [DUBBO] Refer dubbo service com.yjm.provider.SearchInfo from url dubbo://192.168.101.243:20880/com.yjm.provider.SearchInfo?application=test-app-customer&dubbo=2.5.3&interface=com.yjm.provider.SearchInfo&methods=searchService,searchTest&pid=6288&side=consumer×tamp=1455676521541, dubbo version: 2.5.3, current host: 192.168.101.243 dubbo test1 strsearchservice dubbo test2 strtest
时间: 2024-10-29 19:10:54