dubbo架构与简单案例实现

 一 dubbo的概述

       Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。关于注册中心、协议支持、服务监控等内容.

二 dubbo架构

节点角色说明:

Provider: 暴露服务的服务提供方。

Consumer: 调用远程服务的服务消费方。

Registry: 服务注册与发现的注册中心。

Monitor: 统计服务的调用次数和调用时间的监控中心。

Container: 服务运行容器。

调用关系说明:

0. 服务容器负责启动,加载,运行服务提供者。

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

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

3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

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

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

服务注册中心: 保存信息的地方,Dubbo默认使用zookeeper作为注册中心.

三 dubbo的使用

Spring调用Dubbo的服务.

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可。

 

在提供方增加暴露服务配置<dubbo:service>,

在消费方增加引用服务配置<dubbo:reference>。

 

发布服务:

<!-- 和本地服务一样实现远程服务 -->

<bean id="xxxService" class="com.xxx.XxxServiceImpl" />

<!-- 增加暴露远程服务配置 -->

<dubbo:service interface="com.xxx.XxxService" ref="xxxService" />

 

调用服务:

<!-- 增加引用远程服务配置 -->

<dubbo:reference id="xxxService" interface="com.xxx.XxxService" />

<!-- 和本地服务一样使用远程服务 -->

<bean id="xxxAction" class="com.xxx.XxxAction">

<property name="xxxService" ref="xxxService" />

</bean>

 

四 案例参考

4.1 本文是采用zookeeper作为dubbo的注册中心,所以首先需要先配置zookeeper的服务器,linux下zookeeper服务的配置可以参考:

https://blog.csdn.net/qq_34758475/article/details/82494006

4.2 maven依赖的配置

4.3 dubbo和zookeeper的整合

4.3.1 dubbo与zookeeper的简单整合,基于Maven开发模式,创建interface接口:

package com.alibaba.dubbo.demo;

public interface DemoService {

    String sayHello(String name);

}

4.3.2 interface的实现类

package com.alibaba.dubbo.demo.provider;

import com.alibaba.dubbo.demo.DemoService;

 

public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {

        return "Hello " + name;

    }

}

4.3.3 配置服务的提供方(provider)

<?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="demo-provider"/>

    <dubbo:registry address="zookeeeper://127.0.0.1:2181"/>

    <dubbo:protocol name="dubbo" port="20880"/>

    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>

    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>

</beans>

4.3.4 开启服务的提供方(provider)

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class Provider {

    public static void main(String[] args) throws Exception {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

                new String[] {"META-INF/spring/dubbo-demo-provider.xml"});

        context.start();

        System.in.read(); // press any key to exit

    }

}

4.3.5 配置服务的消费方(consumer)

<?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="demo-consumer"/>

    <dubbo:registry address="zookeeper//127.0.0.1:2181"/>

    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>

</beans>

4.3.6 运行服务的消费方(consumer)

import com.alibaba.dubbo.demo.DemoService;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class Consumer {

    public static void main(String[] args) throws Exception {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

                new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});

        context.start();

        DemoService demoService = (DemoService) context.getBean("demoService"); // obtain proxy object for remote invocation

        String hello = demoService.sayHello("world"); // execute remote invocation

        System.out.println(hello); // show the result

    }

}

猜你喜欢

转载自blog.csdn.net/qq_34758475/article/details/82737122
今日推荐