Dubbo

概述:阿里的东西,做分布式管理用的,现在是阿帕奇在维护.....

高端点:

Double是一款高性能,轻量级的java RPC框架,致力于提供高性能和透明化的 RPC
(通过网络传输数据)远程服务调用方案,以及 SOA 服务治理方案
架构图:

 常听到的两个词:

关于dubbo的基础概念:

Qps:每秒钟的请求次数
Tps: 每秒钟的事务次数 测试数值的方式:通过日志记录

注意点:

只要仔细分析:dubbo 的工作流程是,消费端去注册中心拿到服务端的 url 和端口信息,会将该信息缓存,然后去调用服务端(生产端),当服务端的信息发生变化时注册中心会通知消费端将缓存刷新,然后消费端会重新从注册中心获取最新的信息,由于在消费端存在缓存,所以当注册中中心挂了,消费端还能访问到服务端,

超时和重试:

服务消费者在调用服务提供者的时候发生了阻塞、等待的情形,这个时候,服务消 费者会一直等待下去,可能会造成雪崩,服务器瘫痪
编码时使用注解时候设置一下:
@Service(timeout="超时时间",retries="尝试次数") @Reference(timeout="超时时间")
注意:在消费端和服务端都可以设置超时限制,但是超时主要还是看消费端的为主,但 是在设置超时时建议还是在服务端设置超时限制

多版本:

升级呀啥的,编码时注解设置
服务端@Service(version=”版本号”)
消费端@Reference(version=”要使用的版本号”)

负载均衡

概述:负载均衡就是将客户端请求分散在集群的各个节点上 均衡策略:
dubbo的四种负载均衡策略:
Random : 按权随机
RoundRobin:按权轮循
LeastActive: 响应速度最快, 注册中心
ConsistentHash : hash 算法获取访问的服务,其实就是看哪个节点上次执行的和这次服务调用相同的业务,便调用谁
编码时候:
@Reference(loadbalance = "leastactive") 根据情况选取
@Service(weight = 200)设置权重

集群容错模式(6种)从来就没记住过,但是还面试老问:

Failover Cluster:失败重试。默认值。当出现失败,重试其它服务器 ,默认重试 2 次,使用 retries 配置。一般用于读操作
Failfast Cluster 快速失败,只发起一次调用,失败立即报错,通常用于写操作。
Failsafe Cluster :失败安全,出现异常时,直接忽略。返回一个空结果。
Failback Cluster : 失败自动恢复,后台记录失败请求,定时重发。通常用于消息 通知操作。
Forking Cluster :并行调用多个服务器,只要一个成功即返回。
Broadcast Cluster :广播调用所有提供者,逐个调用,任意一台报错则报错

编码设置:

@Reference(cluster = "failover")

序列化

在使用 dubbo 传输模型类时模型类必须实现 serializable 接口,在 dubbo 的内部已
将序列化流做了封装

服务降级

概述 : 可以通过服务降级功能,临时屏蔽某个出错的非关键服务,并定义降级后的返
回策略
Force return null
Fail return null
编码:
@Reference(mock = "fail return null")

小案例(关键是配置文件MVC环境):

准备:使用的zookeeper作为i注册中心:

度盘:链接:https://pan.baidu.com/s/1B4phApJjF1-ZjBhk7o2oXQ 
提取码:7ph8

官网:Apache ZooKeeper

zk的安装很简单:解压即可,linux和window相同

解压后将配置文件中的zoo_sample.cfg改称zoo.cfg

修改配置文件中的数据存贮目录(这块自己创建一个即可):dataDir=文件目录

进入bin目录启动:

wiindow直接点击即可:

linux: ./zkServer.sh  start

   失败会有提示

默认端口号2181:

基本环境完事!

案例开始:

定义生产端消费端统一的依赖包,里面定义pojo,被调用的接口,生产端和消费端依赖这个包即可!!通过maven的依赖传递就实现了,基本思路就这样!

生产端和消费端:

pom中的依赖相同:

<properties>
    <spring.version>5.1.9.RELEASE</spring.version>
    <dubbo.version>2.7.4.1</dubbo.version>
    <zookeeper.version>4.0.0</zookeeper.version>
</properties>

<dependencies>
    <!-- servlet3.0规范的坐标 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <!--spring的坐标-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!--springmvc的坐标-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!--日志-->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.21</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.21</version>
    </dependency>



    <!--Dubbo的起步依赖,版本2.7之后统一为rg.apache.dubb -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>${dubbo.version}</version>
    </dependency>
    <!--ZooKeeper客户端实现 -->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>${zookeeper.version}</version>
    </dependency>
    <!--ZooKeeper客户端实现 -->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>${zookeeper.version}</version>
    </dependency>


    <!--依赖公共的接口模块-->
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>dubbo-inference</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

</dependencies>

生产端 

