Springboot 整合dubbo:

1 简介

Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。

Dubbo[]是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。

其核心部分包含:

  • 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。(Netty)
  • 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
  • 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

2 分布式系统发展演变

ORM:单一应用架构 MVC:Web框架

2.1 RPC-远程过程调用

RPC【Remote Procedure Call】是指远程过程调用,是一种进程间通信方式,他是一种技术的思想,而不是规范。它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显式编码这个远程调用的细节。即程序员无论是调用本地的还是远程的函数,本质上编写的调用代码基本相同。

RPC基本原理

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

3 Apache Dubbo

3.1 dubbo简介

Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

官网:http://dubbo.apache.org/

3.2 基本概念

服务提供者**Provider)**:暴露服务的服务提供方,服务提供者在启动时,向注册中心注册自己提供的服务。

服务消费者**(Consumer)**: 调用远程服务的服务消费方,服务消费者在启动时,向注册中心订阅自己所需的服务,服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

注册中心**(Registry)**:注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者

监控中心**(Monitor)**:服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心

Ø 调用关系说明

l 服务容器负责启动,加载,运行服务提供者。

l 服务提供者在启动时,向注册中心注册自己提供的服务。

l 服务消费者在启动时,向注册中心订阅自己所需的服务。

l 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

l 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

l 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

3.3 dubbo环境搭建

3.3.1 zookeeper

zookeeper下载(推荐版本:3.4.11 推荐理由:较为稳定): https://archive.apache.org/dist/zookeeper/

进入zookeeper解压目录下的conf文件下,找到zoo_sample.cfg,复制并且修改名称为zoo.cfg

(1) 修改zoo.cfg配置文件

注意几个重要位置:

dataDir=./ 临时数据存储的目录(可写相对路径)

clientPort=2181 zookeeper的端口号

修改完成后再次启动zookeeper

(2) 使用zkCli.cmd测试

ls /:列出zookeeper根下保存的所有节点

create –e /atguigu 123:创建一个atguigu节点,值为123

get /atguigu:获取/atguigu节点的值

3.3.2 部署管理控制台

为了让用户更好的管理监控众多的dubbo服务,官方提供了一个可视化的监控程序,不过这个监控即使不装也不影响使用。

管理控制台安装:

官网:https://github.com/apache/incubator-dubbo-admin

git: https://github.com/apache/incubator-dubbo-admin.git

MAVEN 安装:https://www.cnblogs.com/eagle6688/p/7838224.html

推荐使用git拉取,拉取之后的目录如下:

修改properties文件指定zookeeper地址端口:

打包成jar包,cmd运行jar包。或者直接eclipse启动该项目,输入http://192.168.20.215:7001或者http://localhost:7001进入管理控制台 默认账户和密码均为root。

3.3.3 java代码开始(spring整合dubbo)

构建三个maven项目,项目名称分别为下图所示:

gmall-interface:用于提供持久层实体类和公共业务接口声明

user-service-provider:用于业务层接口实现类

order-service-consumer:用于调用gmall-interface暴露的接口

