dubbo个人笔记

一:集群和分布式

集群:一个业务模块,部署在多台服务器上
分布式:一个大的业务系统,拆分为小的业务模块,分别部署在不同的机器上
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

二:dubbo入门

1.概念:阿里巴巴基于java形成的一种高性能RPC(一种运行调用机制)的分布式服务架构。是SOA架构的产物。
2.运行环境:安装zookeeper注册中心下进行。
3.入门案例:
请添加图片描述
普通的spring与springmvc的整合,web依赖于service(在一台电脑上开发时)。

采用dubbo架构后,需要将service与web部署在不同的服务器上,web通过rpc机制去调用service。

首先修改service

3.1将接口实现类的@Service注解改为dubbo包下的

package com.dubbo.service.impl;

import com.dubbo.service.UserService;
import org.apache.dubbo.config.annotation.Service;
//import org.springframework.stereotype.Service;

//@Service//将该类注册到spring容器中
@Service//将该类对外发布,将访问方法的ip,端口,路径放在注册中心中
public class UserServiceImpl implements UserService {

    public String sayHello(){
        return "hello dubbo!";
    }
}

3.2将service改为war包形式,改为wep-app项目,在pom.xml文件中修改,导入webapp包请添加图片描述
请添加图片描述

3.3web.xml文件配置

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         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*:spring/applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


</web-app>

3.4配置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: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://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
     http://dubbo.apache.org/schema/dubbo  http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

<!--扫描service包-->
    <context:component-scan base-package="com.dubbo.service"></context:component-scan>

<!--    dubbo配置-->
<!--    1.配置项目名称(唯一)-->
    <dubbo:application name="dubbo-servie"/>
<!--    2.配置注册中心-->
    <dubbo:registry address="zookeeper:IP地址"/>
<!--    3.配置dubbo包扫描-->
    <dubbo:annotation package="com.dubbo.service.impl"/>
</beans>

3.5将dubbo-service部署在tomcat上s

3.6创建一公告接口模块,service和web依赖于该接口模块,将接口放在改模块中

请添加图片描述
3.7将interface模块导入service和web的pom.xml文件中,将interface模块install在maven仓库中

请添加图片描述

其次修改web层

3.8将控制类的@Autowired修改

package com.dubbo.controller;

import com.dubbo.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    //@Autowired
    /**
     * 1.从zookeeper注册中心获取userService访问的url
     * 2.进行远程调用rpc
     * 3.将结果封装为一个代理对象,给变量赋值
     */

    @Reference//远程注入
    private UserService userService;

    @RequestMapping("/say")
    public String sayHello(){
        System.out.println("controller");
        return userService.sayHello();
    }
}

3.9修改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: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-3.2.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
        http://www.springframework.org/schema/context/spring-context-4.3.xsd ">

    <mvc:annotation-driven>
<!--        版本过高的spring需要手动开启后缀请求-->
        <mvc:path-matching suffix-pattern="true"/>
    </mvc:annotation-driven>
    <context:component-scan base-package="com.dubbo.controller"></context:component-scan>

    <!--    dubbo配置-->
    <!--    配置项目名称(唯一)-->
    <dubbo:application name="dubbo-web">
<!--        该端口(因为我们是在一台电脑上测试,默认端口会冲突)-->
        <dubbo:parameter key="qos.port" value="33333"/>
    </dubbo:application>
    <!--    配置注册中心-->
    <dubbo:registry address="zookeeper:IP地址"/>
    <!--    配置dubbo包扫描-->
    <dubbo:annotation package="com.dubbo.controller"/>


</beans>

3.10修改web.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         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*:spring/applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>-->

<!--    springmvc-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

3.11将web部署在tomcat即可

总体架构
请添加图片描述

三:dubbo高级特性

1:序列化

在dubbo中实现对象的传递需要采用序列化(将对象变为字节序列)与反序列化(将字节序列变为对象),保证数据的完整性和可传递性。
具体过程
请添加图片描述

2.地址缓存
当注册中心挂掉后,消费者是否可以调用方法? 可以的,消费者第一次访问注册中心会将服务 提供方地址缓存在本地,再次访问无需访问注册中心

3.超时
请添加图片描述

提供者和消费者都可以设置timeout
设置timeout后,在这个时间段内无法访问服务,自动断开

提供者请添加图片描述
消费者请添加图片描述
消费者时间会替代提供者 建议在service建立超时设置。

4.重试–处理网络抖动(超时后才会重试)

提供方设置 retries默认为2
请添加图片描述
消费方设置 retries默认为2
请添加图片描述

5.多版本
请添加图片描述
定义接口版本
请添加图片描述
请添加图片描述
根据版本调用接口
请添加图片描述
6.负载均衡(在集群环境上)
请添加图片描述
random
请添加图片描述
2被访问的概ro率是50%,1.3分别为25%。

roundrobin
请添加图片描述

leastactive
请添加图片描述
consistenhash
请添加图片描述
在@Reference中设置
请添加图片描述
7.集群容错
请添加图片描述请添加图片描述
8.服务降级
请添加图片描述
请添加图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值