1. 依賴類庫txc-client.jar, txt-client-spring-cloud-2.0.1.jar
2. 使用TxcDataSource代理源數據源【注意:dbcp2.BasicDataSource不支持,可以使用DruidDataSource】
3. 添加自動配置類文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package com.bodytrack.restapi; import com.taobao.txc.client.aop.TxcTransactionScaner; import com.taobao.txc.client.boot.TxcSpringBootProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; @Configuration @EnableConfigurationProperties ({TxcSpringBootProperties. class }) public class TxcSpringBootAutoConfiguration { @Autowired private TxcSpringBootProperties txcSpringBootProperties; @Autowired private ApplicationContext applicationContext; private static boolean isEmpty(String str) { return str == null || str.length() == 0 ; } @Bean (name = "txcScanner" ) @ConditionalOnProperty ( prefix = "spring.boot.txc" , name = { "txcServerGroup" } ) //定義聲明式事務,要想讓事務annotation感知的話,要在這里定義一下 public TxcTransactionScaner txcTransactionScaner() { String appName = this .txcSpringBootProperties.getTxcAppName() == null ? this .applicationContext.getEnvironment().getProperty( "spring.application.name" ) : this .txcSpringBootProperties.getTxcAppName(); String txServiceGroup = this .txcSpringBootProperties.getTxcServerGroup(); int mode = this .txcSpringBootProperties.getMode() == 0 ? 1 : this .txcSpringBootProperties.getMode(); TxcTransactionScaner txcTransactionScanner = new TxcTransactionScaner(appName, txServiceGroup, mode, this .txcSpringBootProperties.getUrl()); if (!isEmpty( this .txcSpringBootProperties.getAccessKey())) { txcTransactionScanner.setAccessKey( this .txcSpringBootProperties.getAccessKey()); } if (!isEmpty( this .txcSpringBootProperties.getSecretKey())) { txcTransactionScanner.setSecretKey( this .txcSpringBootProperties.getSecretKey()); } return txcTransactionScanner; } } |
4. 添加GTS配置
1
2
3
4
5
6
7
8
|
spring: boot: txc: txcAppName: demo txcServerGroup: txc_test_public. 1129361738553704 .QD #公網測試的專用事務分組 url: https: //test-cs-gts.aliyuncs.com #公網測試url accessKey: xxx #非測試時需提供 secretKey: xxxx #非測試時需提供 |
5. 發送rest請求時,請求添加header(TXC_XID,BEGIN_COUNT,COMMIT_COUNT)
1
2
3
4
5
6
7
8
9
10
|
public String callTestTxc() { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set( "TXC_XID" , String.valueOf(TxcContext.getCurrentXid())); requestHeaders.set( "BEGIN_COUNT" , String.valueOf(TxcContext.getBeginCount())); requestHeaders.set( "COMMIT_COUNT" , String.valueOf(TxcContext.getCommitCount())); HttpEntity<String> entity = new HttpEntity<>( "parameters" , requestHeaders); String restUrl = String.format( "%s/api/scoreService/testTxc" , "http://10.0.0.5:8762" ); ResponseEntity<String> restData = restTemplate.exchange(restUrl, HttpMethod.GET, entity, String. class ); return restData.toString(); } |
6. 發起全局事務使用注解@TxcTransaction
到此這篇關于SpringBoot使用GTS的示例詳解的文章就介紹到這了,更多相關SpringBoot使用GTS內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/jmbkeyes/p/15393437.html