dubbo/dubbox

1.概念

1.1  SOA

面向服务架构,SOA是一种思想

备注:分布式系统的发展与演变  orm -mvc-rpc-soa

1.2 RPC

  RPC是指远程过程调用,是一种进程间通信方式,他是一种技术的思想,而不是规范。它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显式编码这个远程调用的细节。RPC可以理解为SOA的一种实现

RPC两个核心模块:通讯,序列化。

服务消费方(client)调用以本地调用方式调用服务;

1> client stub接收到调用后负责将方法、参数等组装成能够进行网络传输的消息体;

2> client stub找到服务地址,并将消息发送到服务端;

3> server stub收到消息后进行解码;

4> server stub根据解码结果调用本地的服务;

5> 本地服务执行并将结果返回给server stub;

6> server stub将返回结果打包成消息并发送至消费方;

7> client stub接收到消息,并进行解码;

8> 服务消费方得到最终结果。

2 Dubbo(12年停止维护,17后又在维护,依赖于spring环境,dubbo的2.6以上版本是dubbox与以前的dubbo的一个整合并贡献给Apache)

官方文档:    http://dubbo.apache.org/zh-cn/docs/user/quick-start.html

一个分布式、高性能、透明化的RPC服务框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

灰度服务(慢慢的转移到新的服务器的过程)

2.1  Dubbo架构图

Provider :提供者,服务发布方.

Consumer:消费者, 调用服务方

Container:Dubbo容器.依赖于Spring容器.

Registry: 注册中心.当Container启动时把所有可以提供的服务列表上Registry中进行注册.作用:告诉Consumer提供了什么服务和服务方在哪里.

Monitor:监听器

虚线都是异步访问,实线都是同步访问

蓝色虚线:在启动时完成的功能

红色虚线(实线)都是程序运行过程中执行的功能

所有的角色都是可以在单独的服务器上.所以必须遵守特定的协议.

运行原理:

启动容器,相当于在启动Dubbo的Provider

启动后会去注册中心进行注册.注册所有可以提供的服务列表

在Consumer启动后会去Registry中获取服务列表和Provider的地址.进行订阅.

当Provider有修改后,注册中心会把消息推送给Consumme, 使用了观察者设计模式(又叫发布/订阅设计模式)

根据获取到的Provider地址,真实调用Provider中功能.在Consumer方使用了代理设计模式.创建一个Provider方类的一个代理对象.通过代理对象获取Provider中真实功能,起到保护Provider真实功能的作用.

Consumer和Provider每隔1分钟向Monitor发送统计信息,统计信息包含,访问次数,频率等.

备注:invoke 最耗时,register是自动运行的,

2.2  注册中心

Zookeper:优点:支持网络集群,缺点:稳定性受限于Zookeeper

Redis:优点:性能高,缺点:对服务器环境要求较高

Multicast: 优点:面中心化,不需要额外安装软件,缺点:建议同机房(局域网)内使用

Simple:适用于测试环境.不支持集群.

Dubbo支持的协议

2.3  Dubbo协议

Dubbo官方推荐的协议.本质:使用NIO和线程池进行处理.缺点:大文件传输时可能出现文件传输失败问题.

RMI协议

JDK提供的协议,远程方法调用协议. 缺点:偶尔连接失败. 优点:JDK原生,不需要进行额外配置(导入jar)

Hession协议

优点:基于http协议,http请求支持. 缺点:需要额外导入jar,并在短连接时性能低

2.4  zookeeper安装

见zookeeper

2.5  dubbo-admin的配置与使用

2.6.0之后是springboot写的,配置按需修改配置,只需要修改registry.address然后启动即可。

 

javassist异常

<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.6.2</version>
			<exclusions>
				<exclusion>
					<groupId>org.javassist</groupId>
					<artifactId>javassist</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.21.0-GA</version>
		</dependency>

 2.6  monitor 的配置与使用

配置monitor 只需要修改registry.address启动即可。

在 Provide与Consumer的application中添加如下配置

  <!--方式1:注册,自动检查-->
    <dubbo:monitor protocol="registry"/>
    <!--方式2:admin的地址-->
    <dubbo:monitor  address="127.0.0.1:7070" />

2.7  简单实例搭建配置版

依赖provide

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>cn.dubbo.provide</groupId>
    <artifactId>dubbo-provide</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>cn.dubbo.base</groupId>
            <artifactId>dubbo-base</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.4</version>
            <exclusions>
                <exclusion>
                    <groupId>org.javassist</groupId>
                    <artifactId>javassist</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.21.0-GA</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>

    </dependencies>

