標籤:
前提準備:
在本次實驗之前,需要準備一下幾個包:
- Spring中的aop、beans、context、core、expression以及struts中的commons-logging、javassist等都是為了支援配置以及運行時不會報錯:
為了這句話:ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
- dubbo包和netty則是dubbo服務的必須包
- ssssssss.jar則是自己定義的api服務
一、定義服務(api)
定義一個介面GService:
入參:String
傳回值:String
package com.dubbo.zz;public interface GService { String sayHello(String name);}View Code
二、註冊供應商
項目結果圖如下:
GServiceImpl.java:
package com.dubbo.provider;import com.dubbo.zz.GService;public class GServiceImpl implements GService { public String sayHello(String name) { return "Hello "+name; }}View Code
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(); }}View Code
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>
View Code
運行結果
三、消費者(可以是介面測試者,也可以是服務調用方)
注意:為了方便調試,可以另起一個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>
View Code
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); }}View Code
運行結果
至此,示範完畢,謝謝大家。轉載時請註明出處,謝謝:http://www.cnblogs.com/shoubianxingchen/p/4308229.html
此為dubbo入門級示範,後面還有很多東西的。
Webservice之徒手搭建Dubbo(定義服務,供應商,消費者)