Explorar el Código

修改验证分布式事务

lengfaqiang hace 3 años
padre
commit
d53d5741fc

+ 2 - 2
sckw-common/sckw-common-core/src/main/java/com/sckw/core/web/config/com/sckw/core/web/config/GlobalTransactionalConfig.java → sckw-common/sckw-common-core/src/main/java/com/sckw/core/web/config/GlobalTransactionalConfig.java

@@ -1,4 +1,4 @@
-package com.sckw.core.web.config.com.sckw.core.web.config;
+package com.sckw.core.web.config;
 
 import io.seata.spring.annotation.AspectTransactionalInterceptor;
 import org.springframework.aop.Advisor;
@@ -9,7 +9,7 @@ import org.springframework.stereotype.Component;
 
 /**
  * @author lfdc
- * @description 全局事务bean
+ * @description 全局分布式事务{seata}bean
  * @date 2023/6/15 0015
  */
 @Component

+ 3 - 0
sckw-modules-api/sckw-file-api/src/main/java/com/sckw/file/api/dubbo/FileApiDubboService.java

@@ -16,4 +16,7 @@ public interface FileApiDubboService {
     HttpResult fileUpload(String str, byte[] fileByte);
 
     HttpResult fileUploadTodubbo(MultipartFile file);
+
+    HttpResult selectAll();
+
 }

+ 32 - 5
sckw-modules/sckw-example/src/main/java/com/sckw/example/controller/ExcelExportController.java

@@ -1,26 +1,29 @@
 package com.sckw.example.controller;
 
+import com.alibaba.fastjson.JSONObject;
 import com.sckw.core.utils.StringUtils;
 import com.sckw.core.web.response.HttpResult;
+import com.sckw.example.dao.KwsDeptDao;
+import com.sckw.example.model.KwsDept;
 import com.sckw.example.model.vo.SysUserVo;
 import com.sckw.example.service.ExcelExportService;
 import com.sckw.excel.config.easyexcel.ExcelImportListener;
 import com.sckw.excel.config.easyexcel.RequestHolder;
 import com.sckw.excel.utils.DateUtil;
 import com.sckw.excel.utils.ExcelUtil;
+import com.sckw.file.api.dubbo.FileApiDubboService;
+import io.seata.spring.annotation.GlobalTransactional;
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.AllArgsConstructor;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FileUtils;
+import org.apache.dubbo.config.annotation.DubboReference;
 import org.hibernate.validator.constraints.Range;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.CollectionUtils;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
@@ -34,7 +37,13 @@ import java.util.List;
 public class ExcelExportController {
 
     @Autowired
-    public ExcelExportService excelExportService;
+    ExcelExportService excelExportService;
+
+    @DubboReference(version = "2.0.0", group = "design", check = false)
+    FileApiDubboService fileApiDubboService;
+
+    @Autowired
+    KwsDeptDao kwsDeptDao;
 
     /**
      * 模板下载
@@ -112,4 +121,22 @@ public class ExcelExportController {
         ExcelImportListener importListener = new ExcelImportListener();
         return excelExportService.importExcel(file, importListener);
     }
+
+    /**
+     * 分布式事务验证
+     * @return
+     */
+    @GlobalTransactional
+    @RequestMapping(value = "globalTransactionalDemo",method = RequestMethod.GET)
+    public HttpResult globalTransactionalDemo() {
+        //auth 服务 调用example实现的dubbo接口
+        KwsDept kwsDept = new KwsDept();
+        kwsDept.setName("张三");
+        kwsDept.setAccount("张三");
+        kwsDept.setParentIds("2");
+        int i= kwsDeptDao.insert(kwsDept);
+        HttpResult result= fileApiDubboService.selectAll();
+        log.info(JSONObject.toJSONString(result));
+        return result;
+    }
 }

+ 9 - 0
sckw-modules/sckw-example/src/main/java/com/sckw/example/dao/KwsDeptDao.java

@@ -0,0 +1,9 @@
+package com.sckw.example.dao;
+
+import com.sckw.example.model.KwsDept;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface KwsDeptDao {
+    int insert(KwsDept kwsDept);
+}

+ 33 - 31
sckw-modules/sckw-example/src/main/java/com/sckw/example/dao/SysUserDao.java

