Dubbo的优缺点和搭建

一、什么是Dubbo?

Dubbox 是一个分布式服务框架,其前身是阿里巴巴开源项目Dubbo ,被国内电商及互联网项目中使用,后期阿里巴巴停止了该项目的维护,当当网便在Dubbo基础上进行优化,并继续维护,为了与原有的Dubbo区分,故将其命名为Dubbox。
Dubbox 致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbox就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbox这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架。
在这里插入图片描述
节点角色说明:
 Provider: 暴露服务的服务提供方。
 Consumer: 调用远程服务的服务消费方。
 Registry: 服务注册与发现的注册中心。
 Monitor: 统计服务的调用次调和调用时间的监控中心。
 Container: 服务运行容器。
调用关系说明:
 0. 服务容器负责启动,加载,运行服务提供者。
 1. 服务提供者在启动时,向注册中心注册自己提供的服务。
 2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
 3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
 4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
 5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

二、Dubbo的优缺点

优点:

  1. 透明化的远程方法调用

像调用本地方法一样调用远程方法;只需简单配置,没有任何API侵入。

  1. 软负载均衡及容错机制

可在内网替代nginx lvs等硬件负载均衡器。

  1. 服务注册中心自动注册 & 配置管理*

不需要写死服务提供者地址,注册中心基于接口名自动查询提供者ip。
使用类似zookeeper等分布式协调服务作为服务注册中心,可以将绝大部分项目配置移入zookeeper集群。

  1. 服务接口监控与治理

Dubbo-admin与Dubbo-monitor提供了完善的服务接口管理与监控功能,针对不同应用的不同接口,可以进行 多版本,多协议,多注册中心管理。

缺点
只支持JAVA语言*

## 三、开发步骤:

服务提供者开发

(1)创建Maven工程(WAR)dubboxdemo-service ,在pom.xml中引入依赖 。

	<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
 <groupId>com.*.*</groupId>
<artifactId>***</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>  
<properties>		
	<spring.version>4.2.4.RELEASE</spring.version>
</properties>    
<dependencies>
	<!-- Spring -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-beans</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jdbc</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aspects</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jms</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context-support</artifactId>
		<version>${spring.version}</version>
	</dependency>	
	<!-- dubbo相关 -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>dubbo</artifactId>
		<version>2.8.4</version>			
	</dependency>
	<dependency>
		<groupId>org.apache.zookeeper</groupId>
		<artifactId>zookeeper</artifactId>
		<version>3.4.6</version>
	</dependency>
	<dependency>
		<groupId>com.github.sgroschupf</groupId>
		<artifactId>zkclient</artifactId>
		<version>0.1</version>
	</dependency>		
	<dependency>
		<groupId>javassist</groupId>
		<artifactId>javassist</artifactId>
		<version>3.11.0.GA</version>
	</dependency>		
</dependencies>
<build>  
  <plugins>
      <plugin>  
          <groupId>org.apache.maven.plugins</groupId>  
          <artifactId>maven-compiler-plugin</artifactId>  
          <version>2.3.2</version>  
          <configuration>  
              <source>1.7</source>  
              <target>1.7</target>  
          </configuration>  
      </plugin>  
      <plugin>
			<groupId>org.apache.tomcat.maven</groupId>
			<artifactId>tomcat7-maven-plugin</artifactId>
			<configuration>
				<!-- 指定端口 -->
				<port>8081</port>
				<!-- 请求路径 -->
				<path>/</path>
			</configuration>
  	  </plugin>
  </plugins>  
</build>
</project>

(2)在工程的webapps下创建WEB-INF文件夹,创建web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">	
<!-- 加载spring容器 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>		
	<listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
</listener>	
</web-app>

(3)创建业务接口

(4)创建业务实现类

(5)编写配置文件

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 	
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 	
	http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">
<dubbo:application name="dubboxdemo-service"/>  
<dubbo:registry address="zookeeper://192.168.25.132:2181"/> 
<dubbo:annotation package="com.bawei.dubboxdemo.service" /> 
</beans>

(6)测试运行

tomcat7:run

服务消费者开发

开发步骤:

1)创建Maven工程(WAR)dubboxdemo-web ,在pom.xml引入依赖 ,同“dubboxdemo-service”工程。区别就是把tomcat插件的运行端口改为8082 。
(2)在webapps目录下创建WEB-INF 目录,并创建web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">	
 <!-- 解决post乱码 -->
<filter>
	<filter-name>CharacterEncodingFilter</filter-name>		
	<filter-class>
	org.springframework.web.filter.CharacterEncodingFilter</filter-class>
	<init-param>
		<param-name>encoding</param-name>
		<param-value>utf-8</param-value>
	</init-param>
	<init-param>  
        <param-name>forceEncoding</param-name>  
        <param-value>true</param-value>  
    </init-param>  
</filter>
<filter-mapping>
	<filter-name>CharacterEncodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>		
<servlet>
	<servlet-name>springmvc</servlet-name>  	
	<servlet-class>
		org.springframework.web.servlet.DispatcherServlet
	</servlet-class>
<!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
<init-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext-web.xml</param-value>
</init-param>
</servlet>  
<servlet-mapping>
	<servlet-name>springmvc</servlet-name>
	<url-pattern>*.do</url-pattern>
 </servlet-mapping>
</web-app>

(3)拷贝业务接口

(4)编写Controller

(5)编写spring配置文件

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">

<mvc:annotation-driven >
	<mvc:message-converters register-defaults="false">
		<bean class="org.springframework.http.converter.StringHttpMessageConverter">  
			<constructor-arg value="UTF-8" />
		</bean>  
	</mvc:message-converters>	
</mvc:annotation-driven>
<!-- 引用dubbo 服务 -->
	<dubbo:application name="dubboxdemo-web" />
	<dubbo:registry address="zookeeper://10.211.55.16:2181"/>
    <dubbo:annotation package="com.bawei.dubboxdemo.controller" />
</beans>

(6)测试运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值