From c1590a1be8fe7f0b0c1aa62a0eceee199b9c079d Mon Sep 17 00:00:00 2001 From: wangsong Date: Sun, 22 May 2022 11:29:37 +0800 Subject: [PATCH] m --- .../data_scope/aspect/DataScopeAspect.java | 10 +- .../data_scope/DataScopeApplicationTests.java | 6 + seata-at/account/pom.xml | 112 ++++++++++++++++++ .../java/org/javaboy/AccountApplication.java | 15 +++ .../account/controller/AccountController.java | 33 ++++++ .../javaboy/account/mapper/AccountMapper.java | 24 ++++ .../account/service/AccountService.java | 31 +++++ .../src/main/resources/application.properties | 9 ++ seata-at/account/src/main/resources/file.conf | 66 +++++++++++ .../account/src/main/resources/registry.conf | 79 ++++++++++++ .../account/AccountApplicationTests.java | 13 ++ seata-at/business/pom.xml | 101 ++++++++++++++++ .../java/org/javaboy/BusinessApplication.java | 15 +++ .../controller/BusinessController.java | 34 ++++++ .../business/feign/OrderFeignClient.java | 24 ++++ .../business/feign/StorageFeignClient.java | 21 ++++ .../business/service/BusinessService.java | 33 ++++++ .../src/main/resources/application.properties | 5 + .../business/src/main/resources/file.conf | 66 +++++++++++ .../business/src/main/resources/registry.conf | 79 ++++++++++++ .../business/BusinessApplicationTests.java | 13 ++ seata-at/common/pom.xml | 65 ++++++++++ .../java/org/javaboy/common/RespBean.java | 65 ++++++++++ .../common/exception/GlobalException.java | 25 ++++ seata-at/eureka/pom.xml | 57 +++++++++ .../org/javaboy/eureka/EurekaApplication.java | 15 +++ .../src/main/resources/application.properties | 4 + .../eureka/EurekaApplicationTests.java | 13 ++ seata-at/order/pom.xml | 111 +++++++++++++++++ .../java/org/javaboy/OrderApplication.java | 17 +++ .../order/controller/OrderController.java | 32 +++++ .../org/javaboy/order/feign/AccountFeign.java | 22 ++++ .../org/javaboy/order/mapper/OrderMapper.java | 20 ++++ .../javaboy/order/service/OrderService.java | 32 +++++ .../src/main/resources/application.properties | 9 ++ seata-at/order/src/main/resources/file.conf | 66 +++++++++++ .../order/src/main/resources/registry.conf | 79 ++++++++++++ .../javaboy/order/OrderApplicationTests.java | 13 ++ seata-at/pom.xml | 16 +++ seata-at/storage/pom.xml | 111 +++++++++++++++++ .../java/org/javaboy/StorageApplication.java | 13 ++ .../storage/controller/StorageController.java | 32 +++++ .../javaboy/storage/mapper/StorageMapper.java | 24 ++++ .../storage/service/StorageService.java | 28 +++++ .../src/main/resources/application.properties | 9 ++ seata-at/storage/src/main/resources/file.conf | 66 +++++++++++ .../storage/src/main/resources/registry.conf | 79 ++++++++++++ .../storage/StorageApplicationTests.java | 13 ++ .../javaboy/tips/TipsApplicationTests.java | 12 ++ 49 files changed, 1832 insertions(+), 5 deletions(-) create mode 100644 seata-at/account/pom.xml create mode 100644 seata-at/account/src/main/java/org/javaboy/AccountApplication.java create mode 100644 seata-at/account/src/main/java/org/javaboy/account/controller/AccountController.java create mode 100644 seata-at/account/src/main/java/org/javaboy/account/mapper/AccountMapper.java create mode 100644 seata-at/account/src/main/java/org/javaboy/account/service/AccountService.java create mode 100644 seata-at/account/src/main/resources/application.properties create mode 100755 seata-at/account/src/main/resources/file.conf create mode 100755 seata-at/account/src/main/resources/registry.conf create mode 100644 seata-at/account/src/test/java/org/javaboy/account/AccountApplicationTests.java create mode 100644 seata-at/business/pom.xml create mode 100644 seata-at/business/src/main/java/org/javaboy/BusinessApplication.java create mode 100644 seata-at/business/src/main/java/org/javaboy/business/controller/BusinessController.java create mode 100644 seata-at/business/src/main/java/org/javaboy/business/feign/OrderFeignClient.java create mode 100644 seata-at/business/src/main/java/org/javaboy/business/feign/StorageFeignClient.java create mode 100644 seata-at/business/src/main/java/org/javaboy/business/service/BusinessService.java create mode 100644 seata-at/business/src/main/resources/application.properties create mode 100755 seata-at/business/src/main/resources/file.conf create mode 100755 seata-at/business/src/main/resources/registry.conf create mode 100644 seata-at/business/src/test/java/org/javaboy/business/BusinessApplicationTests.java create mode 100644 seata-at/common/pom.xml create mode 100644 seata-at/common/src/main/java/org/javaboy/common/RespBean.java create mode 100644 seata-at/common/src/main/java/org/javaboy/common/exception/GlobalException.java create mode 100644 seata-at/eureka/pom.xml create mode 100644 seata-at/eureka/src/main/java/org/javaboy/eureka/EurekaApplication.java create mode 100644 seata-at/eureka/src/main/resources/application.properties create mode 100644 seata-at/eureka/src/test/java/org/javaboy/eureka/EurekaApplicationTests.java create mode 100644 seata-at/order/pom.xml create mode 100644 seata-at/order/src/main/java/org/javaboy/OrderApplication.java create mode 100644 seata-at/order/src/main/java/org/javaboy/order/controller/OrderController.java create mode 100644 seata-at/order/src/main/java/org/javaboy/order/feign/AccountFeign.java create mode 100644 seata-at/order/src/main/java/org/javaboy/order/mapper/OrderMapper.java create mode 100644 seata-at/order/src/main/java/org/javaboy/order/service/OrderService.java create mode 100644 seata-at/order/src/main/resources/application.properties create mode 100755 seata-at/order/src/main/resources/file.conf create mode 100755 seata-at/order/src/main/resources/registry.conf create mode 100644 seata-at/order/src/test/java/org/javaboy/order/OrderApplicationTests.java create mode 100644 seata-at/pom.xml create mode 100644 seata-at/storage/pom.xml create mode 100644 seata-at/storage/src/main/java/org/javaboy/StorageApplication.java create mode 100644 seata-at/storage/src/main/java/org/javaboy/storage/controller/StorageController.java create mode 100644 seata-at/storage/src/main/java/org/javaboy/storage/mapper/StorageMapper.java create mode 100644 seata-at/storage/src/main/java/org/javaboy/storage/service/StorageService.java create mode 100644 seata-at/storage/src/main/resources/application.properties create mode 100755 seata-at/storage/src/main/resources/file.conf create mode 100755 seata-at/storage/src/main/resources/registry.conf create mode 100644 seata-at/storage/src/test/java/org/javaboy/storage/StorageApplicationTests.java diff --git a/data_scope/src/main/java/org/javaboy/data_scope/aspect/DataScopeAspect.java b/data_scope/src/main/java/org/javaboy/data_scope/aspect/DataScopeAspect.java index f01af45..ffb0e33 100644 --- a/data_scope/src/main/java/org/javaboy/data_scope/aspect/DataScopeAspect.java +++ b/data_scope/src/main/java/org/javaboy/data_scope/aspect/DataScopeAspect.java @@ -37,15 +37,15 @@ public void before(JoinPoint jp, DataScope dataScope) { for (Role role : roles) { if (role.getDataScope().equals("1")) { } else if (role.getDataScope().equals("2")) { - sql.append(" and "+dataScope.deptAlias()+".dept_id in(select dept_id from sys_role_dept srd where srd.role_id="+role.getRoleId()+")"); + sql.append(String.format(" and %s.dept_id in(select dept_id from sys_role_dept srd where srd.role_id=%d)", dataScope.deptAlias(), role.getRoleId())); } else if (role.getDataScope().equals("3")) { - sql.append(" and "+dataScope.deptAlias()+".dept_id =" + user.getDeptId()); + sql.append(String.format(" and %s.dept_id=%d", dataScope.deptAlias(), user.getDeptId())); } else if (role.getDataScope().equals("4")) { - sql.append(" and "+dataScope.deptAlias()+".dept_id in(select dept_id from sys_dept where sys_dept.dept_id=" + user.getDeptId() + " or find_in_set(" + user.getDeptId() + ",ancestors));"); + sql.append(String.format(" and %s.dept_id in(select dept_id from sys_dept where sys_dept.dept_id=%d or find_in_set(%d,ancestors))", dataScope.deptAlias(), user.getDeptId(), user.getDeptId())); } else if (role.getDataScope().equals("5")) { if (!dataScope.userAlias().equals("")) { - sql.append(" and " + dataScope.userAlias() + ".user_id=" + user.getUserId()); - }else{ + sql.append(String.format(" and %s.user_id=%d", dataScope.userAlias(), user.getDeptId())); + } else { sql.append(" and 1=0"); } } diff --git a/data_scope/src/test/java/org/javaboy/data_scope/DataScopeApplicationTests.java b/data_scope/src/test/java/org/javaboy/data_scope/DataScopeApplicationTests.java index c1ee827..ef519cf 100644 --- a/data_scope/src/test/java/org/javaboy/data_scope/DataScopeApplicationTests.java +++ b/data_scope/src/test/java/org/javaboy/data_scope/DataScopeApplicationTests.java @@ -11,6 +11,12 @@ @SpringBootTest class DataScopeApplicationTests { + @Test + void test01() { + String format = String.format("aaa%sbbb%s", "1", "2"); + System.out.println("format = " + format); + } + @Test void contextLoads() { FastAutoGenerator.create("jdbc:mysql:///test06?serverTimezone=Asia/Shanghai&useSSL=false", "root", "123") diff --git a/seata-at/account/pom.xml b/seata-at/account/pom.xml new file mode 100644 index 0000000..f9cb9f9 --- /dev/null +++ b/seata-at/account/pom.xml @@ -0,0 +1,112 @@ + + + 4.0.0 + org.javaboy + account + 0.0.1-SNAPSHOT + + Demo project for Spring Boot + + + 1.8 + UTF-8 + UTF-8 + 2.3.7.RELEASE + 2.2.2.RELEASE + Hoxton.SR9 + + + + + org.springframework.boot + spring-boot-starter-web + + + com.alibaba.cloud + spring-cloud-starter-alibaba-seata + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.1.4 + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + common + org.javaboy + 1.0-SNAPSHOT + + + mysql + mysql-connector-java + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + UTF-8 + + + + org.springframework.boot + spring-boot-maven-plugin + 2.3.7.RELEASE + + + + + + diff --git a/seata-at/account/src/main/java/org/javaboy/AccountApplication.java b/seata-at/account/src/main/java/org/javaboy/AccountApplication.java new file mode 100644 index 0000000..f93cb21 --- /dev/null +++ b/seata-at/account/src/main/java/org/javaboy/AccountApplication.java @@ -0,0 +1,15 @@ +package org.javaboy; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; + +@SpringBootApplication +@EnableEurekaClient +public class AccountApplication { + + public static void main(String[] args) { + SpringApplication.run(AccountApplication.class, args); + } + +} diff --git a/seata-at/account/src/main/java/org/javaboy/account/controller/AccountController.java b/seata-at/account/src/main/java/org/javaboy/account/controller/AccountController.java new file mode 100644 index 0000000..a9ed95d --- /dev/null +++ b/seata-at/account/src/main/java/org/javaboy/account/controller/AccountController.java @@ -0,0 +1,33 @@ +package org.javaboy.account.controller; + +import org.javaboy.account.service.AccountService; +import org.javaboy.common.RespBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@RestController +public class AccountController { + + @Autowired + AccountService accountService; + + @PostMapping("/deductAccount") + public RespBean deductAccount(String account, Double money) { + if (accountService.deductAccount(account, money)) { + return RespBean.ok("扣款成功"); + } + return RespBean.error("扣款失败"); + } +} diff --git a/seata-at/account/src/main/java/org/javaboy/account/mapper/AccountMapper.java b/seata-at/account/src/main/java/org/javaboy/account/mapper/AccountMapper.java new file mode 100644 index 0000000..6fe3b38 --- /dev/null +++ b/seata-at/account/src/main/java/org/javaboy/account/mapper/AccountMapper.java @@ -0,0 +1,24 @@ +package org.javaboy.account.mapper; + +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@Mapper +public interface AccountMapper { + @Update("update account_tbl set money=money-#{money} where user_id=#{account}") + int updateAccount(@Param("account") String account, @Param("money") Double money); + + @Select("select money from account_tbl where user_id=#{account}") + Double getMoneyByAccount(String account); +} diff --git a/seata-at/account/src/main/java/org/javaboy/account/service/AccountService.java b/seata-at/account/src/main/java/org/javaboy/account/service/AccountService.java new file mode 100644 index 0000000..469f2a0 --- /dev/null +++ b/seata-at/account/src/main/java/org/javaboy/account/service/AccountService.java @@ -0,0 +1,31 @@ +package org.javaboy.account.service; + +import org.javaboy.account.mapper.AccountMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@Service +public class AccountService { + + @Autowired + AccountMapper accountMapper; + + public boolean deductAccount(String account, Double money) { + accountMapper.updateAccount(account, money); + Double m = accountMapper.getMoneyByAccount(account); + if (m >= 0) { + return true; + }else{ + throw new RuntimeException("账户余额不足"); + } + } +} diff --git a/seata-at/account/src/main/resources/application.properties b/seata-at/account/src/main/resources/application.properties new file mode 100644 index 0000000..a358dc5 --- /dev/null +++ b/seata-at/account/src/main/resources/application.properties @@ -0,0 +1,9 @@ +server.port=1111 +spring.application.name=account +eureka.client.service-url.defaultZone=http://localhost:8761/eureka + +spring.datasource.username=root +spring.datasource.password=123 +spring.datasource.url=jdbc:mysql:///account?serverTimezone=Asia/Shanghai&useSSL=false + +spring.cloud.alibaba.seata.tx-service-group=my_test_tx_group \ No newline at end of file diff --git a/seata-at/account/src/main/resources/file.conf b/seata-at/account/src/main/resources/file.conf new file mode 100755 index 0000000..e38ee82 --- /dev/null +++ b/seata-at/account/src/main/resources/file.conf @@ -0,0 +1,66 @@ +transport { + # tcp udt unix-domain-socket + type = "TCP" + #NIO NATIVE + server = "NIO" + #enable heartbeat + heartbeat = true + # the client batch send request enable + enableClientBatchSendRequest = true + #thread factory for netty + threadFactory { + bossThreadPrefix = "NettyBoss" + workerThreadPrefix = "NettyServerNIOWorker" + serverExecutorThread-prefix = "NettyServerBizHandler" + shareBossWorker = false + clientSelectorThreadPrefix = "NettyClientSelector" + clientSelectorThreadSize = 1 + clientWorkerThreadPrefix = "NettyClientWorkerThread" + # netty boss thread size,will not be used for UDT + bossThreadSize = 1 + #auto default pin or 8 + workerThreadSize = "default" + } + shutdown { + # when destroy server, wait seconds + wait = 3 + } + serialization = "seata" + compressor = "none" +} +service { + #transaction service group mapping + vgroupMapping.my_test_tx_group = "default" + #only support when registry.type=file, please don't set multiple addresses + default.grouplist = "127.0.0.1:8091" + #degrade, current not support + enableDegrade = false + #disable seata + disableGlobalTransaction = false +} + +client { + rm { + asyncCommitBufferLimit = 10000 + lock { + retryInterval = 10 + retryTimes = 30 + retryPolicyBranchRollbackOnConflict = true + } + reportRetryCount = 5 + tableMetaCheckEnable = false + reportSuccessEnable = false + } + tm { + commitRetryCount = 5 + rollbackRetryCount = 5 + } + undo { + dataValidation = true + logSerialization = "jackson" + logTable = "undo_log" + } + log { + exceptionRate = 100 + } +} \ No newline at end of file diff --git a/seata-at/account/src/main/resources/registry.conf b/seata-at/account/src/main/resources/registry.conf new file mode 100755 index 0000000..7f2236f --- /dev/null +++ b/seata-at/account/src/main/resources/registry.conf @@ -0,0 +1,79 @@ +registry { + # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa + type = "eureka" + + nacos { + application = "seata-server" + serverAddr = "localhost" + namespace = "" + username = "" + password = "" + } + eureka { + serviceUrl = "http://localhost:8761/eureka" + weight = "1" + } + redis { + serverAddr = "localhost:6379" + db = "0" + password = "" + timeout = "0" + } + zk { + serverAddr = "127.0.0.1:2181" + sessionTimeout = 6000 + connectTimeout = 2000 + username = "" + password = "" + } + consul { + serverAddr = "127.0.0.1:8500" + } + etcd3 { + serverAddr = "http://localhost:2379" + } + sofa { + serverAddr = "127.0.0.1:9603" + region = "DEFAULT_ZONE" + datacenter = "DefaultDataCenter" + group = "SEATA_GROUP" + addressWaitTime = "3000" + } + file { + name = "file.conf" + } +} + +config { + # file、nacos 、apollo、zk、consul、etcd3、springCloudConfig + type = "file" + + nacos { + serverAddr = "localhost" + namespace = "" + group = "SEATA_GROUP" + username = "" + password = "" + } + consul { + serverAddr = "127.0.0.1:8500" + } + apollo { + appId = "seata-server" + apolloMeta = "http://192.168.1.204:8801" + namespace = "application" + } + zk { + serverAddr = "127.0.0.1:2181" + sessionTimeout = 6000 + connectTimeout = 2000 + username = "" + password = "" + } + etcd3 { + serverAddr = "http://localhost:2379" + } + file { + name = "file.conf" + } +} diff --git a/seata-at/account/src/test/java/org/javaboy/account/AccountApplicationTests.java b/seata-at/account/src/test/java/org/javaboy/account/AccountApplicationTests.java new file mode 100644 index 0000000..fca712c --- /dev/null +++ b/seata-at/account/src/test/java/org/javaboy/account/AccountApplicationTests.java @@ -0,0 +1,13 @@ +package org.javaboy.account; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class AccountApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/seata-at/business/pom.xml b/seata-at/business/pom.xml new file mode 100644 index 0000000..eba6af4 --- /dev/null +++ b/seata-at/business/pom.xml @@ -0,0 +1,101 @@ + + + 4.0.0 + org.javaboy + business + 0.0.1-SNAPSHOT + + Demo project for Spring Boot + + + 1.8 + UTF-8 + UTF-8 + 2.3.7.RELEASE + 2.2.2.RELEASE + Hoxton.SR9 + + + + + org.springframework.boot + spring-boot-starter-web + + + com.alibaba.cloud + spring-cloud-starter-alibaba-seata + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + common + org.javaboy + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + UTF-8 + + + + org.springframework.boot + spring-boot-maven-plugin + 2.3.7.RELEASE + + + + + diff --git a/seata-at/business/src/main/java/org/javaboy/BusinessApplication.java b/seata-at/business/src/main/java/org/javaboy/BusinessApplication.java new file mode 100644 index 0000000..7ab3255 --- /dev/null +++ b/seata-at/business/src/main/java/org/javaboy/BusinessApplication.java @@ -0,0 +1,15 @@ +package org.javaboy; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableFeignClients +public class BusinessApplication { + + public static void main(String[] args) { + SpringApplication.run(BusinessApplication.class, args); + } + +} diff --git a/seata-at/business/src/main/java/org/javaboy/business/controller/BusinessController.java b/seata-at/business/src/main/java/org/javaboy/business/controller/BusinessController.java new file mode 100644 index 0000000..b4c2974 --- /dev/null +++ b/seata-at/business/src/main/java/org/javaboy/business/controller/BusinessController.java @@ -0,0 +1,34 @@ +package org.javaboy.business.controller; + +import org.javaboy.business.service.BusinessService; +import org.javaboy.common.RespBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@RestController +public class BusinessController { + + @Autowired + BusinessService businessService; + + @GetMapping("/order") + public RespBean order(String account, Integer count, String productId) { + try { + businessService.purchase(account, count, productId); + return RespBean.ok("下单成功"); + } catch (Exception e) { + return RespBean.error(e.getMessage()); + } + + } +} diff --git a/seata-at/business/src/main/java/org/javaboy/business/feign/OrderFeignClient.java b/seata-at/business/src/main/java/org/javaboy/business/feign/OrderFeignClient.java new file mode 100644 index 0000000..7d9cea5 --- /dev/null +++ b/seata-at/business/src/main/java/org/javaboy/business/feign/OrderFeignClient.java @@ -0,0 +1,24 @@ +package org.javaboy.business.feign; + +import org.javaboy.common.RespBean; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import javax.ws.rs.POST; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@FeignClient("order") +public interface OrderFeignClient { + @PostMapping("/createOrder") + RespBean createOrder(@RequestParam("acount") String account, @RequestParam("count") Integer count, @RequestParam("productId") String productId); +} diff --git a/seata-at/business/src/main/java/org/javaboy/business/feign/StorageFeignClient.java b/seata-at/business/src/main/java/org/javaboy/business/feign/StorageFeignClient.java new file mode 100644 index 0000000..e6468a5 --- /dev/null +++ b/seata-at/business/src/main/java/org/javaboy/business/feign/StorageFeignClient.java @@ -0,0 +1,21 @@ +package org.javaboy.business.feign; + +import org.javaboy.common.RespBean; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@FeignClient("storage") +public interface StorageFeignClient { + @PostMapping("/deduct") + RespBean deduce(@RequestParam("productId") String productId, @RequestParam("count") Integer count); +} diff --git a/seata-at/business/src/main/java/org/javaboy/business/service/BusinessService.java b/seata-at/business/src/main/java/org/javaboy/business/service/BusinessService.java new file mode 100644 index 0000000..032e3df --- /dev/null +++ b/seata-at/business/src/main/java/org/javaboy/business/service/BusinessService.java @@ -0,0 +1,33 @@ +package org.javaboy.business.service; + +import io.seata.spring.annotation.GlobalTransactional; +import org.javaboy.business.feign.OrderFeignClient; +import org.javaboy.business.feign.StorageFeignClient; +import org.javaboy.common.RespBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@Service +public class BusinessService { + + @Autowired + StorageFeignClient storageFeignClient; + @Autowired + OrderFeignClient orderFeignClient; + + + @GlobalTransactional + public void purchase(String account, Integer count, String productId) { + storageFeignClient.deduce(productId, count); + orderFeignClient.createOrder(account, count, productId); + } +} diff --git a/seata-at/business/src/main/resources/application.properties b/seata-at/business/src/main/resources/application.properties new file mode 100644 index 0000000..c017008 --- /dev/null +++ b/seata-at/business/src/main/resources/application.properties @@ -0,0 +1,5 @@ +server.port=1112 +spring.application.name=business +eureka.client.service-url.defaultZone=http://localhost:8761/eureka + +spring.cloud.alibaba.seata.tx-service-group=my_test_tx_group \ No newline at end of file diff --git a/seata-at/business/src/main/resources/file.conf b/seata-at/business/src/main/resources/file.conf new file mode 100755 index 0000000..e38ee82 --- /dev/null +++ b/seata-at/business/src/main/resources/file.conf @@ -0,0 +1,66 @@ +transport { + # tcp udt unix-domain-socket + type = "TCP" + #NIO NATIVE + server = "NIO" + #enable heartbeat + heartbeat = true + # the client batch send request enable + enableClientBatchSendRequest = true + #thread factory for netty + threadFactory { + bossThreadPrefix = "NettyBoss" + workerThreadPrefix = "NettyServerNIOWorker" + serverExecutorThread-prefix = "NettyServerBizHandler" + shareBossWorker = false + clientSelectorThreadPrefix = "NettyClientSelector" + clientSelectorThreadSize = 1 + clientWorkerThreadPrefix = "NettyClientWorkerThread" + # netty boss thread size,will not be used for UDT + bossThreadSize = 1 + #auto default pin or 8 + workerThreadSize = "default" + } + shutdown { + # when destroy server, wait seconds + wait = 3 + } + serialization = "seata" + compressor = "none" +} +service { + #transaction service group mapping + vgroupMapping.my_test_tx_group = "default" + #only support when registry.type=file, please don't set multiple addresses + default.grouplist = "127.0.0.1:8091" + #degrade, current not support + enableDegrade = false + #disable seata + disableGlobalTransaction = false +} + +client { + rm { + asyncCommitBufferLimit = 10000 + lock { + retryInterval = 10 + retryTimes = 30 + retryPolicyBranchRollbackOnConflict = true + } + reportRetryCount = 5 + tableMetaCheckEnable = false + reportSuccessEnable = false + } + tm { + commitRetryCount = 5 + rollbackRetryCount = 5 + } + undo { + dataValidation = true + logSerialization = "jackson" + logTable = "undo_log" + } + log { + exceptionRate = 100 + } +} \ No newline at end of file diff --git a/seata-at/business/src/main/resources/registry.conf b/seata-at/business/src/main/resources/registry.conf new file mode 100755 index 0000000..b76b193 --- /dev/null +++ b/seata-at/business/src/main/resources/registry.conf @@ -0,0 +1,79 @@ +registry { + # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa + type = "file" + + nacos { + application = "seata-server" + serverAddr = "localhost" + namespace = "" + username = "" + password = "" + } + eureka { + serviceUrl = "http://localhost:8761/eureka" + weight = "1" + } + redis { + serverAddr = "localhost:6379" + db = "0" + password = "" + timeout = "0" + } + zk { + serverAddr = "127.0.0.1:2181" + sessionTimeout = 6000 + connectTimeout = 2000 + username = "" + password = "" + } + consul { + serverAddr = "127.0.0.1:8500" + } + etcd3 { + serverAddr = "http://localhost:2379" + } + sofa { + serverAddr = "127.0.0.1:9603" + region = "DEFAULT_ZONE" + datacenter = "DefaultDataCenter" + group = "SEATA_GROUP" + addressWaitTime = "3000" + } + file { + name = "file.conf" + } +} + +config { + # file、nacos 、apollo、zk、consul、etcd3、springCloudConfig + type = "eureka" + + nacos { + serverAddr = "localhost" + namespace = "" + group = "SEATA_GROUP" + username = "" + password = "" + } + consul { + serverAddr = "127.0.0.1:8500" + } + apollo { + appId = "seata-server" + apolloMeta = "http://192.168.1.204:8801" + namespace = "application" + } + zk { + serverAddr = "127.0.0.1:2181" + sessionTimeout = 6000 + connectTimeout = 2000 + username = "" + password = "" + } + etcd3 { + serverAddr = "http://localhost:2379" + } + file { + name = "file.conf" + } +} diff --git a/seata-at/business/src/test/java/org/javaboy/business/BusinessApplicationTests.java b/seata-at/business/src/test/java/org/javaboy/business/BusinessApplicationTests.java new file mode 100644 index 0000000..02e5fc2 --- /dev/null +++ b/seata-at/business/src/test/java/org/javaboy/business/BusinessApplicationTests.java @@ -0,0 +1,13 @@ +package org.javaboy.business; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class BusinessApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/seata-at/common/pom.xml b/seata-at/common/pom.xml new file mode 100644 index 0000000..331d9d6 --- /dev/null +++ b/seata-at/common/pom.xml @@ -0,0 +1,65 @@ + + + + seata-at + org.javaboy + 1.0-SNAPSHOT + + 4.0.0 + + common + + Demo project for Spring Boot + + + 1.8 + UTF-8 + UTF-8 + 2.3.7.RELEASE + 2.2.2.RELEASE + Hoxton.SR9 + + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + UTF-8 + + + + org.springframework.boot + spring-boot-maven-plugin + 2.3.7.RELEASE + + + + + + \ No newline at end of file diff --git a/seata-at/common/src/main/java/org/javaboy/common/RespBean.java b/seata-at/common/src/main/java/org/javaboy/common/RespBean.java new file mode 100644 index 0000000..c6ff68f --- /dev/null +++ b/seata-at/common/src/main/java/org/javaboy/common/RespBean.java @@ -0,0 +1,65 @@ +package org.javaboy.common; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +public class RespBean { + private Integer status; + private String message; + private Object data; + + public static RespBean ok(String message, Object data) { + return new RespBean(200, message, data); + } + + public static RespBean ok(String message) { + return new RespBean(200, message, null); + } + + public static RespBean error(String message, Object data) { + return new RespBean(500, message, data); + } + + public static RespBean error(String message) { + return new RespBean(500, message, null); + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + private RespBean() { + } + + private RespBean(Integer status, String message, Object data) { + this.status = status; + this.message = message; + this.data = data; + } +} diff --git a/seata-at/common/src/main/java/org/javaboy/common/exception/GlobalException.java b/seata-at/common/src/main/java/org/javaboy/common/exception/GlobalException.java new file mode 100644 index 0000000..768b192 --- /dev/null +++ b/seata-at/common/src/main/java/org/javaboy/common/exception/GlobalException.java @@ -0,0 +1,25 @@ +package org.javaboy.common.exception; + +import org.javaboy.common.RespBean; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@RestControllerAdvice +public class GlobalException { + @ExceptionHandler(RuntimeException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public RespBean runtimeException(RuntimeException e) { + return RespBean.error(e.getMessage()); + } +} diff --git a/seata-at/eureka/pom.xml b/seata-at/eureka/pom.xml new file mode 100644 index 0000000..ddaf0f4 --- /dev/null +++ b/seata-at/eureka/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.8 + + + org.javaboy + eureka + 0.0.1-SNAPSHOT + eureka + Demo project for Spring Boot + + 17 + 2021.0.2 + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-server + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/seata-at/eureka/src/main/java/org/javaboy/eureka/EurekaApplication.java b/seata-at/eureka/src/main/java/org/javaboy/eureka/EurekaApplication.java new file mode 100644 index 0000000..5d70e8d --- /dev/null +++ b/seata-at/eureka/src/main/java/org/javaboy/eureka/EurekaApplication.java @@ -0,0 +1,15 @@ +package org.javaboy.eureka; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; + +@SpringBootApplication +@EnableEurekaServer +public class EurekaApplication { + + public static void main(String[] args) { + SpringApplication.run(EurekaApplication.class, args); + } + +} diff --git a/seata-at/eureka/src/main/resources/application.properties b/seata-at/eureka/src/main/resources/application.properties new file mode 100644 index 0000000..156cbda --- /dev/null +++ b/seata-at/eureka/src/main/resources/application.properties @@ -0,0 +1,4 @@ +eureka.client.fetch-registry=false +eureka.client.register-with-eureka=false + +server.port=8761 \ No newline at end of file diff --git a/seata-at/eureka/src/test/java/org/javaboy/eureka/EurekaApplicationTests.java b/seata-at/eureka/src/test/java/org/javaboy/eureka/EurekaApplicationTests.java new file mode 100644 index 0000000..0f0b07c --- /dev/null +++ b/seata-at/eureka/src/test/java/org/javaboy/eureka/EurekaApplicationTests.java @@ -0,0 +1,13 @@ +package org.javaboy.eureka; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class EurekaApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/seata-at/order/pom.xml b/seata-at/order/pom.xml new file mode 100644 index 0000000..5a1309a --- /dev/null +++ b/seata-at/order/pom.xml @@ -0,0 +1,111 @@ + + + 4.0.0 + org.javaboy + order + 0.0.1-SNAPSHOT + + Demo project for Spring Boot + + + 1.8 + UTF-8 + UTF-8 + 2.3.7.RELEASE + 2.2.2.RELEASE + Hoxton.SR9 + + + + + org.springframework.boot + spring-boot-starter-web + + + com.alibaba.cloud + spring-cloud-starter-alibaba-seata + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.1.4 + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + common + org.javaboy + 1.0-SNAPSHOT + + + mysql + mysql-connector-java + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + UTF-8 + + + + org.springframework.boot + spring-boot-maven-plugin + 2.3.7.RELEASE + + + + + diff --git a/seata-at/order/src/main/java/org/javaboy/OrderApplication.java b/seata-at/order/src/main/java/org/javaboy/OrderApplication.java new file mode 100644 index 0000000..1904272 --- /dev/null +++ b/seata-at/order/src/main/java/org/javaboy/OrderApplication.java @@ -0,0 +1,17 @@ +package org.javaboy; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableEurekaClient +@EnableFeignClients +public class OrderApplication { + + public static void main(String[] args) { + SpringApplication.run(OrderApplication.class, args); + } + +} diff --git a/seata-at/order/src/main/java/org/javaboy/order/controller/OrderController.java b/seata-at/order/src/main/java/org/javaboy/order/controller/OrderController.java new file mode 100644 index 0000000..157d03e --- /dev/null +++ b/seata-at/order/src/main/java/org/javaboy/order/controller/OrderController.java @@ -0,0 +1,32 @@ +package org.javaboy.order.controller; + +import org.javaboy.common.RespBean; +import org.javaboy.order.service.OrderService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@RestController +public class OrderController { + @Autowired + OrderService orderService; + + @PostMapping("/createOrder") + public RespBean createOrder(@RequestParam("acount") String account, @RequestParam("count") Integer count, @RequestParam("productId") String productId) { + if (orderService.createOrder(account, productId, count)) { + return RespBean.ok("下单成功"); + } + return RespBean.error("下单失败"); + } +} diff --git a/seata-at/order/src/main/java/org/javaboy/order/feign/AccountFeign.java b/seata-at/order/src/main/java/org/javaboy/order/feign/AccountFeign.java new file mode 100644 index 0000000..70c0710 --- /dev/null +++ b/seata-at/order/src/main/java/org/javaboy/order/feign/AccountFeign.java @@ -0,0 +1,22 @@ +package org.javaboy.order.feign; + +import org.apache.ibatis.annotations.Param; +import org.javaboy.common.RespBean; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@FeignClient("account") +public interface AccountFeign { + @PostMapping("/deductAccount") + RespBean deductAccount(@RequestParam("account") String account, @RequestParam("money") Double money); +} diff --git a/seata-at/order/src/main/java/org/javaboy/order/mapper/OrderMapper.java b/seata-at/order/src/main/java/org/javaboy/order/mapper/OrderMapper.java new file mode 100644 index 0000000..2aaee20 --- /dev/null +++ b/seata-at/order/src/main/java/org/javaboy/order/mapper/OrderMapper.java @@ -0,0 +1,20 @@ +package org.javaboy.order.mapper; + +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@Mapper +public interface OrderMapper { + @Insert("insert into order_tbl(user_id,commodity_code,count) values(#{account},#{productId},#{count})") + int createOrder(@Param("account") String account, @Param("productId") String productId, @Param("count") Integer count); +} diff --git a/seata-at/order/src/main/java/org/javaboy/order/service/OrderService.java b/seata-at/order/src/main/java/org/javaboy/order/service/OrderService.java new file mode 100644 index 0000000..ac5e25d --- /dev/null +++ b/seata-at/order/src/main/java/org/javaboy/order/service/OrderService.java @@ -0,0 +1,32 @@ +package org.javaboy.order.service; + +import org.javaboy.common.RespBean; +import org.javaboy.order.feign.AccountFeign; +import org.javaboy.order.mapper.OrderMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@Service +public class OrderService { + + @Autowired + OrderMapper orderMapper; + @Autowired + AccountFeign accountFeign; + + public boolean createOrder(String account, String productId, Integer count) { + //扣款,每件商品 100 块钱 + RespBean respBean = accountFeign.deductAccount(account, count * 100.0); + int order = orderMapper.createOrder(account, productId, count); + return order == 1 && respBean.getStatus() == 200; + } +} diff --git a/seata-at/order/src/main/resources/application.properties b/seata-at/order/src/main/resources/application.properties new file mode 100644 index 0000000..c627bb6 --- /dev/null +++ b/seata-at/order/src/main/resources/application.properties @@ -0,0 +1,9 @@ +server.port=1113 +spring.application.name=order +eureka.client.service-url.defaultZone=http://localhost:8761/eureka + +spring.datasource.username=root +spring.datasource.password=123 +spring.datasource.url=jdbc:mysql:///order?serverTimezone=Asia/Shanghai&useSSL=false + +spring.cloud.alibaba.seata.tx-service-group=my_test_tx_group \ No newline at end of file diff --git a/seata-at/order/src/main/resources/file.conf b/seata-at/order/src/main/resources/file.conf new file mode 100755 index 0000000..e38ee82 --- /dev/null +++ b/seata-at/order/src/main/resources/file.conf @@ -0,0 +1,66 @@ +transport { + # tcp udt unix-domain-socket + type = "TCP" + #NIO NATIVE + server = "NIO" + #enable heartbeat + heartbeat = true + # the client batch send request enable + enableClientBatchSendRequest = true + #thread factory for netty + threadFactory { + bossThreadPrefix = "NettyBoss" + workerThreadPrefix = "NettyServerNIOWorker" + serverExecutorThread-prefix = "NettyServerBizHandler" + shareBossWorker = false + clientSelectorThreadPrefix = "NettyClientSelector" + clientSelectorThreadSize = 1 + clientWorkerThreadPrefix = "NettyClientWorkerThread" + # netty boss thread size,will not be used for UDT + bossThreadSize = 1 + #auto default pin or 8 + workerThreadSize = "default" + } + shutdown { + # when destroy server, wait seconds + wait = 3 + } + serialization = "seata" + compressor = "none" +} +service { + #transaction service group mapping + vgroupMapping.my_test_tx_group = "default" + #only support when registry.type=file, please don't set multiple addresses + default.grouplist = "127.0.0.1:8091" + #degrade, current not support + enableDegrade = false + #disable seata + disableGlobalTransaction = false +} + +client { + rm { + asyncCommitBufferLimit = 10000 + lock { + retryInterval = 10 + retryTimes = 30 + retryPolicyBranchRollbackOnConflict = true + } + reportRetryCount = 5 + tableMetaCheckEnable = false + reportSuccessEnable = false + } + tm { + commitRetryCount = 5 + rollbackRetryCount = 5 + } + undo { + dataValidation = true + logSerialization = "jackson" + logTable = "undo_log" + } + log { + exceptionRate = 100 + } +} \ No newline at end of file diff --git a/seata-at/order/src/main/resources/registry.conf b/seata-at/order/src/main/resources/registry.conf new file mode 100755 index 0000000..7f2236f --- /dev/null +++ b/seata-at/order/src/main/resources/registry.conf @@ -0,0 +1,79 @@ +registry { + # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa + type = "eureka" + + nacos { + application = "seata-server" + serverAddr = "localhost" + namespace = "" + username = "" + password = "" + } + eureka { + serviceUrl = "http://localhost:8761/eureka" + weight = "1" + } + redis { + serverAddr = "localhost:6379" + db = "0" + password = "" + timeout = "0" + } + zk { + serverAddr = "127.0.0.1:2181" + sessionTimeout = 6000 + connectTimeout = 2000 + username = "" + password = "" + } + consul { + serverAddr = "127.0.0.1:8500" + } + etcd3 { + serverAddr = "http://localhost:2379" + } + sofa { + serverAddr = "127.0.0.1:9603" + region = "DEFAULT_ZONE" + datacenter = "DefaultDataCenter" + group = "SEATA_GROUP" + addressWaitTime = "3000" + } + file { + name = "file.conf" + } +} + +config { + # file、nacos 、apollo、zk、consul、etcd3、springCloudConfig + type = "file" + + nacos { + serverAddr = "localhost" + namespace = "" + group = "SEATA_GROUP" + username = "" + password = "" + } + consul { + serverAddr = "127.0.0.1:8500" + } + apollo { + appId = "seata-server" + apolloMeta = "http://192.168.1.204:8801" + namespace = "application" + } + zk { + serverAddr = "127.0.0.1:2181" + sessionTimeout = 6000 + connectTimeout = 2000 + username = "" + password = "" + } + etcd3 { + serverAddr = "http://localhost:2379" + } + file { + name = "file.conf" + } +} diff --git a/seata-at/order/src/test/java/org/javaboy/order/OrderApplicationTests.java b/seata-at/order/src/test/java/org/javaboy/order/OrderApplicationTests.java new file mode 100644 index 0000000..e4b4b00 --- /dev/null +++ b/seata-at/order/src/test/java/org/javaboy/order/OrderApplicationTests.java @@ -0,0 +1,13 @@ +package org.javaboy.order; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class OrderApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/seata-at/pom.xml b/seata-at/pom.xml new file mode 100644 index 0000000..0f78cf3 --- /dev/null +++ b/seata-at/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + org.javaboy + seata-at + pom + 1.0-SNAPSHOT + + common + + + + \ No newline at end of file diff --git a/seata-at/storage/pom.xml b/seata-at/storage/pom.xml new file mode 100644 index 0000000..b510dff --- /dev/null +++ b/seata-at/storage/pom.xml @@ -0,0 +1,111 @@ + + + 4.0.0 + org.javaboy + storage + 0.0.1-SNAPSHOT + + Demo project for Spring Boot + + + 1.8 + UTF-8 + UTF-8 + 2.3.7.RELEASE + 2.2.2.RELEASE + Hoxton.SR9 + + + + + org.springframework.boot + spring-boot-starter-web + + + com.alibaba.cloud + spring-cloud-starter-alibaba-seata + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.1.4 + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + common + org.javaboy + 1.0-SNAPSHOT + + + mysql + mysql-connector-java + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring-cloud-alibaba.version} + pom + import + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + UTF-8 + + + + org.springframework.boot + spring-boot-maven-plugin + 2.3.7.RELEASE + + + + + diff --git a/seata-at/storage/src/main/java/org/javaboy/StorageApplication.java b/seata-at/storage/src/main/java/org/javaboy/StorageApplication.java new file mode 100644 index 0000000..3e4a807 --- /dev/null +++ b/seata-at/storage/src/main/java/org/javaboy/StorageApplication.java @@ -0,0 +1,13 @@ +package org.javaboy; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class StorageApplication { + + public static void main(String[] args) { + SpringApplication.run(StorageApplication.class, args); + } + +} diff --git a/seata-at/storage/src/main/java/org/javaboy/storage/controller/StorageController.java b/seata-at/storage/src/main/java/org/javaboy/storage/controller/StorageController.java new file mode 100644 index 0000000..2361f14 --- /dev/null +++ b/seata-at/storage/src/main/java/org/javaboy/storage/controller/StorageController.java @@ -0,0 +1,32 @@ +package org.javaboy.storage.controller; + +import org.javaboy.common.RespBean; +import org.javaboy.storage.service.StorageService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@RestController +public class StorageController { + + @Autowired + StorageService storageService; + + @PostMapping("/deduct") + public RespBean deduct(@RequestParam("productId") String productId, @RequestParam("count") Integer count) { + if (storageService.deduct(productId, count)) { + return RespBean.ok("扣库存成功"); + } + return RespBean.error("扣库存失败"); + } +} diff --git a/seata-at/storage/src/main/java/org/javaboy/storage/mapper/StorageMapper.java b/seata-at/storage/src/main/java/org/javaboy/storage/mapper/StorageMapper.java new file mode 100644 index 0000000..3280995 --- /dev/null +++ b/seata-at/storage/src/main/java/org/javaboy/storage/mapper/StorageMapper.java @@ -0,0 +1,24 @@ +package org.javaboy.storage.mapper; + +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@Mapper +public interface StorageMapper { + @Update("update storage_tbl set count=count-#{count} where commodity_code=#{productId}") + int deduct(@Param("productId") String productId, @Param("count") Integer count); + + @Select("select count from storage_tbl where commodity_code=#{productId}") + int getCountByProductId(String productId); +} diff --git a/seata-at/storage/src/main/java/org/javaboy/storage/service/StorageService.java b/seata-at/storage/src/main/java/org/javaboy/storage/service/StorageService.java new file mode 100644 index 0000000..5cf1abb --- /dev/null +++ b/seata-at/storage/src/main/java/org/javaboy/storage/service/StorageService.java @@ -0,0 +1,28 @@ +package org.javaboy.storage.service; + +import org.javaboy.storage.mapper.StorageMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @author 江南一点雨 + * @微信公众号 江南一点雨 + * @网站 http://www.itboyhub.com + * @国际站 http://www.javaboy.org + * @微信 a_java_boy + * @GitHub https://github.com/lenve + * @Gitee https://gitee.com/lenve + */ +@Service +public class StorageService { + @Autowired + StorageMapper storageMapper; + public boolean deduct(String productId, Integer count) { + int deduct = storageMapper.deduct(productId, count); + int c = storageMapper.getCountByProductId(productId); + if (c >= 0) { + return true; + } + throw new RuntimeException("库存不足,扣库存失败"); + } +} diff --git a/seata-at/storage/src/main/resources/application.properties b/seata-at/storage/src/main/resources/application.properties new file mode 100644 index 0000000..7ab41a9 --- /dev/null +++ b/seata-at/storage/src/main/resources/application.properties @@ -0,0 +1,9 @@ +server.port=1114 +spring.application.name=storage +eureka.client.service-url.defaultZone=http://localhost:8761/eureka + +spring.datasource.username=root +spring.datasource.password=123 +spring.datasource.url=jdbc:mysql:///storage?serverTimezone=Asia/Shanghai&useSSL=false + +spring.cloud.alibaba.seata.tx-service-group=my_test_tx_group \ No newline at end of file diff --git a/seata-at/storage/src/main/resources/file.conf b/seata-at/storage/src/main/resources/file.conf new file mode 100755 index 0000000..e38ee82 --- /dev/null +++ b/seata-at/storage/src/main/resources/file.conf @@ -0,0 +1,66 @@ +transport { + # tcp udt unix-domain-socket + type = "TCP" + #NIO NATIVE + server = "NIO" + #enable heartbeat + heartbeat = true + # the client batch send request enable + enableClientBatchSendRequest = true + #thread factory for netty + threadFactory { + bossThreadPrefix = "NettyBoss" + workerThreadPrefix = "NettyServerNIOWorker" + serverExecutorThread-prefix = "NettyServerBizHandler" + shareBossWorker = false + clientSelectorThreadPrefix = "NettyClientSelector" + clientSelectorThreadSize = 1 + clientWorkerThreadPrefix = "NettyClientWorkerThread" + # netty boss thread size,will not be used for UDT + bossThreadSize = 1 + #auto default pin or 8 + workerThreadSize = "default" + } + shutdown { + # when destroy server, wait seconds + wait = 3 + } + serialization = "seata" + compressor = "none" +} +service { + #transaction service group mapping + vgroupMapping.my_test_tx_group = "default" + #only support when registry.type=file, please don't set multiple addresses + default.grouplist = "127.0.0.1:8091" + #degrade, current not support + enableDegrade = false + #disable seata + disableGlobalTransaction = false +} + +client { + rm { + asyncCommitBufferLimit = 10000 + lock { + retryInterval = 10 + retryTimes = 30 + retryPolicyBranchRollbackOnConflict = true + } + reportRetryCount = 5 + tableMetaCheckEnable = false + reportSuccessEnable = false + } + tm { + commitRetryCount = 5 + rollbackRetryCount = 5 + } + undo { + dataValidation = true + logSerialization = "jackson" + logTable = "undo_log" + } + log { + exceptionRate = 100 + } +} \ No newline at end of file diff --git a/seata-at/storage/src/main/resources/registry.conf b/seata-at/storage/src/main/resources/registry.conf new file mode 100755 index 0000000..7f2236f --- /dev/null +++ b/seata-at/storage/src/main/resources/registry.conf @@ -0,0 +1,79 @@ +registry { + # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa + type = "eureka" + + nacos { + application = "seata-server" + serverAddr = "localhost" + namespace = "" + username = "" + password = "" + } + eureka { + serviceUrl = "http://localhost:8761/eureka" + weight = "1" + } + redis { + serverAddr = "localhost:6379" + db = "0" + password = "" + timeout = "0" + } + zk { + serverAddr = "127.0.0.1:2181" + sessionTimeout = 6000 + connectTimeout = 2000 + username = "" + password = "" + } + consul { + serverAddr = "127.0.0.1:8500" + } + etcd3 { + serverAddr = "http://localhost:2379" + } + sofa { + serverAddr = "127.0.0.1:9603" + region = "DEFAULT_ZONE" + datacenter = "DefaultDataCenter" + group = "SEATA_GROUP" + addressWaitTime = "3000" + } + file { + name = "file.conf" + } +} + +config { + # file、nacos 、apollo、zk、consul、etcd3、springCloudConfig + type = "file" + + nacos { + serverAddr = "localhost" + namespace = "" + group = "SEATA_GROUP" + username = "" + password = "" + } + consul { + serverAddr = "127.0.0.1:8500" + } + apollo { + appId = "seata-server" + apolloMeta = "http://192.168.1.204:8801" + namespace = "application" + } + zk { + serverAddr = "127.0.0.1:2181" + sessionTimeout = 6000 + connectTimeout = 2000 + username = "" + password = "" + } + etcd3 { + serverAddr = "http://localhost:2379" + } + file { + name = "file.conf" + } +} diff --git a/seata-at/storage/src/test/java/org/javaboy/storage/StorageApplicationTests.java b/seata-at/storage/src/test/java/org/javaboy/storage/StorageApplicationTests.java new file mode 100644 index 0000000..0a32d95 --- /dev/null +++ b/seata-at/storage/src/test/java/org/javaboy/storage/StorageApplicationTests.java @@ -0,0 +1,13 @@ +package org.javaboy.storage; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class StorageApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/tips/src/test/java/org/javaboy/tips/TipsApplicationTests.java b/tips/src/test/java/org/javaboy/tips/TipsApplicationTests.java index 5227307..6f3e03a 100644 --- a/tips/src/test/java/org/javaboy/tips/TipsApplicationTests.java +++ b/tips/src/test/java/org/javaboy/tips/TipsApplicationTests.java @@ -4,6 +4,10 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + @SpringBootTest class TipsApplicationTests { @@ -13,6 +17,14 @@ class TipsApplicationTests { @Test void contextLoads() { System.out.println("name = " + name); + + List list = new ArrayList<>(); + list.forEach(new Consumer() { + @Override + public void accept(String s) { + return; + } + }); } }