代码展示:

  • gmall-interface代码展示
  1. com.ruiec.gmall.bean;

    import java.io.Serializable;
    /**
      * 用户住址实体
     * @author luo_wei<br>
     * @date 2019年4月1日 下午5:10:12
     */
    public class UserAddress implements Serializable {

    private static final long serialVersionUID = -5295818382368154100L;

    private Integer id;
    private String userAddress;// 用户地址
    private String userId;// 用户ID
    private String consignee;// 收货人
    private String phone;// 电话号码
    private String isDefault;// 是否为默认地址

    public Integer getId() {
    return id;
    }

    public void setId(Integer id) {
    this.id = id;
    }

    public String getUserAddress() {
    return userAddress;
    }

    public void setUserAddress(String userAddress) {
    this.userAddress = userAddress;
    }

    public String getUserId() {
    return userId;
    }

    public void setUserId(String userId) {
    this.userId = userId;
    }

    public String getConsignee() {
    return consignee;
    }

    public void setConsignee(String consignee) {
    this.consignee = consignee;
    }

    public String getPhone() {
    return phone;
    }

    public void setPhone(String phone) {
    this.phone = phone;
    }

    public String getIsDefault() {
    return isDefault;
    }

    public void setIsDefault(String isDefault) {
    this.isDefault = isDefault;
    }

    public UserAddress() {
    super();
    }

    public UserAddress(Integer id, String userAddress, String userId, String consignee, String phone,
    String isDefault) {
    super();
    this.id = id;
    this.userAddress = userAddress;
    this.userId = userId;
    this.consignee = consignee;
    this.phone = phone;
    this.isDefault = isDefault;
    }

    @Override
    public String toString() {
    return "UserAddress [id=" + id + ", userAddress=" + userAddress + ", userId=" + userId + ", consignee="
    + consignee + ", phone=" + phone + ", isDefault=" + isDefault + "]";
    }

    }

  1. com.ruiec.gmall.service;

    import java.util.List;

    import com.ruiec.gmall.bean.UserAddress;

    /**
      * 用户业务层接口
     * @author luo_wei<br>
     * @date 2019年4月1日 下午5:27:31
     */
    public interface UserService {

    /**
     * 根据用户编号查询所有收货地址
    * @author luo_wei<br>
    * @date 2019年4月1日 下午5:09:46
    */
    public List<UserAddress> getUserAddressList(String userId);

    }

  1. com.ruiec.gmall.service;

    import java.util.List;

    import com.ruiec.gmall.bean.UserAddress;

    /**
      * 订单业务接口
     * @author luo_wei<br>
     * @date 2019年4月1日 下午5:22:03
     */
    public interface OrderService {

    /**
     * 初始化用户订单
    * @author luo_wei<br>
    * @date 2019年4月1日 下午5:20:56
    */
    public List<UserAddress> initOrder(String userId);
    }

  • user-service-provider代码展示
  1. com.ruiec.gmall.service.impl;

    import java.util.Arrays;
    import java.util.List;

    import com.ruiec.gmall.bean.UserAddress;
    import com.ruiec.gmall.service.UserService;
    /**
      * 用户接口实现类
     * 1. 将服务提供者注册到注册中心 (暴露服务)
     *   1)导入dubbo依赖(2.6.2)
     *   
     * 2. 让服务消费者订阅服务提供者的服务地址
     * @author luo_wei<br>
     * @date 2019年4月1日 下午5:10:59
     */
    public class UserServiceImpl implements UserService{

    /**
     * 根据用户编号查询所有收货地址
    * @author luo_wei<br>
    * @date 2019年4月1日 下午5:09:46
    */
    @Override
    public List<UserAddress> getUserAddressList(String userId) {

    UserAddress address=new UserAddress(1,"浙江省绍兴市诸暨市浣东街道西子公寓北区","1001","我是等爱的黑鬼","10086","Y");
    UserAddress address2=new UserAddress(2,"广东省梅州市五华县安流镇青江村琴江御城201商场","1002","一条溺死的鱼","10081","N");

    return Arrays.asList(address,address2);
    }

    }

  1. 提供者运行类
  1. com.ruiec.gmall;

    import java.io.IOException;

    import org.springframework.context.support.ClassPathXmlApplicationContext;
    /**
      * 提供者运行类
     * @author luo_wei<br>
     * @date 2019年4月1日 下午8:12:05
     */
    public class ProviderApplication {

    @SuppressWarnings("resource")
    public static void main(String[] args) throws IOException{
    ClassPathXmlApplicationContext ioc=new ClassPathXmlApplicationContext("provider.xml");
    ioc.start();
    System.in.read();
    }

    }

  1. 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>com.ruiec.gmall</groupId>

    <artifactId>user-service-provider</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <dependencies>

    <dependency>

    <groupId>com.ruiec.gmall</groupId>

    <artifactId>gmall-interface</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    </dependency>

    <!-- 引入dubbo -->

    <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->

    <dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>dubbo</artifactId>

    <version>2.6.2</version>

    </dependency>

    <!-- 注册中心 使用zookeeper 引入操作客户端-->

    <dependency>

    <groupId>org.apache.curator</groupId>

    <artifactId>curator-framework</artifactId>

    <version>2.12.0</version>

    </dependency>

    </dependencies>

    </project>

  • resources中创建:provider.xml
  1. 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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 引入名称空间  ctrl+shift+f -->
    <!--1 指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名)-->
    <dubbo:application name="user-service-provider"></dubbo:application>
    <!-- 指定注册中心 -->
    <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>

    <!-- 指定通信规则 (协议?端口) -->
    <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>

    <!-- 暴露服务 ref:指向真正的服务实现-->
    <dubbo:service interface="com.ruiec.gmall.service.UserService" ref="userServiceImpl"></dubbo:service>
    <!-- 服务实现 -->
    <bean id="userServiceImpl" class="com.ruiec.gmall.service.impl.UserServiceImpl"></bean>

    <!-- 监控中心协议,如果为protocol="registry",表示从注册中心发现监控中心地址 -->
    <dubbo:monitor protocol="registry"></dubbo:monitor>
    <!-- 直连监控中心服务器地址,address="10.20.130.230:12080" -->
    <!-- <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor> -->
    </beans>

  • order-service-consumer代码展示
  1. com.ruiec.gmall.service.impl;

    import java.util.List;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import com.ruiec.gmall.bean.UserAddress;
    import com.ruiec.gmall.service.OrderService;
    import com.ruiec.gmall.service.UserService;

    /**
     * 用户订单业务层实现类
     * @author luo_wei<br>
     * @date 2019年4月2日 下午2:59:57
     */
    @Service
    public class OrderServiceImpl implements OrderService {

    @Autowired
    UserService userService;

    /**
     * 初始化用户订单
    * @author luo_wei<br>
    * @date 2019年4月1日 下午5:20:56
    */
    @Override
    public List<UserAddress> initOrder(String userId) {
    System.out.println("用户ID:" + userId);
    List<UserAddress> list = userService.getUserAddressList("1");
    for (UserAddress userAddress : list) {
    System.out.println(userAddress.getUserAddress());
    }
    return list;
    }

    }

  • resources里面创建consumer.xml
  1. 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 http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 扫描容器组件 -->
    <context:component-scan  base-package="com.ruiec.gmall.service.impl"></context:component-scan>
    <dubbo:application name="order-service-consumer"></dubbo:application>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
    <!-- 声明需要调用的远程服务接口:生成远程服务代理 -->
    <dubbo:reference
    interface="com.ruiec.gmall.service.UserService" id="userService"></dubbo:reference>
    <!-- 监控中心协议,如果为protocol="registry",表示从注册中心发现监控中心地址 -->
    <dubbo:monitor protocol="registry"></dubbo:monitor>
    <!-- 直连监控中心服务器地址,address="10.20.130.230:12080" -->
    <!-- <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor> -->
    </beans>
  • ConsumerApplication
  1. com.ruiec.gmall;

    import java.io.IOException;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

    import com.ruiec.gmall.service.OrderService;

    /**
     * 消费者启动类
     *
     * @author luo_wei<br>
     * @date 2019年4月1日 下午8:32:26
     */
    public class ConsumerApplication {

    @SuppressWarnings("resource")
    public static void main(String[] args) throws IOException {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
    OrderService orderService = applicationContext.getBean(OrderService.class);
    orderService.initOrder("1");
    System.out.println("调用结束");
    System.in.read();
    }
    }