生产端配置文件(主要就是这个难搞,要不我都懒得整理这个):
<?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://dubbo.apache.org/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.xsd
        http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
	<!--dubbo内部占用的端口号,多个项目同一个环境使用配置,开发线上无视-->
	<dubbo:protocol port="2083" />
	<!--dubbo的配置-->
	<!--1.配置项目的名称,唯一-->
	<dubbo:application name="dubbo-service">
		<!-- 设置qos的监控绑定端口号,因为这块生产段的监控系统和消费端的监控系统在同一环境下测试,默认的2222端口
      会冲突 ,生产线上忽略 -->
		<dubbo:parameter key="qos.port" value="12121"/>
	</dubbo:application>
	<!--2.配置注册中心的地址-->
	<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
	<!--3.配置dubbo包扫描-->
	<dubbo:annotation package="nanfeng" />
	<!-- 元数据配置 ,方便web控制台查看详细信息,没有控制台并不影响dubbo的使用-->
	<dubbo:metadata-report address="zookeeper://127.0.0.1:2181" />
</beans>

生产端代码(参考注释修改即可):

**
 * 注意:导入包为dubbo目录下的包
 */

/**@Service注解参数解释:
 *
 *
 */
/**
 * @Service注解
 * @Service(timeout = 5000,retries = 1,version = "2.0",weight = 200)
 * 将这个类提供的方法(服务)对外发布。将访问的地址 ip,端口,路径注册到注册中心中
 * timeout设置超时:默认是1s超时,下面设置5s,超时配置在生产段和消费端都可以配置,但是生产端的配置会被消费端的覆盖
 * retries设置超时重试次数:默认重试次数为2次(实际发送加上第一次共3次),重试次数是为了方式网络抖动等外在因素,
 * version设置版本:通过version设置版本型号
 * weight权重:负载均衡搭配使用,指实质是概数
 */
@Service
public class iPersionServiceImp implements PersionService {
    @Override
    public Persion find() {
        return new Persion("张三",12);
    }
}

web.xml(谁写都一样,不过这个案例生产端只是打了个war包,将原来的jar包作为一个服务提供给消费端,听起来似乎很模糊,其实就是提供服务包),这块就不需要配置加载mvc的东西了

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

通用的的日志配置文件log4j.properties(必须叫这个名字),自己配太慢,

# DEBUG < INFO < WARN < ERROR < FATAL
# Global logging configuration
log4j.rootLogger=info, stdout,file
# My logging configuration...
#log4j.logger.com.tocersoft.school=DEBUG
#log4j.logger.net.sf.hibernate.cache=debug
## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n

log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=../logs/iask.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}  %l  %m%n

消费端

消费端配置文件:

<?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://dubbo.apache.org/schema/dubbo"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">


    <mvc:annotation-driven/>
    <context:component-scan base-package="nanfeng"/>
    <!--dubbo的配置-->
    <!--1.配置项目的名称,唯一-->
    <dubbo:application name="dubbo-consumer">
        <!-- 设置qos的监控绑定端口号,因为这块生产段的监控系统和消费端的监控系统在同一环境下测试,默认的2222端口会冲突 ,生产线上忽略 -->
        <dubbo:parameter key="qos.port" value="33333"/>
    </dubbo:application>
    <!--2.配置注册中心的地址-->
    <!--zk默认端口2181-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!--3.配置dubbo包扫描-->
    <dubbo:annotation package="nanfeng"/>


</beans>

消费端代码:

package nanfeng;

import nanfeng.pojo.Persion;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

    /**
     *  @Reference注解注意是dubbo包下的,别弄错了
     *   1. 从zookeeper注册中心获取Service的访问url
     *   2. 进行远程调用RPC
     *   3. 将结果封装为一个代理对象。给变量赋值
     */

    /**
     *  @Reference注解参数解析:
     *@Reference(cluster = "failover",timeout = 1000,version = "版本号",loadbalance = "leastactive",mock = "fail return null")
     * cluster:集群容错模式6种,根据情况选取,参数为字符串类型的模式名字,如failover
     * timeout:超时时间,消费端的配置会覆盖生产端的配置,默认是一秒1000ms
     * version:注入对象的版本信息,参数是字符串版本号
     * loadbalance:负载均衡策略,四种,参数数字符串类型名字,直接找LoadBalance抽象类的四个实现类第一行字符串name就是参数,如"leastactive"
     * mock:服务降级配置,参数:force return null 强制降级,返回null,不会调用service
     *                     fail return null  失败降级,返回null,调用service调用失败后执行
      */
    @Reference(mock = "fail return null")
    private PersionService service;

    @RequestMapping("/find")
    public Persion find() {
        return service.find();
    }
}

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">
	<!-- Springmvc -->
    <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:springmvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


</web-app>

差不多dubbo结合SpringMVC使用就这样子啦!!!! ~~~~~~~~~~~~~~~~````参考修改即可使用!!!

换成SpringBoot环境

dubbo配置文件:

dubbo.scan.basePackages = com.tanhua.dubbo.server
dubbo.application.name = dubbo-provider-tanhua

dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880

dubbo.registry.address = zookeeper://127.0.0.1:2181
dubbo.registry.client = zkclient

依赖:

<!--dubbo的springboot支持-->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>
<!--dubbo框架-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.4</version>
</dependency>
<!--zk依赖-->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.13</version>
</dependency>
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
</dependency>

参考修改,代码层面上东西不变 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值