标签:
前提准备:
在本次实验之前,需要准备一下几个包:
为了这句话:ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
一、定义服务(api)
定义一个接口GService:
入参:String
返回值:String
package com.dubbo.zz; public interface GService { String sayHello(String name); }
二、注册供应商
项目结果图如下:
GServiceImpl.java:
package com.dubbo.provider; import com.dubbo.zz.GService; public class GServiceImpl implements GService { public String sayHello(String name) { return "Hello "+name; } }
Provider.java:
package com.dubbo.provider; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); System.out.println("service have startup"); context.start(); System.in.read(); } }
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="sdfsd"/> <!-- 使用multicast广播注册中心暴露服务地址 --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.dubbo.zz.GService" ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.dubbo.provider.GServiceImpl"/> </beans>
运行结果如图:
三、消费者(可以是接口测试者,也可以是服务调用方)
注意:为了方便调试,可以另起一个Eclipse应用。
项目结果图:
consumer.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="consum"/> <!-- 使用multicast广播注册中心暴露服务地址 --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.dubbo.zz.GService" /> </beans>
TestServices.java:
package com.dubbo.consumer; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dubbo.zz.GService; public class TestServices { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); System.out.println("consumer have startup"); GService service=(GService)context.getBean("demoService"); String str=service.sayHello("fjsdfsd"); System.out.println(str); } }
运行结果如图:
至此,演示完毕,谢谢大家。转载时请注明出处,谢谢:http://www.cnblogs.com/shoubianxingchen/p/4308229.html
此为dubbo入门级演示,后面还有很多东西的。
Webservice之徒手搭建Dubbo(定义服务,供应商,消费者)
标签:
原文地址:http://www.cnblogs.com/shoubianxingchen/p/4308229.html