How SpringBoot uses GTS
This article mainly shows you "SpringBoot how to use GTS", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "SpringBoot how to use GTS" this article.
1. Dependent class libraries txc-client.jar, txt-client-spring-cloud-2.0.1.jar
two。 Use TxcDataSource proxy source data source [Note: dbcp2.BasicDataSource does not support it, you can use DruidDataSource]
3. Add automatic configuration class file
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"}) / / define declarative transactions. To make the transaction annotation aware, define 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. Add GTS configuration
Spring: boot: txc: txcAppName: demo txcServerGroup: txc_test_public.1129361738553704.QD # Private transaction packet for public network testing url: https://test-cs-gts.aliyuncs.com # Public network test url accessKey: xxx # provide secretKey: xxxx # for non-testing
5. When sending a rest request, request to add header (TXC_XID,BEGIN_COUNT,COMMIT_COUNT)
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 entity = new HttpEntity ("parameters", requestHeaders) String restUrl = String.format ("% s/api/scoreService/testTxc", "http://10.0.0.5:8762"); ResponseEntity restData = restTemplate.exchange (restUrl, HttpMethod.GET, entity, String.class); return restData.toString ();}
6. Initiating a global transaction using the annotation @ TxcTransaction
The above is all the content of the article "how SpringBoot uses GTS". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!