栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

分布式RPC框架Apache Dubbo

分布式RPC框架Apache Dubbo

1. 软件架构的演进过程         软件架构的发展经历了由单体架构、垂直架构、SOA 架构到微服务架构的演进过程,下面我们分别了解一下这几个架构。 1.1 单体架构 架构说明: 全部功能集中在一个项目内(All in one )。 架构优点: 架构简单,前期开发成本低、开发周期短,适合小型项目。 架构缺点:         1.全部功能集成在一个工程中,对于大型项目不易开发、扩展和维护。         2.技术栈受限,只能使用一种语言开发。         3.系统性能扩展只能通过扩展集群节点,成本高。 1.2 垂直架构  架构说明: 按照业务进行切割,形成小的单体项目。 架构优点: 技术栈可扩展(不同的系统可以用不同的编程语言编写)。 架构缺点:         1.功能集中在一个项目中,不利于开发、扩展、维护。         2.系统扩张只能通过集群的方式。         3.项目之间功能冗余、数据冗余、耦合性强。 1.3 SOA架构         OA全称为 Service-Oriented Architecture ,即面向服务的架构。它可以根据需求通过网络对松散耦合的粗粒度应用组件( 服务 ) 进行分布式部署、组合和使用。一个服务通常以独立的形式存在于操作系统进程中。         站在功能的角度,把业务逻辑抽象成可复用的服务,通过服务的编排实现业务的快速再生,目的:把原先固有的业务功能转变为通用的业务服务,实现业务逻辑的快速复用。 架构说明:         将重复功能或模块抽取成组件的形式,对外提供服务,在项目与服务之间使用ESB (企业服务总线)的形式作为通信的桥梁。 架构优点:         1.重复功能或模块抽取为服务,提高开发效率。         2,可重用性高。         3.可维护性高。 架构缺点:         1.各系统之间业务不同,很难确认功能或模块是重复的。         2.抽取服务的粒度大。         3.系统和服务之间耦合度高。  1.4 微服务架构 架构说明: 将系统服务层完全独立出来,抽取为一个一个的微服务。抽取的粒度更细,遵循单一原则。 采用轻量级框架协议传输。 架构优点:         1.服务拆分粒度更细,有利于提高开发效率。         2.可以针对不同服务制定对应的优化方案。         3.适用于互联网时代,产品迭代周期更短。 架构缺点:         1.粒度太细导致服务太多,维护成本高。         2.分布式系统开发的技术成本高,对团队的挑战大。  2. Apache Dubbo概述 2.1 Dubbo简介         Apache Dubbo是一款高性能的 Java RPC 框架。其前身是阿里巴巴公司开源的、轻量级的开源 Java RPC框架,可以和Spring 框架无缝集成, 2018 年阿里巴巴把这个框架捐献给了 apache 基金会。 2.2 Dubbo架构 

        实现服务发现的方式有很多种,Dubbo 提供的是一种 Client-based 的服务发现机制,通常还需要部署额外的第三方注册中心组件来协调服务发现过程,如常用的 Nacos、Consul、Zookeeper 等,Dubbo 自身也提供了对多种注册中心组件的对接,用户可以灵活选择。

 3. 服务注册中心Zookeeper         通过前面的Dubbo 架构图可以看到, Registry (服务注册中心)在其中起着至关重要的作用。 Dubbo 官方推荐使用Zookeeper作为服务注册中心。 3.1 Zookeeper介绍

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

下载地址:http://archive.apache.org/dist/zookeeper/

3.3 修改zookeeper的配置文件

        拷贝zookeeper目录下得conf文件夹下得zoo_sample.cfg,取名zoo.cfg,打开zoo.cfg文件并修改。

# The number of milliseconds of each tick
tickTime=2000

# The number of ticks that the initial 
# synchronization phase can take
initLimit=10

# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5

# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
#dataDir=/tmp/zookeeper
dataDir=C:/app/apache-zookeeper-3.7.0-bin/mylog

# the port at which the clients will connect
clientPort=2181

# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60

#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true


#解决zookeeper启动占用了8080端口
admin.serverPort=8888
3.4 执行命令

 3.5 打包dubbo-admin并运行

        解压dubbo-admin ,在 dubbo-admin 解压目录下打开命令行窗口执行, 然后默默等待执行完毕。

 

 

        打包完毕以后,我们找到target 目录下的 jar 包,命令行窗口下执行 java -jar jar 包全名,即可运行dubbo-admin服务了,访问: http://localhost:8080 用户名和密码: root root

 

        在dubbo-admin-server目录下可以查看到关于zookeeper的配置以及默认登录账号密码的配置。 

