dubbo+Zookeeper使用
版本信息:
dubbo:2.6.0
zookeeper:3.4.6
spring:5.0.5
Dubbo 简介
Apache Dubbo是一款高性能的Java RPC框架。
RPC:远程及调用:两个应用部署在不同的服务器上,不在同一个内存空间,不能直接调用数据,所以需要通过网络来表达调用的数据.
Dubbo三大核心功能:
面向接口的远程方法调用;
智能负载均衡
自动注册和发现服务
Dubbo的架构图:
调用关系:
服务容器负责启动,加载,运行服务提供者。
服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者在启动时,向注册中心订阅自己所需的服务。
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给
消费者。
- 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失
败,再选另一台调用。
- 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中
心。
Zoopkeeper:注册中心
zookeeper是Apache 的子项目,是一个树形的目录服务:
安装过程:
1.解压缩压缩包 tar -zxvf zookeeper-3.4.6.tar.gz -C /usr
2.进入zookeeper-3.4.6目录,创建data目录 mkdir data
3.进入conf目录 ,把zoo_sample.cfg 改名为zoo.cfg ;
4.cd confmv zoo_sample.cfg zoo.cfg
5.打开zoo.cfg文件, 修改data属性:dataDir=/usr/zookeeper-3.4.6/data
zookeeper常用命令:
进入Zookeeper的bin目录:
启动服务命令 ./zkServer.sh start
停止服务命令 ./zkServer.sh stop
查看服务状态: ./zkServer.sh status
客户端连接 ./zkCli.sh
进入客户端:查看结点:
ls +路径:
Dubbo管理控制台:
版本:dubbo-admin-2.6.0.war
修改WEB-INF下的dubbo.properties文件,注意dubbo.registry.address对应的值需要对应当前
使用的Zookeeper的ip地址和端口号.
使用tomcat运行项目.
用户名密码均为root
Dubbo的配置:
使用applicationContext.xml
需要的头信息:
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
服务提供方配置:
<?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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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://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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
<dubbo:application name="dubbodemo_provider"/>
<!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
<dubbo:registry address="zookeeper://192.168.111.132:2181"/>
<!-- 注册 协议和port -->
<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
<!-- 扫描指定包,加入@Service注解的类会被发布为服务 -->
<dubbo:annotation package="com.lt.service"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务控制的注解支持-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target- class="true"/>
</beans>
服务方web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="3.0" xmlns="http://java.sun.com/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_3_0.xsd">
<display-name>dubbo-provider</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
服务方注解:
@Service(interfaceClass = HelloService.class) public class HolleServiceImpl implements HolleService {
注意:服务实现类上使用的Service注解是Dubbo提供的,用于对外发布服务
interfaceclass指定对应的接口
####消费方配置:
<?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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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://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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
<dubbo:application name="dubbodemo_consumer"/>
<!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
<dubbo:registry address="zookeeper://192.168.111.132:2181"/>
<!-- 扫描指定包, -->
<dubbo:annotation package="com.lt.controller"/>
<!--xml配置文件方式配置:将@Refence改为@Autowrite-->
<!--生成RPC远程调用的远程对象-->
<!--<dubbo:reference interface="com.lt.service.HolleService" id="holleService"></dubbo:reference>-->
<!--<context:component-scan base-package="com.lt.controller"></context:component-scan>-->
</beans>
消费方web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="3.0" xmlns="http://java.sun.com/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_3_0.xsd">
<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:applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
消费方注解:
@Reference(loadbalance = "random") private HolleService holleService;
使用Dubbo自动注入代理对象.
注意:Controller中注入HelloService使用的是Dubbo提供的@Reference注解
loadbalance:负载均衡算法,不指定为默认随机算法
Dubbo无法发布被事务代理的Service问题:
是事务控制的底层原理是为服务提供者类创建代理对象,而默认情况下Spring是基于JDK动态代理方式创建
代理对象,而此代理对象的完整类名为com.sun.proxy.$Proxy42(最后两位数字不是固定的),导致
Dubbo在发布服务前进行包匹配时无法完成匹配,进而没有进行服务的发布。
可以通过断点调试的方式查看Dubbo执行过程,Dubbo通过AnnotationBean的
postProcessAfterInitialization方法进行处理
解决方案:
指定proxy-target-class属性,值为true。其作用是使用cglib代理方式为Service类创建代理对象
<!--开启事务控制的注解支持-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
在Service注解中加入interfaceClass属性,值为HelloService.class作用是指定服务的接口类型
@Service(interfaceClass = HelloService.class)
@Transactional
public class HelloServiceImpl implements HelloService