Seata介紹
官網(wǎng):http://seata.io/zh-cn/docs/overview/what-is-seata.html
Seata 是一款開源的分布式事務(wù)解決方案,致力于提供高性能和簡單易用的分布式事務(wù)服務(wù)。Seata 將為用戶提供了 AT、TCC、SAGA 和 XA 事務(wù)模式,為用戶打造一站式的分布式解決方案。
dubbo介紹
官網(wǎng);https://cn.dubbo.apache.org/zh-cn/overview/what/
Apache Dubbo 是一款 RPC 服務(wù)開發(fā)框架,用于解決微服務(wù)架構(gòu)下的服務(wù)治理與通信問題,官方提供了 Java、Golang 等多語言 SDK 實(shí)現(xiàn)。使用 Dubbo 開發(fā)的微服務(wù)原生具備相互之間的遠(yuǎn)程地址發(fā)現(xiàn)與通信能力, 利用 Dubbo 提供的豐富服務(wù)治理特性,可以實(shí)現(xiàn)諸如服務(wù)發(fā)現(xiàn)、負(fù)載均衡、流量調(diào)度等服務(wù)治理訴求。Dubbo 被設(shè)計(jì)為高度可擴(kuò)展,用戶可以方便的實(shí)現(xiàn)流量攔截、選址的各種定制邏輯。
在云原生時代,Dubbo 相繼衍生出了 Dubbo3、Proxyless Mesh 等架構(gòu)與解決方案,在易用性、超大規(guī)模微服務(wù)實(shí)踐、云原生基礎(chǔ)設(shè)施適配、安全性等幾大方向上進(jìn)行了全面升級。
目標(biāo)
我們先說目標(biāo),為各位看官節(jié)省不匹配的時間
1、使用nacos做配置中心
2、使用nacos做注冊中心
3、微服務(wù)模塊化
4、使用dubbo作為服務(wù)管理
5、使用springboot做腳手架
6、使用seata做分布式事務(wù)
版本說明和代碼地址
Dubbo :3.1.0
Springboot:2.3.1.RELEASE
Seata:1.6.1
Nacos-config:0.2.10
實(shí)現(xiàn)源代碼地址
分支: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>
<!-- 微服務(wù)相關(guān) -->
<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>
驗(yàn)證模塊
兩個模塊
microservice-boot-common
microservice-boot- plat
模擬:
plat中controller請求,本地服務(wù)(此服務(wù)使用分布式事務(wù)),本地服務(wù)調(diào)用dubbo服務(wù)(包括palt的保存數(shù)據(jù)和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
# 默認(rèn)就是這個額,可以不配置
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服務(wù)代碼
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
# 默認(rèn)就是這個額,可以不配置
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服務(wù)實(shí)現(xiàn)
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;
/**
* 登錄基本服務(wù)
* @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、分布式事務(wù)服務(wù)實(shí)現(xiàn)
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("開始全局事務(wù):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("登錄信息保存系統(tǒng)異常");
}
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("日志保存系統(tǒng)異常");
}
return result;
}
}
驗(yàn)證結(jié)果
注意事項(xiàng)
1、seata在注冊到nacos時,訂閱端的應(yīng)用名稱為unknown
臨時解決方案:文章來源:http://www.zghlxwxcb.cn/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時,訂閱端的應(yīng)用名稱為unknown,經(jīng)驗(yàn)證是獲取不到ProjectNameConfig中設(shè)置的${spring.application.name}
//估計(jì)是加載順序獲取其他問題,現(xiàn)在這獲取數(shù)據(jù),優(yōu)先處理
System.setProperty("project.name", YmlUtils.getApplicationName());
SpringApplication.run(CommonApplication.class, args);
}
}
2、分布式事務(wù)默認(rèn)AT模式文章來源地址http://www.zghlxwxcb.cn/news/detail-536076.html
到了這里,關(guān)于springboot dubbo seata nacos集成 分布式事務(wù)seata實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!