淘寶SOA框架dubbo學習(1)--first demo

部署開發,須要三部分:服務提供者、服務容器、服務消費者java

本人用 eclipse 開發git

一、服務提供者jar生成github

A、項目截圖spring


B、源碼:app

package com.alibaba.dubbo.demo;

public interface DemoService {
	String sayHello(String name);
}
package com.alibaba.dubbo.demo.provider;

import com.alibaba.dubbo.demo.DemoService;

public class DemoServiceImpl implements DemoService{
	public String sayHello(String name) {
		return "Hello " + name;
	}
}

provider.xmleclipse

<?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">
 
    <!-- 提供方應用信息,用於計算依賴關係 -->
    <dubbo:application name="hello-world-app"  />
 
    <!-- 使用multicast廣播註冊中心暴露服務地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
 
    <!-- 用dubbo協議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 聲明須要暴露的服務接口 -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
 
    <!-- 和本地bean同樣實現服務 -->
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
 
</beans>

C、生成jar包
ide

右鍵項目名稱->export->java ->jar file - > 點擊下一步
測試

按下圖所示,進行選擇
spa

點擊finish,jar包導出到路徑:C:\Users\michael\Desktop\app.jar代理

注:jar包叫什麼名字,沒任何關係

二、部署服務容器:

下載

dubbo-demo-provider-2.5.4-SNAPSHOT-assembly.tar.gz

下載頁面

http://alibaba.github.io/dubbo-doc-static/Download-zh.htm

注:可能下載不下來,我是從dubbo的QQ羣裏下載下來的

解壓縮後,容器部署完成

三、部署服務

第2步中解壓縮的目錄,將第2步中的jar包,放入到 dubbo-demo-provider-2.5.4-SNAPSHOT\conf或者dubbo-demo-provider-2.5.4-SNAPSHOT\lib目錄下

進入demo-provider-2.5.4-SNAPSHOT\bin

注:修改bin\start.bat文件,classpath中加入:..\conf\app.jar

雙擊:start.bat

服務正式啓動

四、在eclipse中寫消費者程序測試部署的服務

A、項目分佈

B、源碼

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.demo.DemoService;

public class Consumer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "classpath:consumer.xml" });
		context.start();

		DemoService demoService = (DemoService) context.getBean("demoService"); // 獲取遠程服務代理
		String hello = demoService.sayHello("world"); // 執行遠程方法

		System.out.println(hello); // 顯示調用結果
	}
}
package com.alibaba.dubbo.demo;

public interface DemoService {
	String sayHello(String name);
}

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.xsd        
    http://code.alibabatech.com/schema/dubbo        
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
    <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方同樣 -->
    <dubbo:application name="consumer-of-helloworld-app"  />
 
    <!-- 使用multicast廣播註冊中心暴露發現服務地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
 
    <!-- 生成遠程服務代理,能夠和本地bean同樣使用demoService -->
    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />
 
</beans>


C、啓動

Consumer類中的main方法

D、OK,完事

相關文章
相關標籤/搜索