Seata介绍
官网:http://seata.io/zh-cn/docs/overview/what-is-seata.html
Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。
dubbo介绍
官网;https://cn.dubbo.apache.org/zh-cn/overview/what/
Apache Dubbo 是一款 RPC 服务开发框架,用于解决微服务架构下的服务治理与通信问题,官方提供了 Java、Golang 等多语言 SDK 实现。使用 Dubbo 开发的微服务原生具备相互之间的远程地址发现与通信能力, 利用 Dubbo 提供的丰富服务治理特性,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。Dubbo 被设计为高度可扩展,用户可以方便的实现流量拦截、选址的各种定制逻辑。
在云原生时代,Dubbo 相继衍生出了 Dubbo3、Proxyless Mesh 等架构与解决方案,在易用性、超大规模微服务实践、云原生基础设施适配、安全性等几大方向上进行了全面升级。
目标
我们先说目标,为各位看官节省不匹配的时间
1、使用nacos做配置中心
2、使用nacos做注册中心
3、微服务模块化
4、使用dubbo作为服务管理
5、使用springboot做脚手架
6、使用seata做分布式事务
版本说明和代码地址
Dubbo :3.1.0
Springboot:2.3.1.RELEASE
Seata:1.6.1
Nacos-config:0.2.10
实现源代码地址
分支:microservice-boot-1.0.4-seata
代码演示和测试:
microservice-boot-common模块
microservice-boot-plat模块
pom.xml
直接上pom文件吧
<dubbo.version>3.1.0</dubbo.version>
<spring-boot.version>2.3.1.RELEASE</spring-boot.version>
<spring-context-support.version>1.0.11</spring-context-support.version>
<!-- 微服务相关 -->
<nacos-config.version>0.2.10</nacos-config.version>
<io.seata.version>1.6.1</io.seata.version>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!--nacos config -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${nacos-config.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</dependency>
验证模块
两个模块
microservice-boot-common
microservice-boot- plat
模拟:
plat中controller请求,本地服务(此服务使用分布式事务),本地服务调用dubbo服务(包括palt的保存数据和common的保存日志)
microservice-boot-common
1、yaml配置
# seata 配置
seata:
application-id: ${spring.application.name}
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: fb91b495-9490-436f-b9cd-023f2ca42b08
group: SEATA_GROUP
username: nacos
password: nacos
context-path:
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key:
#secret-key:
data-id: seataServer.properties
# 默认就是这个额,可以不配置
tx-service-group: default_tx_group
registry:
custom:
name: ${spring.application.name}
type: nacos
nacos:
server-addr: 127.0.0.1:8848
application: seata-server
group: SEATA_GROUP
namespace: 920bb73f-17da-4128-9de0-41893097ce38
username: nacos
password: nacos
2、dubbo服务代码
package org.lwd.microservice.boot.common.service.dubbo;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.lwd.microservice.boot.common.api.dto.VisitDubboDTO;
import org.lwd.microservice.boot.common.api.dubbo.VisitDubboService;
import org.lwd.microservice.boot.common.entity.dto.VisitDTO;
import org.lwd.microservice.boot.common.service.VisitService;
import org.lwd.microservice.boot.core.constant.HttpStatusEnum;
import org.lwd.microservice.boot.core.entity.BaseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author weidong
* @version V1.0.0
* @since 2023/6/28
*/
@Slf4j
@DubboService
public class VisitDubboServiceImpl implements VisitDubboService {
@Autowired
VisitService visitService;
/**
* 保存
*
* @return
*/
@Override
public BaseResult<Integer> saveVisitDubboService(VisitDubboDTO visitDubboDTO) {
log.info("----i am do saveVisitDubboService-----:{}", JSON.toJSONString(visitDubboDTO));
BaseResult<Integer> baseResult = BaseResult.success();
VisitDTO visitDTO = new VisitDTO();
BeanUtils.copyProperties(visitDubboDTO, visitDTO);
BaseResult<Integer> result = visitService.saveVisit(visitDTO);
if (result.isSuccess()) {
baseResult.setData(baseResult.getData());
} else {
baseResult.setCode(HttpStatusEnum.REQUEST_FAIL.getCode());
baseResult.setMessage("保存日志失败");
}
return baseResult;
}
}
microservice-boot- plat
1、yaml配置
# seata 配置
seata:
application-id: ${spring.application.name}
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: fb91b495-9490-436f-b9cd-023f2ca42b08
group: SEATA_GROUP
username: nacos
password: nacos
context-path:
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key:
#secret-key:
data-id: seataServer.properties
# 默认就是这个额,可以不配置
tx-service-group: default_tx_group
registry:
custom:
name: ${spring.application.name}
type: nacos
nacos:
server-addr: 127.0.0.1:8848
application: seata-server
group: SEATA_GROUP
namespace: 920bb73f-17da-4128-9de0-41893097ce38
username: nacos
password: nacos
2、dubbo服务实现
package org.lwd.microservice.boot.plat.service.dubbo;
import org.apache.dubbo.config.annotation.DubboService;
import org.lwd.microservice.boot.core.entity.BaseResult;
import org.lwd.microservice.boot.plat.api.dto.UserLoginDubboDTO;
import org.lwd.microservice.boot.plat.api.dubbo.UserLoginDubboService;
import org.lwd.microservice.boot.plat.entity.dto.UserLoginDTO;
import org.lwd.microservice.boot.plat.service.UserLoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 登录基本服务
* @author weidong
* @version V1.0.0
* @since 2023/7/3
*/
@DubboService
public class UserLoginDubboServiceImpl implements UserLoginDubboService {
@Autowired
UserLoginService userLoginService;
@Override
public BaseResult<Integer> saveUserLoginDubbo(UserLoginDubboDTO userLoginDubboDTO) {
BaseResult<Integer> result = BaseResult.success();
UserLoginDTO userLoginDTO = new UserLoginDTO();
BeanUtils.copyProperties(userLoginDubboDTO,userLoginDTO);
BaseResult<Integer> baseResult = userLoginService.saveUserLogin(userLoginDTO);
if(baseResult.isSuccess()){
result.setData(baseResult.getData());
}
return result;
}
}
3、分布式事务服务实现
package org.lwd.microservice.boot.plat.service.impl;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.lwd.microservice.boot.common.api.dto.VisitDubboDTO;
import org.lwd.microservice.boot.common.api.dubbo.VisitDubboService;
import org.lwd.microservice.boot.core.constant.HttpStatusEnum;
import org.lwd.microservice.boot.core.entity.BaseResult;
import org.lwd.microservice.boot.plat.api.dto.UserLoginDubboDTO;
import org.lwd.microservice.boot.plat.api.dubbo.UserLoginDubboService;
import org.lwd.microservice.boot.plat.entity.dto.UserLoginDTO;
import org.lwd.microservice.boot.plat.service.UserLoginSeataService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
/**
* @author weidong
* @version V1.0.0
* @since 2023/7/3
*/
@Slf4j
@Service
public class UserLoginSeataServiceImpl implements UserLoginSeataService {
@DubboReference
UserLoginDubboService userLoginDubboService;
@DubboReference
VisitDubboService visitDubboService;
@Override
@GlobalTransactional(timeoutMills = 30000, name = "default_tx_group")
public BaseResult<Integer> saveUserLoginSeata(UserLoginDTO dto) throws Exception {
BaseResult<Integer> result = BaseResult.success();
log.info("开始全局事务:xid=" + RootContext.getXID());
log.info("begin userLogin: " + dto);
//保存登录信息
UserLoginDubboDTO userLoginDubboDTO = new UserLoginDubboDTO();
BeanUtils.copyProperties(dto, userLoginDubboDTO);
BaseResult<Integer> userResult = userLoginDubboService.saveUserLoginDubbo(userLoginDubboDTO);
if (userResult.isSuccess()) {
result.setData(userResult.getData());
} else {
result.setCode(HttpStatusEnum.REQUEST_FAIL.getCode());
throw new Exception("登录信息保存系统异常");
}
if (!result.isSuccess()) {
return result;
}
//保存日志
VisitDubboDTO visitDubboDTO = new VisitDubboDTO();
visitDubboDTO.setServerIpAddress("3.3.3.3");
if (dto.getEnabled().equals(1)) {
visitDubboDTO.setCreateTime(dto.getCreateTime());
}
BaseResult<Integer> visitResult = visitDubboService.saveVisitDubboService(visitDubboDTO);
if (visitResult.isSuccess()) {
log.info("visit info pk:{}", visitResult.getData());
result.setData(visitResult.getData());
} else {
result.setCode(HttpStatusEnum.REQUEST_FAIL.getCode());
throw new Exception("日志保存系统异常");
}
return result;
}
}
验证结果
注意事项
1、seata在注册到nacos时,订阅端的应用名称为unknown
临时解决方案:文章来源:https://www.toymoban.com/news/detail-536076.html
package org.lwd.microservice.boot.common;
import org.lwd.microservice.boot.middle.runtime.util.YmlUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
* @author weidong
* @version V1.0.0
* @since 2023/4/7
*/
@SpringBootApplication
public class CommonApplication {
public static void main(String[] args) {
//TODO 这块有一个问题,seata在注册到nacos时,订阅端的应用名称为unknown,经验证是获取不到ProjectNameConfig中设置的${spring.application.name}
//估计是加载顺序获取其他问题,现在这获取数据,优先处理
System.setProperty("project.name", YmlUtils.getApplicationName());
SpringApplication.run(CommonApplication.class, args);
}
}
2、分布式事务默认AT模式文章来源地址https://www.toymoban.com/news/detail-536076.html
到了这里,关于springboot dubbo seata nacos集成 分布式事务seata实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!