分布式架构(1):Dubbo
1、Dubbo
官网:http://dubbo.apache.org/
参考文档:http://dubbo.apache.org/books/dubbo-user-book/preface/background.html
1.1什么是Dubbo
Dubbo是 阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成。
-
一款分布式服务框架
-
高性能和透明化的RPC远程服务调用方案
-
SOA服务治理方案
1.2什么是RPC
RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务的分布式服务框架。而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。
1.3微服务
微服务架构是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间互相协调、互相配合,为用户提供最终价值。
了解垂直结构和分部结构
https://blog.csdn.net/noaman_wgs/article/details/70214612/
1.4dubbo主要核心部件:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次数和调用时间的监控中心。
1.5dubbo的结构
1.6查看dubbo的官方,首页快速入门案例
http://dubbo.apache.org/
调用流程
0.服务容器负责启动,加载,运行服务提供者。
1.服务提供者在启动时,向注册中心注册自己提供的服务。
2.服务消费者在启动时,向注册中心订阅自己所需的服务。
3.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。 使用类似zookeeper等分布式协调服务作为服务注册中心,可以将绝大部分项目配置移入zookeeper集群。
4.服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5.服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
1.7dubbo使用注意
模型需要系列化
添加dubbo和zookeeper依赖
<!-- start..............dubbo.................-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- end..............dubbo.................-->
2、Dubbo应用-Provider服务方
2.1创建一个独立的service接口模块
2.2创建service接口实现类模块
模块需要引用公共的dao模块
引用service接口模块
实现的service类加个名称
2.3在resoures中添加spring的dao配置
2.4在spring添加dubbo服务
<?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-4.3.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="edu-eb-service-provider" />
<!-- 使用zookeeper注册中心-->
<dubbo:registry protocol="zookeeper" address="192.168.1.110:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.yuxiu.edu.eb.service.IUserService" ref="userService" />
</beans>
2.5》发布微服务
使用Main方法,加载spring配置文件,然后发布服务
public class App {
public static void main( String[] args ){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
context.start();
//线程等待
synchronized (App.class) {
while (true) {
try {
App.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
3、Dubbo应用-Comsumer消费者
3.1添加一个Web模块,并引用服务接口模块
edu-eb-web
3.2然后配置好springmvc
3.3在spring的配置文件中配置dubbo的消费者
<?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-4.3.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="edu-eb-web" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<!-- 注册中心地址 -->
<dubbo:registry protocol="zookeeper" address="192.168.1.110:2181" />
<!-- 用户服务接口 -->
<dubbo:reference interface="com.yuxiu.edu.eb.service.IUserService" id="userService" check="false" />
</beans>
3.4配置个控制器调用service
其实也就是远程调用
4.1Dubbo后台管理安装
-
将
dubbo-admin-2.5.3.war放在tomcat7
中执行 -
记得Java_Home要改成1.7,高版本的jdk不兼容
-
然后访问http://localhost:8888/dubbo-admin-2.5.3,帐号和密码默认都为root
-
要修改帐号和密码可以修改
dubbo.properties
文件