@@ -1,31 +1,33 @@
-//package com.sckw.example.dao;
-//
-//import com.baomidou.dynamic.datasource.annotation.DS;
-//import com.sckw.example.model.SysUser;
-//import org.apache.ibatis.annotations.Mapper;
-//import org.apache.ibatis.annotations.Select;
-//import java.util.List;
-//import java.util.Map;
-//
-//@Mapper
-//public interface SysUserDao {
-//
-//    /**
-//     * 账号查询用户信息
-//     * @param params {account:账号, systemType:系统类别}
-//     * @return
-//     */
-//    @DS("slave_1")
-//    SysUser findByAccount(Map params);
-//
-//    @DS("master")
-//    @Select("SELECT * FROM user limit 1")
-//    List<SysUser> findUserByAccount(String account);
-//
-//    @DS("slave_1")
-//    @Select("SELECT * FROM sys_user limit 10")
-//    List<Map> findUserByAccount1(String account);
-//
-//    @DS("slave_1")
-//    List<Map<String, Object>> findPage(Map<String, Object> params);
-//}
+package com.sckw.example.dao;
+
+import com.baomidou.dynamic.datasource.annotation.DS;
+import com.sckw.example.model.SysUser;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
+import java.util.List;
+import java.util.Map;
+
+@Mapper
+public interface SysUserDao {
+
+    /**
+     * 账号查询用户信息
+     * @param params {account:账号, systemType:系统类别}
+     * @return
+     */
+    @DS("slave_1")
+    SysUser findByAccount(Map params);
+
+    @DS("master")
+    @Select("SELECT * FROM user limit 1")
+    List<SysUser> findUserByAccount(String account);
+
+    @DS("slave_1")
+    @Select("SELECT * FROM sys_user limit 10")
+    List<Map> findUserByAccount1(String account);
+
+    @DS("slave_1")
+    List<Map<String, Object>> findPage(Map<String, Object> params);
+
+    int insert(SysUser sysUser);
+}

+ 28 - 0
sckw-modules/sckw-example/src/main/java/com/sckw/example/dubbo/FileApiServiceImpl.java

@@ -0,0 +1,28 @@
+package com.sckw.example.dubbo;
+
+import com.sckw.core.web.response.HttpResult;
+import com.sckw.file.api.dubbo.FileApiDubboService;
+import org.springframework.web.multipart.MultipartFile;
+
+/**
+ * @author lfdc
+ * @description fileDubbo调用
+ * @date 2023/6/20 0020
+ */
+public class FileApiServiceImpl implements FileApiDubboService {
+
+    @Override
+    public HttpResult fileUpload(String str, byte[] fileByte) {
+        return null;
+    }
+
+    @Override
+    public HttpResult fileUploadTodubbo(MultipartFile file) {
+        return null;
+    }
+
+    @Override
+    public HttpResult selectAll() {
+        return null;
+    }
+}

+ 18 - 0
sckw-modules/sckw-example/src/main/java/com/sckw/example/model/KwsDept.java

@@ -0,0 +1,18 @@
+package com.sckw.example.model;
+
+import lombok.Data;
+
+/**
+ * @author lfdc
+ * @description
+ * @date 2023/6/19 0019
+ */
+@Data
+public class KwsDept  {
+
+    private String name;
+    private String parentIds;
+//    private String contacts;
+    private String account;
+
+}

+ 7 - 0
sckw-modules/sckw-example/src/main/resources/bootstrap-dev.yml

@@ -1,3 +1,5 @@
+server:
+  port: 10300
 spring:
   rabbitmq:
     username: admin
@@ -114,6 +116,10 @@ seata:
       namespace: sckw-service-platform-dev
       group: sckw-service-platform
   application-id: ${spring.application.name}
+  #  当前不生效,使用驼峰
+  enabled: true
+  #  enable-auto-data-source-proxy: false
+  enableAutoDataSourceProxy: false
 # 支持feign对sentinel支持
 #feign:
 #  sentinel:
@@ -127,6 +133,7 @@ dubbo:
     name: dubbo
     port: -1
     prefer-serialization: java
+#    host: 10.10.10.5
   registry:
     # 配置dubbo的注册中心为nacos
     address: nacos://${spring.cloud.nacos.discovery.server-addr}

+ 4 - 0
sckw-modules/sckw-example/src/main/resources/bootstrap-local.yml

@@ -127,6 +127,10 @@ seata:
       namespace: ${spring.cloud.nacos.discovery.namespace}
       group: ${spring.cloud.nacos.discovery.group}
   application-id: ${spring.application.name}
+  #  当前不生效,使用驼峰
+  enabled: true
+  #  enable-auto-data-source-proxy: false
+  enableAutoDataSourceProxy: false
 #  application-id: sckw-seata-file
 # 支持feign对sentinel支持
 #feign:

+ 35 - 0
sckw-modules/sckw-example/src/main/resources/mapper/KwsDeptDao.xml