4、SpringBoot+Zookeeper+Dubbo整合  4.1 服务提供方开发 开发步骤: (1)创建 maven 工程(打包方式为 war ) dubbodemo_provider ,在 pom.xml 文件中导入如下坐标

 UTF-8
 1.8
 1.8
 5.0.5.RELEASE


 
  org.springframework
  spring-context
  ${spring.version}
 
 
  org.springframework
  spring-beans
  ${spring.version}
 
 
  org.springframework
  spring-webmvc
  ${spring.version}
 
 
  org.springframework
  spring-jdbc
  ${spring.version}
 
 
  org.springframework
  spring-aspects
  ${spring.version}
 
 
  org.springframework
  spring-jms
  ${spring.version}
 
 
  org.springframework
  spring-context-support
  ${spring.version}
 
 
 
  com.alibaba
  dubbo
  2.6.0
 
 
  org.apache.zookeeper
  zookeeper
  3.4.7
 
 
  com.github.sgroschupf
  zkclient
  0.1
 
 
  javassist
  javassist
  3.12.1.GA
 
 
  com.alibaba
  fastjson
  1.2.47
 


 
  
   org.apache.maven.plugins
   maven-compiler-plugin
   2.3.2
   
    1.8
    1.8
   
  
  
   org.apache.tomcat.maven
   tomcat7-maven-plugin
   
    
    8081    
    /
   
  
 
( 2 )配置 web.xml 文件


 ArchetypeCreatedWebApplication
 
  contextConfigLocation
  classpath:applicationContext*.xml
 
 
  org.springframework.web.context.ContextLoaderListener
 
( 3 )创建服务接口
package com.lxs.service;

public interface HelloService{
    public String sayHello(Stringname);
}
( 4 )创建服务实现类
package com.lxs.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.lxs.service.HelloService;

@Service
public class HelloServiceImpl implementsHelloService{  
    public String sayHello(Stringname){
        return "hello"+name;
    }
}
注意:服务实现类上使用的 Service 注解是 Dubbo 提供的,用于对外发布服务 ( 5 )在 src/main/resources 下创建 applicationContext-service.xml


    
    
                                
    
    
    
        
    
  
   contextConfigLocation
   classpath:applicationContext-web.xml
  
  1
 
 
  springmvc
  *.do
 
( 3 )将服务提供者工程中的 HelloService 接口复制到当前工程 ( 4 )编写 Controller 5 、 SpringBoot+Zookeeper+Dubbo 整合
package com.lxs.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.lxs.service.HelloService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/demo")
public class HelloController{
  @Reference
  private HelloService helloService;
  @RequestMapping("/hello")
  @ResponseBody
  public String getName(Stringname){
    //远程调用
    String result=helloService.sayHello(name);
    System.out.println(result);
    return result;
  }
}
注意: Controller 中注入 HelloService 使用的是 Dubbo 提供的 @Reference 注解 ( 5 )在 src/main/resources 下创建 applicationContext-web.xml


    
    
    
    
    
    
( 6 )运行测试 在浏览器输入 http://localhost:8082/demo/hello.do?name=Jack ,查看浏览器输出结果 5. Dubbo管理控制台

        我们在开发时,需要知道Zookeeper 注册中心都注册了哪些服务,有哪些消费者来消费这些服务。我们可以通过部署一个管理中心来实现。其实管理中心就是一个web 应用,部署到 tomcat 即可。 5.1 安装

安装步骤:

(1)将资料中的dubbo-admin-2.6.0.war文件复制到tomcat的webapps目录下

(2)启动tomcat,此war文件会自动解压

( 3 )修改 WEB-INF 下的 dubbo.properties 文件,注意 dubbo.registry.address 对应的值需要对应当前使用的Zookeeper 的 ip 地址和端口号dubbo.registry.address=zookeeper://192.168.134.129:2181dubbo.admin.root.password=root dubbo.admin.guest.password=guest ( 4 )重启 tomcat 5.2 使用

操作步骤:

(1)访问http://localhost:8080/dubbo-admin-2.6.0/,输入用户名(root)和密码(root)

(2)启动服务提供者工程和服务消费者工程,可以在查看到对应的信息 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/753126.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号