基於開源Dubbo分佈式RPC服務框架的部署整合

詳細介紹請參照官方地址:http://alibaba.github.io/dubbo-doc-static/Home-zh.htm,再也不重複描述,本文主要記錄了詳細的開發整合步驟。html

1、源碼構建

這個網上不少教程,你們能夠google/baidu,或者直接下載我已經構建好的。我作了更改,兼容JDK1.8.java

網盤下載 http://pan.baidu.com/s/1sjFqt8T 密碼:60u7git

2、Zookeeper部署(Registry 服務註冊中心)

    Dubbo 缺省配置經過 multicast 註冊中心廣播實現 Provider 和 Consumer 之間的簡單遠程過程調用(Simple RPC),不須要經過 Registry 註冊中心進行註冊調度,相似於spring rmi remoting調用,但因爲不是Cluster部署,因此做爲分佈式RPC框架官方建議使用 Zookeeper 做爲Registry註冊中心服務器(一樣支持Redis)實現服務的註冊、發現、路由功能。github

    Dubbo在Zookeeper服務器端只增長了dubbo數據節點(以下圖),無需其餘任何配置,因此只需安裝或使用現有 Zookeeper 服務器便可,關於Zookeeper的安裝部署能夠參照以前的博文:http://my.oschina.net/ihanfeng/blog/525255spring

3、服務提供方(Provider)

一、建立基於maven管理的dubbo-provider項目。
apache

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.hanfeng.dubbo</groupId>
	<artifactId>dubbo-provider</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>dubbo-provider Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.0.8.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
			<exclusions>
				<exclusion>
					<groupId>com.sun.jmx</groupId>
					<artifactId>jmxri</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jdmk</groupId>
					<artifactId>jmxtools</artifactId>
				</exclusion>
				<exclusion>
					<groupId>javax.jms</groupId>
					<artifactId>jms</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>

		<dependency>
			<groupId>com.netflix.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>1.1.16</version>
		</dependency>
	</dependencies>

	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<warSourceDirectory>WebRoot</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

二、建立測試服務接口(HelloService)服務器

package com.hanfeng.dubbo.provider;

public interface HelloService {
	public String sayHello(String text);
}

三、建立服務接口實現類(HelloServiceImpl)app

package com.hanfeng.dubbo.provider.impl;

import com.alibaba.dubbo.rpc.RpcContext;
import com.hanfeng.dubbo.provider.HelloService;

public class HelloServiceImpl implements HelloService{

	@Override
	public String sayHello(String text) {
		return "hello "+ text + "/n response form provider: " + RpcContext.getContext().getLocalAddress();
	}

}

四、經過spring 集成並配置dubbo測試服務,同時指定registry的zookeeper服務器地址。框架

<?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 服務提供者應用名稱 -->
    <dubbo:application name="dubbo-provider" />

    <!--dubbo 註冊中心-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!--服務提供者 端口-->
    <dubbo:protocol name="dubbo" port="30001" />

    <!--dubbo提供服務-->
    <dubbo:service interface="com.hanfeng.dubbo.provider.HelloService" ref="helloService" />

    <!--spring bean 對象-->
    <bean id="helloService" class="com.hanfeng.dubbo.provider.impl.HelloServiceImpl" /> 
</beans>

五、編寫控制檯程序啓動spring容器,編譯並打包Provider.jarmaven

package com.hanfeng.dubbo.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderTest {
	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-dubbo-provider.xml"});
        context.start();
        System.out.println("請按任意鍵退出");
        System.in.read(); // 按任意鍵退出
    }
}

4、服務消費方開發(Consumer)

一、經過spring 配置指定registry的zookeeper地址,實現對dubbo遠程服務的調用

<?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="dubbo-consumer" />

    <dubbo:registry address="zookeeper://127.0.0.1:2181" /> 
    
    <!-- 生成遠程服務代理,能夠和本地bean同樣使用helloService -->
   <dubbo:reference  interface="com.hanfeng.dubbo.provider.HelloService" id="helloService" />
</beans>

二、編寫調用測試客戶端代碼,從容器中獲取遠程bean並調用。

package com.hanfeng.dubbo.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hanfeng.dubbo.provider.HelloService;

public class ConsumerTest {
	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-dubbo-consumer.xml"});
        context.start();
  
        HelloService hellService = (HelloService)context.getBean("helloService"); // 獲取遠程服務代理
        String res = hellService.sayHello("world"); // 執行遠程方法
        System.out.println( res ); // 顯示調用結果
    }
}

5、deno測試

首先啓動zookeeper服務器,再啓動dubbo服務器。

測試開始,咱們首先運行ProviderTest,而後運行ConsumerTest

輸出結果:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
hello world/n response form provider: 192.168.5.13:30001

登陸 http://127.0.0.1:8080/,咱們發現服務數、應用數、提供者都有對應的數據。

源代碼:連接:http://pan.baidu.com/s/1sj8AyNb 密碼:hwd* (上面案例還須要源代碼得話,密碼最後一個字母本身嘗試)

相關文章
相關標籤/搜索