Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架
1.远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
2.集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
3.自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
1.透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
2.软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
3.服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。
 
  节点角色说明
| 节点 | 角色说明 | 
|---|---|
| Provider | 暴露服务的服务提供方 | 
| Consumer | 调用远程服务的服务消费方 | 
| Registry | 服务注册与发现的注册中心 | 
| Monitor | 统计服务的调用次调和调用时间的监控中心 | 
| Container | 服务运行容器 | 
1.服务容器负责启动,加载,运行服务提供者。
2.服务提供者在启动时,向注册中心注册自己提供的服务。
3.服务消费者在启动时,向注册中心订阅自己所需的服务。
4.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
5.服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
6.服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
当服务集群规模进一步扩大,带动IT治理结构进一步升级,需要实现动态部署,进行流动计算,现有分布式服务架构不会带来阻力。下图是未来可能的一种架构:
 
  节点角色说明
| 节点 | 角色说明 | 
|---|---|
| Deployer | 自动部署服务的本地代理 | 
| Repository | 仓库用于存储服务应用发布包 | 
| Scheduler | 调度中心基于访问压力自动增减服务提供者 | 
| Admin | 统一管理控制台 | 
| Registry | 服务注册与发现的注册中心 | 
| Monitor | 统计服务的调用次调和调用时间的监控中心 | 
Dubbo 采用全 Spring 配置方式,透明化接入应用,对应用没有任何 API 侵入,只需用 Spring 加载 Dubbo 的配置即可,Dubbo 基于 Spring 的 Schema 扩展进行加载。
任选其一
CentOs7.3 搭建 ZooKeeper-3.4.9 单机服务
CentOs7.3 搭建 ZooKeeper-3.4.9 Cluster 集群服务 代码我已放到 Github ,导入 ymq-dubbo-spring-boot 项目 
github github.com/souyunku/ym…
 在项目中添加 dubbo 依赖 
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.6</version>
</dependency> 
 ymq-dubbo-api public interface DemoService {
    String sayHello(String name);
} 
 ymq-dubbo-provider ,在服务提供方实现接口 @Service("demoService")
public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }
} 
 @Configuration
@PropertySource("classpath:dubbo.properties")
@ImportResource({"classpath:dubbo/*.xml"})
public class PropertiesConfig {
} 
  在提供方增加暴露服务配置 : <dubbo:service> 
 dubbo-provider.xml 
<!-- 声明需要暴露的服务接口 --> <dubbo:service interface="io.ymq.dubbo.api.DemoService" ref="demoService"/>
ymq-dubbo-consumer ,消费消费远程方法 @Service("consumerDemoService")
public class ConsumerDemoService {
    @Autowired
    private DemoService demoService;
    public void sayHello(String name) {
        String hello = demoService.sayHello(name); // 执行消费远程方法
        System.out.println(hello); // 显示调用结果
    }
} 
 @Configuration
@PropertySource("classpath:dubbo.properties")
@ImportResource({"classpath:dubbo/*.xml"})
public class PropertiesConfig {
} 
  在消费方增加引用服务配置: <dubbo:reference> 
 dubbo-consumer.xml 
<!-- 增加引用远程服务配置 可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" check="false" interface="io.ymq.dubbo.api.DemoService"/>
 项目: ymq-dubbo-provider ,  ymq-dubbo-consumer  一样配置 
 dubbo.xml 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="${spring.application.name}"  />
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry protocol="zookeeper" address="${zookeeper.connect}"  file="${dubbo.cache}"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="${dubbo.protocol.port}"  threadpool="${dubbo.protocol.threadpool}"  threads="${dubbo.protocol.threads}"/>
    <!-- 提供方的缺省值,当ProtocolConfig和ServiceConfig某属性没有配置时,采用此缺省值,可选。-->
    <dubbo:provider connections="${dubbo.provider.connections}" timeout="${dubbo.provider.timeout}" retries="${dubbo.provider.retries}" version="${dubbo.provider.version}" />
    <!-- 消费方缺省配置,当ReferenceConfig某属性没有配置时,采用此缺省值,可选。-->
    <dubbo:consumer version="${dubbo.provider.version}" />
    <!-- 监控中心配置,用于配置连接监控中心相关信息,可选。-->
    <dubbo:monitor protocol="registry"/>
</beans> 
  dubbo.properties 
######################################################### # dubbo config #暴露服务端口 dubbo.protocol.port=20880 #提供方超时时间 dubbo.provider.timeout=10000 #提供方版本 dubbo.provider.version=1.0 #表示该服务使用独的五条条长连 dubbo.provider.connections=5 # 固定大小线程池,启动时建立线程,不关闭,一直持有。(缺省) dubbo.protocol.threadpool=fixed # 线程数量 dubbo.protocol.threads=500 #配置重试次数,最好只用于读的重试,写操作可能会引起多次写入 默认retries="0" dubbo.provider.retries=0 # dubbo缓存文件 dubbo.cache=/data/dubbo/cache/ymq-dubbo-provider ######################################################### # zookeeper config zookeeper.connect=127.0.0.1:2181
启动服务
/opt/zookeeper-3.4.9/bin/zkServer.sh start
package io.ymq.dubbo.provider.run;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
 * 描述:启动提供方服务
 *
 * @author yanpenglei
 * @create 2017-10-27 11:49
 **/
@SpringBootApplication
@ComponentScan(value = {"io.ymq.dubbo"})
public class Startup {
    public static void main(String[] args) {
        SpringApplication.run(Startup.class, args);
    }
} 
 package io.ymq.dubbo.test;
import io.ymq.dubbo.consumer.run.Startup;
import io.ymq.dubbo.consumer.service.ConsumerDemoService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
 * 描述: 测试消费远程服务
 *
 * @author yanpenglei
 * @create 2017-10-27 14:15
 **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Startup.class)
public class ConsumerTest {
    @Autowired
    private ConsumerDemoService consumerDemoService;
    @Test
    public void sayHello(){
        consumerDemoService.sayHello("Peng Lei");
    }
} 
 响应:
[15:54:00] Hello Peng Lei, request from consumer: /10.4.82.6:63993
 代码我已放到 Github ,导入 ymq-dubbo-spring-boot 项目 
github github.com/souyunku/ym…