启动项目进入管理控制台查看效果。

3.3.4 监控中心 Simple Monitor

找到此项目的文件目录,在上面输入cmd打开命令窗口,执行mvn package(需要安装maven,并且配置系统环境变量)。

打包成功如下图所示

解压dubbo-monitor-simple-2.0.0-assembly.tar.gz,然后打开此文件,进入dubbo-monitor-simple-2.0.0\conf,如下图所示目录

确认无误后,进入dubbo-monitor-simple-2.0.0\assembly.bin,点击start.bat目录启动监控中心。

输入localhost:8080 进入监控中心首页

dubbo开发介绍文档地址:http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-monitor.html

输入网址进入dubbo监控中心描述界面

分别在之前新建的消费者和提供者项目中添加监控中心的配置,配置为从注册中心发现监控中心地址。(ps:7070是监控中心服务端暴露的端口)

再次启动两个项目,然后在监控中心可以看到相应的调用状况。

(ps:statistics-调用信息,charts-调用统计图)

3.3.5 springboot整合dubbo 

1) 代码构建

构建两个springboot项目boot-order-service-consumer(消费者)和boot-user-service-provide(提供者)。分别在两个项目中将之前的公共接口项目引入pom文件。

  • boot-order-service-consumer(消费者)的pom文件
  1. 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>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ruiec</groupId>
    <artifactId>boot-order-service-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-order-service-consumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
    <java.version>1.8</java.version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>com.ruiec.gmall</groupId>
    <artifactId>gmall-interface</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency>
    </dependencies>

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    </project>

  • boot-user-service-provide(提供者)的pom文件
  1. 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>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ruiec</groupId>
    <artifactId>boot-user-service-provide</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-user-service-provide</name>
    <description>Demo project for Spring Boot</description>

    <properties>
    <java.version>1.8</java.version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>com.ruiec.gmall</groupId>
    <artifactId>gmall-interface</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <!-- Spring Boot Dubbo 依赖 -->
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>0.2.0</version>
            </dependency>
    </dependencies>

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    </project>

创建服务者项目代码

  • UserServiceImpl.java 代码
  1. com.ruiec.gmall.service.impl;

    import java.util.Arrays;
    import java.util.List;

    import com.ruiec.gmall.bean.UserAddress;
    import com.ruiec.gmall.service.UserService;

    /**
      * 用户接口实现类
     * 1. 将服务提供者注册到注册中心 (暴露服务) 1)导入dubbo依赖(2.6.2)
     * 2. 让服务消费者订阅服务提供者的服务地址
     * @author luo_wei<br>
     * @date 2019年4月1日 下午5:10:59
     */
    public class UserServiceImpl implements UserService {

    /**
     * 根据用户编号查询所有收货地址
    * @author luo_wei<br>
    * @date 2019年4月1日 下午5:09:46
    */
    @Override
    public List<UserAddress> getUserAddressList(String userId) {

    UserAddress address = new UserAddress(1, "浙江省绍兴市诸暨市浣东街道西子公寓北区", "1001", "我是等爱的黑鬼", "10086", "Y");
    UserAddress address2 = new UserAddress(2, "广东省梅州市五华县安流镇青江村琴江御城201商场", "1002", "一条溺死的鱼", "10081", "N");

    return Arrays.asList(address, address2);
    }

    }

创建消费者项目代码

OrderServiceImpl.java代码

package com.ruiec.gmall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruiec.gmall.bean.UserAddress;
import com.ruiec.gmall.service.OrderService;
import com.ruiec.gmall.service.UserService;

@Service
public class OrderServiceImpl implements OrderService{

@Autowired
UserService userService;
/**
 * 初始化用户订单
* @author luo_wei<br>
* @date 2019年4月1日 下午5:20:56
*/
@Override
public List<UserAddress> initOrder(String userId) {
System.out.println("用户ID:"+userId);
//调用gmall-service远程暴露接口
List<UserAddress> list=userService.getUserAddressList("1");
return list;
}

}

OrderController.java代码

package com.ruiec.gmall.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruiec.gmall.bean.UserAddress;
import com.ruiec.gmall.service.OrderService;

/**
 * 订单控制器
 * @author luo_wei<br>
 * @date 2019年4月2日 上午8:59:48
 */
public class OrderController {

@Autowired
private OrderService orderService;

/**
* 初始化订单
* @author luo_wei<br>
* @date 2019年4月2日 上午9:07:30
*/
@ResponseBody
@RequestMapping("/initOrder")
public List<UserAddress> initOrder(@RequestParam("id") String userId) {
return orderService.initOrder(userId);
}

}

ps: dubbo-springboot 代码网址:https://github.com/apache/incubator-dubbo-spring-boot-project

引入pom依赖注意:

maven update 该项目后可以打开Maven Dependencies看到如图所示。

boot-user-service-provide(提供者) application.properties基础配置

#dubbo应用名称
dubbo.application.name=user-service-provider
#dubbo注册中心
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 通信协议
dubbo.protocol.name=dubbo
#协议端口
dubbo.protocol.port=20880
#监控中心
dubbo.monitor.protocol=registry

暴露服务

  1. com.ruiec.gmall;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

    /**
     * 1 导入依赖
     * 1)、导入dubbo-starter -pom
     * 2)、导入dubbo的其他依赖
     * @author luo_wei<br>
     * @date 2019年4月2日 上午9:23:58
     */
    @EnableDubbo //开启基于注解的dubbo功能
    @SpringBootApplication
    public class ProvideApplication {

    public static void main(String[] args) {
    SpringApplication.run(ProvideApplication.class, args);
    }

    }

  • zookeeper和管理中心、注册中心。效果如下图所示

  • 1.引入对应的依赖和引入dubbo对应的启动包
  1. <dependency>

    <artifactId>gmall-interface</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <!-- Spring Boot Dubbo 依赖 -->
    <dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
    </dependency>

