分布式架构(1):Dubbo

1、Dubbo

官网:http://dubbo.apache.org/
参考文档:http://dubbo.apache.org/books/dubbo-user-book/preface/background.html

1.1什么是Dubbo

Dubbo是 阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成。

  • 一款分布式服务框架

  • 高性能和透明化的RPC远程服务调用方案

  • SOA服务治理方案

1.2什么是RPC

RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务的分布式服务框架。而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。

1.3微服务

微服务架构是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间互相协调、互相配合,为用户提供最终价值。

了解垂直结构和分部结构
https://blog.csdn.net/noaman_wgs/article/details/70214612/

1.4dubbo主要核心部件:

Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次数和调用时间的监控中心。

1.5dubbo的结构

在这里插入图片描述

1.6查看dubbo的官方,首页快速入门案例

http://dubbo.apache.org/
在这里插入图片描述
调用流程
0.服务容器负责启动,加载,运行服务提供者

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

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

3.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。 使用类似zookeeper等分布式协调服务作为服务注册中心,可以将绝大部分项目配置移入zookeeper集群。

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

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

1.7dubbo使用注意

模型需要系列化
添加dubbo和zookeeper依赖

<!-- start..............dubbo.................-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.3</version>
    <exclusions>
        <exclusion>
            <artifactId>spring</artifactId>
            <groupId>org.springframework</groupId>
        </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>

<!-- end..............dubbo.................-->

2、Dubbo应用-Provider服务方

2.1创建一个独立的service接口模块

在这里插入图片描述

2.2创建service接口实现类模块

模块需要引用公共的dao模块
引用service接口模块
实现的service类加个名称
在这里插入图片描述

2.3在resoures中添加spring的dao配置

在这里插入图片描述

2.4在spring添加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-4.3.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
       <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="edu-eb-service-provider"  />

    <!-- 使用zookeeper注册中心-->
    <dubbo:registry protocol="zookeeper" address="192.168.1.110:2181"/>

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.yuxiu.edu.eb.service.IUserService" ref="userService" />

</beans>

2.5》发布微服务
使用Main方法,加载spring配置文件,然后发布服务

public class App {
    public static void main( String[] args ){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
        context.start();
		//线程等待
        synchronized (App.class) {
            while (true) {
                try {
                    App.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

3、Dubbo应用-Comsumer消费者

3.1添加一个Web模块,并引用服务接口模块

 edu-eb-web

3.2然后配置好springmvc

3.3在spring的配置文件中配置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-4.3.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="edu-eb-web" />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <!-- 注册中心地址 -->
    <dubbo:registry protocol="zookeeper" address="192.168.1.110:2181" />

    <!-- 用户服务接口 -->
    <dubbo:reference interface="com.yuxiu.edu.eb.service.IUserService" id="userService" check="false" />
</beans>

3.4配置个控制器调用service

其实也就是远程调用
在这里插入图片描述

4.1Dubbo后台管理安装

  • dubbo-admin-2.5.3.war放在tomcat7中执行

  • 记得Java_Home要改成1.7,高版本的jdk不兼容

  • 然后访问http://localhost:8888/dubbo-admin-2.5.3,帐号和密码默认都为root

  • 要修改帐号和密码可以修改dubbo.properties文件

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值