mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
[enhance issue #1492 ]Improved nacos configuration parsing, based on PropertySourceLoader---check
This commit is contained in:
parent
c07701d98e
commit
5135916175
@ -65,9 +65,11 @@ class UserConfig {
|
|||||||
private int age;
|
private int age;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private String hr;
|
private String hr;
|
||||||
|
|
||||||
private Map<String, Object> map;
|
private Map<String, Object> map;
|
||||||
|
|
||||||
private List<User> users;
|
private List<User> users;
|
||||||
|
|
||||||
public int getAge() {
|
public int getAge() {
|
||||||
@ -112,18 +114,16 @@ class UserConfig {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "UserConfig{" +
|
return "UserConfig{" + "age=" + age + ", name='" + name + '\'' + ", map=" + map
|
||||||
"age=" + age +
|
+ ", hr='" + hr + '\'' + ", users=" + JSON.toJSONString(users) + '}';
|
||||||
", name='" + name + '\'' +
|
|
||||||
", map=" + map +
|
|
||||||
", hr='" + hr + '\'' +
|
|
||||||
", users=" + JSON.toJSONString(users) +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class User {
|
public static class User {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private String hr;
|
private String hr;
|
||||||
|
|
||||||
private String avg;
|
private String avg;
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@ -152,13 +152,11 @@ class UserConfig {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "User{" +
|
return "User{" + "name='" + name + '\'' + ", hr=" + hr + ", avg=" + avg + '}';
|
||||||
"name='" + name + '\'' +
|
|
||||||
", hr=" + hr +
|
|
||||||
", avg=" + avg +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@ -215,7 +213,6 @@ class SampleRunner implements ApplicationRunner {
|
|||||||
@RefreshScope
|
@RefreshScope
|
||||||
class SampleController {
|
class SampleController {
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
UserConfig userConfig;
|
UserConfig userConfig;
|
||||||
|
|
||||||
@ -238,7 +235,7 @@ class SampleController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/get/{name}")
|
@RequestMapping("/get/{name}")
|
||||||
public String getValue(@PathVariable(value = "name")String name) {
|
public String getValue(@PathVariable String name) {
|
||||||
return String.valueOf(environment.getProperty(name));
|
return String.valueOf(environment.getProperty(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,6 @@ import com.alibaba.cloud.examples.service.EchoService;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author lengleng
|
* @author lengleng
|
||||||
* @date 2019-08-01
|
|
||||||
* <p>
|
* <p>
|
||||||
* sentinel 降级处理
|
* sentinel 降级处理
|
||||||
*/
|
*/
|
||||||
@ -35,7 +34,7 @@ public class EchoServiceFallback implements EchoService {
|
|||||||
/**
|
/**
|
||||||
* 调用服务提供方的输出接口.
|
* 调用服务提供方的输出接口.
|
||||||
* @param str 用户输入
|
* @param str 用户输入
|
||||||
* @return
|
* @return String
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String echo(String str) {
|
public String echo(String str) {
|
||||||
|
@ -22,7 +22,6 @@ import org.springframework.stereotype.Component;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author lengleng
|
* @author lengleng
|
||||||
* @date 2019-08-01
|
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class EchoServiceFallbackFactory implements FallbackFactory<EchoServiceFallback> {
|
public class EchoServiceFallbackFactory implements FallbackFactory<EchoServiceFallback> {
|
||||||
|
@ -24,7 +24,6 @@ import org.springframework.web.bind.annotation.PathVariable;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author lengleng
|
* @author lengleng
|
||||||
* @date 2019-08-01
|
|
||||||
* <p>
|
* <p>
|
||||||
* example feign client
|
* example feign client
|
||||||
*/
|
*/
|
||||||
|
@ -22,7 +22,6 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author lengleng
|
* @author lengleng
|
||||||
* @date 2019-08-01
|
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
public class EchoController {
|
public class EchoController {
|
||||||
|
@ -28,71 +28,71 @@ import org.springframework.util.StringUtils;
|
|||||||
*/
|
*/
|
||||||
public class ConsulDataSourceProperties extends AbstractDataSourceProperties {
|
public class ConsulDataSourceProperties extends AbstractDataSourceProperties {
|
||||||
|
|
||||||
public ConsulDataSourceProperties(){
|
public ConsulDataSourceProperties() {
|
||||||
super(ConsulDataSourceFactoryBean.class.getName());
|
super(ConsulDataSourceFactoryBean.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* consul server host.
|
* consul server host.
|
||||||
*/
|
*/
|
||||||
private String host;
|
private String host;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* consul server port.
|
* consul server port.
|
||||||
*/
|
*/
|
||||||
private int port=8500;
|
private int port = 8500;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* data key in Redis.
|
* data key in Redis.
|
||||||
*/
|
*/
|
||||||
private String ruleKey;
|
private String ruleKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request of query will hang until timeout (in second) or get updated value.
|
* Request of query will hang until timeout (in second) or get updated value.
|
||||||
*/
|
*/
|
||||||
private int waitTimeoutInSecond = 1;
|
private int waitTimeoutInSecond = 1;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void preCheck(String dataSourceName) {
|
public void preCheck(String dataSourceName) {
|
||||||
if(StringUtils.isEmpty(host)){
|
if (StringUtils.isEmpty(host)) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException("ConsulDataSource server-host is empty");
|
||||||
"ConsulDataSource server-host is empty");
|
}
|
||||||
}
|
if (StringUtils.isEmpty(ruleKey)) {
|
||||||
if(StringUtils.isEmpty(ruleKey)){
|
throw new IllegalArgumentException(
|
||||||
throw new IllegalArgumentException(
|
"ConsulDataSource ruleKey can not be empty");
|
||||||
"ConsulDataSource ruleKey can not be empty");
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public String getHost() {
|
public String getHost() {
|
||||||
return host;
|
return host;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHost(String host) {
|
public void setHost(String host) {
|
||||||
this.host = host;
|
this.host = host;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPort() {
|
public int getPort() {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPort(int port) {
|
public void setPort(int port) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRuleKey() {
|
public String getRuleKey() {
|
||||||
return ruleKey;
|
return ruleKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRuleKey(String ruleKey) {
|
public void setRuleKey(String ruleKey) {
|
||||||
this.ruleKey = ruleKey;
|
this.ruleKey = ruleKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWaitTimeoutInSecond() {
|
public int getWaitTimeoutInSecond() {
|
||||||
return waitTimeoutInSecond;
|
return waitTimeoutInSecond;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setWaitTimeoutInSecond(int waitTimeoutInSecond) {
|
||||||
|
this.waitTimeoutInSecond = waitTimeoutInSecond;
|
||||||
|
}
|
||||||
|
|
||||||
public void setWaitTimeoutInSecond(int waitTimeoutInSecond) {
|
|
||||||
this.waitTimeoutInSecond = waitTimeoutInSecond;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -29,68 +29,64 @@ import org.springframework.beans.factory.FactoryBean;
|
|||||||
*/
|
*/
|
||||||
public class ConsulDataSourceFactoryBean implements FactoryBean<ConsulDataSource> {
|
public class ConsulDataSourceFactoryBean implements FactoryBean<ConsulDataSource> {
|
||||||
|
|
||||||
private String host;
|
private String host;
|
||||||
|
|
||||||
private int port;
|
private int port;
|
||||||
|
|
||||||
private String ruleKey;
|
private String ruleKey;
|
||||||
|
|
||||||
private int waitTimeoutInSecond;
|
private int waitTimeoutInSecond;
|
||||||
|
|
||||||
private Converter converter;
|
private Converter converter;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConsulDataSource getObject() throws Exception {
|
public ConsulDataSource getObject() throws Exception {
|
||||||
return new ConsulDataSource(
|
return new ConsulDataSource(host, port, ruleKey, waitTimeoutInSecond, converter);
|
||||||
host,
|
}
|
||||||
port,
|
|
||||||
ruleKey,
|
|
||||||
waitTimeoutInSecond,
|
|
||||||
converter);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class<?> getObjectType() {
|
public Class<?> getObjectType() {
|
||||||
return ConsulDataSource.class;
|
return ConsulDataSource.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHost() {
|
public String getHost() {
|
||||||
return host;
|
return host;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHost(String host) {
|
public void setHost(String host) {
|
||||||
this.host = host;
|
this.host = host;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPort() {
|
public int getPort() {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPort(int port) {
|
public void setPort(int port) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRuleKey() {
|
public String getRuleKey() {
|
||||||
return ruleKey;
|
return ruleKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRuleKey(String ruleKey) {
|
public void setRuleKey(String ruleKey) {
|
||||||
this.ruleKey = ruleKey;
|
this.ruleKey = ruleKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWaitTimeoutInSecond() {
|
public int getWaitTimeoutInSecond() {
|
||||||
return waitTimeoutInSecond;
|
return waitTimeoutInSecond;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setWaitTimeoutInSecond(int waitTimeoutInSecond) {
|
public void setWaitTimeoutInSecond(int waitTimeoutInSecond) {
|
||||||
this.waitTimeoutInSecond = waitTimeoutInSecond;
|
this.waitTimeoutInSecond = waitTimeoutInSecond;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Converter getConverter() {
|
public Converter getConverter() {
|
||||||
return converter;
|
return converter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setConverter(Converter converter) {
|
||||||
|
this.converter = converter;
|
||||||
|
}
|
||||||
|
|
||||||
public void setConverter(Converter converter) {
|
|
||||||
this.converter = converter;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ import com.alibaba.cloud.nacos.NacosPropertySourceRepository;
|
|||||||
import com.alibaba.cloud.nacos.parser.NacosDataParserHandler;
|
import com.alibaba.cloud.nacos.parser.NacosDataParserHandler;
|
||||||
import com.alibaba.nacos.api.config.ConfigService;
|
import com.alibaba.nacos.api.config.ConfigService;
|
||||||
import com.alibaba.nacos.api.exception.NacosException;
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -99,10 +98,10 @@ public class NacosPropertySourceBuilder {
|
|||||||
fileExtension);
|
fileExtension);
|
||||||
}
|
}
|
||||||
catch (NacosException e) {
|
catch (NacosException e) {
|
||||||
log.error("get data from Nacos error,dataId:{}, ", dataId, e);
|
log.error("get data from Nacos error,dataId:{} ", dataId, e);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
log.error("parse data from Nacos error,dataId:{},data:{},", dataId, data, e);
|
log.error("parse data from Nacos error,dataId:{},data:{}", dataId, data, e);
|
||||||
}
|
}
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
@ -50,9 +50,10 @@ public abstract class AbstractPropertySourceLoader implements PropertySourceLoad
|
|||||||
/**
|
/**
|
||||||
* Prevent interference with other loaders.Nacos-specific loader, unless the reload
|
* Prevent interference with other loaders.Nacos-specific loader, unless the reload
|
||||||
* changes it.
|
* changes it.
|
||||||
* @param name
|
* @param name the root name of the property source. If multiple documents are loaded
|
||||||
* @param resource
|
* an additional suffix should be added to the name for each source loaded.
|
||||||
* @return
|
* @param resource the resource to load
|
||||||
|
* @return if the resource can be loaded
|
||||||
*/
|
*/
|
||||||
protected boolean canLoad(String name, Resource resource) {
|
protected boolean canLoad(String name, Resource resource) {
|
||||||
return resource instanceof NacosByteArrayResource;
|
return resource instanceof NacosByteArrayResource;
|
||||||
|
@ -19,7 +19,7 @@ package com.alibaba.cloud.nacos.parser;
|
|||||||
import org.springframework.core.io.ByteArrayResource;
|
import org.springframework.core.io.ByteArrayResource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Nacos-specific resource
|
* Nacos-specific resource.
|
||||||
*
|
*
|
||||||
* @author zkz
|
* @author zkz
|
||||||
*/
|
*/
|
||||||
|
@ -28,11 +28,9 @@ import java.util.stream.Collectors;
|
|||||||
import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
||||||
import org.springframework.boot.env.PropertySourceLoader;
|
import org.springframework.boot.env.PropertySourceLoader;
|
||||||
import org.springframework.core.env.EnumerablePropertySource;
|
import org.springframework.core.env.EnumerablePropertySource;
|
||||||
import org.springframework.core.env.MapPropertySource;
|
|
||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,10 +38,19 @@ import org.springframework.util.StringUtils;
|
|||||||
*/
|
*/
|
||||||
public final class NacosDataParserHandler {
|
public final class NacosDataParserHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* symbol: dot.
|
||||||
|
*/
|
||||||
public static final String DOT = ".";
|
public static final String DOT = ".";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* constant.
|
||||||
|
*/
|
||||||
public static final String VALUE = "value";
|
public static final String VALUE = "value";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* default extension.
|
||||||
|
*/
|
||||||
public static final String DEFAULT_EXTENSION = "properties";
|
public static final String DEFAULT_EXTENSION = "properties";
|
||||||
|
|
||||||
private static List<PropertySourceLoader> propertySourceLoaders;
|
private static List<PropertySourceLoader> propertySourceLoaders;
|
||||||
@ -101,6 +108,12 @@ public final class NacosDataParserHandler {
|
|||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check the current extension can be processed.
|
||||||
|
* @param loader the propertySourceLoader
|
||||||
|
* @param extension file extension
|
||||||
|
* @return if can match extension
|
||||||
|
*/
|
||||||
private boolean canLoadFileExtension(PropertySourceLoader loader, String extension) {
|
private boolean canLoadFileExtension(PropertySourceLoader loader, String extension) {
|
||||||
return Arrays.stream(loader.getFileExtensions())
|
return Arrays.stream(loader.getFileExtensions())
|
||||||
.anyMatch((fileExtension) -> StringUtils.endsWithIgnoreCase(extension,
|
.anyMatch((fileExtension) -> StringUtils.endsWithIgnoreCase(extension,
|
||||||
|
@ -34,7 +34,7 @@ import static com.alibaba.cloud.nacos.parser.NacosDataParserHandler.VALUE;
|
|||||||
/**
|
/**
|
||||||
* @author zkz
|
* @author zkz
|
||||||
*/
|
*/
|
||||||
public class JsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
public class NacosJsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the file extensions that the loader supports (excluding the '.').
|
* Returns the file extensions that the loader supports (excluding the '.').
|
||||||
@ -46,9 +46,14 @@ public class JsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param name
|
* Load the resource into one or more property sources. Implementations may either
|
||||||
* @param resource
|
* return a list containing a single source, or in the case of a multi-document format
|
||||||
* @return
|
* such as yaml a source for each document in the resource.
|
||||||
|
* @param name the root name of the property source. If multiple documents are loaded
|
||||||
|
* an additional suffix should be added to the name for each source loaded.
|
||||||
|
* @param resource the resource to load
|
||||||
|
* @return a list property sources
|
||||||
|
* @throws IOException if the source cannot be loaded
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected List<PropertySource<?>> doLoad(String name, Resource resource)
|
protected List<PropertySource<?>> doLoad(String name, Resource resource)
|
||||||
@ -57,9 +62,9 @@ public class JsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
|||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
Map<String, Object> nacosDataMap = mapper.readValue(resource.getInputStream(),
|
Map<String, Object> nacosDataMap = mapper.readValue(resource.getInputStream(),
|
||||||
LinkedHashMap.class);
|
LinkedHashMap.class);
|
||||||
flattenedMap(result, this.reloadMap(nacosDataMap), null);
|
flattenedMap(result, nacosDataMap, null);
|
||||||
return Collections.singletonList(
|
return Collections.singletonList(
|
||||||
new OriginTrackedMapPropertySource(name, nacosDataMap, true));
|
new OriginTrackedMapPropertySource(name, this.reloadMap(result), true));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,168 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2018 the original author or authors.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* https://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.alibaba.cloud.nacos.parser;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.NamedNodeMap;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
|
import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
||||||
|
import org.springframework.boot.env.PropertiesPropertySourceLoader;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.env.PropertySource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parsing for XML requires overwriting the default
|
||||||
|
* {@link PropertiesPropertySourceLoader}, because it internally rigorously validates
|
||||||
|
* ({@conde DOCTYPE}) THE XML in a way that makes it difficult to customize the
|
||||||
|
* configuration; at finally, make sure it's in the first place.
|
||||||
|
*
|
||||||
|
* @author zkz
|
||||||
|
*/
|
||||||
|
public class NacosXmlPropertySourceLoader extends AbstractPropertySourceLoader
|
||||||
|
implements Ordered {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the order value of this object.
|
||||||
|
* <p>
|
||||||
|
* Higher values are interpreted as lower priority. As a consequence, the object with
|
||||||
|
* the lowest value has the highest priority (somewhat analogous to Servlet
|
||||||
|
* {@code load-on-startup} values).
|
||||||
|
* <p>
|
||||||
|
* Same order values will result in arbitrary sort positions for the affected objects.
|
||||||
|
* @return the order value
|
||||||
|
* @see #HIGHEST_PRECEDENCE
|
||||||
|
* @see #LOWEST_PRECEDENCE
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Integer.MIN_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the file extensions that the loader supports (excluding the '.').
|
||||||
|
* @return the file extensions
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String[] getFileExtensions() {
|
||||||
|
return new String[] { "xml" };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the resource into one or more property sources. Implementations may either
|
||||||
|
* return a list containing a single source, or in the case of a multi-document format
|
||||||
|
* such as yaml a source for each document in the resource.
|
||||||
|
* @param name the root name of the property source. If multiple documents are loaded
|
||||||
|
* an additional suffix should be added to the name for each source loaded.
|
||||||
|
* @param resource the resource to load
|
||||||
|
* @return a list property sources
|
||||||
|
* @throws IOException if the source cannot be loaded
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected List<PropertySource<?>> doLoad(String name, Resource resource)
|
||||||
|
throws IOException {
|
||||||
|
Map<String, Object> nacosDataMap = parseXml2Map(resource);
|
||||||
|
return Collections.singletonList(
|
||||||
|
new OriginTrackedMapPropertySource(name, nacosDataMap, true));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> parseXml2Map(Resource resource) throws IOException {
|
||||||
|
Map<String, Object> map = new LinkedHashMap<>(32);
|
||||||
|
try {
|
||||||
|
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance()
|
||||||
|
.newDocumentBuilder();
|
||||||
|
Document document = documentBuilder.parse(resource.getInputStream());
|
||||||
|
if (null == document) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
parseNodeList(document.getChildNodes(), map, "");
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
throw new IOException("The xml content parse error.", e.getCause());
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parseNodeList(NodeList nodeList, Map<String, Object> map,
|
||||||
|
String parentKey) {
|
||||||
|
if (nodeList == null || nodeList.getLength() < 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
parentKey = parentKey == null ? "" : parentKey;
|
||||||
|
for (int i = 0; i < nodeList.getLength(); i++) {
|
||||||
|
Node node = nodeList.item(i);
|
||||||
|
String value = node.getNodeValue();
|
||||||
|
value = value == null ? "" : value.trim();
|
||||||
|
String name = node.getNodeName();
|
||||||
|
name = name == null ? "" : name.trim();
|
||||||
|
|
||||||
|
if (StringUtils.isEmpty(name)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String key = StringUtils.isEmpty(parentKey) ? name
|
||||||
|
: parentKey + NacosDataParserHandler.DOT + name;
|
||||||
|
NamedNodeMap nodeMap = node.getAttributes();
|
||||||
|
parseNodeAttr(nodeMap, map, key);
|
||||||
|
if (node.getNodeType() == Node.ELEMENT_NODE && node.hasChildNodes()) {
|
||||||
|
parseNodeList(node.getChildNodes(), map, key);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (value.length() < 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
map.put(parentKey, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parseNodeAttr(NamedNodeMap nodeMap, Map<String, Object> map,
|
||||||
|
String parentKey) {
|
||||||
|
if (null == nodeMap || nodeMap.getLength() < 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < nodeMap.getLength(); i++) {
|
||||||
|
Node node = nodeMap.item(i);
|
||||||
|
if (null == node) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
|
||||||
|
if (StringUtils.isEmpty(node.getNodeName())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (StringUtils.isEmpty(node.getNodeValue())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
map.put(String.join(NacosDataParserHandler.DOT, parentKey,
|
||||||
|
node.getNodeName()), node.getNodeValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -6,4 +6,5 @@ com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration
|
|||||||
org.springframework.boot.diagnostics.FailureAnalyzer=\
|
org.springframework.boot.diagnostics.FailureAnalyzer=\
|
||||||
com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureAnalyzer
|
com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureAnalyzer
|
||||||
org.springframework.boot.env.PropertySourceLoader=\
|
org.springframework.boot.env.PropertySourceLoader=\
|
||||||
com.alibaba.cloud.nacos.parser.JsonPropertySourceLoader
|
com.alibaba.cloud.nacos.parser.NacosJsonPropertySourceLoader,\
|
||||||
|
com.alibaba.cloud.nacos.parser.NacosXmlPropertySourceLoader
|
@ -50,7 +50,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
@RunWith(PowerMockRunner.class)
|
@RunWith(PowerMockRunner.class)
|
||||||
@PowerMockIgnore("javax.management.*")
|
@PowerMockIgnore({ "javax.management.*", "javax.xml.parsers.*",
|
||||||
|
"com.sun.org.apache.xerces.internal.jaxp.*" })
|
||||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||||
@PrepareForTest({ NacosConfigService.class })
|
@PrepareForTest({ NacosConfigService.class })
|
||||||
@SpringBootTest(classes = NacosConfigurationNoSuffixTest.TestConfig.class, properties = {
|
@SpringBootTest(classes = NacosConfigurationNoSuffixTest.TestConfig.class, properties = {
|
||||||
|
@ -49,7 +49,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
|||||||
* @author zkz
|
* @author zkz
|
||||||
*/
|
*/
|
||||||
@RunWith(PowerMockRunner.class)
|
@RunWith(PowerMockRunner.class)
|
||||||
@PowerMockIgnore("javax.management.*")
|
@PowerMockIgnore({ "javax.management.*", "javax.xml.parsers.*",
|
||||||
|
"com.sun.org.apache.xerces.internal.jaxp.*", "org.w3c.dom.*" })
|
||||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||||
@PrepareForTest({ NacosConfigService.class })
|
@PrepareForTest({ NacosConfigService.class })
|
||||||
@SpringBootTest(classes = NacosConfigurationXmlJsonTest.TestConfig.class, properties = {
|
@SpringBootTest(classes = NacosConfigurationXmlJsonTest.TestConfig.class, properties = {
|
||||||
@ -83,13 +84,15 @@ public class NacosConfigurationXmlJsonTest {
|
|||||||
throws Throwable {
|
throws Throwable {
|
||||||
|
|
||||||
if ("xmlApp.xml".equals(args[0]) && "test-group".equals(args[1])) {
|
if ("xmlApp.xml".equals(args[0]) && "test-group".equals(args[1])) {
|
||||||
return "<top>\n" + " <first>one</first>\n"
|
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<top>\n"
|
||||||
|
+ " <first>one</first>\n"
|
||||||
+ " <sencond value=\"two\">\n"
|
+ " <sencond value=\"two\">\n"
|
||||||
+ " <third>three</third>\n" + " </sencond>\n"
|
+ " <third>three</third>\n" + " </sencond>\n"
|
||||||
+ "</top>";
|
+ "</top>";
|
||||||
}
|
}
|
||||||
if ("test-name.xml".equals(args[0]) && "test-group".equals(args[1])) {
|
if ("test-name.xml".equals(args[0]) && "test-group".equals(args[1])) {
|
||||||
return "<Server port=\"8005\" shutdown=\"SHUTDOWN\"> \n"
|
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
|
||||||
|
+ "<Server port=\"8005\" shutdown=\"SHUTDOWN\"> \n"
|
||||||
+ " <Service name=\"Catalina\"> \n"
|
+ " <Service name=\"Catalina\"> \n"
|
||||||
+ " <Connector value=\"第二个连接器\"> \n"
|
+ " <Connector value=\"第二个连接器\"> \n"
|
||||||
+ " <open>开启服务</open> \n"
|
+ " <open>开启服务</open> \n"
|
||||||
@ -108,7 +111,8 @@ public class NacosConfigurationXmlJsonTest {
|
|||||||
|
|
||||||
if ("test-name-dev.xml".equals(args[0])
|
if ("test-name-dev.xml".equals(args[0])
|
||||||
&& "test-group".equals(args[1])) {
|
&& "test-group".equals(args[1])) {
|
||||||
return "<application android:label=\"@string/app_name\" android:icon=\"@drawable/osg\">\n"
|
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
|
||||||
|
+ "<application android:label=\"@string/app_name\" android:icon=\"@drawable/osg\">\n"
|
||||||
+ " <activity android:name=\".osgViewer\"\n"
|
+ " <activity android:name=\".osgViewer\"\n"
|
||||||
+ " android:label=\"@string/app_name\" android:screenOrientation=\"landscape\">\n"
|
+ " android:label=\"@string/app_name\" android:screenOrientation=\"landscape\">\n"
|
||||||
+ " <intent-filter>\n"
|
+ " <intent-filter>\n"
|
||||||
|
@ -44,7 +44,9 @@ import org.springframework.web.context.request.RequestContextHolder;
|
|||||||
*/
|
*/
|
||||||
public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
|
public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(SeataHystrixConcurrencyStrategy.class);
|
private final Logger logger = LoggerFactory
|
||||||
|
.getLogger(SeataHystrixConcurrencyStrategy.class);
|
||||||
|
|
||||||
private HystrixConcurrencyStrategy delegate;
|
private HystrixConcurrencyStrategy delegate;
|
||||||
|
|
||||||
public SeataHystrixConcurrencyStrategy() {
|
public SeataHystrixConcurrencyStrategy() {
|
||||||
@ -53,45 +55,54 @@ public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy
|
|||||||
if (this.delegate instanceof SeataHystrixConcurrencyStrategy) {
|
if (this.delegate instanceof SeataHystrixConcurrencyStrategy) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance().getCommandExecutionHook();
|
HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins
|
||||||
HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier();
|
.getInstance().getCommandExecutionHook();
|
||||||
HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance().getMetricsPublisher();
|
HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance()
|
||||||
HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance().getPropertiesStrategy();
|
.getEventNotifier();
|
||||||
logCurrentStateOfHystrixPlugins(eventNotifier, metricsPublisher, propertiesStrategy);
|
HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance()
|
||||||
|
.getMetricsPublisher();
|
||||||
|
HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance()
|
||||||
|
.getPropertiesStrategy();
|
||||||
|
logCurrentStateOfHystrixPlugins(eventNotifier, metricsPublisher,
|
||||||
|
propertiesStrategy);
|
||||||
HystrixPlugins.reset();
|
HystrixPlugins.reset();
|
||||||
HystrixPlugins.getInstance().registerConcurrencyStrategy(this);
|
HystrixPlugins.getInstance().registerConcurrencyStrategy(this);
|
||||||
HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook);
|
HystrixPlugins.getInstance()
|
||||||
|
.registerCommandExecutionHook(commandExecutionHook);
|
||||||
HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
|
HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
|
||||||
HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);
|
HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);
|
||||||
HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);
|
HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);
|
||||||
} catch (Exception ex) {
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
logger.error("Failed to register Seata Hystrix Concurrency Strategy", ex);
|
logger.error("Failed to register Seata Hystrix Concurrency Strategy", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logCurrentStateOfHystrixPlugins(HystrixEventNotifier eventNotifier,
|
private void logCurrentStateOfHystrixPlugins(HystrixEventNotifier eventNotifier,
|
||||||
HystrixMetricsPublisher metricsPublisher,
|
HystrixMetricsPublisher metricsPublisher,
|
||||||
HystrixPropertiesStrategy propertiesStrategy) {
|
HystrixPropertiesStrategy propertiesStrategy) {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug("Current Hystrix plugins configuration is [" + "concurrencyStrategy [" + this.delegate + "],"
|
logger.debug("Current Hystrix plugins configuration is ["
|
||||||
+ "eventNotifier [" + eventNotifier + "]," + "metricPublisher [" + metricsPublisher + "],"
|
+ "concurrencyStrategy [" + this.delegate + "]," + "eventNotifier ["
|
||||||
+ "propertiesStrategy [" + propertiesStrategy + "]," + "]");
|
+ eventNotifier + "]," + "metricPublisher [" + metricsPublisher + "],"
|
||||||
|
+ "propertiesStrategy [" + propertiesStrategy + "]," + "]");
|
||||||
logger.debug("Registering Seata Hystrix Concurrency Strategy.");
|
logger.debug("Registering Seata Hystrix Concurrency Strategy.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey, HystrixProperty<Integer> corePoolSize,
|
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
|
||||||
HystrixProperty<Integer> maximumPoolSize,
|
HystrixProperty<Integer> corePoolSize,
|
||||||
HystrixProperty<Integer> keepAliveTime, TimeUnit unit,
|
HystrixProperty<Integer> maximumPoolSize,
|
||||||
BlockingQueue<Runnable> workQueue) {
|
HystrixProperty<Integer> keepAliveTime, TimeUnit unit,
|
||||||
return this.delegate.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize, keepAliveTime, unit,
|
BlockingQueue<Runnable> workQueue) {
|
||||||
workQueue);
|
return this.delegate.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize,
|
||||||
|
keepAliveTime, unit, workQueue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
|
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
|
||||||
HystrixThreadPoolProperties threadPoolProperties) {
|
HystrixThreadPoolProperties threadPoolProperties) {
|
||||||
return this.delegate.getThreadPool(threadPoolKey, threadPoolProperties);
|
return this.delegate.getThreadPool(threadPoolKey, threadPoolProperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,7 +112,8 @@ public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> HystrixRequestVariable<T> getRequestVariable(HystrixRequestVariableLifecycle<T> rv) {
|
public <T> HystrixRequestVariable<T> getRequestVariable(
|
||||||
|
HystrixRequestVariableLifecycle<T> rv) {
|
||||||
return this.delegate.getRequestVariable(rv);
|
return this.delegate.getRequestVariable(rv);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,14 +126,16 @@ public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy
|
|||||||
Callable<K> wrappedCallable;
|
Callable<K> wrappedCallable;
|
||||||
if (this.delegate != null) {
|
if (this.delegate != null) {
|
||||||
wrappedCallable = this.delegate.wrapCallable(c);
|
wrappedCallable = this.delegate.wrapCallable(c);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
wrappedCallable = c;
|
wrappedCallable = c;
|
||||||
}
|
}
|
||||||
if (wrappedCallable instanceof SeataContextCallable) {
|
if (wrappedCallable instanceof SeataContextCallable) {
|
||||||
return wrappedCallable;
|
return wrappedCallable;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new SeataContextCallable<>(wrappedCallable, RequestContextHolder.getRequestAttributes());
|
return new SeataContextCallable<>(wrappedCallable,
|
||||||
|
RequestContextHolder.getRequestAttributes());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class SeataContextCallable<K> implements Callable<K> {
|
private static class SeataContextCallable<K> implements Callable<K> {
|
||||||
@ -144,7 +158,8 @@ public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy
|
|||||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||||
RootContext.bind(xid);
|
RootContext.bind(xid);
|
||||||
return actual.call();
|
return actual.call();
|
||||||
} finally {
|
}
|
||||||
|
finally {
|
||||||
RootContext.unbind();
|
RootContext.unbind();
|
||||||
RequestContextHolder.resetRequestAttributes();
|
RequestContextHolder.resetRequestAttributes();
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,7 @@ public abstract class AbstractHttpRequestMatcher implements HttpRequestMatcher {
|
|||||||
* <p>
|
* <p>
|
||||||
* For example {@code " || "} for URL patterns or {@code " && "} for param
|
* For example {@code " || "} for URL patterns or {@code " && "} for param
|
||||||
* expressions.
|
* expressions.
|
||||||
|
* @return str
|
||||||
*/
|
*/
|
||||||
protected abstract String getToStringInfix();
|
protected abstract String getToStringInfix();
|
||||||
|
|
||||||
|
@ -169,8 +169,8 @@ public class RocketMQMessageChannelBinder extends
|
|||||||
rocketMQTemplate, destination.getName(), producerGroup,
|
rocketMQTemplate, destination.getName(), producerGroup,
|
||||||
producerProperties.getExtension().getTransactional(),
|
producerProperties.getExtension().getTransactional(),
|
||||||
instrumentationManager, producerProperties,
|
instrumentationManager, producerProperties,
|
||||||
((AbstractMessageChannel) channel).getInterceptors().stream()
|
((AbstractMessageChannel) channel).getInterceptors().stream().filter(
|
||||||
.filter(channelInterceptor -> channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor)
|
channelInterceptor -> channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor)
|
||||||
.map(channelInterceptor -> ((MessageConverterConfigurer.PartitioningInterceptor) channelInterceptor))
|
.map(channelInterceptor -> ((MessageConverterConfigurer.PartitioningInterceptor) channelInterceptor))
|
||||||
.findFirst().orElse(null));
|
.findFirst().orElse(null));
|
||||||
messageHandler.setBeanFactory(this.getApplicationContext().getBeanFactory());
|
messageHandler.setBeanFactory(this.getApplicationContext().getBeanFactory());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user