配置application.properties

  1. dubbo.registry.address=zookeeper://127.0.0.1:2181
    dubbo.monitor.protocol=registry

OrderServiceImpl.java代码实现

  1. com.ruiec.gmall.service.impl;

    import java.util.List;

    import org.springframework.stereotype.Service;

    import com.alibaba.dubbo.config.annotation.Reference;
    import com.ruiec.gmall.bean.UserAddress;
    import com.ruiec.gmall.service.OrderService;
    import com.ruiec.gmall.service.UserService;

    @Service
    public class OrderServiceImpl implements OrderService {

    @Reference //从注册中心发现服务
    UserService userService;

    /**
     * 初始化用户订单
    * @author luo_wei<br>
    * @date 2019年4月1日 下午5:20:56
    */
    @Override
    public List<UserAddress> initOrder(String userId) {
    System.out.println("用户ID:" + userId);
    List<UserAddress> list = userService.getUserAddressList("1");
    return list;
    }
    }

ConsumerApplication.java启动类 启动dubbo注解

  1. com.ruiec.gmall;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

    @EnableDubbo
    @SpringBootApplication
    public class ConsumerApplication {

    public static void main(String[] args) {
    SpringApplication.run(ConsumerApplication.class, args);
    }
    }

启动项目效果如图所示,查看管理中心效果如下:

  • 6.在浏览器输入http://localhost:8081/initOrder?id=1 ,返回结果如图所示。(证明调用成功)

  • 2)dubbo.properties&属性加载顺序
  • http://dubbo.apache.org/zh-cn/docs/user/references/xml/introduction.html该网址,选中schema配置参考手册可以学习相关dubbo的配置详解,以及每个标签的详细解释和属性值。

ps(网址如下:http://dubbo.apache.org/zh-cn/docs/user/configuration/properties.html)

属性配置和映射规则以及覆盖策略。

测试覆盖策略

1 在提供者boot-user-service-provide中新增dubbo.properties文件,配置信息如下所示

# 通信协议
dubbo.protocol.name=dubbo
#协议端口
dubbo.protocol.port=20882

2 修改application.properties文件协议端口为20881

3 点击提供者代码启动类在代码出右击选中Run as 设置Run configurations选中提供者启动类输入以下代码。设置jvm参数,配置dubbo协议的端口为20880

4 运行测试,如下图所示,可以查看到启动端口为20880.

覆盖顺序如图所示,先是jvm参数然后是application.properties,最后才是dubbo.properties.

3)启动检查 (网址:http://dubbo.apache.org/zh-cn/docs/user/demos/preflight-check.html)

先不启动提供者,直接启动消费者,会直接报错,说明此时暂时没用提供者。

这时,

方式一:在consumer.xml 中的dubbo:reference配置check="false" 这样在服务不调用时就不会检查报错了。

方式二:在consumer.xml 中新增一下配置设置所有服务

<!-- 配置当前消费者的统一规则:所有服务都不检查 -->
    <dubbo:consumer check="false"></dubbo:consumer>

启动测试:

其次,注册中心也可以设置为不检查

<dubbo:registry address="zookeeper://127.0.0.1:2181" check="flase"></dubbo:registry>

4)超时&配置覆盖关系

1 在order-service-consumer的consumer.xml设置超时设置

<!-- 声明需要调用的远程服务接口:生成远程服务代理 --><!-- timeout设置超时时间,单位毫秒 --><dubbo:reference interface="com.ruiec.gmall.service.UserService" id="userService" timeout="3000"></dubbo:reference>

也可以设置单个method方法

<!-- 声明需要调用的远程服务接口:生成远程服务代理 --><!-- timeout设置超时时间,单位毫秒 --><dubbo:reference interface="com.ruiec.gmall.service.UserService" id="userService" timeout="3000"><dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method></dubbo:reference>

2 在user-service-provider中的UserServiceImpl.java设置休眠4秒

package com.ruiec.gmall.service.impl;
​
import java.util.Arrays;
import java.util.List;
​
import com.ruiec.gmall.bean.UserAddress;
import com.ruiec.gmall.service.UserService;
​
/*** 用户接口实现类* 1. 将服务提供者注册到注册中心 (暴露服务)*   1)导入dubbo依赖(2.6.2)*   * 2. 让服务消费者订阅服务提供者的服务地址* @author luo_wei<br>* @date 2019年4月1日 下午5:10:59*/
public class UserServiceImpl implements UserService {
​/*** 根据用户编号查询所有收货地址* @author luo_wei<br>* @date 2019年4月1日 下午5:09:46*/@Overridepublic List<UserAddress> getUserAddressList(String userId) {
​UserAddress address = new UserAddress(1, "浙江省绍兴市诸暨市浣东街道西子公寓北区", "1001", "我是等爱的黑鬼", "10086", "Y");UserAddress address2 = new UserAddress(2, "广东省梅州市五华县安流镇青江村琴江御城201商场", "1002", "一条溺死的鱼", "10081", "N");try {Thread.sleep(4000);} catch (InterruptedException e) {e.printStackTrace();}return Arrays.asList(address, address2);}
}

3 启动项目user-service-provider和order-service-consumer,便会提示时间超时。

超时默认时间为1000ms。

也可以在dubbo:consumer为所有的服务设置超时时间。

