微服务技术→Dubbo分布式服务治理框架-服务注册与发现(一)

文章参考: 蚂蚁课堂笔记 及下面这位博主 感谢输出分享
https://blog.csdn.net/noaman_wgs/article/details/70214612/


Dubbo产生的背景

什么是Dubbo

  • 一款分布式服务框架
  • 高性能和透明化的RPC远程服务调用方案
  • SOA服务治理方案

Dubbo架构

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

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

Dubbo注册中心

  • 对于服务提供方,它需要发布服务,而且由于应用系统的复杂性,服务的数量、类型也不断膨胀;
  • 对于服务消费方,它最关心如何获取到它所需要的服务,而面对复杂的应用系统,需要管理大量的服务调用。
    而且,对于服务提供方和服务消费方来说,他们还有可能兼具这两种角色,即既需要提供服务,有需要消费服务。

通过将服务统一管理起来,可以有效地优化内部应用对服务发布/使用的流程和管理。服务注册中心可以通过特定协议来完成服务对外的统一。

Dubbo提供的注册中心有如下几种类型可供选择:

  • Multicast注册中心
  • Zookeeper注册中心
  • Redis注册中心
  • Simple注册中心

Dubbo优缺点

优点

  1. 透明化的远程方法调用
    像调用本地方法一样调用远程方法;只需简单配置,没有任何API侵入。
  2. 软负载均衡及容错机制
    可在内网替代nginx lvs等硬件负载均衡器。
  3. 服务注册中心自动注册 & 配置管理
    不需要写死服务提供者地址,注册中心基于接口名自动查询提供者ip。
    使用类似zookeeper等分布式协调服务作为服务注册中心,可以将绝大部分项目配置移入zookeeper集群。
  4. 服务接口监控与治理
    Dubbo-admin与Dubbo-monitor提供了完善的服务接口管理与监控功能,针对不同应用的不同接口,可以进行 多版本,多协 议,多注册中心管理。

缺点

  1. 只支持JAVA语言

Dubbo快速入门

环境步骤

  1. 安装Zookeepr启动
  2. 创建Maven项目搭建生产者和消费者
  3. 安装DubboAdmin平台,实现监控

1. Zookeeper介绍与安装

本Demo中的Dubbo注册中心采用的是Zookeeper。为什么采用Zookeeper呢?

Zookeeper是一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据 ,这里能很好的作为Dubbo服务的注册中心。Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求

具体的安装方法在此不一一叙述,可参考博文:
http://blog.csdn.net/tlk20071/article/details/52028945

安装完成后,进入到bin目录,并且启动zkServer.cmd,这个脚本中会启动一个java进程:
(注:需要先启动zookeeper后,后续dubbo demo代码运行才能使用zookeeper注册中心的功能)
在这里插入图片描述

2. 创建MAVEN项目

项目结构:
主要分三大模块:

  • dubbo-api : 存放公共接口;
  • dubbo-consumer : 调用远程服务;
  • dubbo-provider : 提供远程服务。

在这里插入图片描述

2.1 首先创建父项目 DubboDemo
2.1.1 创建空的父项目即可,删除src文件夹
2.2 创建存放公共接口模块 Itmayiedu-dubbo-api
2.2.1 创建对外暴露的接口 服务提供者会实现该接口 写入相关业务逻辑
package com.itmayiedu.api;

public interface DemoApiService {
    public String getUser(Long userId);
}

2.3 创建提供远程服务(生产者提供服务)模块 Itmayiedu-dubbo-provider
2.3.1 pom文件添加相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>DubboDemo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itmayiedu</groupId>
    <artifactId>Itmayiedu-dubbo-provider</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.mayiedu</groupId>
            <artifactId>Itmayiedu-dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- ZK客户端工具-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- dubbo底层框架基于Netty实现-->
        <dependency>
            <groupId>org.jboss.netty</groupId>
            <artifactId>netty</artifactId>
            <version>3.2.5.Final</version>
        </dependency>
        <!-- Spring框架组件-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <!-- dubbo框架  2.5.3-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jboss.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</project>
2.3.2 创建实现接口的实现类
package com.itmayiedu.service.impl;

import com.itmayiedu.api.DemoApiService;

public class DemoApiServiceImpl implements DemoApiService {
    public String getUser(Long userId) {
        System.out.println("生产者调用消费者服务接口userId:" + userId);
        return "yushengjun";
    }
}

2.3.3 新增 dubbo-provider.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-admin 或 dubbo-monitor 会显示这个名字,方便辨识 -->
    <dubbo:application name="demotest-provider" />
    <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper -->
    <dubbo:registry address="zookeeper://localhost:2181" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口 -->
    <dubbo:service interface="com.itmayiedu.api.DemoApiService" ref="demoService" protocol="dubbo" />
    <!--具体实现该接口的 bean -->
    <bean id="demoService" class="com.itmayiedu.service.impl.DemoApiServiceImpl" />
</beans>
2.3.4 启动远程服务
package com.itmayiedu;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class ProviderApp {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml");
        System.out.println(context.getDisplayName() + ": here");
        context.start();
        System.out.println("服务已经启动...");  // 保持服务一直在运行
        System.in.read();
    }
}

2.4 创建调用远程服务(消费者调用服务)模块 Itmayiedu-dubbo-consumer
2.4.1 pom文件添加相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>DubboDemo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Itmayiedu-dubbo-consumer</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.mayiedu</groupId>
            <artifactId>Itmayiedu-dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.netty</groupId>
            <artifactId>netty</artifactId>
            <version>3.2.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jboss.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

</project>
2.4.2 新增 dubbo-consumer.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:application name="demotest-consumer" owner="programmer" organization="dubbox"/>
    <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
    <dubbo:reference id="demoApiService" interface="com.itmayiedu.api.DemoApiService"/>
</beans>
2.4.3 启动远程服务
package com.itmayiedu;

import com.itmayiedu.api.DemoApiService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConsumerApp {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
        context.start();
        System.out.println("consumer start");
        DemoApiService demoApiService = context.getBean(DemoApiService.class);
        String result = demoApiService.getUser(1l);
        System.out.println("result:" + result);
    }
}

2.4.4 控制台成功打印 服务提供者的输出信息

在这里插入图片描述

当然,这只是一个模拟的项目,实际中有多提供者多消费者情况,比这要复杂的多,当然只有这样才能体现dubbo的特性。

Dubbo管理控制台介绍

DubboAdmin部署

将dubbo-admin.zip放入到TomcatWebapps目录下,修改dubbo.properties中的Zookeeper连接地址即可。

管理控制台功能

路由规则,动态配置,服务降级
访问控制,权重调整
负载均衡
在这里插入图片描述

下载dubbo-admin,可自行根据网上介绍安装。大致做法就是将dubbo-admin中 的某个文件夹内容替换到tomcat的conf中,再运行tomcat即可。但我在实际操作中发现JDK8无法运行,后来找到一个JDK8可以实现的dubbo-admin版本,下载地址:http://www.itmayun.com/it/files/226631678709806/resource/901920001882583/1.html。

成功开启输入用户名密码root后,即可进入控制台首页查看消费者提供者情况:
查看提供者:
在这里插入图片描述
查看消费者:
在这里插入图片描述

目前,阿里又开始更新,有兴趣可以查看:
https://github.com/apache/incubator-dubbo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iBaoxing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值