分布式框架——Dubbox知识总结

一、什么是SOA架构

  SOA是Service-Oriented Architecture的首字母简称,它是一种支持面向服务的架构样式。从服务、基于服务开发和服务的结果来看,面向服务是一种思考方式。其实SOA架构更多应用于互联网项目开发。
  为什么互联网项目会采用SOA架构呢?随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,迫切需一个治理系统确保架构有条不紊的演进。
在这里插入图片描述

二、Dubbox简介

  Dubbo 是一个分布式服务框架,其前身是阿里巴巴开源项目Dubbo ,被国内电商及互联网项目中使用,后期阿里巴巴停止了该项目的维护,当当网便在Dubbo基础上进行优化,并继续维护,为了与原有的Dubbo区分,故将其命名为Dubbox。
  Dubbox 致力于提供高性能和透明化的RPC[RPC是远程过程调用(Remote Procedure Call)的缩写形式]远程服务调用方案,以及SOA服务治理方案。简单的说,dubbox就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbox这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架。
  Dubbo是一个基于RPC的分布式服务框架【只有在分布式】

Dubbo运行结构图
在这里插入图片描述

节点角色说明:
•
Provider: 暴露服务的服务提供方。
  Consumer: 调用远程服务的服务消费方。
  Registry: 服务注册与发现的注册中心。
  Monitor: 统计服务的调用次调和调用时间的监控中心。
  Container: 服务运行容器。
调用关系说明:

   0. 服务容器负责启动,加载,运行服务提供者。
   1. 服务提供者在启动时,向注册中心注册自己提供的服务。
   2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
   3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

三、Zookeeper 介绍

  官方推荐使用 zookeeper 注册中心。注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小。
  Zookeeper 是 Apacahe Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为Dubbox 服务的注册中心,工业强度较高,可用于生产环境。

四、Zookeeper 在Linux系统的安装

安装步骤:

  第1步:安装 Linux(此步省略)[因为我们使用的windows系统,所以我们只能把linux安装在虚拟机中]
  【设置虚拟机网络连接方式为仅主机。】

  第2步:下载zookeeper安装包
  https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz

  第3步:打开linux虚拟机,查看ip地址
在这里插入图片描述

  第4步:打开SecureCRT工具连接到对应的ip地址所指示的 linux 系统。
在这里插入图片描述

  第5步:通过SecureCRT工具将在windows系统中下载好的zookeeper安装包上传到 linux 系统。
    5.1 Alt+P 进入SFTP 输入put zookeeper安装包路径上传
在这里插入图片描述

    5.2查看上传成功以后的zookeeper安装包

[wxrj@wxrj ~]$ ll
鎬荤敤閲?452488
drwxrwxr-x. 9 wxrj wxrj      4096 12鏈?15 14:30 apache-tomcat-7.0.103
-rwxrwxrwx. 1 wxrj wxrj   9604761 4鏈? 18 2020 apache-tomcat-7.0.103.tar.gz
-rw-rw-r--. 1 wxrj wxrj  12515974 12鏈?24 11:16 apache-zookeeper-3.6.2-bin.tar.gz
drwxrwxr-x. 9 wxrj wxrj      4096 12鏈?15 14:17 eclipse
-rwxrwxrwx. 1 wxrj wxrj 264956749 8鏈?  2 2019 eclipse-jee-luna-SR2-linux-gtk-x86_64.tar.gz
........

    5.3 修改zookeeper安装包的访问权限

[wxrj@wxrj ~]$ 
[wxrj@wxrj ~]$ chmod 777 apache-zookeeper-3.6.2-bin.tar.gz 
[wxrj@wxrj ~]$ ll
鎬荤敤閲?452488
drwxrwxr-x. 9 wxrj wxrj      4096 12鏈?15 14:30 apache-tomcat-7.0.103
-rwxrwxrwx. 1 wxrj wxrj   9604761 4鏈? 18 2020 apache-tomcat-7.0.103.tar.gz
-rwxrwxrwx. 1 wxrj wxrj  12515974 12鏈?24 11:16 apache-zookeeper-3.6.2-bin.tar.gz
drwxrwxr-x. 9 wxrj wxrj      4096 12鏈?15 14:17 eclipse
.......

    5.4解压缩zookeepe压缩包

