zoukankan      html  css  js  c++  java
  • Dubbo和Zookeeper

    一.软件架构演进

      软件架构的发展经历了由单体架构、垂直架构、分布式架构到流动计算架构的演进过程。

    1.单一架构

    ​   当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。此时,用于简化增删改查工作量的数据访问框架(ORM)是关键。

    2.垂直应用架构

      当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。此时,用于加速前端页面开发的 Web 框架(MVC)是关键。

    3.分布式服务架构

      当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。此时,用于提高业务复用及整合的分布式服务框架(RPC)是关键。

    4.流动计算架构

      当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。此时,用于提高机器利用率的资源调度和治理中心(SOA)是关键。

    5.SOA是什么

      SOA 全称为 Service-Oriented Architecture,即面向服务的架构。它可以根据需求通过网络对松散耦合的粗粒度应用组件(服务)进行分布式部署、组合和使用。一个服务通常以独立的形式存在于操作系统进程中。站在功能的角度,把业务逻辑抽象成可复用、可组装的服务,通过服务的编排实现业务的快速再生,目的:把原先固有的业务功能转变为通用的业务服务,实现业务逻辑的快速复用。通过上面的描述可以发现 SOA 有如下几个特点:

    • 分布式

    • 可重用

    • 扩展灵活

    • 松耦合

    6.单体项目如何改为SOA架构

      原来的单体工程项目大多分为三层:表现层(Controller)、业务层(Service)、持久层(Dao),要改为SOA 架构,其实就是将业务层提取为服务并且独立部署即可,表现层通过网络和业务层进行通信,如下图:

    二.Apache Dubbo概述

      Apache Dubbo 是一款高性能的 Java RPC 框架。其前身是阿里巴巴公司开源的一个高性能、轻量级的开源Java RPC 框架,可以和 Spring 框架无缝集成。

    RPC 全称为 remote procedure call, 即远程过程调用。比如两台服务器 A 和 B,A 服务器上部署一个应用,B服务器上部署一个应用,A服务器上的应用想调用B服务器上的应用提供的方法,由于两个应用不在一个内存空间,不能直接调用,所以需要通过网络来表达调用的语义和传达调用的数据。需要注意的是 RPC 并不是一个具体的技术,而是指整个网络远程调用过程。RPC 是一个泛化的概念,严格来说一切远程过程调用手段都属于 RPC 范畴。各种开发语言都有自己的 RPC 框架。Java 中的 RPC 框架比较多,广泛使用的有 RMI、Hessian、Dubbo等。

    Dubbo官网地址:http://dubbo.apache.org/zh-cn/

    dubbo架构

    三.Zookeeper概述

      Zookeeper 是 Apache Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用 。

     

      服务提供者(Provider)启动时: 向 /dubbo/com.foo.BarService/providers 目录下写入自己的URL 地址服务消费者(Consumer)启动时: 订阅 /dubbo/com.foo.BarService/providers 目录下的提供者URL 地址。并向 /dubbo/com.foo.BarService/consumers 目录下写入自己的 URL 地址监控中心(Monitor)启动时: 订阅 /dubbo/com.foo.BarService 目录下的所有提供者和消费者 URL地址

    Zookeeper安装和启动

    windows版安装比较简单,安装好后启动bin目录中的zkServer.cmd即可(启动后保持窗口开启)。

    四.Dubbo使用步骤

      Dubbo 作为一个 RPC 框架,其最核心的功能就是要实现跨网络的远程调用。我们可以将服务提供者(Service层)单独部署到一台服务器上,将服务消费者(表现层)单独部署到另一台服务器上。

    1.服务提供者开发

    创建Maven工程(打包方式为war)

    1.第一步:导入Maven的坐标(dubbo相关的)

    <!-- dubbo相关 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.6</version>
    </dependency>
    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-all</artifactId>
      <version>4.1.32.Final</version>
    </dependency>
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-framework</artifactId>
      <version>4.0.0</version>
      <exclusions>
        <exclusion>
          <groupId>org.apache.zookeeper</groupId>
          <artifactId>zookeeper</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.7</version>
    </dependency>
    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.1</version>
    </dependency>

    2.第二步:配置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">
      <!-- 监听器监听其他的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>

    3.第三步:创建服务接口

    /**
     * 服务提供者的service层接口
     * @author Mr.song
     * @date 2019/05/12 21:42
     */
    public interface HelloDubboService {
        String helloDubbo(String name);
    }

    4.第四步:创建服务实现类

    /**
     * 服务提供者者(service层)
     * @author Mr.song
     * @date 2019/05/12 21:33
     */
    @Service
    public class HelloDubboServiceImpl implements HelloDubboService {
        public String helloDubbo(String name){
            return "Hello" + name;
        }
    }

    Tips: 实现类上的@Service注解不是Spring的,而是com.alibaba.dubbo.config.annotation.Service。用于对外发布服务。

    5.第五步:resources下创建applicationContext-service.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.xsd
            http://code.alibabatech.com/schema/dubbo 
            http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        <!--指定dubbo的服务名称,不能重复-->
        <dubbo:application name="dubbodemo_provider"></dubbo:application>
        <!--指定注册到zookeeper的地址 ip为zookeeper所在服务器的地址 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
        <!--指定当前服务使用的 协议和端口 端口默认是:20880 -->
        <dubbo:protocol name="dubbo" port="20888"></dubbo:protocol>
        <!--指定提供服务类所在的包,有@Service注解的类会被发布为服务-->
        <dubbo:annotation package="cn.dintalk.service"></dubbo:annotation>
    </beans>

    6.第六步:启动服务

    Tips: 开发阶段可能会有很多服务,我们可以采用在测试包下通过main方法启动服务提供者。

    /**
     * @author Mr.song
     * @date 2019/05/13 14:27
     */
    public class HelloDubboProvider {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext context =
                    new ClassPathXmlApplicationContext("classpath*:applicationContext-*.xml");
            context.start();
            System.in.read(); // 按任意键退出
        }

    2.服务消费者开发

    创建Maven工程(打包方式为war)

    1.第一步:导入Maven的坐标(dubbo相关的)(同提供者一致)

    2.第二步:配置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">
      <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:applicationContext-web.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
    </web-app>

    3.第三步:创建服务接口(消费者需要有提供者的服务接口)

    /**
     * 服务消费者的service层接口: 和提供者一模一样
     * @author Mr.song
     * @date 2019/05/12 21:43
     */
    public interface HelloDubboService {
        String helloDubbo(String name);
    }

    4.第四步:编写Controller

    /**
     * 服务消费者(web层)
     * @author Mr.song
     * @date 2019/05/12 21:35
     */
    @Controller
    public class HelloDubboController {
        @Reference
        private HelloDubboService helloDubboService;
        @RequestMapping("/hello")
        public @ResponseBody String hello(String name){
            return helloDubboService.helloDubbo(name);
        }
    }

    Tips: controller中注入HelloDubboServcie使用的注解是@Reference,是com.alibaba.dubbo.config.annotation.Reference 。

    5.第五步:在resources下创建applicationContext-web.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"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           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://code.alibabatech.com/schema/dubbo 
            http://code.alibabatech.com/schema/dubbo/dubbo.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">
        <!--创建容器时要扫描的包-->
        <context:component-scan base-package="cn.dintalk.web"></context:component-scan>
        <!--开启springmvc对注解的支持-->
        <mvc:annotation-driven></mvc:annotation-driven>
        
        <!--指定注册到dubbo中的服务名称,它不能重复-->
        <dubbo:application name="dubbodemo_consumer"></dubbo:application>
        <!--指定zookeeper的地址:连接服务中心 ip 为zookeeper所在服务器ip-->
        <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
        <!--指定dubbo的注解包扫描-->
        <dubbo:annotation package="cn.dintalk.web"></dubbo:annotation>
        <!--做合理化检查的看看注册中心中有没有提供者信息(开发阶段默认false)
           check的取值:true:表示做检查  false:表示不检查
        -->
        <dubbo:consumer check="false"></dubbo:consumer>
    </beans>

    6.第六步:运行测试

    Tips:

    • Zookeeper支持集群模式,防止单点故障

    • 集群负载均衡时,Dubbo提供了多种均衡策略(随机,轮询,最少活跃调用数,一致性Hash)缺省为random随机调用。

    负载均衡:可配置在提供者和消费者任意一方

    //提供者配置负载均衡
    @Service(interfaceClass=HelloDubboService.class,loadbalance="random")
    //消费者配置负载均衡
    @Reference(check=false,loadbalance="random")

    五.Dubbo管理控制台

      开发时,我们可能需要知道Zookeeper注册中心都有哪些服务提供者和消费者。我们可以通过部署一个管理中心来实现,管理中心就是一个web应用,部署到tomcat下即可。

    Tips: 部署后,修改WEB-INF下的dubbo.propertis文件,注意dubbo.registry.address对应的值需要对应当前使用的Zookeeper的ip地址和端口号。
    部署后访问即可:用户名和密码均为root

    关注微信公众号,随时随地学习

  • 相关阅读:
    Thinking in java(八)-正则表达式
    order by与索引
    order by与索引
    004_常量的设置
    008_ajax没有跳转页面的错误
    007_缺少aspactj依赖
    006_为什么我的本机地址是0.0.0.0.0.1
    005_mybatis逆向工程错误
    004_当用数据库账号密码不对时?
    059_SSM——JDK动态代理是怎么回事?它又是怎么运用到了SSM框架中的?
  • 原文地址:https://www.cnblogs.com/dintalk/p/10878697.html
Copyright © 2011-2022 走看看