Dubbo概述以及使用

28 篇文章 0 订阅
2 篇文章 0 订阅

1. Dubbo是什么?

Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架(告别Web Service模式中的WSdl,以服务者与消费者的方式在dubbo上注册)
其核心部分包含:

1. 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
2. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
3. 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

2. Dubbo能做什么?

1.透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。      
2.软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
3. 服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。

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

3. dubbo的架构

dubbo架构图如下所示:
dubbo
节点角色说明:

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

调用关系说明:

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

4. dubbo使用方法【使用示例】

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

下面我们就来看看spring配置方式的写法:

下载zookeeper注册中心并启动:

下载地址:http://archive.apache.org/dist/zookeeper/【以zookeeper-3.4.5为例】http://archive.apache.org/dist/zookeeper/zookeeper-3.4.5/zookeeper-3.4.5.tar.gz,http://archive.apache.org/dist/zookeeper/zookeeper-3.4.5/下载后解压即可,进入D:\zookeeper-3.4.5\bin,双击zkServer.cmd启动注册中心服务。
在这里插入图片描述

下面这个例子不错,写的很详细可以做个model.
在这里插入图片描述

common模块:

在这里插入图片描述
注意: 实体类在远程rpc调用时必须要实现Serializable接口【must implement java.io.Serializable】

package com.shuchang.entities;

import java.io.Serializable;

//注意:实体类必须实现 可序列化Serializable接口,否则远程调用将会报错
//定义两个成员变量name和age,构造方法/setter()/getter()方法/toString()方法
//也可使用lombok工具生成对应的方法,提高开发效率
public class User implements Serializable {
    private String name;
    private int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User)) return false;

        User user = (User) o;

        if (getAge() != user.getAge()) return false;
        return getName() != null ? getName().equals(user.getName()) : user.getName() == null;
    }

    @Override
    public int hashCode() {
        int result = getName() != null ? getName().hashCode() : 0;
        result = 31 * result + getAge();
        return result;
    }
}

interface模块

在这里插入图片描述
在服务提供方实现接口:(对服务消费方隐藏实现)

package com.shuchang.service;

import com.shuchang.entities.User;

import java.util.List;

public interface SampleService {
    public String sayHello(String name);

    public List<User> getUsers();
}

依赖关系pom.xml

<?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>zookeeper-dubbo-study</artifactId>
        <groupId>com.shuchang</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>service-interface-dubbo</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.shuchang</groupId>
            <artifactId>service-common-dubbo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

provider模块

在这里插入图片描述
接口实现类:

package com.shuchang.service.impl;

import com.shuchang.entities.User;
import com.shuchang.service.SampleService;

import java.util.ArrayList;
import java.util.List;

public class SampleServiceImpl implements SampleService {

    public String sayHello(String name) {
        return "hello  " + name;
    }

    public List<User> getUsers() {
        //demo模拟从数据库中查找到的数据
        List<User> userList = new ArrayList();
        User user1 = new User("张三", 18);
        User user2 = new User("李四", 19);
        userList.add(user1);
        userList.add(user2);
        return userList;
    }
}

用Spring配置声明暴露服务:

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

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

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

    <!--声明需要暴露的服务接口-->
    <dubbo:service interface="com.shuchang.service.SampleService" ref="sampleService"/>

    <bean id="sampleService" class="com.shuchang.service.impl.SampleServiceImpl"></bean>
</beans>

加载Spring配置,启动服务:

package com.shuchang;


import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class ProviderStart {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application-dubbo.xml");
        context.start();
        // 为保证服务一直开着,利用输入流的阻塞来模拟,并在dubbo-admin的web页面观察服务
        System.in.read();
    }
}

依赖关系pom.xml

<?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>zookeeper-dubbo-study</artifactId>
        <groupId>com.shuchang</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-provider-dubbo</artifactId>

    <dependencies>
        <!--interface 依赖-->
        <dependency>
            <groupId>com.shuchang</groupId>
            <artifactId>service-interface-dubbo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--dubbo 依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.6</version>
        </dependency>
        <!-- zkclient操作zookeeper-->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>

</project>

consumer模块

在这里插入图片描述
1.通过Spring配置引用远程服务:

<?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="sample-consumer"/>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
    <dubbo:reference id="sampleService" interface="com.shuchang.service.SampleService"/>
</beans>

2.加载Spring配置,并调用远程服务:

package com.shuchang;

import com.shuchang.entities.User;
import com.shuchang.service.SampleService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.List;

public class ConsumerStart {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-dubbo.xml");
        applicationContext.start();
        SampleService sampleService = (SampleService) applicationContext.getBean("sampleService");

        String result = sampleService.sayHello("张三丰");
        System.out.println(result);
        List<User> userList = sampleService.getUsers();
        if (userList != null && userList.size() > 0) {
            for (int i = 0; i < userList.size(); i++) {
                System.out.println(userList.get(i));
            }
        }
        // 为保证服务一直开着,利用输入流的阻塞来模拟,并在dubbo-admin的web页面观察服务
        System.in.read();
    }
}

依赖关系pom.xml

<?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>zookeeper-dubbo-study</artifactId>
        <groupId>com.shuchang</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-consumer-dubbo</artifactId>
    <dependencies>
        <!--interface 依赖-->
        <dependency>
            <groupId>com.shuchang</groupId>
            <artifactId>service-interface-dubbo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--dubbo 依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.6</version>
        </dependency>
        <!-- zkclient操作zookeeper-->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>

</project>

调用结果为:
在这里插入图片描述
dubbo-admin web页面管理配置:
下载地址:
https://github.com/apache/dubbo-admin/releases
在这里插入图片描述
dubbo管理页面:
访问地址:http://127.0.0.1:8080/dubbo-admin/
在这里插入图片描述
应用页面:
在这里插入图片描述
提供者页面:
在这里插入图片描述
消费者页面:
在这里插入图片描述
服务页面:
在这里插入图片描述
测试是否成功,我觉得只要看看状态是否正常,就ok了 …

扩展:注解方式使用dubbo:

服务提供方配置:
在这里插入图片描述
服务消费方配置:
在这里插入图片描述

优化:修改项目访问路径:

当将war包直接部署到tomcat中时,如何修改对项目的访问路径?
在tomcat的目录下D:\Tools\apache-tomcat-7.0.105\conf\server.xml,在该配置文件中的Host标签下添加对项目的处理

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">

	<!-- 添加<Context path="" docBase="dubbo-admin" reloadable="true" /> -->
	<Context path="" docBase="dubbo-admin" reloadable="true" />

	<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

在host中添加 其中dubbo-admin是项目名称。
这时访问localhost:8080时就可以直接访问到dubbo-admin这个项目了(不需要localhost:8080/dubbo-admin/来访问项目了,避免一些操作因路径没添加项目名而报错出现的情况)。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值