配置超时时间作用优先级,网址:http://dubbo.apache.org/zh-cn/docs/user/configuration/xml.html

总结:

  • 方法级优先,接口级次之,全局配置再次之。

  • 如果级别一样,则消费方优先,提供方次之。

    5)重试次数 ps:超时属性一般配合重试次数联合使用

    <!--retries 重试次数,不包含第一次  幂等操作(设置重试次数)【方法运行多少次】效果一样,推荐查询删除修改--><dubbo:method name="getUserAddressList" timeout="1000" retries="3"></dubbo:method>

6)多版本

1 在user-service-provider创建UserServiceImpl2.java

package com.ruiec.gmall.service.impl;
​
import java.util.Arrays;
import java.util.List;
​
import com.ruiec.gmall.bean.UserAddress;
import com.ruiec.gmall.service.UserService;
​
/*** 用户接口实现类* 1. 将服务提供者注册到注册中心 (暴露服务)*   1)导入dubbo依赖(2.6.2)*   * 2. 让服务消费者订阅服务提供者的服务地址* @author luo_wei<br>* @date 2019年4月1日 下午5:10:59*/
public class UserServiceImpl2 implements UserService {
​/*** 根据用户编号查询所有收货地址* @author luo_wei<br>* @date 2019年4月1日 下午5:09:46*/@Overridepublic List<UserAddress> getUserAddressList(String userId) {System.out.println("------------------new----");UserAddress address = new UserAddress(1, "浙江省绍兴市诸暨市浣东街道西子公寓北区", "1001", "我是等爱的黑鬼", "10086", "Y");UserAddress address2 = new UserAddress(2, "广东省梅州市五华县安流镇青江村琴江御城201商场", "1002", "一条溺死的鱼", "10081", "N");return Arrays.asList(address, address2);}
​
}
​

2 设置provider.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.xsdhttp://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"><!-- 引入名称空间 --><!-- ctrl+shift+f 1 指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名)--><dubbo:application name="user-service-provider"></dubbo:application><!-- 指定注册中心 --><!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> --><dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry><!-- 指定通信规则 (协议?端口) --><dubbo:protocol name="dubbo" port="20880"></dubbo:protocol><!-- 暴露服务 ref:指向真正的服务实现--><dubbo:service interface="com.ruiec.gmall.service.UserService" ref="userServiceImpl01"  version="1.0.0"></dubbo:service><!-- 服务实现 --><bean id="userServiceImpl01" class="com.ruiec.gmall.service.impl.UserServiceImpl"></bean><!-- 暴露服务 ref:指向真正的服务实现--><dubbo:service interface="com.ruiec.gmall.service.UserService" ref="userServiceImpl02"  version="2.0.0"></dubbo:service><!-- 服务实现 --><bean id="userServiceImpl02" class="com.ruiec.gmall.service.impl.UserServiceImpl2" ></bean><!-- 监控中心协议,如果为protocol="registry",表示从注册中心发现监控中心地址 --><dubbo:monitor protocol="registry"></dubbo:monitor><!-- 直连监控中心服务器地址,address="10.20.130.230:12080" --><!-- <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor> -->
</beans>

3 消费者设置consumer.xml,声明调用版本号

<!-- 设置 version 版本 *:任意 2.0.0 :新  1.0.0 :旧--><dubbo:reference interface="com.ruiec.gmall.service.UserService" id="userService" version="2.0.0"><!--retries 重试次数,不包含第一次  幂等操作(设置重试次数)【方法运行多少次】效果一样,推荐查询删除修改--><dubbo:method name="getUserAddressList" timeout="2000" retries="3"></dubbo:method></dubbo:reference>

7)本地存根 ps:(网址:http://dubbo.apache.org/zh-cn/docs/user/demos/local-stub.html)

远程服务后,客户端通常只剩下接口,而实现全在服务器端,但提供方有些时候想在客户端也执行部分逻辑,比如:做 ThreadLocal 缓存,提前验证参数,调用失败后伪造容错数据等等,此时就需要在 API 中带上 Stub,客户端生成 Proxy 实例,会把 Proxy 通过构造函数传给 Stub [1],然后把 Stub 暴露给用户,Stub 可以决定要不要去调 Proxy。

实现:

1 在order-service-consumer项目中创建UserServiceStub.java代码

package com.ruiec.gmall.service.impl;
​
import java.util.List;
​
import org.springframework.util.StringUtils;
​
import com.ruiec.gmall.bean.UserAddress;
import com.ruiec.gmall.service.UserService;
​
public class UserServiceStub implements UserService {
​private final UserService userService;
​/*** 传入userService的远程代理对象* @param userService*/public UserServiceStub(UserService userService) {super();this.userService = userService;}
​@Overridepublic List<UserAddress> getUserAddressList(String userId) {System.out.println("====UserServiceStub====");if (!StringUtils.isEmpty(userId)) {return userService.getUserAddressList(userId);}return null;}
​
}
​

2 在consumer.xml 中的dubbo:reference指定stub(注意写全路径)

<!-- 声明需要调用的远程服务接口:生成远程服务代理 --><!-- timeout设置超时时间,单位毫秒 --><!-- 设置 version 版本 *:任意 2.0.0 :新 1.0.0 :旧 --><!-- 配置本地存根 stub --><dubbo:reference interface="com.ruiec.gmall.service.UserService" id="userService" version="1.0.0" timeout="2000" retries="3"stub="com.ruiec.gmall.service.impl.UserServiceStub"><!--retries 重试次数,不包含第一次 幂等操作(设置重试次数)【方法运行多少次】效果一样,推荐查询删除修改 --><!-- <dubbo:method name="getUserAddressList" timeout="2000" retries="3"></dubbo:method> --></dubbo:reference>

3 启动项目,控制台输出如下:

8)与springboot整合的三种方式

