增加seata-demo工程

This commit is contained in:
zlt
2019-09-15 10:48:17 +08:00
parent 6632da7b19
commit deac4ac957
44 changed files with 1045 additions and 0 deletions
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zlt</groupId>
<artifactId>seata-demo</artifactId>
<version>2.6.0</version>
</parent>
<artifactId>business-service</artifactId>
<dependencies>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-ribbon-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- seata-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,21 @@
package com.central.business;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* 业务
*
* @author zlt
* @date 2019/9/14
*/
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class BusinessServiceApplication {
public static void main(String[] args) {
SpringApplication.run(BusinessServiceApplication.class, args);
}
}
@@ -0,0 +1,36 @@
package com.central.business.controller;
import com.central.business.service.BusinessService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author zlt
* @date 2019/9/14
*/
@RestController
public class BusinessController {
@Resource
private BusinessService businessService;
/**
* 下单场景测试-正常
*/
@RequestMapping(path = "/placeOrder")
public Boolean placeOrder() {
businessService.placeOrder("U001");
return true;
}
/**
* 下单场景测试-回滚
*/
@RequestMapping(path = "/placeOrderFallBack")
public Boolean placeOrderFallBack() {
businessService.placeOrder("U002");
return true;
}
}
@@ -0,0 +1,18 @@
package com.central.business.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* 订单
*
* @author zlt
* @date 2019/9/14
*/
@FeignClient(name = "order-service")
public interface OrderFeignClient {
@GetMapping("order/create")
Boolean create(@RequestParam("userId") String userId, @RequestParam("commodityCode") String commodityCode, @RequestParam("count") Integer count);
}
@@ -0,0 +1,18 @@
package com.central.business.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* 库存
*
* @author zlt
* @date 2019/9/14
*/
@FeignClient(name = "storage-service")
public interface StorageFeignClient {
@GetMapping("storage/deduct")
Boolean deduct(@RequestParam("commodityCode") String commodityCode, @RequestParam("count") Integer count);
}
@@ -0,0 +1,38 @@
package com.central.business.service;
import com.central.business.feign.OrderFeignClient;
import com.central.business.feign.StorageFeignClient;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* 业务
*
* @author zlt
* @date 2019/9/14
*/
@Slf4j
@Service
public class BusinessService {
private static final String COMMODITY_CODE = "P001";
private static final int ORDER_COUNT = 1;
@Resource
private OrderFeignClient orderFeignClient;
@Resource
private StorageFeignClient storageFeignClient;
/**
* 下订单
*/
@GlobalTransactional
public void placeOrder(String userId) {
storageFeignClient.deduct(COMMODITY_CODE, ORDER_COUNT);
orderFeignClient.create(userId, COMMODITY_CODE, ORDER_COUNT);
}
}
@@ -0,0 +1,6 @@
spring:
cloud:
alibaba:
seata:
# seata 服务分组,要与服务端nacos-config.txt中service.vgroup_mapping的后缀对应,默认是${spring.application.name}-fescar-service-group
tx-service-group: business-service-group
@@ -0,0 +1,10 @@
server:
port: 9090
spring:
application:
name: business-service
cloud:
nacos:
discovery:
server-addr: 192.168.28.130:8848
@@ -0,0 +1,26 @@
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
serverAddr = "192.168.28.130"
namespace = "public"
cluster = "default"
}
file {
name = "file.conf"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "192.168.28.130"
namespace = "public"
cluster = "default"
}
file {
name = "file.conf"
}
}