[wxrj@wxrj-linux ~]$ tar -zxvf  apache-zookeeper-3.6.2-bin.tar.gz
.........
[wxrj@wxrj ~]$ ll
鎬荤敤閲?452492
drwxrwxr-x. 9 wxrj wxrj      4096 12鏈?15 14:30 apache-tomcat-7.0.103
-rwxrwxrwx. 1 wxrj wxrj   9604761 4鏈? 18 2020 apache-tomcat-7.0.103.tar.gz
drwxrwxr-x. 6 wxrj wxrj      4096 12鏈?24 11:30 apache-zookeeper-3.6.2-bin
-rwxrwxrwx. 1 wxrj wxrj  12515974 12鏈?24 11:16 apache-zookeeper-3.6.2-bin.tar.gz
drwxrwxr-x. 9 wxrj wxrj      4096 12鏈?15 14:17 eclipse
......

    5.5进入 zookeeper的解压目录,创建 data文件夹。

[wxrj@wxrj ~]$ cd apache-zookeeper-3.6.2-bin
[wxrj@wxrj apache-zookeeper-3.6.2-bin]$ ll
鎬荤敤閲?40
drwxr-xr-x. 2 wxrj wxrj  4096 9鏈?  4 20:43 bin
drwxr-xr-x. 2 wxrj wxrj  4096 9鏈?  4 20:43 conf
drwxr-xr-x. 5 wxrj wxrj  4096 9鏈?  4 20:44 docs
drwxrwxr-x. 2 wxrj wxrj  4096 12鏈?24 11:30 lib
-rw-r--r--. 1 wxrj wxrj 11358 9鏈?  4 20:43 LICENSE.txt
-rw-r--r--. 1 wxrj wxrj   432 9鏈?  4 20:43 NOTICE.txt
-rw-r--r--. 1 wxrj wxrj  1963 9鏈?  4 20:43 README.md
-rw-r--r--. 1 wxrj wxrj  3166 9鏈?  4 20:43 README_packaging.md
[wxrj@wxrj apache-zookeeper-3.6.2-bin]$ mkdir data
[wxrj@wxrj apache-zookeeper-3.6.2-bin]$ ll
鎬荤敤閲?44
drwxr-xr-x. 2 wxrj wxrj  4096 9鏈?  4 20:43 bin
drwxr-xr-x. 2 wxrj wxrj  4096 9鏈?  4 20:43 conf
drwxrwxr-x. 2 wxrj wxrj  4096 12鏈?24 11:33 data
drwxr-xr-x. 5 wxrj wxrj  4096 9鏈?  4 20:44 docs
drwxrwxr-x. 2 wxrj wxrj  4096 12鏈?24 11:30 lib
-rw-r--r--. 1 wxrj wxrj 11358 9鏈?  4 20:43 LICENSE.txt
-rw-r--r--. 1 wxrj wxrj   432 9鏈?  4 20:43 NOTICE.txt
-rw-r--r--. 1 wxrj wxrj  1963 9鏈?  4 20:43 README.md
-rw-r--r--. 1 wxrj wxrj  3166 9鏈?  4 20:43 README_packaging.md

    5.6 修改conf目录中的zoo_sample.cfg文件的名称,修改成zoo.cfg

