首页 > 其他分享 >springboot+seata+nacos+dubbo搭建分布式事务

springboot+seata+nacos+dubbo搭建分布式事务

时间:2024-05-31 16:34:02浏览次数:25  
标签:dubbo 127.0 springboot 0.1 nacos server mysql seata

一、环境

springboot:2.3.1.RELEASE

dubbo:2.7.13

seata:1.8

nacos:

zookeeper:3.7.1

java11

mysql8

依赖:以下版本经测试无依赖冲突

# 父工程springboot版本
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.RELEASE</version> </parent>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.13</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.7.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>5.6.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>5.6.0</version>
        </dependency>

        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.8.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>0.2.12</version>
        </dependency>
     
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
     <version>8.0.12</version>
    <scope>runtime</scope>
  </dependency>

  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
 
    </dependencies>

 

二、依赖组件启动

1、nacos

nacos以standalone模式启动

startup.sh -m standalone

2、zookeeper

zkServer.sh start ../conf/zoo.cfg

3、seata

seata启动前需要进行配置,本次是以nacos为注册中心,配置中心

1)修改seata conf目录下的application.yml配置(此时以不需要配置之前老版本中的register.conf 和file.conf文件)

server:
  port: 7091

spring:
  application:
    name: seata-server

logging:
  config: classpath:logback-spring.xml
  file:
    path: ${log.home:${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:
 # 配置中心为nacos,后续seata需要的配置直接在nacos上进行修改 config: # support: nacos, consul, apollo, zk, etcd3 type: nacos nacos: server-addr: 127.0.0.1:8848 namespace: 467933e6-ee4e-4e23-954f-0219805f51c5 # nacos上创建的命名空间id 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
 # 注册中心为nacos,将seata作为一个服务直接注册到nacos,方便后续直接在nacos上调整seata配置 registry: # support: nacos, eureka, redis, zk, consul, etcd3, sofa type: nacos nacos: application: seata-server server-addr: 127.0.0.1:8848 group: SEATA_GROUP namespace: 467933e6-ee4e-4e23-954f-0219805f51c5 cluster: default username: nacos password: nacos context-path: ##if use MSE Nacos with auth, mutex with username/password attribute #access-key: #secret-key: # 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,/**/*.jpeg,/**/*.ico,/api/v1/auth/login

2)seata配置更新到nacos

seata提供了将配置更新到nacos的脚本。位于seata目录下的script/config-center/nacos

运行nacos-config.sh脚本即可

sh ./nacos-config.sh -h 127.0.0.1 -p 8848 -g SEATA_GROUP -t 5a3c7d6c-f497-4d68-a71a-2e5e3340b3ca -u nacos -w nacos

h:nacos 部署ip

p:nacos端口

g:nacos分组

t:nacos命名空间id

u:nacos 用户名

w:nacos 密码

3)创建mysql表

本次使用mysql存储分布式事务中见信息,seata提供了建表语句,位于seata目录下的script/server/db

创建数据库seata

执行mysql.sql语句完成数据表创建

4)启动seata

sh seata-server.sh -h 127.0.0.1 -p 8091 -m db

注意点:需要jdk1.8以上版本。本次使用的是jdk11

启动完成后可以通过seata的ui页面查看,同时在nacos中的服务列表也可以查看

seata ui: http://localhost:7091/

账号:seata

密码:seata

启动seata过程中可能遇到的问题

1、seata启动失败:使用jdk11及以上版本

2、seata启动后提示连接数据库异常

1)查看nacos配置列表中的db配置

store.db.driverClassName:com.mysql.cj.jdbc.Driver         jdbc驱动

store.db.url:jdbc:mysql://127.0.0.1:13306/seata?useUnicode=true&rewriteBatchedStatements=true&characterEncoding=utf8&useSSL=false  

store.db.user: root

store.db.password:root

2)查看mysql驱动版本

在seata目录下/lib/jdbc,保留最高版本驱动

3)其他查看日志

客户端程序

1、配置

spring:
  application:
    name: order-service
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: none
    properties:
      hibernate.show_sql: true
      hibernate.format_sql: true
    open-in-view: false
  datasource:
    # 数据源
    url: jdbc:mysql://127.0.0.1:13306/seata?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false
    username: root
    password: root
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 连接池
    hikari:
      maximum-pool-size: 8
      minimum-idle: 4
      idle-timeout: 30000
      connection-timeout: 30000
      max-lifetime: 45000
      auto-commit: true
      pool-name: ImoocEcommerceHikariCP
dubbo:
  application:
    name: provider-order
    qos-enable: false
  registry:
    address: zookeeper://127.0.0.1:2181
  scan:
    base-packages: com.example.order
  protocol:
    name: dubbo
    port: 12345
seata:  # seata配置、读取nacos配置连接到seata
  application-id: order-service
  tx-service-group: default_tx_group
  enabled: true
  enable-auto-data-source-proxy: true
  use-jdk-proxy: false
  config:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      namespace: 467933e6-ee4e-4e23-954f-0219805f51c5
      group: SEATA_GROUP
      username: nacos
      password: nacos
      data-id: seataServer.properties

 

2、服务提供者

@Service
// dubbo服务注册 @DubboService(version = DubboVersion.VERSION) public class SeataAccountServiceImpl implements SeataAccountService { private SeataAccountDao seataAccountDao; public SeataAccountServiceImpl(SeataAccountDao seataAccountDao){ this.seataAccountDao = seataAccountDao; } @Override public boolean debit(String userId, long money) { if (money<0){ return false; } try { SeataAccount seataAccount = seataAccountDao.findByUserId(userId); // 扣减金额大于账户余额,扣减失败 long deductMoney ; if (null == seataAccount || (deductMoney = seataAccount.getMoney()-money)<0){ return false; } // 开始扣减 seataAccount.setMoney(deductMoney); log.info("开始扣减金额: [{}]", JSON.toJSONString(seataAccount)); seataAccountDao.save(seataAccount); }catch (Exception e){ log.error("金额扣减异常: [{}]",e.getMessage(),e); throw e; } return true; } @Override public SeataAccountVO getUserAccount(String userId) { SeataAccount byUserId = seataAccountDao.findByUserId(userId); SeataAccountVO seataAccountVO = new SeataAccountVO(); BeanUtils.copyProperties(byUserId,seataAccountVO); return seataAccountVO; } }

 

3、业务消费者

  @DubboReference(version = DubboVersion.VERSION)
    private SeataOrderService seataOrderService;

    @DubboReference(version = DubboVersion.VERSION)
    private SeataAccountService seataAccountService;

  // 开启全局事物 @GlobalTransactional(name = "spring-dubbo-tx1",timeoutMills = 3000,rollbackFor = Exception.class) @Override public void doBusiness(String userId, String code, int count, long money) { boolean b = seataAccountService.debit(userId, money); log.info("seataAccountService.debit result :[{}]",b); log.info("business start.........."); SeataOrderVO seataOrderVO = seataOrderService.create(userId, code, count); log.info("seataOrderService.create :[{}]", JSON.toJSONString(seataOrderVO)); }

 

标签:dubbo,127.0,springboot,0.1,nacos,server,mysql,seata
From: https://www.cnblogs.com/tangs1/p/18224802

相关文章