@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.sckw.example.dao.KwsDeptDao">
+  <resultMap id="BaseResultMap" type="com.sckw.example.model.KwsDept">
+    <result column="account" property="account" />
+    <result column="parent_ids" property="parentIds" />
+    <result column="name" property="name" />
+  </resultMap>
+
+    <insert id="insert" parameterType="com.sckw.example.model.KwsDept">
+        insert into sckw_system.kws_user
+        <trim prefix="(" suffix=")" suffixOverrides=",">
+            <if test="name != null">
+                name,
+            </if>
+            <if test="account != null">
+                contacts,
+            </if>
+            <if test="parentIds != null">
+                parent_ids,
+            </if>
+        </trim>
+        <trim prefix="values (" suffix=")" suffixOverrides=",">
+            <if test="name != null">
+                #{name,jdbcType=VARCHAR},
+            </if>
+            <if test="account != null">
+                #{account,jdbcType=VARCHAR},
+            </if>
+            <if test="parentIds != null">
+                #{parentIds,jdbcType=VARCHAR},
+            </if>
+        </trim>
+    </insert>
+</mapper>

+ 2 - 0
sckw-modules/sckw-file/src/main/java/com/sckw/file/FileApplication.java

@@ -1,6 +1,7 @@
 package com.sckw.file;
 
 
+import io.seata.spring.annotation.datasource.EnableAutoDataSourceProxy;
 import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -19,6 +20,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
 @EnableDubbo
 @EnableFeignClients({"com.sckw.*.api.feign"})
 @EnableDiscoveryClient
+@EnableAutoDataSourceProxy
 @SpringBootApplication
 public class FileApplication {
 

+ 6 - 0
sckw-modules/sckw-file/src/main/java/com/sckw/file/controller/FileApiController.java

@@ -105,4 +105,10 @@ public class FileApiController {
         return HttpResult.ok(map);
     }
 
+
+    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
+    public HttpResult selectAll() {
+        return fileService.selectAll();
+    }
+
 }

+ 7 - 0
sckw-modules/sckw-file/src/main/java/com/sckw/file/service/FileService.java

@@ -161,4 +161,11 @@ public class FileService {
         FileUtils.downOSSFile(fileName, response);
 //        FileUtils.downloadByFileName(fileName);
     }
+
+    public HttpResult selectAll() {
+        BigDecimal bigDecimal1 = new BigDecimal(NumberConstant.ZERO);
+        BigDecimal bigDecimal2 = new BigDecimal(NumberConstant.SIX);
+        BigDecimal multiply = bigDecimal2.divide(bigDecimal1);
+        return HttpResult.ok("",multiply);
+    }
 }

+ 1 - 1
sckw-modules/sckw-file/src/main/resources/banner.txt

@@ -1,5 +1,5 @@
 ====================================================================================================================
 
-                    欢迎使用 [sckw-example] 开物供应链服务平台-文件服务 - Powered By https://www.xxxx.com
+                    欢迎使用 [sckw-file] 开物供应链服务平台-文件服务 - Powered By https://www.xxxx.com
 
 ====================================================================================================================

+ 7 - 0
sckw-modules/sckw-file/src/main/resources/bootstrap-dev.yml

@@ -100,6 +100,9 @@ seata:
     vgroup-mapping:
       # key是事务分组名称 value要和服务端的机房名称保持一致
       file-seata-service-group: default
+#    配置与nacos同ip
+    grouplist:
+      default: 10.10.10.230
   registry:
     # 指定nacos作为注册中心
     type: nacos
@@ -116,6 +119,10 @@ seata:
       namespace: ${spring.cloud.nacos.discovery.namespace}
       group: ${spring.cloud.nacos.discovery.group}
   application-id: ${spring.application.name}
+#  当前不生效,使用驼峰
+  enabled: true
+#  enable-auto-data-source-proxy: false
+  enableAutoDataSourceProxy: false
 # 支持feign对sentinel支持
 #feign:
 #  sentinel:

+ 6 - 2
sckw-modules/sckw-file/src/main/resources/bootstrap-local.yml

@@ -115,11 +115,11 @@ spring:
 seata:
   # seata 服务分组,要与服务端nacos-config.txt中service.vgroup_mapping的后缀对应
   # 事务分组名称,要和服务端对应
-  tx-service-group: file-seata-group
+  tx-service-group: file-seata-service-group
   service:
     vgroup-mapping:
       # key是事务分组名称 value要和服务端的机房名称保持一致
-      file-seata-group: default
+      file-seata-service-group: default
   registry:
     # 指定nacos作为注册中心
     type: nacos
@@ -138,6 +138,10 @@ seata:
       namespace: ${spring.cloud.nacos.discovery.namespace}
       group: ${spring.cloud.nacos.discovery.group}
   application-id: ${spring.application.name}
+  #  当前不生效,使用驼峰
+  enabled: true
+  #  enable-auto-data-source-proxy: false
+  enableAutoDataSourceProxy: false
 #  application-id: sckw-seata-file
 # 支持feign对sentinel支持
 #feign: