DUBBO框架搭建

由于这发图片麻烦,需要图文版的请加 q群 216396734     
源码下载 https://github.com/liuchunxue/dubbo
DUBBO框架搭建
一.Dubbo介绍
     Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架
二.Dubbo搭建
Dubbo框架搭建主要包括以下几个组成部分
   1)  注册中心安装,这里我使用zookeeper做为注册中心(Registry)
       2)  Dubbo监控中心部署
       3)  爆露服务的服务提供方开发(Provider)
4)  服务运行容器开发(Container)
5)  服务消费放开发

1)zookeeper安装
   注意:安装zookeeper前要先安装jdk,jdk最好使用1.7的版本,不然后面的监控中心会出问题
   官网下载zookeeper http://www.apache.org/dist/zookeeper/
   这里我选择的是3.4.9版本,是目前发布的最新一个稳定版本

解压到/opt/目录
tar -zxvf zookeeper-3.4.9.tar.gz -C /opt/

切换到该目录下的conf目录

因为zk启动的时候会默认加载conf下的zoo.cfg文件,所以我们需要把zoo_sample.cfg复制一份
cp zoo_sample.cfg zoo.cfg

如果不修改dataDir=/tmp/zookeeper目录的话,这个文件默认的就可以了
切换到bin目录启动服务

至此,zookeeper安装完成

2)部署dubbo-admin
Tomcat自己安装。
下载dubbo-admin-2.4.1.war包,在Linux的tomcat部署,先把dubbo-admin-2.4.1放在tomcat的webapps/ROOT下,然后进行解压:
  #jar -xvf dubbo-admin-2.4.1.war
解压以后在webapps/ROOT/WEB-INF目录下有一个dubbo.properties文件编辑该文件
主要就是刚刚安装的zookeeper地址

启动tomcat并访问服务

账户为root密码也是root

我们可以通过


看到我们的zookeeper已经连接上

3)爆露服务的服务提供方开发(Provider)
创建dubbo-api项目并编写代码

package com.feng;

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



该应用发布为jar供服务器和消费端使用

4) 服务运行容器开发(Container)

DemoServiceImpl.java
package com.feng.impl;
import com.feng.DemoService;
public class DemoServiceImpl implements DemoService {
	public String sayHello(String name) throws Exception {
		if("Excetion".equals(name)){
			throw new Exception("myException");
		}
		return "Hello " + name;
	}
}


service.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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	default-lazy-init="false">
	<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
	<dubbo:application name="dubbo_provider"></dubbo:application>
	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry address="zookeeper://192.168.137.129:2181"
		check="false" subscribe="false" register=""></dubbo:registry>
	<dubbo:protocol name="dubbo" port="20880" />
	<!-- 要暴露的服务接口 -->
	<dubbo:service interface="com.feng.DemoService" ref="demoService" />
	<bean id="demoService" class="com.feng.impl.DemoServiceImpl" />
</beans>


编写一个起点测试程序
Provider.java
package com.feng;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "service.xml" });
		System.out.println("发布成功");
		context.start();
		System.in.read(); // 按任意键退出 } }
	}
}


查看dubbo-admin

所以服务已经注册成功。

5)消费者开发

创建dubbo-customer应用


</beans>
customer.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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
	default-lazy-init="false" >
   <dubbo:application name="dubbo_consumer"></dubbo:application>
   <!-- 使用zookeeper注册中心暴露服务地址 -->  
   <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"></dubbo:registry>  
     <!-- 要引用的服务 -->  
   <dubbo:reference interface="com.feng.DemoService" id="demoService"></dubbo:reference>


编写一个启动测试程序
运行成功
看bubbo-admin上也多了一个消费者
Custom.java 并运行
package com.feng;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Custom{
	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "customer.xml" });
		DemoService demoService=(DemoService)context.getBean("demoService");
		String xx=null;
		
		try{
		 xx=demoService.sayHello("feng");
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println(xx);
		 context.start();
		 System.in.read();
	}}

至此。。。。。Dubbo全部搭建成功。  为简单所有服务都没有使用web服务器。

猜你喜欢

转载自lxlcx201.iteye.com/blog/2326176