修改tx-common/pom.xml,添加配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
TxManagerConfig.class
/*
* Copyright 2017-2019 CodingApi .
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.codingapi.txlcn.tm.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/**
* Description: Company: CodingApi Date: 2018/11/29
*
* @author ujued
*/
@ConfigurationProperties(prefix = "tx-lcn.manager")
@Component
@Data
public class TxManagerConfig {
@Autowired
private InetUtils inet;
public static final int DTX_PORT = 8070;
/**
* manager host
*/
private String host;
/**
* support port
*/
private int port = DTX_PORT;
/**
* netty heart check time (ms)
*/
private long heartTime = 5 * 60 * 1000;
/**
* 事务处理并发等级
*/
private int concurrentLevel;
/**
* 分布式事务锁超时时间
*/
private long dtxLockTime = -1;
/**
* 分布式事务超时时间(ms)
*/
private long dtxTime = 8 * 1000;
/**
* 后台密码
*/
private String adminKey = "codingapi";
/**
* 是否允许异常回调
*/
private boolean exUrlEnabled = true;
/**
* 异常回调地址
*/
private String exUrl = "/provider/email-to/ujued@qq.com";
/**
* ID序列长度
*/
private int seqLen = 12;
private long machineId;
public String getHost() {
if(StringUtils.isEmpty(this.host)){
return inet.findFirstNonLoopbackHostInfo().getIpAddress();
}
return this.host;
}
private void setMachineId(long machineId) {
this.machineId = machineId;
}
public void applyMachineId(long machineId) {
setMachineId(machineId);
}
public long getDtxLockTime() {
return dtxLockTime == -1 ? dtxTime : dtxLockTime;
}
}
TCAutoConfiguration.java
/*
* Copyright 2017-2019 CodingApi .
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.codingapi.txlcn.tc;
import com.codingapi.txlcn.common.runner.TxLcnApplicationRunner;
import com.codingapi.txlcn.common.util.ApplicationInformation;
import com.codingapi.txlcn.common.util.id.ModIdProvider;
import com.codingapi.txlcn.logger.TxLoggerConfiguration;
import com.codingapi.txlcn.tc.config.EnableDistributedTransaction;
import com.codingapi.txlcn.tracing.TracingAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtils.HostInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;
/**
* Description:
* Date: 1/19/19
*
* @author ujued
* @see EnableDistributedTransaction
*/
@Configuration
@ComponentScan(
excludeFilters = @ComponentScan.Filter(
type = FilterType.ASPECTJ, pattern = "com.codingapi.txlcn.tc.core.transaction.txc..*"
)
)
@Import({TxLoggerConfiguration.class, TracingAutoConfiguration.class})
public class TCAutoConfiguration {
@Autowired
private InetUtils inet;
/**
* All initialization about TX-LCN
*
* @param applicationContext Spring ApplicationContext
* @return TX-LCN custom runner
*/
@Bean
public ApplicationRunner txLcnApplicationRunner(ApplicationContext applicationContext) {
return new TxLcnApplicationRunner(applicationContext);
}
@Bean
@ConditionalOnMissingBean
public ModIdProvider modIdProvider(ConfigurableEnvironment environment,
@Autowired(required = false) ServerProperties serverProperties) {
HostInfo hostInfo = inet.findFirstNonLoopbackHostInfo();
return () -> hostInfo.getIpAddress() + ":" + ApplicationInformation.serverPort(serverProperties);
}
}
TMAutoConfiguration.class
/*
* Copyright 2017-2019 CodingApi .
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.codingapi.txlcn.tm;
import com.codingapi.txlcn.common.runner.TxLcnApplicationRunner;
import com.codingapi.txlcn.common.util.ApplicationInformation;
import com.codingapi.txlcn.common.util.id.ModIdProvider;
import com.codingapi.txlcn.logger.TxLoggerConfiguration;
import com.codingapi.txlcn.tm.config.TxManagerConfig;
import com.codingapi.txlcn.tm.core.storage.FastStorage;
import com.codingapi.txlcn.tm.core.storage.FastStorageProvider;
import com.codingapi.txlcn.tm.core.storage.redis.RedisStorage;
import com.codingapi.txlcn.txmsg.MessageConfiguration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtils.HostInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.client.RestTemplate;
/**
* Description:
* Date: 19-1-24 下午3:37
*
* @author ujued
*/
@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 {
@Autowired
private InetUtils inet;
@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) {
HostInfo hostInfo = inet.findFirstNonLoopbackHostInfo();
return () -> hostInfo.getIpAddress() + ":" + ApplicationInformation.serverPort(serverProperties);
}
}
TMAutoConfiguration.class 和 TCAutoConfiguration.java 添加
@Autowired
private InetUtils inet;
使用HostInfo hostInfo = inet.findFirstNonLoopbackHostInfo();方法获取ip