</project>

application配置

<?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">

    <!--应用名称-->
    <dubbo:application name="dubboService"/>
    <!--注册中心  协议  地址-->
    <dubbo:registry address="192.168.1.128:2181" protocol="zookeeper"/>
    <!--协议  名称与端口-->
    <dubbo:protocol name="dubbo" port="20880" />
    <!--暴露接口  ref指向的实体类-->
    <dubbo:service interface="cn.dubbo.service.ProvideService" ref="demoControllerImpl"/>
    <bean  id="demoControllerImpl" class="cn.dubbo.service.ProvideServiceImpl"/>

</beans>

 其他

public interface ProvideService {
    public String  test();
}
public class ProvideServiceImpl implements ProvideService { public String test() { return "hello"; } }
public class ProvideTest { public static void main(String[] args) { new ClassPathXmlApplicationContext("classpath:application.xml"); Main.main(args); } }

 依赖consumer

<?xml version="1.0" encoding="UTF-8"?>
    <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>cn.dubboc.test</groupId>
        <artifactId>DubbocTest</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>war</packaging>

        <dependencies>
            <dependency>
                <groupId>cn.dubbo.provide</groupId>
                <artifactId>dubbo-provide</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </project>

 application配置

<?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">


    <dubbo:application name="dubboService"/>
    <dubbo:registry address="192.168.1.128:2181" protocol="zookeeper"/>
    <dubbo:annotation package="cn.dubbo.service"/>//也可以使用refrence,但是需要使用@Autowired以及@service
   <!--注册监控中心,自动检查-->
   <dubbo:monitor protocol="registry"/> <bean id="consumerServiceImpl" class="cn.dubbo.service.ConsumerServiceImpl"/> </beans>

 其他:

public interface ConsumerService {
    public  void test();
}

public class ConsumerServiceImpl implements ConsumerService {
    @Reference
    ProvideService   provideService;
    public void test() {
        String test = provideService.test();
        System.out.println(test);
    }
}

public class ConsumerTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx =
                new ClassPathXmlApplicationContext("classpath:application.xml");
        ConsumerServiceImpl consumerServiceImpl = ctx.getBean("consumerServiceImpl", ConsumerServiceImpl.class);
        consumerServiceImpl.test();
    }
}

2.8  在springoot中使用

在springboot中添加如下依赖

dubbo.application.name=provide
dubbo.registry.protocol=zookeeper
dubbo.registry.address=192.168.1.128:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881

 暴露接口,当然还需要注入容器,夏苗苗两个注解没有注入容器

@Service//import com.alibaba.dubbo.config.annotation.Service;
@EnableDubbo
public class ProvideServiceImpl implements ProvideService {

 

2.9   配置文件读取优先级

2.10 启动时检查

<!--单个配置-->
@Reference(check = false) ProvideService provideService;
<!--统一配置-->
<dubbo:consumer check="false"/>

 假如没有check = false,那么启动时必须调用提供者的方法否者报错,设置后只有真正在调用的时候才会去检查

2.11 超时设置

由于网络或服务端不可靠,会导致调用出现一种不确定的中间状态(超时)。为了避免超时导致客户端资源(线程)挂起耗尽,必须设置超时时间,默认也设置超时时间

Dubbo消费端

<!--单个配置-->
@Reference(check = false,timeout = 1000)
<!--统一配置-->
<dubbo:consumer timeout="5000" />

Dubbo服务端

<!--单个配置-->
<dubbo:provider interface="com.foo.BarService" timeout="2000"> <dubbo:method name="sayHello" timeout="3000" /> </dubbo:provider> <!--统一配置--> <dubbo:provider timeout="5000" />

 配置原则

dubbo推荐在Provider上尽量多配置Consumer端属性:

方法级配置别优先于接口级别,即小Scope优先

Consumer端配置 优先于 Provider配置 优于 全局配置

2.12 重试次数

失败自动切换,当出现失败,重试其它服务器,但重试会带来更长延迟。可通过 retries="2" 来设置重试次数(不含第一次)。

<!--重试次数配置方式1-->
<dubbo:service retries="2" />
<!--重试次数配置方式2-->
@Reference(check = false,timeout = 1000,retries = 2)
ProvideService provideService;

