dubbo的配置和使用

  • 为什么使用dubbo?/duddo是为了解决什么问题?
  • 什么是dubbo?
  • dubbo能做什么?
  • Dubbo有哪些协议?
  • dubbo架构
  • 调用过程
  • 使用方法


为什么使用dubbo?/duddo是为了解决什么问题?

随着互联网的不断发展,网站的应用规模不断扩大,常规垂直应用架构已经无法应对。

什么是dubbo?

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

dubbo能做什么?

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

Dubbo有哪些协议?

默认用的dubbo协议、Http、RMI、Hessian

dubbo架构

如图:

Dubbo 服务接口 dubbo服务配置_Dubbo 服务接口


角色说明:

Container:运行服务的容器。

Provider:服务提供者,暴露服务。

Registy:注册中心,服务注册和发现。

Consumer:服务消费者,调用远程服务。

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

调用过程

1.启动注册中心应用,例如官方推荐的zookeeper、redis等(不推荐)。
2.服务容器负责启动、加载、运行服务提供者。
3.服务提供者在启动时,向注册中心注册自己提供的服务。
4.服务消费者在启动时,向注册中心订阅自己需要的服务。
5.注册中心返回服务提供者地址列表给服务消费者,如果有变更,注册中心基于长连接推送变更数据给消费者。
6.服务消费者,基于软负载均衡算法,从服务提供者地址列表中选中一个,进行调用。如果调用失败,再选一台调用。
7.服务提供者和消费者,定时每分钟发送一次,累计调用次数和调用时间,到监控中心。

使用方法

1 .使用dubbo前先启动zookeeper。
2.加入maven依赖:

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>dubbo</artifactId>
	<version>2.5.6</version>
</dependency>
<!-- 添加zk客户端依赖 -->
<dependency>
	<groupId>com.github.sgroschupf</groupId>
	<artifactId>zkclient</artifactId>
	<version>0.1</version>
</dependency>

3.定义服务接口: (该接口需单独打包,在服务提供方和消费方添加该接口的依赖)
例:

package cn.jiangdoc.service;  
  
import java.util.List;  
  
public interface DemoService {  
  
    String sayHello(String name);  
  
    public List getUsers();  
  
}

4.服务提供者

(1).定义服务提供者服务

package cn.jiangdoc.service.impl;  
  
import java.util.ArrayList;  
import java.util.LinkedList;  
import java.util.List;  
  
  
public class DemoServiceImpl implements DemoService{  
      
     public String sayHello(String name) {  
            return "Hello " + name;  
     }  
     public List getUsers() {  
         List list = new ArrayList();  
         User u1 = new User();  
         u1.setName("jack");  
         u1.setAge(20);  
         u1.setSex("男");  
           
         User u2 = new User();  
         u2.setName("tom");  
         u2.setAge(21);  
         u2.setSex("女");  
           
         User u3 = new User();  
         u3.setName("rose");  
         u3.setAge(19);  
         u3.setSex("女");  
           
         list.add(u1);  
         list.add(u2);  
         list.add(u3);  
         return list;  
     }  
}

(2).用Spring配置声明暴露服务: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:application name="demo_provider"  />  
   
    <!-- 使用multicast广播注册中心暴露服务地址   
    <dubbo:registry address="multicast://224.5.6.7:1234" />-->  
    
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
  <!--实际项目中使用properties文件的形式定义zookeeper的地址 -->
  <-- <dubbo:registry protocol="zookeeper" address="${zookeeper.address}"  check="false" file="dubbo.properties" /> -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />   
    
    <!-- 用dubbo协议在20880端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="20880" />  
   
    <!-- 声明需要暴露的服务接口 version是服务的版本号dubbo只找对应版本号的服务提供者进行调用 timeout 超时时间 超过时间过报错-->  
    <dubbo:service interface="cn.jiangdoc.service.DemoService" ref="demoService" version="1.0" timeout="5000"/>  
    <!-- 具体的实现bean  一般实际项目中 不会把bean写在dubbo配置中,例如采用注解开发时,通过扫描的方式把bean交给spring管理,这里不需要写,直接在dubbo-service引用就好-->  
    <bean id="demoService" class="cn.jiangdoc.service.impl.DemoServiceImpl" />  
    
	<!--监控中心配置 监控中心协议,如果为protocol="registry",表示从注册中心发现监控中心地址,否则直连监控中心。 -->
	<!--<dubbo:monitor protocol="registry"></dubbo:monitor>-->
	<!-- 直连监控中心服务器地址 -->
	<!-- <dubbo:monitor address="localhost:6379"></dubbo:monitor> -->
      
</beans>

(3).启动dubbo服务

// 启动服务
public class ProviderServer {
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("dubbo-provider.xml");
		applicationContext.start();
		System.out.println("服务提供者启动成功...");
		System.in.read();
	}
}

5.定义消费者
(1).通过Spring配置引用远程服务: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="consumer" />
	<!-- 使用multicast广播注册中心暴露发现服务地址 -->
	<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
	<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
	<dubbo:reference id="demoService" interface="cn.jiangdoc.service.DemoService" />
</beans>

(2).应用服务

// 启动服务
public class ConsumerServer {
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("dubbo-consumer.xml");
		applicationContext.start();
		System.out.println("###消费者启动####");
		 DemoService demoService=(DemoService) applicationContext.getBean("demoService");
		 System.out.println("消费者调用生产者服务开始");
		 System.out.println(demoService.sayHello("dubbo"));
	}
}