文章目录
前言
提示:以下是本篇文章正文内容,下面案例可供参考
一、Dubbo相关
1.1 Dubbo介绍
Dubbo全称是Apache Dubbo 是一款高性能的Java RPC框架,前身是阿里巴巴开发的开源、轻量级的Java RPC框架,可以和Spring无缝集成。2018年的时候捐给了apache基金会。
Dubbo官网
1.2 RPC
- RPC全称是Remote Procedure Call 即 远程过程调用,比如服务器A和B上分别部署有两个应用A和应用B,由于两个应用不在同一个内存空间,因此不能直接调用,只能通过网络来表达调用的语义和传达调用的数据(简单来说就是,只能通过网络进行接口调用);
- RPC 并不是一个具体的技术,而是指的整个网络远程调用的过程;
- RPC是一个泛化的概念,严格来说,一切远程调用的手段都属于RPC的范畴,各种语言都有自己的RPC框架。Java的比较多,使用比较广泛的有,RMI、Hessian、Dubbo等
1.3 Dubbo架构图
dubbo架构图(Dubbo官方提供的)如下:
-
节点
节点 | 角色名称 | 作用
------ | ------ | ------
Provider | 服务提供方 | 提供服务
Consumer | 服务消费方 | 消费服务
Registry | 注册中心 | 服务的注册与发现
Monitor | 监控中心 | 统计服务的调用次数和调用时间
Container | 运行容器 | 运行时容器 -
连接线:
虚线:都是异步访问;
实线:都是同步访问;
蓝色:在启动时完成的功能;
红色:程序运行过程中执行的功能。 -
调用关系
0:服务容器(一般都是Spring容器),负责启动、加载和运行服务的提供者;
1:服务提供者在启动时,向注册中心注册自己提供的服务;
2:服务消费者在启动时,想注册中心订阅自己所需要的服务;
3:注册中心返回服务提供者的地址列表给服务消费者,如有变更,注册中心将基于长连接,推送变更数据给服务消费者;
4:服务消费者从服务提供者的地址列表中,基于负载均衡算法,选择一台服务提供者进行调用,如果失败,则继续选取下一台服务;
5:服务消费者和提供者,在内存中累计调用次数和调用时间,定时没分钟发送一次统计数据到监控中心。
1.4 注册中心zookeeper
Dubbo官方推荐zookeeper作为服务的注册中心;
1.4.1 zookeeper介绍
zookeeper是Apache Hadoop的一个子项目,是一个树型的目录服务,支持变更推送,比较适合作为Dubbo的注册中心;工业强度高,可用于生产环境,推荐使用。
1.4.2 树型目录
可以类比到我们的电脑磁盘,我们的电脑分为多个磁盘(C、D、E盘等);每个盘下又可以创建多个目录,每个目录下又可以创建子目录和文件,最终构成了一个树状结构。通过这样的树型结构的目录,我们可以将文件分门别类的进行存放,编译后期查找。而磁盘上的每个文件都有一个唯一的访问路径,如:C:\DrvPath\Network\Lan\Realtek\20180828\test.txt;
而zookeeper的树型目录服务如下:
流程说明:
- Provider(服务提供者)启动时:向 /dubbo/com.foo.BarService/providers 目录下写入自己的URL地址;
- Consumer(服务消费者)启动时:订阅 /dubbo/com.foo.BarService/providers目录下的服务提供者的URL地址。并向 /dubbo/com.foo.BarService/consumers 目录下写入自己的 URL 地址;
- Monitor(监控中心)启动时:订阅 /dubbo/com.foo.BarService 目录下的所有服务提供者和服务消费者的URL地址。
1.4.3 安装zookeeper
我们使用的Zookeeper版本为3.4.6,安装环境是CentOS 7;安装流程如下:
- 安装JDK:这里就不多说了;
- 上传、解压zookeeper的压缩包
解压(到/usr路径下):tar -zxvf zookeeper-3.4.6.tar.gz -C /usr
- 在zookeeper-3.4.6目录中,创建data目录:存放运行时数据
创建命令:mkdir data
- 配置文件
刚解压出来的conf目录下有把zoo_sample.cfg的示例配置文件,我们直接改名字mv zoo_sample.cfg zoo.cfg
- 修改配置文件内容
修改dataDir的值,修改为我们刚刚创建的data目录的路径;
1.4.1 启动、停止
进入zookeeper的bin目录:
-
启动:./zkServer.sh start
-
停止: ./zkServer.sh stop
-
查看状态: ./zkServer.sh status
没启动的时候:
启动成功
-
客户端连接
命令:./zkCli.sh
如果直接使用,默认是连接到本地的zookeeper,若是想连接到其他主机的zookeeper,就需要加上ip和port:zkCli.sh -server ip:port
若是进入下面的这个情况,则表示链接成功:
二、入门案例
- Dubbo是一个RPC框架,最核心的功能就是实现夸王论的远程调用。我们的案例模拟两个:服务提供方和服务调用方。通过Dubbo实现远程调用。
- 我们使用idea创建一个空项目(Empty Project),在这里面创建服务:
- 我们的项目类型(pom中的packing类型)是war,需要在项目中补充wepapp和WEB-INF目录,如下:
- webapp创建好以后如果没有前面的小圆点,则说明目录的属性部队,就需要通过“File–>Project Structure”,然后在 “Project Structure --> Module -->web” 中进行配置:
2.1 服务提供方
在我们的空工程中创建Maven项目(具体过程不说了,直接上代码);
2.1.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.kaikeba</groupId>
<artifactId>dubbo-demo-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 指定端口 -->
<port>8081</port>
<!-- 请求路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.1.2 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:applicationContext-service.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
2.1.3 applicationContext-service.xml
这是Dubbo的配置文件,简单的配置如说明下:
- dubbo:application:配置当前服务注册的应用名称,注意不能和其他的重复,便于注册中心计算应用之间的依赖关系;
- dubbo:registry:注册中心的地址端口,多个时,在加一行配置就行;
- dubbo:protocol:注册的协议和端口,默认是dubbo,也可以指定其他的协议;
- dubbo:annotation:扫描指定的文件,扫描指定的包下的带有@Service注解的文件,发不成服务(注意是dubbo的@Service注解不是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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 当应用名称:用于注册中心计算应用之间的依赖关系, 注意名称不能相同-->
<dubbo:application name="dubbo-demo-provider"/>
<!-- 连接服务注册中心zookeeper ip为zookeeper服务器的地址 -->
<dubbo:registry address="zookeeper://192.168.119.128:2181"/>
<!-- 注册 协议和端口 -->
<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
<!-- 扫描指定包:加入@Service注解的类会被发布成服务 -->
<dubbo:annotation package="com.kaikeba.service"></dubbo:annotation>
</beans>
2.1.4 HelloService.java
package com.kaikeba.service;
public interface HelloService {
String sayHello(String name);
}
2.1.5 HelloServiceImpl.java
**PS:注意@Service的包;**要是Dubbo下的;
package com.kaikeba.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.kaikeba.service.HelloService;
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello " + name;
}
}
2.2 服务消费方
也是在刚才的空工程中创建Maven项目:
2.2.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>dubbo-demo-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 指定端口 -->
<port>8082</port>
<!-- 请求路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2.2 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:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc-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>
</servlet>
<servlet-mapping>
<servlet-name>springmvc-dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.2.3 applicationContext-web.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 当应用名称:用于注册中心计算应用之间的依赖关系, 注意名称不能相同-->
<dubbo:application name="dubbo-demo-consumer"/>
<!-- 连接服务注册中心zookeeper ip为zookeeper服务器的地址 -->
<dubbo:registry address="zookeeper://192.168.119.128:2181"/>
<!-- 扫描有@Reference注解的地方 -->
<dubbo:annotation package="com.kaikeba.controller"></dubbo:annotation>
</beans>
2.2.4 HelloService.java
package com.kaikeba.service;
public interface HelloService {
String sayHello(String name);
}
2.2.5 HelloController.java
package com.kaikeba.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.kaikeba.service.HelloService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class HelloController {
@Reference
private HelloService helloService;
@RequestMapping("/hello")
private String sayHello(String name) {
// 远程调用
String sayHello = helloService.sayHello(name);
// 输出一下放回信息
System.out.println(sayHello);
return sayHello;
}
}
2.3 测试
2.3.1 访问
访问结果如下:
2.3.1 zookeeper
在zookeeper中就看到了如下信息
2.4 改进项
-
HelloService在服务的消费者和提供者双方都有,这种做法是否合适?还有没有更好的方式?
同一个接口(HelloService)被复制了两份,不利于后期维护。更好的方式是单独创建一个maven工程,将此接口创建在这个maven工程中。需要依赖此接口的工程只需要在自己工程的pom.xml文件中引入maven坐标即可;
-
Dubbo是如何做到远程调用的?
Dubbo底层是基于代理技术为HelloService接口创建代理对象,远程调用是通过此代理对象完成的。可以通过开发工具的debug功能查看此代理对象的内部结构。另外,Dubbo实现网络传输底层是基于Netty框架完成的。
-
单点故障
我们的项目中只有一个Zookeeper,就会有单点故障的问题。不过Zookeeper其实是支持集群模式的,可以配置Zookeeper集群来达到Zookeeper服务的高可用,防止出现单点故障。
2.5 Dubbo管理控制台
我们在开发时,需要知道Zookeeper注册中心都注册了哪些服务,有哪些消费者来消费这些服务。我们可以通过部署一个管理中心来实现。其实管理中心就是一个web应用,部署到tomcat即可。
2.5.1 安装
- 下载dubbo-admin的包,我们用的是dubbo-admin-2.6.0.war的包;把war包放入tomcat的webapps目录;
- 直接启动tomcat,我们的war文件就会自动解压;或者我们把后缀改成(.zip或.rar自己解压也行);
- 修改WEB-INF下的dubbo.properties文件,注意dubbo.registry.address 对应的值需要对应当前使用的Zookeeper的ip地址和端口号
- 修改配置文件之后,记得重启tomcat。
2.5.2 测试
访问地址:http://localhost:8080/dubbo-admin-2.6.0
从刚才的配置文件可以看大登陆密码,登陆之后:
找到“服务治理–>服务”就能看到我们注册的服务:
点击我们注册的服务就能看到具体的信息:
服务提供者:
服务消费者:
我们的应用:
2.6 扫描Dubbo配置说明
2.6.1 包扫描
<dubbo:annotation package="com.kaikeba.service" />
服务提供者和服务消费者都需要配置,表示包扫描,作用是扫描指定包(包括子包)下的类,注册到服务。
2.6.2 配置扫描
如果我们不实用包扫描发布服务,也可已通过下面的配置实现;
- 服务提供者:
<bean id="helloService" class="com.lxs.service.impl.HelloServiceImpl" />
<dubbo:service interface="com.lxs.api.HelloService" ref="helloService" />
通过上面的配置 ,把HelloService发布成服务
- 服务消费者配置
<!-- 生成远程服务代理,可以和本地bean一样使用helloService -->
<dubbo:reference id="helloService" interface="com.lxs.api.HelloService" />
表示把远程的HelloService代理到本地,我们就可以直接通过@Autowried当成本地文件直接注入使用。
2.6.3 比较
上面这种方式发布和引用服务,一个配置项(dubbo:service、dubbo:reference)只能发布或者引用一个服务,如果有多个服务,这种方式就比较繁琐了。推荐使用包扫描方式。
2.7 协议
<dubbo:protocol name="dubbo" port="20880"/>
- 一般在服务提供者一方配置,可以指定使用的协议名称和端口号。
- Dubbo支持的协议有:dubbo、rmi、hessian、http、webservice、rest、redis等;推荐使用dubbo协议。
- dubbo协议采用单一长连接和NIO异步通讯,适合于小数据量、大并发量的服务调用;以及服务消费组合机器数量远远多余服务提供者机器数量的情况。不适合大数据两点额服务,如文件、视频传输;除非请求量很低很低的时候。
- 也可以在同意个工程服务中配置多个协议:
<!-- 多协议配置 -->
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:protocol name="rmi" port="1099" />
<!-- 使用dubbo协议暴露服务 -->
<dubbo:service interface="com.kaikeba.api.HelloService" ref="helloService"
protocol="dubbo" />
<!-- 使用rmi协议暴露服务 -->
<dubbo:service interface="com.kaikeba.api.DemoService" ref="demoService"
protocol="rmi" />
2.8 负载均衡
- 负载均衡(Load Balance):其实就是将请求分摊到多个操作单元上进行执行,从而共同完成工作任务;
- 在集群负载均衡时,Dubbo 提供了多种均衡策略(包括随机、轮询、最少活跃调用数、一致性Hash),缺省为random随机调用。
- 配置负载均衡策略,既可以在服务提供者一方配置,也可以在服务消费者一方配置;配置如下:
服务消费者方配置:
package com.kaikeba.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.kaikeba.service.HelloService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class HelloController {
@Reference(check = false, loadbalance = "random") // 负载均衡
private HelloService helloService;
@RequestMapping("/hello")
private String sayHello(String name) {
// 远程调用
String sayHello = helloService.sayHello(name);
// 输出一下放回信息
System.out.println(sayHello);
return sayHello;
}
}
服务提供方配置:
package com.kaikeba.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.kaikeba.service.HelloService;
@Service(loadbalance = "random")
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello " + name;
}
}
2.9 Dubbo无法发布被事务代理的Service问题
2.9.1 问题描述
我们上面已经完成了Dubbo的案例;@Service注解的类通过扫描就可以发布成服务;
但是当我们为我们的服务加上事物控制以后,服务就发布不成功了。
服务提供者:
增加的依赖
<!-- 持久化 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/tx
http://www.springframework.org/schema/mvc/spring-tx.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 当应用名称:用于注册中心计算应用之间的依赖关系, 注意名称不能相同-->
<dubbo:application name="dubbo-demo-provider"/>
<!-- 连接服务注册中心zookeeper ip为zookeeper服务器的地址 -->
<dubbo:registry address="zookeeper://192.168.119.128:2181"/>
<!-- 注册 协议和端口 -->
<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
<!-- 扫描指定包:加入@Service注解的类会被发布成服务 -->
<dubbo:annotation package="com.kaikeba.service"></dubbo:annotation>
<!--数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<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/springcloud" />
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务控制的注解支持-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
HelloServiceImpl.java增加@Transactional注解:
package com.kaikeba.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.kaikeba.service.HelloService;
import org.springframework.transaction.annotation.Transactional;
//@Service(loadbalance = "random")
@Service
@Transactional
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello " + name;
}
}
2.9.2 现象
访问:
dubbo-admin:
2.9.3 问题分析
在加上**@Transactional**注解之后,服务就发布失败了,原因是,事物控制的底层原理是为对应的类创建代理对象,而默认情况下,Spring是基于JDK的动态代理方式创建代理对象,代理对象的完整的全路径类名是 com.sun.proxy.$Proxy25(后两位数字不固定),而我们在配置文件中配置的路径是 com.kaikeba.service; 导致了Dubbo在发不服务前进行包匹配时,无法完成匹配,进而无法发不服务。错误
2.9.4 解决方案
第一步:修改applicationContext-service.xml配置文件,开启事务控制注解支持时指定proxy-target-class属性,值为true。其作用是使用cglib代理方式为Service类创建代理对象
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
第二步:修改HelloServiceImpl.java的@Service注解的interfaceClass属性值,修改为HelloService.class;
作用是指定服务的接口类型。
package com.kaikeba.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.kaikeba.service.HelloService;
import org.springframework.transaction.annotation.Transactional;
@Service(interfaceClass = HelloService.class)
@Transactional
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello " + name;
}
}
2.9.5 测试
dubbo-admin: