seata1.1.0+springcloud自定义配置,从springboot配置文件读取
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
⽂件结构如下:
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);
}
今年19号台风路径
});
}
@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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。