什么是Dubbo?
Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,SOA服务治理方案。简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用,说白了就是个远程服务调用的分布式框架。告别Web Service模式中的wsdl,以服务者与消费者的方式在dubbo 上注册)。
其核心部分包含:
1.远程通讯,提供对多种基于长连接的NiO框架抽象封装,包括多种线程模型,序列化,以及“请求一响应”模式的信息交换方式。
2.集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持。以
及负载均衡,失败容错,地址路由,动态配置等集群支持。
3.自动发现:基于注册中心目录服务,使用服务消费能动态查找服务提供方,使地址透明,使用服务提供方可以平滑增加或减少服务器
2.dubbo原理解析
1.创建itmayiedu-member-interface工程提供会员接口(只写接口文件,pom.xml里面什么都不加)
//会员服务接口
public interface UserService {
//使用userid查询 用户信息
public String getUser(Long userId);
}
2.创建itmayiedu-member-provider工程生产者(pom.xml文件里面要添加itmayiedu-member-interface的依赖,还有dubbo,zookeeper的依赖也要添加进去)
Maven依赖参数
<dependencies>
<dependency>
<groupId>com.itmayiedu</groupId>
<artifactId>itmayiedu-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<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>
</dependencies>
provider.xml详解
3.创建itmayiedu-order-consumer工程(消费者)
pom.xml里面添加的是itmayiedu-member-interface依赖(面向接口编程),还有dubbo,zookeeper的依赖也要添加进去)
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itmayiedu</groupId>
<artifactId>itmayiedu-order-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.itmayiedu</groupId>
<artifactId>itmayiedu-member-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<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>
</dependencies>
</project>
consumer.xml详解
然后我们在provider工程里面的test文件夹下建一个测试Java文件,该段代码用于发布服务。
然后在consumer工程的test文件夹下建一个orderService类,main方法用于测试接收dubbo服务