1 在application.properties配置属性,使用@Service暴露服务使用@Reference引用服务

ps:这个上续已经操作过了

2 保留dubbo xml配置文件

一 将user-service-provider的provider.xml文件复制到\boot-user-service-provide\src\main\resources下面。

二 注释boot-user-service-provide的application.properties文件

三 在boot-user-service-provide中的ProvideApplication.java中使用@ImportResource导入dubbo的配置文件。(ps:注意注释掉@Service注解)

package com.ruiec.gmall;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
​
/*** 1 导入依赖*      1)、导入dubbo-starter -pom*      2)、导入dubbo的其他依赖,*    Springboot与dubbo整合的三种方式:*         1)在application.properties配置属性,使用@Service暴露服务使用@Reference引用服务*         2)保留dubbo  xml配置文件 ,使用@ImportResource导入dubbo的配置文件*         3 )  使用注解api的方式: 将每一个组件手动创建到容器中,让dubbo来扫描其他组件* @author luo_wei<br>* @date 2019年4月2日 上午9:23:58*/
//@EnableDubbo //开启基于注解的dubbo功能
@ImportResource(locations = "classpath:provider.xml")
//@EnableDubbo(scanBasePackages = "com.ruiec.gmall")
@SpringBootApplication
public class ProvideApplication {
​public static void main(String[] args) {SpringApplication.run(ProvideApplication.class, args);}
​
}
​

3 使用注解api的方式,将每一个组件手动创建到容器中,让dubbo来扫描其他组件。

一 创建MyDubboConfig.java手动创建到spring容器中。

package com.ruiec.gmall.config;
​
import java.util.ArrayList;
import java.util.List;
​
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
​
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.MethodConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.ruiec.gmall.service.UserService;
/*** dubbo组件* @author luo_wei<br>* @date 2019年4月17日 上午10:44:10*/
@Configuration
public class MyDubboConfig {
​/*** 名称空间* @author luo_wei<br>* @date 2019年4月17日 上午9:43:57*/@Beanpublic ApplicationConfig applicationConfig() {ApplicationConfig applicationConfig = new ApplicationConfig();applicationConfig.setName("boot-user-service-provide");return applicationConfig;}
​/*** 注册中心* @author luo_wei<br>* @date 2019年4月17日 上午9:43:47*/@Beanpublic RegistryConfig registryConfig() {RegistryConfig registryConfig = new RegistryConfig();registryConfig.setProtocol("zookeeper");registryConfig.setAddress("127.0.0.1:2181");return registryConfig;}
​/*** 通信规则* @author luo_wei<br>* @date 2019年4月17日 上午9:43:38*/@Beanpublic ProtocolConfig protocolConfig() {ProtocolConfig protocolConfig = new ProtocolConfig();protocolConfig.setName("dubbo");protocolConfig.setPort(20880);return protocolConfig;}
​/*** 暴露服务 ref:指向真正的服务实现* @author luo_wei<br>* @date 2019年4月17日 上午10:12:52*/@Beanpublic ServiceConfig<UserService> userServiceConfig(UserService userService) {ServiceConfig<UserService> userServiceConfig = new ServiceConfig<>();userServiceConfig.setInterface(UserService.class);userServiceConfig.setRef(userService);
​userServiceConfig.setVersion("1.0.0");userServiceConfig.setTimeout(3000);userServiceConfig.setRetries(3);
​//设置method信息MethodConfig methodConfig = new MethodConfig();methodConfig.setName("getUserAddressList");methodConfig.setTimeout(3000);
​//将method设置关联到service配置中List<MethodConfig> methods = new ArrayList<>();methods.add(methodConfig);userServiceConfig.setMethods(methods);return userServiceConfig;}
​/*** 监控中心* @author luo_wei<br>* @date 2019年4月17日 上午9:43:32*/@Beanpublic MonitorConfig monitorConfig() {MonitorConfig monitorConfig = new MonitorConfig();monitorConfig.setProtocol("registry");return monitorConfig;}
}
​

二 修改ProvideApplication.java 使用@DubboComponentScan(basePackages="com.ruiec.gmall")或者@EnableDubbo(scanBasePackages = "com.ruiec.gmall")让dubbo来扫描其他组件。

package com.ruiec.gmall;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
​
/*** 1 导入依赖*      1)、导入dubbo-starter -pom*      2)、导入dubbo的其他依赖,*    Springboot与dubbo整合的三种方式:*         1)在application.properties配置属性,使用@Service暴露服务使用@Reference引用服务*         2)保留dubbo  xml配置文件 ,使用@ImportResource导入dubbo的配置文件*         3 )  使用注解api的方式: 将每一个组件手动创建到容器中,让dubbo来扫描其他组件* @author luo_wei<br>* @date 2019年4月2日 上午9:23:58*/
//@EnableDubbo //开启基于注解的dubbo功能
//@ImportResource(locations = "classpath:provider.xml")
//@DubboComponentScan(basePackages="com.ruiec.gmall")
@EnableDubbo(scanBasePackages = "com.ruiec.gmall")
@SpringBootApplication
public class ProvideApplication {
​public static void main(String[] args) {SpringApplication.run(ProvideApplication.class, args);}
​
}
​

高可用

9)zookeeper宕机与dubbo直连

zookeeper宕机

调用过一次之后,注册中心宕机,还是可以通过缓存消费dubbo暴露到服务。

dubbo直连:在@Reference注解指定(url="127.0.0.1:20880") 即可。即可绕过注册中心。

10)负载均衡机制

种类

一 Random LoadBalance:基于权重到随机 (默认使用)

