Dubbo是是一个高性能,基于Java的RPC框架,由阿里巴巴开源。一个分布式的服务框架。能够实现SOA(面向服务的架构)架构。 Dubbo使用的公司:京东、当当、阿里巴巴、中国电信等等。java
如下式架构演变过程(如下案例纯粹为了说明问题,跟业务自己无关):redis
早期,电信只有座机的时候,系统只有一个打电话的功能和一个计费的功能。由于业务单一,因此只有一个系统。spring
后来,电信业务丰富起来了。新增了“短信”、“宽带”、“手机流量”等业务功能。按照常规作法,也只会在原有的“打电话”单一业务系统的基础上,多添加几个业务功能模块而已。全部的业务功能(““电话”、“短信”、“宽带”、“手机流量””)都仍是在一个项目内部。以下图:性能优化
好比淘宝、天猫、阿里巴巴三个项目都须要用到支付,设想,将淘宝、天猫、阿里巴巴三个项目整合成一个项目的三个业务功能模块,将会比较杂乱。因此,出现了淘宝、天猫、阿里巴巴三个独立的项目,相似下图:服务器
经过一步一步演变,架构已经成为如图所示的垂直式架构。可是你们都发现了其中的计费功能出现了4次。这样确定不利于项目的维护和统一配置。(而且上图的计费只是众多可能重复模块中的一员)。因此不得不将多个项目都要使用的相同模块独立出来,共享给业务功能使用。这样,就演变成以下图架构:架构
如图所示,计费被单独提炼出来成为一个独立的app,共其余app共同使用。图中“其余”模块用来代替千千万万相似计费的模块。并发
这样一来,每个方块就是一个独立的应用。这样解决了业务复杂度,将业务模块化、独立化,方便共享和扩展。这样的架构带给咱们须要解决的问题以下:app
RPC(Remote Procedure Call Protocol)远程过程调用协议。服务器A调用服务器B上的方法的一种技术。Dubbo就是一个RPC框架,实现了远程过程调用。负载均衡
Dubbo的原理图框架
dubbo主要的三个要素:一、接口的远程调用二、负载均衡。三、服务自动注册和发现
已经将(源码,笔记,PPT,学习视频)整理好、因资料太多,请加群8-9-7-8-8-9-5-1-0 领取免费的学习资源(有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化、分布式架构等)
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.5</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.11.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.11.RELEASE</version> </dependency> </dependencies>
6.定义Dubbo服务接口
其实就是建立一个独立的module(而且在pom.xml导入以上依赖),在module中建立一些接口和方法(也叫服务)。好比在dubbo_service_interface中建立一个接口IDubboService,代码以下:
package com.javen.dubbo.service; import java.util.List; /** * Dubbo的服务 * @author sun */ public interface IDubboService { List<String> getData(String data); }
7.定义服务提供者(接口具体实现者)
建立另一个module,命名dubbo_provider1(而且在pom.xml导入以上依赖)。将dubbo_service_interface做为依赖添加进来。
一、在其中建立类DubboService,而且继承IDubboService接口。代码以下:
package com.javen.dubbo.provider; import com.javen.dubbo.service.IDubboService; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; /** * 服务提供者 * @author sun */ @Component("dubboService") public class DubboService implements IDubboService { public List<String> getData(String data) { ArrayList<String> list = new ArrayList<String>(); list.add("这是Dubbo中Provider返回的数据:" + data); return list; } }
二、配置spring-dubbo.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:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <context:annotation-config /> <context:component-scan base-package="com.javen.dubbo.provider" /> <!--配置dubbo服务的惟一名称 --> <dubbo:application name="dubbo_provider1" /> <!--将服务注册到redis中,而且配置协议和端口为20880 --> <dubbo:registry address="redis://192.168.72.188:6379" /> <dubbo:protocol name="dubbo" port="20880" /> <!--配置服务接口,ref关联到服务实现类 --> <dubbo:service interface="com.javen.dubbo.service.IDubboService" ref="dubboService" /> </beans>
三、启动Provider,代码以下:
package com.javen.dubbo.provider; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class StartProvider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "spring-dubbo.xml" }); System.out.println("服务1启动~~~"); context.start(); // 线程阻塞:保证服务一直存在,若是线程结束,服务终止 System.in.read(); // press // any key to exit }} } }
六、定义服务消费者(接口的具体调用者)
再建立一个module,命名dubbo_consumer(而且在pom.xml导入以上依赖)。将dubbo_service_interface做为依赖添加进来。
一、配置spring-dubbo.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="demo-consumer" /> <dubbo:registry address="redis://192.168.72.188:6379" /> <dubbo:reference id="demoService" interface="com.javen.dubbo.service.IDubboService" /> </beans>
二、测试
package com.test; import com.javen.dubbo.service.IDubboService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring-dubbo.xml") public class DubboTest { @Resource(name = "demoService") private IDubboService dubboService; @Test public void testDubbo() { List<String> haha = dubboService.getData("haha"); System.out.println(haha.get(0)); } }
Dubbo、MyCat、主从配置读写分离、redis分布式、JTA分布式事务的关系。
dubbo默认每次只访问一个服务器,须要主从配合完成数据同步。
<!--replicate能够实现全部服务器同步写,可是只读取单台服务器。默认是failover,读写都是单台服务器 --> <dubbo:registry cluster="replicate" address="redis://192.168.72.188:6379" />