[wxrj@wxrj apache-zookeeper-3.6.2-bin]$ cd conf
[wxrj@wxrj conf]$ ll
鎬荤敤閲?12
-rw-r--r--. 1 wxrj wxrj  535 9鏈?  4 20:43 configuration.xsl
-rw-r--r--. 1 wxrj wxrj 3435 9鏈?  4 20:43 log4j.properties
-rw-r--r--. 1 wxrj wxrj 1148 9鏈?  4 20:43 zoo_sample.cfg
[wxrj@wxrj conf]$ mv zoo_sample.cfg zoo.cfg
[wxrj@wxrj conf]$ ll
鎬荤敤閲?12
-rw-r--r--. 1 wxrj wxrj  535 9鏈?  4 20:43 configuration.xsl
-rw-r--r--. 1 wxrj wxrj 3435 9鏈?  4 20:43 log4j.properties
-rw-r--r--. 1 wxrj wxrj 1148 9鏈?  4 20:43 zoo.cfg

    5.7打开zoo.cfg , 修改 data 属性

[wxrj@wxrj conf]$ vim  zoo.cfg

  点击“i”进入插入模式
  修改 data 属性:dataDir=/home/wxrj/apache-zookeeper-3.6.2-bin/data
  按“Esc”,输入“:wq”

    5.8 进入bin目录,启动zookeeper服务输入命令
  进入bin目录,启动服务输入命令

[wxrj@wxrj conf]$ cd ..
[wxrj@wxrj apache-zookeeper-3.6.2-bin]$ cd bin
[wxrj@wxrj bin]$ ./zKServer.sh start

  输出以下内容表示启动成功
在这里插入图片描述

    5.8 进入bin目录,关闭zookeeper服务输入命令
[wxrj@wxrj-linux bin]$ ./zKServer.sh stop
  输出以下提示信息
在这里插入图片描述

五、服务提供者开发

5.1 创建一个空的项目

5.2 删除src目录

5.3 选中空项目右键新建module[新建一个子模块{war}]

在这里插入图片描述

5.4 打开dubboxdemoservice子模块的pom导入依赖包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>
<!-- dubbo相关 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.3</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
  <groupId>org.apache.zookeeper</groupId>
  <artifactId>zookeeper</artifactId>
  <version>3.4.6</version>
</dependency>
<dependency>
  <groupId>com.github.sgroschupf</groupId>
  <artifactId>zkclient</artifactId>
  <version>0.1</version>
</dependency>

5.完善子模块的项目结构

6.在web.xml文件中配置加载spring配置文件【applicationContext-service.xml】

<!-- 配置加载spring配置文件 -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext-service.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

7.在resources目录下创建spring配置文件【applicationContext-service.xml】,添加dubbo命名空间地址

<?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.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
</beans>

5.8 创建一个业务访问接口

package com.click369.dubbodemo.service;
/**
 * 用户信息的服务层访问接口
 */
public interface UserService {
    //得到用户名
    public String getUserName();
}

5.9 创建接口实现类

package com.click369.dubbodemo.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.click369.dubbodemo.service.UserService;
@Service
public class UserServiceImpl implements UserService{
    @Override
    public String getUserName() {
        return "zhangsan";
    }
}

  注意:Service注解与原来不同,需要引入com.alibaba包下的

5.10 在spring的配置文件中注册服务

<dubbo:application name="dubboxdemoservice"/>
<dubbo:registry address="zookeeper://192.168.137.128:2181"/>
<dubbo:annotation package="com.click369.dubbodemo.service.impl"/>

5.11 部署服务提供者

在这里插入图片描述

六、服务消费者开发

6.1 选中空项目右键新建module[新建一个子模块{war}]

6.2 打开dubboxdemoweb子模块的pom导入依赖包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>
<!-- dubbo相关 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.3</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
  <groupId>org.apache.zookeeper</groupId>
  <artifactId>zookeeper</artifactId>
  <version>3.4.6</version>
</dependency>
<dependency>
  <groupId>com.github.sgroschupf</groupId>
  <artifactId>zkclient</artifactId>
  <version>0.1</version>
</dependency>

6.3 完善子模块的项目结构

6.4 配置web.xml文件

<filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
  </init-param>
  <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置中央处理器-->