 2.13 版本号

当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用。可以按照以下的步骤进行版本迁移:

在低压力时间段,先升级一半提供者为新版本再将所有消费者升级为新版本然后将剩下的一半提供者升级为新版本

老版本服务提供者配置:
<dubbo:service interface="com.foo.BarService" version="1.0.0" />
新版本服务提供者配置:
<dubbo:service interface="com.foo.BarService" version="2.0.0" />
<dubbo:reference id="barService" interface="com.foo.BarService" version="1.0.0" />
新版本服务消费者配置:
<dubbo:reference id="barService" interface="com.foo.BarService" version="2.0.0" />
如果不需要区分版本,可以按照以下的方式配置:
<dubbo:reference id="barService" interface="com.foo.BarService" version="*" />

 2.14 本地存根

调用接口之前,先调用stub

<dubbo:service interface="com.foo.BarService" stub="true" />
或
<dubbo:service interface="com.foo.BarService" stub="com.foo.BarServiceStub" />

 2.15 zookeeper宕机与dubbo直连

zookeeper宕机

监控中心宕掉不影响使用,只是丢失部分采样数据

数据库宕掉后,注册中心仍能通过缓存提供服务列表查询,但不能注册新服务

注册中心对等集群,任意一台宕掉后,将自动切换到另一台

注册中心全部宕掉后,服务提供者和服务消费者仍能通过本地缓存通讯

服务提供者无状态,任意一台宕掉后,不影响使用

服务提供者全部宕掉后,服务消费者应用将无法使用,并无限次重连等待服务提供者恢复

dubbo直连

@Reference(url = "127.0.0.1:20880")

2.16  负载均衡策略

Random LoadBalance

随机,按权重设置随机概率。

在一个截面上碰撞的概率高,但调用量越大分布越均匀,而且按概率使用权重后也比较均匀,有利于动态调整提供者权重。

RoundRobin LoadBalance

轮循,按公约后的权重设置轮循比率。

存在慢的提供者累积请求的问题,比如:第二台机器很慢,但没挂,当请求调到第二台时就卡在那,久而久之,所有请求都卡在调到第二台上。

LeastActive LoadBalance

最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。

使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大。

ConsistentHash LoadBalance

一致性 Hash,相同参数的请求总是发到同一提供者。

当某一台提供者挂时,原本发往该提供者的请求,基于虚拟节点,平摊到其它提供者,不会引起剧烈变动。算法参见:http://en.wikipedia.org/wiki/Consistent_hashing

缺省只对第一个参数 Hash,如果要修改,请配置 <dubbo:parameter key="hash.arguments" value="0,1" />

缺省用 160 份虚拟节点,如果要修改,请配置 <dubbo:parameter key="hash.nodes" value="320" />

2.17 服务降级

当服务器压力剧增的情况下,根据实际业务情况及流量,对一些服务和页面有策略的不处理或换种简单的方式处理,从而释放服务器资源以保证核心交易正常运作或高效运作。

可以使用屏蔽消费者返回为空,也可以是容错,但是容错是在失败之后才会调用

2.18 集群容错

在集群调用失败时,Dubbo 提供了多种容错方案,缺省为 failover 重试。

集群容错模式

Failover Cluster

失败自动切换,当出现失败,重试其它服务器。通常用于读操作,但重试会带来更长延迟。可通过 retries="2" 来设置重试次数(不含第一次)。

重试次数配置如下:

<dubbo:service retries="2" />

<dubbo:reference retries="2" />

<dubbo:reference>

    <dubbo:method name="findFoo" retries="2" />

</dubbo:reference>

 

Failfast Cluster

快速失败,只发起一次调用,失败立即报错。通常用于非幂等性的写操作,比如新增记录。

 

Failsafe Cluster

失败安全,出现异常时,直接忽略。通常用于写入审计日志等操作。

 

Failback Cluster

失败自动恢复,后台记录失败请求,定时重发。通常用于消息通知操作。

 

Forking Cluster

并行调用多个服务器,只要一个成功即返回。通常用于实时性要求较高的读操作,但需要浪费更多服务资源。可通过 forks="2" 来设置最大并行数。

 

Broadcast Cluster

广播调用所有提供者,逐个调用,任意一台报错则报错 [2]。通常用于通知所有提供者更新缓存或日志等本地资源信息。

 

集群模式配置

按照以下示例在服务提供方和消费方配置集群模式

<dubbo:service cluster="failsafe" />

<dubbo:reference cluster="failsafe" />

整合hystrix见springcloud

posted @ 2018-10-26 22:57  fatale  阅读(429)  评论(0编辑  收藏  举报