增加seata-demo工程
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
package com.central.account;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
/**
|
||||
* 账号
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/9/14
|
||||
*/
|
||||
@EnableDiscoveryClient
|
||||
@MapperScan({"com.central.account.mapper"})
|
||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
|
||||
public class AccountServiceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AccountServiceApplication.class, args);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.central.account.controller;
|
||||
|
||||
import com.central.account.service.AccountService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2019/9/14
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class AccountController {
|
||||
@Resource
|
||||
private AccountService accountService;
|
||||
|
||||
/**
|
||||
* 账号扣钱
|
||||
*/
|
||||
@PostMapping(value = "/account/reduce")
|
||||
public Boolean reduce(String userId, Integer money) {
|
||||
accountService.reduce(userId, money);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.central.account.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.central.account.model.Account;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2019/9/14
|
||||
*/
|
||||
public interface AccountMapper extends BaseMapper<Account> {
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.central.account.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* 账号
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/9/14
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@TableName("account_tbl")
|
||||
public class Account {
|
||||
@TableId
|
||||
private Long id;
|
||||
private String userId;
|
||||
private Integer money;
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.central.account.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.central.account.model.Account;
|
||||
import com.central.account.mapper.AccountMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2019/9/14
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class AccountService {
|
||||
@Resource
|
||||
private AccountMapper accountMapper;
|
||||
|
||||
/**
|
||||
* 减账号金额
|
||||
*/
|
||||
//@Transactional(rollbackFor = Exception.class)
|
||||
public void reduce(String userId, int money) {
|
||||
if ("U002".equals(userId)) {
|
||||
throw new RuntimeException("this is a mock Exception");
|
||||
}
|
||||
|
||||
QueryWrapper<Account> wrapper = new QueryWrapper<>();
|
||||
wrapper.setEntity(new Account().setUserId(userId));
|
||||
Account account = accountMapper.selectOne(wrapper);
|
||||
account.setMoney(account.getMoney() - money);
|
||||
accountMapper.updateById(account);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://192.168.28.130:3306/seata-demo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
|
||||
username: root
|
||||
password: 1q2w3e4r
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
cloud:
|
||||
alibaba:
|
||||
seata:
|
||||
# seata 服务分组,要与服务端nacos-config.txt中service.vgroup_mapping的后缀对应,默认是${spring.application.name}-fescar-service-group
|
||||
tx-service-group: account-service-group
|
||||
@@ -0,0 +1,10 @@
|
||||
server:
|
||||
port: 9093
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: account-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"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user