(十)Spring Boot 整合dubbo

Spring Boot 做的一个简单的增删改查,前台页面整合Thymeleaf模板,数据源druid,声明式事务,整合redis,并开启redis事务,整合ActiveMQ,整合dubbo 项目下载地址:点此下载
如果有不需要使用的功能,只需要删除com.test.springboot.config下对应的配置再启动就行了
下载文件介绍:

 
spring-boot-consumer 为服务消费者,只有Controller层,没有操作redis ,mysql数据库和amq队列。
spring-boot-example 为服务提供者,不包含Controller层,操作数据库,缓存等都包含在内。
spring-boot-service 打包方式为jar ,上面两个项目,都要依赖该项目,里面只有两个类,一个StudentService和所需的pojo(Student)POJO记得实现序列化接口

整合过程

1.在src/main/resources下创建spring文件夹,用来存放dubbo的xml的配置文件

2.在程序的入口启动类上@ImportResource("classpath:spring/*.xml")

3.服务提供者和服务消费者的pom.xml添加如下依赖
<!-- 整合dubbox -->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>dubbo</artifactId>
	<version>2.8.4</version>
	<exclusions>
		<exclusion>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<!-- zookeeper注册中心 -->
<dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
	<version>3.4.6</version>
	<exclusions>
		<exclusion>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
		</exclusion>
		<exclusion>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>com.101tec</groupId>
	<artifactId>zkclient</artifactId>
	<version>0.10</version>
</dependency>
4.服务提供者的application.properties中添加以下内容
#配置dubbo
#应用名称
dubbo.application.name=spring-boot-example-provider
#注册中心地址
#dubbo.address=zookeeper://172.16.10.14:2181?backup=172.16.10.15:2181,172.16.20.29:2181
dubbo.registry.address=zookeeper://172.20.1.154:2181
#暴露服务端口
dubbo.protocol.port=20880
5.服务提供者的dubbo-provider.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: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://code.alibabatech.com/schema/dubbo          
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
      
    <!-- 搭建服务提供方-->  
    <!-- 提供方应用名,用于计算依赖关系,不是匹配条件,不要与消费方一样 -->  
    <dubbo:application name="${dubbo.application.name}"/>
  
    <!-- 使用zookeeper注册中心暴露服务地址,使用多个注册中心 -->
    <dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" />
      
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="${dubbo.protocol.port}" />
      
    <!-- 用户服务接口 -->  
    <dubbo:service interface="com.test.springboot.dubbo.student.StudentService" ref="studentService" />
    <bean id="studentService" class="com.test.springboot.service.impl.StudentServiceImpl"/>  
</beans>
6.服务消费者的application.properties中添加以下内容
#配置dubbo
#应用名称  
dubbo.application.name=spring-boot-example-consumer
#注册中心地址
#dubbo.address=zookeeper://172.16.10.14:2181?backup=172.16.10.15:2181,172.16.20.29:2181
dubbo.registry.address=zookeeper://172.20.1.154:2181
#消费服务设置全局超时时间,如果超过了设置的时间程序没有做出响应,则报错
dubbo.timeout=600000
7.服务消费者的dubbo-consumer.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: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://code.alibabatech.com/schema/dubbo          
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
      
    <!-- 搭建服务消费方-->  
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->  
    <dubbo:application name="${dubbo.application.name}"/>
  
    <!-- 使用zookeeper注册中心暴露服务地址,使用多个注册中心 -->
    <dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" />
    
    <!-- 全局设置超时时间 -->  
    <dubbo:consumer timeout="${dubbo.timeout}"/>
      
    <!-- 用户服务接口 -->  
    <dubbo:reference interface="com.test.springboot.dubbo.student.StudentService" id="studentService" check="false" />
</beans>

8.启动两个工程测试服务调用,添加一个用户试试,可以看到服务调用没问题

注意:如果是系统内部互相调用,一定要走zookeeper的注册与发现,如果是对外发布RESTful 接口,只需要再添加一个Controller就可以了,比使用dubbox发布RESTful,还要简单,毕竟Spring的配置是你所熟悉的。

9.如何保证dubbo服务既不往外抛异常,也可以保证事务

有的时候,我们在使用dubbo服务的时候,不想让服务直接抛出异常,而是类似于接口定义一样返回 code data message这种格式的json,但是又要保证事务性。所以可以使用下面这种方式,出现异常手动回滚事务。

	@Transactional
	public ModelsReturn selectStudentList() {
		
		try {
		
			//业务逻辑都写到 try catch 中
			ModelsReturn modelsReturn = new ModelsReturn();
			modelsReturn.setCode("0000");
			modelsReturn.setMessage("成功");
			modelsReturn.setData("");
			return modelsReturn;
		} catch (Exception e) {
			//打印异常堆栈信息
			e.printStackTrace();
			//手动回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			//返回错误码
			ModelsReturn modelsReturn = new ModelsReturn();
			modelsReturn.setCode("1111");
			modelsReturn.setMessage("服务异常");
			modelsReturn.setData("");
			return modelsReturn;
		}
	}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值