Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add springcloud-jpa-seata sample (#87)
* add springcloud-jpa-seata sample * add spring repo * remove order-service dependencyManagement * fix userId equals
- Loading branch information
Showing
42 changed files
with
1,496 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# seata-sample | ||
基于spring cloud+feign+spring jpa+spring cloud alibaba fescar+mysql | ||
|
||
### 准备工作 | ||
1. 执行sql/all_in_one.sql | ||
|
||
2. 下载[0.4.1](https://github.com/seata/seata/releases/tag/v0.4.1)版本server | ||
|
||
客户端与服务端版本号保持一致 | ||
3. 启动fescar server | ||
|
||
sh fescar-server.sh 8091 ../data/ | ||
4. 启动business、storage、account、order | ||
|
||
数据库默认连接127.0.0.1:3306,不同的注意修改 | ||
|
||
5. 事务成功 GET http://127.0.0.1:8084/purchase/commit | ||
|
||
6. 事务回滚 GET http://127.0.0.1:8084/purchase/rollback | ||
|
||
### 验证数据 | ||
1. 事务成功 | ||
|
||
库存减1、订单加1、余额减5 | ||
2. 事务回滚 | ||
|
||
数据无变化 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?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> | ||
<groupId>com.alibaba.fescar</groupId> | ||
<artifactId>springcloud-jpa-seata</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>business-service</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<name>business-service</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-openfeign</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-alibaba-fescar</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.fescar</groupId> | ||
<artifactId>fescar-spring</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
15 changes: 15 additions & 0 deletions
15
...pa-seata/business-service/src/main/java/com/alibaba/seata/sample/BusinessApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.alibaba.seata.sample; | ||
|
||
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); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...usiness-service/src/main/java/com/alibaba/seata/sample/controller/BusinessController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.alibaba.seata.sample.controller; | ||
|
||
import com.alibaba.seata.sample.service.BusinessService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class BusinessController { | ||
|
||
@Autowired | ||
private BusinessService businessService; | ||
|
||
/** | ||
* 购买下单,模拟全局事务提交 | ||
* @return | ||
*/ | ||
@RequestMapping("/purchase/commit") | ||
public Boolean purchaseCommit(){ | ||
businessService.purchase("1001", "2001", 1); | ||
return true; | ||
} | ||
|
||
/** | ||
* 购买下单,模拟全局事务回滚 | ||
* @return | ||
*/ | ||
@RequestMapping("/purchase/rollback") | ||
public Boolean purchaseRollback(){ | ||
try { | ||
businessService.purchase("1002", "2001", 1); | ||
}catch (Exception e){ | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...seata/business-service/src/main/java/com/alibaba/seata/sample/feign/OrderFeignClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.alibaba.seata.sample.feign; | ||
|
||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
/** | ||
* Description: | ||
* | ||
* @author fangliangsheng | ||
* @date 2019-04-04 | ||
*/ | ||
@FeignClient(name = "order-service", url = "127.0.0.1:8082") | ||
public interface OrderFeignClient { | ||
|
||
@GetMapping("/create") | ||
void create(@RequestParam("userId") String userId, | ||
@RequestParam("commodityCode") String commodityCode, | ||
@RequestParam("count") Integer count); | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...ata/business-service/src/main/java/com/alibaba/seata/sample/feign/StorageFeignClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.alibaba.seata.sample.feign; | ||
|
||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
/** | ||
* Description: | ||
* | ||
* @author fangliangsheng | ||
* @date 2019-04-04 | ||
*/ | ||
@FeignClient(name ="storage-service", url = "127.0.0.1:8081") | ||
public interface StorageFeignClient { | ||
|
||
@GetMapping("/deduct") | ||
void deduct(@RequestParam("commodityCode") String commodityCode, | ||
@RequestParam("count") Integer count); | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...eata/business-service/src/main/java/com/alibaba/seata/sample/service/BusinessService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.alibaba.seata.sample.service; | ||
|
||
import com.alibaba.fescar.spring.annotation.GlobalTransactional; | ||
import com.alibaba.seata.sample.feign.OrderFeignClient; | ||
import com.alibaba.seata.sample.feign.StorageFeignClient; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* Description: | ||
* | ||
* @author fangliangsheng | ||
* @date 2019-04-05 | ||
*/ | ||
@Service | ||
public class BusinessService { | ||
|
||
@Autowired | ||
private StorageFeignClient storageFeignClient; | ||
@Autowired | ||
private OrderFeignClient orderFeignClient; | ||
|
||
/** | ||
* 减库存,下订单 | ||
* @param userId | ||
* @param commodityCode | ||
* @param orderCount | ||
*/ | ||
@GlobalTransactional | ||
public void purchase(String userId, String commodityCode, int orderCount){ | ||
storageFeignClient.deduct(commodityCode, orderCount); | ||
|
||
orderFeignClient.create(userId, commodityCode, orderCount); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
springcloud-jpa-seata/business-service/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
spring.application.name=business-service | ||
server.port=8084 | ||
|
||
logging.level.com.alibaba.fescar=debug | ||
spring.cloud.alibaba.fescar.tx-service-group=my_test_tx_group |
41 changes: 41 additions & 0 deletions
41
springcloud-jpa-seata/business-service/src/main/resources/file.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
transport { | ||
# tcp udt unix-domain-socket | ||
type = "TCP" | ||
#NIO NATIVE | ||
server = "NIO" | ||
#enable heartbeat | ||
heartbeat = true | ||
#thread factory for netty | ||
thread-factory { | ||
boss-thread-prefix = "NettyBoss" | ||
worker-thread-prefix = "NettyServerNIOWorker" | ||
server-executor-thread-prefix = "NettyServerBizHandler" | ||
share-boss-worker = false | ||
client-selector-thread-prefix = "NettyClientSelector" | ||
client-selector-thread-size = 1 | ||
client-worker-thread-prefix = "NettyClientWorkerThread" | ||
# netty boss thread size,will not be used for UDT | ||
boss-thread-size = 1 | ||
#auto default pin or 8 | ||
worker-thread-size = 8 | ||
} | ||
} | ||
service { | ||
#vgroup->rgroup | ||
vgroup_mapping.my_test_tx_group = "default" | ||
#only support single node | ||
default.grouplist = "127.0.0.1:8091" | ||
#degrade current not support | ||
enableDegrade = false | ||
#disable | ||
disable = false | ||
disableGlobalTransaction = false | ||
} | ||
|
||
client { | ||
async.commit.buffer.limit = 10000 | ||
lock { | ||
retry.internal = 10 | ||
retry.times = 30 | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
springcloud-jpa-seata/business-service/src/main/resources/registry.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
registry { | ||
# file 、nacos 、eureka、redis、zk | ||
type = "file" | ||
|
||
nacos { | ||
serverAddr = "localhost" | ||
namespace = "public" | ||
cluster = "default" | ||
} | ||
eureka { | ||
serviceUrl = "http://localhost:1001/eureka" | ||
application = "default" | ||
weight = "1" | ||
} | ||
redis { | ||
serverAddr = "localhost:6381" | ||
db = "0" | ||
} | ||
zk { | ||
cluster = "default" | ||
serverAddr = "127.0.0.1:2181" | ||
session.timeout = 6000 | ||
connect.timeout = 2000 | ||
} | ||
file { | ||
name = "file.conf" | ||
} | ||
} | ||
|
||
config { | ||
# file、nacos 、apollo、zk | ||
type = "file" | ||
|
||
nacos { | ||
serverAddr = "localhost" | ||
namespace = "public" | ||
cluster = "default" | ||
} | ||
apollo { | ||
app.id = "fescar-server" | ||
apollo.meta = "http://192.168.1.204:8801" | ||
} | ||
zk { | ||
serverAddr = "127.0.0.1:2181" | ||
session.timeout = 6000 | ||
connect.timeout = 2000 | ||
} | ||
file { | ||
name = "file.conf" | ||
} | ||
} |
Oops, something went wrong.