seata1.1.0+springcloud⾃定义配置,从springboot配置⽂件读
取
1. 版本:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
<version>2.2.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.1.0</version>
</dependency>
<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
2.⾃定义seata-starter
犯罪电影大全⽂件结构如下:
4399.小游戏3.上源码:
CqlivingSeataConfiguration.java
package org.cqliving.framework.cloud.seata;
import java.util.Set;
import urrent.ConcurrentHashMap;
import urrent.ConcurrentMap;
import org.apachemons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import t.ApplicationContext;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ObjectUtils;
import fig.AbstractConfiguration;
import fig.ConfigurationChangeListener;
import io.seata.spring.boot.autoconfigure.util.SpringUtils;
public class CqlivingSeataConfiguration extends AbstractConfiguration{
private static final Logger logger = Logger(CqlivingSeataConfiguration.class);
private static final String CONFIG_TYPE = "CqlivingSeataConfig";
private static final String PREFIX = "seata.";
private static volatile CqlivingSeataConfiguration instance;
private static final ConcurrentMap<String, Set<ConfigurationChangeListener>> LISTENER_SERVICE_MAP = new ConcurrentHashMap<>();
public static CqlivingSeataConfiguration getInstance() {
if (null == instance) {
synchronized (CqlivingSeataConfiguration.class) {
if (null == instance) {
instance = new CqlivingSeataConfiguration();
}
}
}
return instance;
}
private CqlivingSeataConfiguration() {}
private static final char LINE = '-';
@Override
陈坤儿子生母何琳public String getConfig(String dataId, String defaultValue, long timeoutMills) {
dataId = dataId.startsWith(PREFIX)?dataId: at(formatCamel(dataId, LINE));
String value;
// 从系统变量获取
if ((value = getConfigFromSysPro(dataId)) != null) {
return value;
}
// 从配置⽂件获取
ApplicationContext applicationContext = ApplicationContext();
if (null == applicationContext) {
return defaultValue;
}
value = Environment().getProperty(dataId);
if(null == value) {
<("properties null [{}] ", dataId);
return defaultValue;
}
return value;
}
private String formatCamel(String param, char sign) {
if (ObjectUtils.isEmpty(param)) {
return StringUtils.EMPTY;
}
int len = param.length();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (Character.isUpperCase(c)) {
if (Character.isUpperCase(c)) {
sb.append(sign);
sb.LowerCase(c));
} else {
sb.append(c);
}
}
String();
读取配置文件失败}
@Override
非诚勿扰邢星微博public boolean putConfig(String dataId, String content, long timeoutMills) {
throw new UnsupportedOperationException();
}
@Override
public boolean putConfigIfAbsent(String dataId, String content, long timeoutMills) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeConfig(String dataId, long timeoutMills) {
throw new UnsupportedOperationException();
}
@Override
public void addConfigListener(String dataId, ConfigurationChangeListener listener) {
if (null == dataId || null == listener) {
return;
}
LISTENER_SERVICE_MAP.putIfAbsent(dataId, wKeySet()); LISTENER_(dataId).add(listener);
}
@Override
public void removeConfigListener(String dataId, ConfigurationChangeListener listener) { if (!LISTENER_ainsKey(dataId) || listener == null) {
return;
}
LISTENER_(dataId).remove(listener);
}
@Override
public Set<ConfigurationChangeListener> getConfigListeners(String dataId) {
return LISTENER_(dataId);
}
@Override
public String getTypeName() {
return CONFIG_TYPE;
}
本兮几岁}
CqlivingSeataConfigurationProvider.java
package org.cqliving.framework.cloud.seata;
import flect.Method;
import lib.proxy.Enhancer;
import lib.proxy.MethodInterceptor;
import lib.proxy.MethodProxy;
import t.ApplicationContext;
import io.seatamon.loader.LoadLevel;
import fig.Configuration;
import fig.ConfigurationProvider;
import fig.ExtConfigurationProvider;
import io.seata.spring.boot.autoconfigure.util.SpringUtils;
@LoadLevel(name = "CqlivingSeataConfig", order = 1)
public class CqlivingSeataConfigurationProvider implements ExtConfigurationProvider, ConfigurationProvider {
private static final String GET ="get";
private static final String PREFIX ="seata.";
@Override
public Configuration provide(Configuration originalConfiguration) {
ApplicationContext applicationContext = ApplicationContext();
return (Class(), new MethodInterceptor() {
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
throws Throwable {
if (Name().startsWith(GET) && args.length > 0) {
Object result = null;
String rawDataId = (String)args[0];
if (args.length == 1) {
result = Environment().at(rawDataId));
}
if (null != result) {
return result;
}
}
return method.invoke(originalConfiguration, args);
}
});
}
@Override
public Configuration provide() {
Instance();
}
}
CustomProperties.java
@Component
@ConfigurationProperties(prefix = CustomProperties.PREFIX)
@Data
public class CustomProperties {
public static final String PREFIX = "fig.custom";
private String name = "CqlivingSeataConfig";
}
SeataAutoConfiguration.java
@Configuration
@PropertySource("classpath:seata.properties")
@EnableAutoDataSourceProxy
@EnableSeataSpringConfig
@EnableConfigurationProperties({CustomProperties.class})
public class SeataAutoConfiguration {
}
fig.ConfigurationProvider
org.cqliving.framework.cloud.seata.CqlivingSeataConfigurationProvider
fig.ExtConfigurationProvider
org.cqliving.framework.cloud.seata.CqlivingSeataConfigurationProvider
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.cqliving.framework.cloud.seata.SeataAutoConfiguration seata.properties
发布评论