The server is very simple. It only needs an annotation @EnableTransaction Manager Server configuration on the startup class to turn on the distributed transaction server function.
First, start the entrance.
Annotation @Enable Transaction Manager Server
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Import(value = {TMAutoConfiguration.class}) public @interface EnableTransactionManagerServer { }
The TMAutoConfiguration class is introduced. All functions are in the TMAutoConfiguration class, which is automatically configured by name.
@Configuration @ComponentScan @Import({TxLoggerConfiguration.class, MessageConfiguration.class}) @EnableJpaRepositories("com.codingapi.txlcn.tm.support.db.jpa") @EntityScan("com.codingapi.txlcn.tm.support.db.domain") public class TMAutoConfiguration { @Bean(destroyMethod = "shutdown") public ExecutorService executorService() { int coreSize = Runtime.getRuntime().availableProcessors() * 2; return new ThreadPoolExecutor(coreSize, coreSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()) { @Override public void shutdown() { super.shutdown(); try { this.awaitTermination(10, TimeUnit.MINUTES); } catch (InterruptedException ignored) { } } }; } @Bean public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) { return restTemplateBuilder.build(); } @Bean @ConditionalOnMissingBean public FastStorageProvider fastStorageProvider(RedisTemplate<String, Object> redisTemplate, StringRedisTemplate stringRedisTemplate, TxManagerConfig managerConfig) { return () -> new RedisStorage(redisTemplate, stringRedisTemplate, managerConfig); } @Bean public FastStorage fastStorage(FastStorageProvider fastStorageProvider) { return fastStorageProvider.provide(); } @Bean public TxLcnApplicationRunner txLcnApplicationRunner(ApplicationContext applicationContext) { return new TxLcnApplicationRunner(applicationContext); } @Bean @ConditionalOnMissingBean public ModIdProvider modIdProvider(ConfigurableEnvironment environment, ServerProperties serverProperties) { return () -> ApplicationInformation.modId(environment, serverProperties); } }
Summary: Here are all the configurations
1. Introducing log configuration TxLogger Configuration
2. Introduce message configuration MessageConfiguration class, configure some classes of client-server communication, heartbeat listener
3. Open jpa support to write some transaction information to database Enable jpa Repositories
4. Custom thread pool
5. Construct restTemplate Bean
6. Building FastStorage Provider Bean class to provide fast storage class requires three parameters, where RedisStorage is built by default.
7. Building RedisStorage Based on Fast Storage Provider
8. Build TxLcnApplication Runner Bean to do something after spring boot starts. (Important)
9. Build ModIdProvider Bean to return module identity.
2. MessageConfiguration Configuration Configuration Configuration Class
@Configuration @ComponentScan @Slf4j @Data public class MessageConfiguration { @Bean @ConditionalOnMissingBean @ConfigurationProperties("tx-lcn.message.netty") //rpc configuration such as heartbeat, retry, number of cache locks public RpcConfig rpcConfig() { return new RpcConfig(); } @Bean @ConditionalOnMissingBean //rpc reply class, just print the message public RpcAnswer rpcClientAnswer() { return rpcCmd -> log.info("cmd->{}", rpcCmd); } @Bean @ConditionalOnMissingBean //rpc load balancing strategy, using Random LoadBalance class public RpcLoadBalance rpcLoadBalance() { return new RandomLoadBalance(); } @Bean @ConditionalOnMissingBean //The client initializes the callback class and prints only the log public ClientInitCallBack clientInitCallBack() { return new DefaultClientInitCallback(); } @Bean @ConditionalOnMissingBean //rpc connection listener, default implementation public RpcConnectionListener rpcConnectionListener(){ return new DefaultRpcConnectionListener(); } @Bean @ConditionalOnMissingBean //Heart beat monitor, default empty implementation public HeartbeatListener heartbeatListener(){ return new DefaultHeartbeatListener(); } }