简介

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

官网:​​http://dubbo.io/​​​ GIT地址:​​https://github.com/alibaba/dubbo​​ 用户手册:​​http://dubbo.apache.org/​

Dubbo的架构:
Dubbo 学习1 快速开始_分布式

开始

  • JDK: version 6 or higher
  • Maven: version 3 or higher

项目结构:
provider
Dubbo 学习1 快速开始_阿里巴巴_02
consumer
Dubbo 学习1 快速开始_阿里巴巴_03

Maven

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
</dependency>

Provider

定义服务接口
建议将接口定义在客户端、服务端公用的模块里。

package com.alibaba.dubbo.demo;

public interface DemoService {
String sayHello(String name);
}

实现

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;
}
}

配置服务生产者

<?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: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>

启动生产者

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"dubbo-demo-provider.xml"});
context.start();
System.in.read(); // press any key to exit
}
}

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="multicast://224.5.6.7:1234"/>
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>

执行

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[]{"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
}
}

运行结果:
Dubbo 学习1 快速开始_阿里巴巴_04

一些问题处理

dubbo 使用logback

<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--zkClient和disconf都会依赖zookeeper,而zookeeper会依赖slf4j-log4j12和log4j,
在我们使用logback的情况下,需要将log4j转为logback,则需依赖log4j-over-slf4j。
slf4j-log4j12会与log4j-over-slf4j循环依赖导致冲突,
所以所有依赖zookeeper的jar必须exclude掉slf4j-log4j12-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<!--
https://issues.apache.org/jira/browse/ZOOKEEPER-1371
zookeeper Remove dependency on log4j in the source code.
slf4j-log4j12会与log4j-over-slf4j循环依赖导致冲突
-->
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</dependency>

在logback.xml里写上:

<logger name="com.alibaba.dubbo" level="DEBUG"/>

在dubbo的配置文件里写:

<dubbo:application name="controlservice-rpc-provider" logger="slf4j">