<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-web.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <!--http://127.0.0.1:8080/springmvc1/add.do-->
  <url-pattern>*.do</url-pattern>
</servlet-mapping>

6.5 resources下创建applicationContext-web.xml,添加dubbo命名空间

<?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.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
</beans>

6.6 复制在服务提供者模块中创建的业务访问接口

6.7 创建控制器

package com.click369.dubbodemo.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.click369.dubbodemo.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
    @Reference
    private UserService userService;
    /**
     *得到用户名的请求处理方法
     */
    @RequestMapping("/show.do")
    @ResponseBody
    public  String  showUserName(){
        return userService.getUserName();
    }
}

6.8 在spring的配置文件中订阅服务

<!-- 消费者订阅服务 -->
<dubbo:application name="dubboxdemoweb" />
<dubbo:registry address="zookeeper://192.168.137.128:2181"/>
<dubbo:annotation package="com.click369.dubbodemo.controller" />

6.9 部署服务消费者

在这里插入图片描述

6.10启动注册中心

6.11 启动dubboxdemoservice服务【服务提供者】

6.12 启动dubboxdemoweb服务【服务消费者】

6.13 打开浏览器输入http://localhost:9090/dubboweb/show.do

七、可能出现的错误

问题1:连接zookeeper失败

  运行dubboxdemo-service以后,控制输出的提示信息的最后一行中没有出现下面语句

“信息: Starting ProtocolHandler ["http-bio-8080"]”

  说明dubboxdemo-service程序没有正常连接到注册中心所在的计算机。
  原因:可能是注册中心所在的计算机【Linux ContOS6.5】的防火墙未关闭。
  解决方法:
  计算机【Linux ContOS6.5】
  1.切换至root账号下

[wxrj@wxrj-linux ~]$ su root
密码:
[root@wxrj-linux ~]#

  2.永久性关闭防火墙

[root@wxrj-linux ~]# chkconfig   iptables  off
重启系统

  3.查看防火墙状态

[root@wxrj-linux ~]# service iptables status
iptables:未运行防火墙。

问题2:
java.lang.NoSuchMethodError: org.springframework.core.io.ResourceEditor.<init>(Lorg/springframework/core/io/ResourceLoader;Lorg/springframework/core/env/PropertyResolver;)V

  原因: 引入dubbo依赖时,dubbo自带有spring依赖包,导致与spring-webmvc引入的spring依赖包冲突
  解决方法:排除dubbo引入时自带spring依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.3</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
        </exclusion>
    </exclusions>
</dependency>

问题3: 启动服务请提供者时
java.lang.IllegalStateException: No ConfigurableListableBeanFactory set

  原因:使用的dubbo版本和spring的版本不兼容,具体是dubbo的2.5.3版本与spring的5.1.5.RELEASE版本不兼容或者说不相互支持。
  解决方法:降低spring的版本从spring的5.1.5.RELEASE版本降低到 5.0.5 RELEASE版本以下

拓展知识:

@Reference 、@Resource和@Autowired的简单理解

  1.@Autowired

org.springframework.beans.factory.annotation.Autowired

  SpringBoot项目中常用。简单来说就是引入由Spring容器管理的bean。
  2.@Resource

javax.annotation.Resource

  作用相当于@Autowired,只不过@Autowired是byType自动注入,而@Resource默认byName自动注入。
  3. @Reference

com.alibaba.dubbo.config.annotation.Reference;

  @Reference是dubbo的注解,也是注入,他一般注入的是分布式的远程服务的对象,需要dubbo配置使用。
  简单来说他们的区别:
  @Reference注入的是分布式中的远程服务对象,@Resource和@Autowired注入的是本地spring容器中的对象。

@Controller和@RestController的区别?

  @RestController注解相当于@ResponseBody + @Controller合在一起的作用
  1)如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
  例如:本来应该到success.jsp页面的,则其显示success.
  2)如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
  3)如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值