二 RoundRobin LoadBalance:基于权重到轮询(根据权重选择轮询)

三 LeastActive LoadBalance : 最少活跃数(选择响应速度最快到服务器)

四 ConsistentHash LoadBalance : 一致性hash (根据参数)

代码

使用

在@Reference注解上面配置loadbalance的值即可

设置权重

1 在暴露服务时用注解配置,即@Service注解设置weight属性

2 在管理控制台操作使用接口设置倍权或者半权

11)服务降级

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

可以通过服务降级功能临时屏蔽某个出错到非关键业务,并定义降级后到返回策略。

mock=force:return +null 表示消费方对该服务到方法调用都直接返回null值 ,不发起远程调用。用来屏蔽不重要服务队不可用时对调用方的影响。(屏蔽)

mock=fail:return+null 表示消费方对该服务的方法调用在失败后,在返回null值,不抛异常。用来容忍不重要服务不稳定时对调用方的影响。(容错)

实现:

12)集群容错 &hystrix

容错模式:

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:

Hystrix 旨在通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。

添加依赖

  • <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
      <version>1.4.4.RELEASE</version>
     </dependency>

在springboot启动类添加@EnableHystrix注解开启服务容错。

提供者端操作

  • boot-user-service-provide的UserServiceImpl.java中的getUserAddressList添加@HystrixCommand注解。
  1. com.ruiec.gmall.service.impl;

    import java.util.Arrays;
    import java.util.List;

    import org.springframework.stereotype.Component;

    import com.alibaba.dubbo.config.annotation.Service;
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import com.ruiec.gmall.bean.UserAddress;
    import com.ruiec.gmall.service.UserService;

    /**
      * 用户接口实现类
     * 1. 将服务提供者注册到注册中心 (暴露服务) 1)导入dubbo依赖(2.6.2)
     * 2. 让服务消费者订阅服务提供者的服务地址
     * @author luo_wei<br>
     * @date 2019年4月1日 下午5:10:59
     */
    //@Service(weight=1) //暴露服务
    @Service
    @Component
    public class UserServiceImpl implements UserService {

    /**
     * 根据用户编号查询所有收货地址
    * @author luo_wei<br>
    * @date 2019年4月1日 下午5:09:46
    */
    @Override
    @HystrixCommand
    public List<UserAddress> getUserAddressList(String userId) {

    UserAddress address = new UserAddress(1, "浙江省绍兴市诸暨市浣东街道西子公寓北区", "1001", "我是等爱的黑鬼", "10086", "Y");
    UserAddress address2 = new UserAddress(2, "广东省梅州市五华县安流镇青江村琴江御城201商场", "1002", "一条溺死的鱼", "10081", "N");
    if(Math.random()>0.5) {
    throw new RuntimeException();
    }
    return Arrays.asList(address, address2);
    }

    }

消费者端操作

  • boot-order-service-consumer中的OrderServiceImpl.java添加@HystrixCommand注解,并且指定调用失败返回后调用的方法。再添加注解声明中方法的实现。
  1. com.ruiec.gmall.service.impl;

    import java.util.Arrays;
    import java.util.List;

    import org.springframework.stereotype.Service;
    import org.springframework.util.StringUtils;

    import com.alibaba.dubbo.config.annotation.Reference;
    import com.alibaba.dubbo.rpc.cluster.LoadBalance;
    import com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance;
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import com.ruiec.gmall.bean.UserAddress;
    import com.ruiec.gmall.service.OrderService;
    import com.ruiec.gmall.service.UserService;

    @Service
    public class OrderServiceImpl implements OrderService {

    //@Reference(url="127.0.0.1:20880") //从注册中心发现服务  //dubbo直连
    //负载均衡策略 leastactive roundrobin random
    //@Reference(loadbalance="random")
    @Reference
    UserService userService;

    /**
     * 初始化用户订单
    * @author luo_wei<br>
    * @date 2019年4月1日 下午5:20:56
    */
    @Override
    @HystrixCommand(fallbackMethod="hello")
    public List<UserAddress> initOrder(String userId) {
    //LoadBalance
    //RandomLoadBalance
    System.out.println("用户ID:" + userId);
    List<UserAddress> list = userService.getUserAddressList(userId);
    return list;
    }

    public List<UserAddress> hello(String userId) {
    return Arrays.asList(new UserAddress(10, "测试地址", "10066", "2333", "112", "yes"));
    }
    }

  •  dubbo原理 

1、RPC原理

一次完整的RPC调用流程(同步调用,异步另说)如下:

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

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

’3)client stub找到服务地址,并将消息发送到服务端;

4)server stub收到消息后进行解码;

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

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

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

8)client stub接收到消息,并进行解码;

9)服务消费方得到最终结果。RPC框架的目标就是要2~8这些步骤都封装起来,这些细节对用户来说是透明的,不可见的。"

2、netty通信原理

Netty是一个异步事件驱动的网络应用程序框架, 用于快速开发可维护的高性能协议服务器和客户端。它极大地简化并简化了TCP和UDP套接字服务器等网络编程。

BIO:(Blocking IO)

NIO (Non-Blocking IO)

Selector 一般称 为选择器 ,也可以翻译为 多路复用器,

Connect(连接就绪)、Accept(接受就绪)、Read(读就绪)、Write(写就绪)

Netty基本原理:

3、dubbo原理

1、dubbo原理 -框架设计

l config 配置层:对外配置接口,以 ServiceConfig, ReferenceConfig 为中心,可以直接初始化配置类,也可以通过 spring 解析配置生成配置类

l proxy 服务代理层:服务接口透明代理,生成服务的客户端 Stub 和服务器端 Skeleton, 以 ServiceProxy 为中心,扩展接口为 ProxyFactory

