Webservice之徒手搭建Dubbo(定義服務,供應商,消費者)

來源:互聯網
上載者:User

標籤:

前提準備:

  在本次實驗之前,需要準備一下幾個包: 

                        

  • 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(定義服務,供應商,消費者)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.