按照国际惯例,我们先写个hello world示例。但是在写第一个hello world示例之前,我想有必要先给大家简单介绍些dubbo的框架原理及基本组成。
dubbo是阿里巴巴著名的开源分布式SOA治理框架,它每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。
其配置灵活性强,性能表现也相当不错,并且支持很多服务特性(如异步调用、参数回调、事件监听等)是一款非常优秀的SOA框架。
要知道,dubbo首先是一个rpc(远程过程调用)框架,其次才是一个soa治理框架。
既然是rpc,就必然存在服务端和客户端。服务端提供服务接口,客户端远程调用服务方的接口,这是我们脑海中经常出现的场景。然而dubbo却不止于此,除了服务方和调用方,dubbo还提出了注册中心、监控中心等组件。所有的服务提供方的服务接口都需要到注册中心注册,所有的服务调用方(消费方)需要到注册中心监听/订阅自己感兴趣的服务。基于此,服务消费方就可以调用服务提供方提供的接口。在整个过程中,服务消费方甚至都不知道服务提供方的具体地址。实际上服务提供方和服务消费方都只需要知道注册中心的地址就可以了。
那么监控中心又是什么呢?
监控中心可以直接连接到注册中心,能够监控到注册中心中有已注册了哪些服务,有多少服务提供方正在提供服务,有多少服务消费方正在消费服务等。实际上监控中心的作用还不仅于此,它还能控制接口的上下线,配置服务访问的ACL路由策略等。
说了这么多,我们还是来看下阿里官方给出的dubbo架构图吧。
节点角色说明:
- Provider: 暴露服务的服务提供方。
- Consumer: 调用远程服务的服务消费方。
- Registry: 服务注册与发现的注册中心。
- Monitor: 统计服务的调用次调和调用时间的监控中心。
- Container: 服务运行容器。
调用关系说明:
- 0. 服务容器负责启动,加载,运行服务提供者。
- 1. 服务提供者在启动时,向注册中心注册自己提供的服务。
- 2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
- 3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
- 4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
- 5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
是的,这就是dubbo架构。相信大家看完这个架构图,心里对dubbo的认识都有个谱了吧。
好吧,接下来我们开始介绍dubbo第一个helloworld 示例。
hello world力求尽可能地简单易懂,我们只需要建立两个简单的java工程即可(一个做服务提供方dubboProvider,
一个做服务消费方dubboConsumer)。由于消费方需要使用提供方接口的定义及实体类元信息,
故服务方需要将接口及实体类等打成jar包供消费方使用。
第一个示例(d1) :
dubboProvider 服务提供方示例代码及配置:
DemoService接口
package com.ruihao.dubboProvider.d1;
public interface DemoService {
public String sayHello(String name);
}
DemoServiceImpl 接口的实现
package com.ruihao.dubboProvider.d1;
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "hello "+name;
}
}
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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 服务提供方配置 -->
<!-- 应用名称 用于计算依赖-->
<dubbo:application name="ruihao-d1"/>
<!--注册中心 -->
<dubbo:registry id="registry" address="N/A" protocol="zookeeper" />
<!-- rpc远程调用协议 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 业务bean -->
<bean id="demoService" class="com.ruihao.dubboProvider.d1.DemoServiceImpl"></bean>
<!-- 服务提供方,提供一个demoService的服务 -->
<dubbo:service interface="com.ruihao.dubboProvider.d1.DemoService" ref="demoService" >
</dubbo:service>
</beans>
主类,启动服务提供方应用(实际上启动的是spring容器)。
package com.ruihao.dubboProvider.d1;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ProviderA {
public static void main(String[] args){
try {
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("/com/ruihao/dubboProvider/d1/provider.xml",ProviderA.class );
System.out.println("服务器启动");
context.start();
System.in.read();
context.close();
System.out.println("服务器停止");
} catch (Exception e) {
e.printStackTrace();
}
}
}
dubboCusomer服务消费方示例代码及配置:
主类,启动客户端应用
public class CustomerA {
public static void main(String[] args) {
try {
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("/com/ruihao/dubboCustomer/d1/customer.xml",
CustomerA.class );
context.start();
System.out.println("客户端启动");
DemoService demoService = (DemoService)context.getBean("demoService");
String msg = demoService.sayHello("world");
System.out.println( msg );
System.out.println("客户端停止");
context.close();
} catch ( Exception e) {
e.printStackTrace();
}
}
}
customer.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 服务消费方 -->
<!-- 消费方应用名称,用于计算依赖 -->
<dubbo:application name="ruihao-d1-customer"/>
<!-- 直接引入 服务提供方接口 -->
<dubbo:reference id="demoService" interface="com.ruihao.dubboProvider.d1.DemoService"
url="dubbo://127.0.0.1:20880"/>
</beans>
ok,我们先启动服务提供方,再启动服务消费方,即可看到客户端rpc调用效果。
d1 这个例子是如此地简单,以致于连dubbo注册中心这个最为重要的组件都没有使用到。在实际应用中,我们通常是将服务注册到注册中心,消费方直接到注册中心进行服务订阅即可,d2这个例子相对完整一些,它使用到了注册中心组件。(关于监控中心组件,我们会稍后介绍,它的搭建必须前提必须要有注册中心)
第二个示例(d2):
provider提供方示例代码及相关配置
DemoService 接口
package com.ruihao.dubboProvider.d2;
public interface DemoService {
public String sayHello(String name);
}
DemoService接口实现
package com.ruihao.dubboProvider.d2;
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "hello "+name;
}
}
provider.xml 配置。 这里我们使用到了zookeeper注册中心,我们需要zookeeper服务的支持。
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 服务提供方配置 -->
<!-- 应用名称 用于计算依赖-->
<dubbo:application name="ruihao-d2"/>
<!--注册中心 -->
<dubbo:registry address="192.168.9.130:2181" protocol="zookeeper" />
<!-- rpc远程调用协议 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 业务bean -->
<bean id="demoService" class="com.ruihao.dubboProvider.d2.DemoServiceImpl"></bean>
<!-- 服务提供方,提供一个demoService的服务 -->
<dubbo:service interface="com.ruihao.dubboProvider.d2.DemoService" ref="demoService" >
</dubbo:service>
</beans>
主类,启动服务应用
package com.ruihao.dubboProvider.d2;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ProviderB {
public static void main(String[] args){
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"/com/ruihao/dubboProvider/d2/provider.xml",
ProviderB.class );
System.out.println("服务器启动");
context.start();
System.in.read();
context.close();
System.out.println("服务器停止");
} catch (Exception e) {
e.printStackTrace();
}
}
}
customer消费方示例代码及相关配置
customer.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 服务消费方 -->
<!-- 消费方应用名称,用于计算依赖 -->
<dubbo:application name="ruihao-d2-customer"/>
<!--注册中心 -->
<dubbo:registry id="registry" address="192.168.9.130:2181" protocol="zookeeper" />
<!-- 直接引入 服务提供方接口 -->
<dubbo:reference id="demoService" interface="com.ruihao.dubboProvider.d2.DemoService" registry="registry"/>
</beans>
主类,启动消费方应用。
package com.ruihao.dubboCustomer.d2;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ruihao.dubboProvider.d2.DemoService;
public class CustomerB {
public static void main(String[] args) {
try {
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("/com/ruihao/dubboCustomer/d2/customer.xml",
CustomerB.class );
context.start();
DemoService demoService = (DemoService)context.getBean("demoService");
String msg = demoService.sayHello("asdfasd");
System.out.println( msg );
context.close();
} catch ( Exception e) {
e.printStackTrace();
}
}
}
ok, 我们在启动第二个例子的服务方和消费方,看看调用效果。。。
至此,我们已经将dubbo架构图中的服务方、消费方、注册中心组件都用上, 唯一最后没有派上用场的
是监控中心组件。别急,接下来我们就要用上监控中心了。
dubbo监控中心的使用十分简单。下载dubbo-admin包,解压将其中的dubbo-admin放出来
到tomcat的webapp目录下,修改dubbo-admin应用中的WEB-INF/classes/dubbo.properties配置。
设置zookeeper注册中心的连接地址,admin账号和guest账号的密码 。
dubbo.registry.address=zookeeper://192.168.9.130:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
启动所在tomcat,在浏览器中输入http://localhost:端口/dubbo-admin 即可访问了。
helloworld 工程源码 百度云下载:
链接:http://pan.baidu.com/s/1skN80RN 密码:v1l5
。。。。。。