12.Seata配nacos
一、配置导入nacos
1.进入资源目录(上一篇下载的资源文件)的config-center
2.修改config.txt文件
3.事务分组修改
service.vgroupMapping.[客户端会用到这个值]=[服务端的会用到这个值]
4.存储修改为db(数据库地址为上一篇创建的数据库地址)
5.启动nacos并创建命名空间
6.创建配置文件
7.配置文件中添加,“config.txt”中的内容,我就添加了修改部分。
service.vgroupMapping.cd=lry
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3305/seata-server?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=123456
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
二、服务的配置
1.修改配置文件
注:cluster: lry 为上面第三步事务分组填写的值
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ${user.home}/logs/seata
extend:
logstash-appender:
destination: 127.0.0.1:4560
kafka-appender:
bootstrap-servers: 127.0.0.1:9092
topic: logback_to_logstash
console:
user:
username: seata
password: seata
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: e2b58124-d455-4cbe-a0fb-f9fd22966660
group: DEFAULT_GROUP
username: nacos
password: nacos
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key: ""
#secret-key: ""
data-id: seataServer.properties
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
group: DEFAULT_GROUP
namespace: e2b58124-d455-4cbe-a0fb-f9fd22966660
cluster: lry
username: nacos
# server:
# service-port: 8091 #If not configured, the default is '${server.port} + 1000'
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
2.启动服务的
三、客户端配置
1.客户端数据库导入资源文件中的mysql.sql
2.导入后的表结构
3.导入依赖
父依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.luoruiyuan</groupId>
<artifactId>Spring-Cloud-Lry</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-Cloud-Lry</name>
<description>Spring-Cloud-Lry</description>
<packaging>pom</packaging>
<modules>
<module>stock</module>
<module>order</module>
<module>nacos-config</module>
<module>sentinel-java</module>
<module>order-sentinel</module>
<module>order-oepnfeign-sentinel</module>
<module>stock-oepnfeign-sentinel</module>
<module>seata/seata-order</module>
<module>seata/seata-stock</module>
</modules>
<properties>
<java.version>1.8</java.version>
<spring.boot.version>2.3.12.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR12</spring.cloud.version>
<spring.cloud.alibaba.version>2.2.8.RELEASE</spring.cloud.alibaba.version>
</properties>
<!-- 版本管理(子工程引入了才会导jar包) -->
<dependencyManagement>
<dependencies>
<dependency>
<!--Spring Boot 版本管理-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Spring Cloud 版本管理-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Spring Cloud Alibaba 版本管理-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 直接导入jar包 -->
<dependencies>
<!-- Spring Boot启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- WEB场景 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署 -->
<!-- 开发工具(feign.sentinel.enabled=true 时不要导入此依赖,不然项目报错)
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
项目依赖
<?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">
<parent>
<artifactId>Spring-Cloud-Lry</artifactId>
<groupId>cn.luoruiyuan</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>seata-order</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- alibaba nacos注册中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- seata分布式事务 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
<!-- 数据库访问 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!--mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
</project>
4.修改配置文件(这的 tx-service-group: cd 就是第一步的第3步设置的值)
server:
port: 5002
spring:
application:
name: seata-stock
datasource:
url: jdbc:mysql://127.0.0.1:3305/stock?serverTimezone=UTC&characterEncoding=utf-8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
cloud:
#当前项目注册到注册中心
nacos:
discovery:
server-addr: 192.168.152.1:8848
username: nacos
password: nacos
group: DEFAULT_GROUP
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml
#服务器配置的信息
seata:
enabled: true
#事务分组
tx-service-group: cd
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: e2b58124-d455-4cbe-a0fb-f9fd22966660
group: DEFAULT_GROUP
username: nacos
password: nacos
data-id: seataServer.properties
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
group: DEFAULT_GROUP
namespace: e2b58124-d455-4cbe-a0fb-f9fd22966660
cluster: default
username: nacos
5.创建数据源配置类(非常重要,不创建事务不回滚)
6.编写业务类,调用openfeign接口
赞(1)
赏