Dubbo和Zookeeper

一.软件架构演进

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

1.单一架构

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

2.垂直应用架构

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

3.分布式服务架构

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

4.流动计算架构

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

5.SOA是什么

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

  • 分布式apache

  • 可重用windows

  • 扩展灵活spring-mvc

  • 松耦合tomcat

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

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

相关文章
相关标签/搜索