如何在微服务中回滚事务?

时间:2018-05-29 08:23:19

标签: spring spring-boot spring-cloud

我试图通过基于 spring-cloud 创建微服务来构建系统。参考(https://spring.io/blog/2015/07/14/microservices-with-spring
我更新了一些新内容:

申请1:http://localhost:3333/accounts
请求正文:

{
    "id":"123",
    "number":"223",
    "owner":"888",
    "balance":"100"
}

-WebAccountsController.java

 @PostMapping(value = "/accounts")
        public void insertAccount(@RequestBody Account account){
            accountsService.createAccount(account);
        }

-WebAccountsService.java

@Transactional(rollbackFor = NullPointerException.class)
    public void createAccount(Account account) {
        try {
            account.setOwner(account.getOwner()+System.currentTimeMillis()%1000);
            HttpEntity<Account> request = new HttpEntity<Account>(account);
            System.out.println("==================="+account.getOwner());
            String url=serviceUrl + "/create-accounts";
            ResponseEntity<String> response = restTemplate.postForEntity( url, request , String.class );
            String s = null;
            s.equalsIgnoreCase("asa");
        } catch (NullPointerException e) {
            throw e;
        }

    }

申请2:((http://localhost:2222/create-accounts

-AccountsController.java

@PostMapping(value = "/create-accounts")
    @Transactional(rollbackFor = Exception.class)
    public void createAccount(@RequestBody Account account) throws Exception {
        try {
            logger.info("========================================================= " + account.getOwner());
            accountRepository.save(account);
            logger.info("accounts-service byNumber() found: " + account);
        } catch (Exception e) {
            throw e;
        }
    }

在测试用例中,如果有异常,如何回滚数据?
在WebAccountsService.java(应用程序1)中,在调用API Web应用程序2之后,发生了异常。如何通过调用属于application2的api create-accounts回滚结果(将删除从先前进程创建的记录)?
你也试着跑(https://github.com/haipv2/microservices-demo-master
感谢!!!

0 个答案:

没有答案