l registry 注册中心层:封装服务地址的注册与发现,以服务 URL 为中心,扩展接口为 RegistryFactory, Registry, RegistryService

l cluster 路由层:封装多个提供者的路由及负载均衡,并桥接注册中心,以 Invoker 为中心,扩展接口为 Cluster, Directory, Router, LoadBalance

l monitor 监控层:RPC 调用次数和调用时间监控,以 Statistics 为中心,扩展接口为 MonitorFactory, Monitor, MonitorService

l protocol 远程调用层:封装 RPC 调用,以 Invocation, Result 为中心,扩展接口为 Protocol, Invoker, Exporter

l exchange 信息交换层:封装请求响应模式,同步转异步,以 Request, Response 为中心,扩展接口为 Exchanger, ExchangeChannel, ExchangeClient, ExchangeServer

l transport 网络传输层:抽象 mina 和 netty 为统一接口,以 Message 为中心,扩展接口为 Channel, Transporter, Client, Server, Codec

l serialize 数据序列化层:可复用的一些工具,扩展接口为 Serialization, ObjectInput, ObjectOutput, ThreadPool

2、dubbo原理 -启动解析、加载配置信息

3、dubbo原理 -服务暴露

4、dubbo原理 -服务引用

5、dubbo原理 -服务调用

Springboot整合Dubbo简单示例相关推荐

  1. SpringBoot整合Dubbo+Zookeeper进行分布式搭建系统

    QUESTIONl:SpringBoot整合Dubbo+Zookeeper进行分布式搭建系统? ANSWER: 一:创建项目模块 1.1.创建一个Empty Project 名称:Dubbo 1.2. ...

  2. springboot整合dubbo\zookeeper做注册中心

    springboot整合dubbo发布服务,zookeeper做注册中心.前期的安装zookeeper以及启动zookeeper集群就不说了. dubbo-admin-2.5.4.war:dubbo服 ...

  3. springboot整合dubbo时连接zookeeper——天坑

    本文主要针对使用springboot整合dubbo框架时使用zookeeper作为注册中心,在服务启动连接zookeeper产生的问题做一个详细的讲解. 主要针对两个异常 (1)java.lang.I ...

  4. 【SpringBoot整合Dubbo和Zookeeper】

    本笔记内容为狂神说SpringBoot集成Dubbo和Zookeeper部分 目录 一.Dubbo dubbo基本概念 调用关系说明 二.Dubbo环境搭建 三.Window下安装zookeeper ...

  5. 搭建大型分布式服务(十四)SpringBoot整合dubbo starter

    一.本文要点 接上文,我们已经把SpringBoot整合mybatis+Hikari+es+redis+kafka了,本文将介绍SpringBoot如何整合dubbo.系列文章完整目录 dubbo注解 ...

  6. spring整合dubbo和springboot整合dubbo,实现服务暴露区别

    spring整合dubbo的时候实现服务暴露是这么做的,在xml里配置 那么springboot整合dubbo的时候,是通过dubbo的@Service 注解实现的 之前我们是通过@Autowired ...

  7. springBoot整合Dubbo使用与采坑

    参考:springBoot整合Dubbo 下载源码到:我的下载中心去下

  8. 【SpringBoot】12.SpringBoot整合Dubbo+Zookeeper

    1. 准 备 1.1 Dubbo 简介 Apache Dubbo 是一款高性能.轻量级的开源 Java RPC 框架,它提供了三大核心能力:面向接口的远程方法调用.智能容错和 负载均衡.以及服务自动注 ...

  9. 解决springboot整合dubbo中No provider available from registry 127.0.0.1:2181 for service x.x.x on consumer

    解决springboot整合dubbo中No provider available from registry 127.0.0.1:2181 for service x.x.x on consumer ...

最新文章

  1. 终于有人把计算机视觉讲明白了 。。。
  2. Linux 操作系统原理 — 文件系统 —文件
  3. EOS下控制台以及图形界面打印sql语句
  4. Android 自带图标库 android.R.drawable
  5. @JsonProperty的使用
  6. Python学习笔记:常用内建模块1
  7. net start mysql 发生系统错误2 系统找不到指定的文件
  8. Python打卡第四周
  9. 第一个Spark程序
  10. 【渝粤题库】陕西师范大学202801 中国古代文学(五) 作业
  11. redux中间件原理-讲义
  12. 【有内鬼,终止交易】风靡朋友圈的壁纸,实现代码竟如此简单 | 原力计划
  13. 计算机应用技术参加文献,面向科技文献的机器翻译(4)-计算机应用技术专业毕业论文.docx...
  14. EAST实现自然场景下文本检测tensorflow
  15. 软考-系统分析师-论文写作-备考总结笔记
  16. No provider available from registry
  17. 教你三步实现CDH到星环TDH的平滑迁移
  18. ado连接mysql_ADO连接各种数据库的基本方法
  19. C++系列(纯虚函数和抽象类)
  20. Dynamics CRM 系统自定义部分的语言翻译

热门文章

  1. 自己免费申请的公网IP搭网盘太爽了
  2. “寻梦之路 阿里之行”北城大数据学院学生代表团赴北京阿里中心参观...
  3. Dragon Mainland(龙大陆)深度攻略
  4. k8s 创建job 执行oc命令 删除/操作宿主机内pod
  5. Wannafly挑战赛26-B 冥土追魂
  6. Java : 一个帝国的诞生
  7. 解决“RIP宣告网络”问题
  8. sqlalchemy文档笔记
  9. Java面试重点项目推荐,吃透15个项目五个offer拿到手软 轻松应对2022春招
  10. 直通车没有展现量?那你一定不知道这几点!_淘宝直通车