Dubbo学习
1了解分布式
分布式系统是若干独立系统的集合,但是用户使用起来像是在使用一套系统
应用架构的发展演变:
(1)单一架构:当网站的流量很小的时候,我们将所有的应用放到一台服务器上
如公司内部的管理系统/超市收银系统
(2)垂直架构:将大的应用拆分成小的应用,按照业务拆分,如订单系统,支付系统,展示系统等,优点是扩展较容易,缺点是业务越多,没办法解决业务之间的相互条用
(3)分布式架构(基于RPC:远程过程调用,Remote Procedure Call):将业务拆分之后,用某种凡是实现各个业务模块的远程调用和复用,于是Dubbo就出现了,是一个高性能的RPC框架,解决了分布式中的远程调用问题,搭配注册中心进行分布式控制效果更好,于是要学习Zookeeper。后面再说
2 Dubbo介绍
Dubbo高性能的原因
既然涉及到远程调用,就需要利用序列化和网络通信
1序列化方面:序列化方案有很多,比如下xml、json、二进制流。。其中效率最高的就是二进制流的方式进行序列化,而dubbo采用的就是二进制流的方式实现序列化的
2网络通信方面,不同于HTTP协议中的7步走(三次握手4次挥手
策略),Dubbo采用的是Socket通信,一步到位,提高了通信的效率,并且可以建立长连接,不用反复的连接。
其他的RPC框架:gRPC,thrift,HSF等
Dubbo 介绍
18年阿里把这个捐给了apache,让apache去更新和维护,官网为
是一款高性能轻量级的开永远java RPC框架,他提供了三大核心能力:面向接口的远程调用,智能容错和负载均衡,以及服务自动注册和发现。
服务提供者:暴露服务的服务提供方,服务提供者在启动时,向注册中心注册自己提供的服务
服务消费者:条用远程服务的服务消费方,服务消费者在启动时向注册中心订阅自己所需的服务,服务消费者从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败则在选取另一台
注册中心:注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者
监控中心:服务消费者和提供者,在内存中积累调用次数和调用时间,定时每分钟发送依次统计数据到监控中心
粒度要求
- 服务接口尽可能大力度,每一个服务的方法应代表一个功能,而不是某功能的一个步骤
- 服务接口建议以业务场景为单位划分,并对相近的业务做抽象,防止接口数量爆炸
- 不建议使用过于抽象的通用接口
- 每一个接口都要定义版本号,区分同一接口的不同实现,可以根据版本来区别不同的接口,可以解决因为功能扩展而产生接口调用的歧义
3dubbo在spring框架中的应用
- 首先要先建一个普通的Java接口工程,里面有接口和实体类,用来帮助消费者调取提供者提供的业务。注意实体类一定要实现可序列化的接口,因为dubbo的核心之一就是通过序列化在网络上传输。
注意实体类一定要实现可序列化接口
2. 创建生产者,是一个web工程,加入web依赖、dubbo依赖、接口工程依赖、zookeeper依赖,日志信息依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!--把接口工程引入-->
<dependency>
<groupId>com.njupt</groupId>
<artifactId>020-zkdubbo-interface</artifactId>
<version>1.0.0</version>
</dependency>
<!--zookeeper依赖-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-annotations-api</artifactId>
<version>9.0.54</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.11</version>
<scope>compile</scope>
</dependency>
</dependencies>
在web工程中要有接口的具体实现类,完成业务的实现,并且在资源目录下要配置dubbo配置文件,来暴漏接口和连接zookeeper注册管理中心
a 接口的具体实现
b 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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--先声明服务提u供着的名称-->
<dubbo:application name="020-zkdubbo-provideer"/>
<!--声明dubbo使用的名称和接口-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--使用zookeeper注册中心,下面是是linux上的地址和端口号-->
<dubbo:registry address="zookeeper://192.168.66.***:2181"/>
<!--暴漏服务额接口-->
<dubbo:service interface="com.njupt.service.UserService" ref="uerServiceImpl"/>
<!--加载接口实现类-->
<bean id="uerServiceImpl" class="com.njupt.service.imp.UserServiceImpl"/>
</beans>
c:web工程在web.xml中配置监听器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-zk-provider.xml</param-value>
</context-param>
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
- 创建消费者,用来调用注册中心里面的接口,然后通过接口调用到提供者提供的服务(具体的接口实现),也是一个web工程,和提供者一样加入web依赖、dubbo依赖、接口工程依赖、zookeeper依赖等
a:编写Controller,来显示调用的方法和结果
b: 编写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://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!--声明dubbo消费者名称,来保证唯一性-->
<dubbo:application name="020-zkdubbo-consumer"/>
<!--指定注册中心,从中获取接口方法-->
<dubbo:registry address="zookeeper://192.168.66.***:2181"/>
<!--使用注册中心里面的接口-->
<dubbo:reference id="userService" interface="com.njupt.service.UserService"/>
</beans>
c. spring的配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.njupt.web"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
d. 在web.xml中配置中央调度器和加载配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml,classpath:dubbo-zk-consumer.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
最后可以通过浏览器页面发起访问,会出现结果过程如下:controller->dubbo->zookeeper->provider