dubbo介绍及实战

1. dubbo是什么?

Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
其核心部分包含:

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

2. dubbo的架构

节点角色说明:

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

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

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

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

       Container: 服务运行容器。

调用关系说明:

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

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

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

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

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

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

3. dubbo使用方法

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。如果不想使用Spring配置,而希望通过API的方式进行调用(不推荐)。

服务提供者
1. 定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)
package m.dubbo.demo.api;

public interface DemoService {
     String sayHello();
}
2.在服务提供方实现接口:
package m.dubbo.demo.provider;

import m.dubbo.demo.api.DemoService;

public class DemoServiceImpl implements DemoService{

    @Override
    public String sayHello() {
        return "Hello, World!";
        
    }
}
3. 用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="demo-provider"/>
    <dubbo:registry address="multicast://224.5.6.7:1234"></dubbo:registry>
    <dubbo:protocol name="dubbo" port="20880" host="10.0.0.5"></dubbo:protocol>
    <bean id="userService" class="m.dubbo.demo.provider.UserServiceImpl"/>
    <bean id="demoService" class="m.dubbo.demo.provider.DemoServiceImpl"/>
    <dubbo:service interface="m.dubbo.demo.api.UserService" ref="userService"></dubbo:service>
    <dubbo:service interface="m.dubbo.demo.api.DemoService" ref="demoService"></dubbo:service>
</beans>
注意:如果服务提供者所在机器是有多网卡的,需要设置dubbo:protocol的host属性,不然服务消费者就有可能条用不到(报错误no provider available for the service....)。
4. 加载Spring配置,启动服务:
package m.dubbo.demo.provider;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoProvider {
    public static void main(String[] args) throws IOException{
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:*.xml");
        context.start();
        System.out.println("服务已经启动...");
        System.in.read();
    }
}
消费者
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="demo-consumer"></dubbo:application>
    <dubbo:registry address="multicast://224.5.6.7:1234"></dubbo:registry>
    <dubbo:reference check="false" id="userService" interface="m.dubbo.demo.api.UserService"/>
    <dubbo:reference check="false" id="demoService" interface="m.dubbo.demo.api.DemoService"/>
</beans>
1. 加载Spring配置,并调用远程服务:
package m.dubbo.demo.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import m.dubbo.demo.api.DemoService;
import m.dubbo.demo.api.UserService;

public class DemoConsumer {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:*.xml");
        context.start();
        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService.getUser(1L));
        DemoService demoService = context.getBean("demoService", DemoService.class);
        System.out.println(demoService.sayHello());
    }

}

 以下为基于gradle创建的项目源代码https://files.cnblogs.com/files/jmbkeyes/dubbo_demo.zip

转载于:https://www.cnblogs.com/jmbkeyes/p/7531980.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值