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 String name;
|
||||
|
||||
private String hr;
|
||||
|
||||
private Map<String, Object> map;
|
||||
|
||||
private List<User> users;
|
||||
|
||||
public int getAge() {
|
||||
@ -112,18 +114,16 @@ class UserConfig {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserConfig{" +
|
||||
"age=" + age +
|
||||
", name='" + name + '\'' +
|
||||
", map=" + map +
|
||||
", hr='" + hr + '\'' +
|
||||
", users=" + JSON.toJSONString(users) +
|
||||
'}';
|
||||
return "UserConfig{" + "age=" + age + ", name='" + name + '\'' + ", map=" + map
|
||||
+ ", hr='" + hr + '\'' + ", users=" + JSON.toJSONString(users) + '}';
|
||||
}
|
||||
|
||||
public static class User {
|
||||
|
||||
private String name;
|
||||
|
||||
private String hr;
|
||||
|
||||
private String avg;
|
||||
|
||||
public String getName() {
|
||||
@ -152,13 +152,11 @@ class UserConfig {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"name='" + name + '\'' +
|
||||
", hr=" + hr +
|
||||
", avg=" + avg +
|
||||
'}';
|
||||
return "User{" + "name='" + name + '\'' + ", hr=" + hr + ", avg=" + avg + '}';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Component
|
||||
@ -215,7 +213,6 @@ class SampleRunner implements ApplicationRunner {
|
||||
@RefreshScope
|
||||
class SampleController {
|
||||
|
||||
|
||||
@Autowired
|
||||
UserConfig userConfig;
|
||||
|
||||
@ -238,7 +235,7 @@ class SampleController {
|
||||
}
|
||||
|
||||
@RequestMapping("/get/{name}")
|
||||
public String getValue(@PathVariable(value = "name")String name) {
|
||||
public String getValue(@PathVariable String name) {
|
||||
return String.valueOf(environment.getProperty(name));
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,6 @@ import com.alibaba.cloud.examples.service.EchoService;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019-08-01
|
||||
* <p>
|
||||
* sentinel 降级处理
|
||||
*/
|
||||
@ -35,7 +34,7 @@ public class EchoServiceFallback implements EchoService {
|
||||
/**
|
||||
* 调用服务提供方的输出接口.
|
||||
* @param str 用户输入
|
||||
* @return
|
||||
* @return String
|
||||
*/
|
||||
@Override
|
||||
public String echo(String str) {
|
||||
|
@ -22,7 +22,6 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019-08-01
|
||||
*/
|
||||
@Component
|
||||
public class EchoServiceFallbackFactory implements FallbackFactory<EchoServiceFallback> {
|
||||
|
@ -24,7 +24,6 @@ import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019-08-01
|
||||
* <p>
|
||||
* example feign client
|
||||
*/
|
||||
|
@ -22,7 +22,6 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019-08-01
|
||||
*/
|
||||
@RestController
|
||||
public class EchoController {
|
||||
|
@ -28,71 +28,71 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class ConsulDataSourceProperties extends AbstractDataSourceProperties {
|
||||
|
||||
public ConsulDataSourceProperties(){
|
||||
super(ConsulDataSourceFactoryBean.class.getName());
|
||||
}
|
||||
public ConsulDataSourceProperties() {
|
||||
super(ConsulDataSourceFactoryBean.class.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* consul server host.
|
||||
*/
|
||||
private String host;
|
||||
/**
|
||||
* consul server host.
|
||||
*/
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* consul server port.
|
||||
*/
|
||||
private int port=8500;
|
||||
/**
|
||||
* consul server port.
|
||||
*/
|
||||
private int port = 8500;
|
||||
|
||||
/**
|
||||
* data key in Redis.
|
||||
*/
|
||||
private String ruleKey;
|
||||
/**
|
||||
* data key in Redis.
|
||||
*/
|
||||
private String ruleKey;
|
||||
|
||||
/**
|
||||
* Request of query will hang until timeout (in second) or get updated value.
|
||||
*/
|
||||
private int waitTimeoutInSecond = 1;
|
||||
/**
|
||||
* Request of query will hang until timeout (in second) or get updated value.
|
||||
*/
|
||||
private int waitTimeoutInSecond = 1;
|
||||
|
||||
@Override
|
||||
public void preCheck(String dataSourceName) {
|
||||
if(StringUtils.isEmpty(host)){
|
||||
throw new IllegalArgumentException(
|
||||
"ConsulDataSource server-host is empty");
|
||||
}
|
||||
if(StringUtils.isEmpty(ruleKey)){
|
||||
throw new IllegalArgumentException(
|
||||
"ConsulDataSource ruleKey can not be empty");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void preCheck(String dataSourceName) {
|
||||
if (StringUtils.isEmpty(host)) {
|
||||
throw new IllegalArgumentException("ConsulDataSource server-host is empty");
|
||||
}
|
||||
if (StringUtils.isEmpty(ruleKey)) {
|
||||
throw new IllegalArgumentException(
|
||||
"ConsulDataSource ruleKey can not be empty");
|
||||
}
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getRuleKey() {
|
||||
return ruleKey;
|
||||
}
|
||||
public String getRuleKey() {
|
||||
return ruleKey;
|
||||
}
|
||||
|
||||
public void setRuleKey(String ruleKey) {
|
||||
this.ruleKey = ruleKey;
|
||||
}
|
||||
public void setRuleKey(String ruleKey) {
|
||||
this.ruleKey = ruleKey;
|
||||
}
|
||||
|
||||
public int getWaitTimeoutInSecond() {
|
||||
return waitTimeoutInSecond;
|
||||
}
|
||||
public int getWaitTimeoutInSecond() {
|
||||
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> {
|
||||
|
||||
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
|
||||
public ConsulDataSource getObject() throws Exception {
|
||||
return new ConsulDataSource(
|
||||
host,
|
||||
port,
|
||||
ruleKey,
|
||||
waitTimeoutInSecond,
|
||||
converter);
|
||||
}
|
||||
@Override
|
||||
public ConsulDataSource getObject() throws Exception {
|
||||
return new ConsulDataSource(host, port, ruleKey, waitTimeoutInSecond, converter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return ConsulDataSource.class;
|
||||
}
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return ConsulDataSource.class;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getRuleKey() {
|
||||
return ruleKey;
|
||||
}
|
||||
public String getRuleKey() {
|
||||
return ruleKey;
|
||||
}
|
||||
|
||||
public void setRuleKey(String ruleKey) {
|
||||
this.ruleKey = ruleKey;
|
||||
}
|
||||
public void setRuleKey(String ruleKey) {
|
||||
this.ruleKey = ruleKey;
|
||||
}
|
||||
|
||||
public int getWaitTimeoutInSecond() {
|
||||
return waitTimeoutInSecond;
|
||||
}
|
||||
public int getWaitTimeoutInSecond() {
|
||||
return waitTimeoutInSecond;
|
||||
}
|
||||
|
||||
public void setWaitTimeoutInSecond(int waitTimeoutInSecond) {
|
||||
this.waitTimeoutInSecond = waitTimeoutInSecond;
|
||||
}
|
||||
public void setWaitTimeoutInSecond(int waitTimeoutInSecond) {
|
||||
this.waitTimeoutInSecond = waitTimeoutInSecond;
|
||||
}
|
||||
|
||||
public Converter getConverter() {
|
||||
return converter;
|
||||
}
|
||||
public Converter getConverter() {
|
||||
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.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -99,10 +98,10 @@ public class NacosPropertySourceBuilder {
|
||||
fileExtension);
|
||||
}
|
||||
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) {
|
||||
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();
|
||||
}
|
||||
|
@ -50,9 +50,10 @@ public abstract class AbstractPropertySourceLoader implements PropertySourceLoad
|
||||
/**
|
||||
* Prevent interference with other loaders.Nacos-specific loader, unless the reload
|
||||
* changes it.
|
||||
* @param name
|
||||
* @param resource
|
||||
* @return
|
||||
* @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 if the resource can be loaded
|
||||
*/
|
||||
protected boolean canLoad(String name, Resource resource) {
|
||||
return resource instanceof NacosByteArrayResource;
|
||||
|
@ -19,7 +19,7 @@ package com.alibaba.cloud.nacos.parser;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
|
||||
/**
|
||||
* Nacos-specific resource
|
||||
* Nacos-specific resource.
|
||||
*
|
||||
* @author zkz
|
||||
*/
|
||||
|
@ -28,11 +28,9 @@ import java.util.stream.Collectors;
|
||||
import org.springframework.boot.env.OriginTrackedMapPropertySource;
|
||||
import org.springframework.boot.env.PropertySourceLoader;
|
||||
import org.springframework.core.env.EnumerablePropertySource;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@ -40,10 +38,19 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public final class NacosDataParserHandler {
|
||||
|
||||
/**
|
||||
* symbol: dot.
|
||||
*/
|
||||
public static final String DOT = ".";
|
||||
|
||||
/**
|
||||
* constant.
|
||||
*/
|
||||
public static final String VALUE = "value";
|
||||
|
||||
/**
|
||||
* default extension.
|
||||
*/
|
||||
public static final String DEFAULT_EXTENSION = "properties";
|
||||
|
||||
private static List<PropertySourceLoader> propertySourceLoaders;
|
||||
@ -101,6 +108,12 @@ public final class NacosDataParserHandler {
|
||||
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) {
|
||||
return Arrays.stream(loader.getFileExtensions())
|
||||
.anyMatch((fileExtension) -> StringUtils.endsWithIgnoreCase(extension,
|
||||
|
@ -34,7 +34,7 @@ import static com.alibaba.cloud.nacos.parser.NacosDataParserHandler.VALUE;
|
||||
/**
|
||||
* @author zkz
|
||||
*/
|
||||
public class JsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
||||
public class NacosJsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
||||
|
||||
/**
|
||||
* Returns the file extensions that the loader supports (excluding the '.').
|
||||
@ -46,9 +46,14 @@ public class JsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name
|
||||
* @param resource
|
||||
* @return
|
||||
* 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)
|
||||
@ -57,9 +62,9 @@ public class JsonPropertySourceLoader extends AbstractPropertySourceLoader {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
Map<String, Object> nacosDataMap = mapper.readValue(resource.getInputStream(),
|
||||
LinkedHashMap.class);
|
||||
flattenedMap(result, this.reloadMap(nacosDataMap), null);
|
||||
flattenedMap(result, nacosDataMap, null);
|
||||
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=\
|
||||
com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureAnalyzer
|
||||
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)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockIgnore({ "javax.management.*", "javax.xml.parsers.*",
|
||||
"com.sun.org.apache.xerces.internal.jaxp.*" })
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosConfigurationNoSuffixTest.TestConfig.class, properties = {
|
||||
|
@ -49,7 +49,8 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||
* @author zkz
|
||||
*/
|
||||
@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)
|
||||
@PrepareForTest({ NacosConfigService.class })
|
||||
@SpringBootTest(classes = NacosConfigurationXmlJsonTest.TestConfig.class, properties = {
|
||||
@ -83,13 +84,15 @@ public class NacosConfigurationXmlJsonTest {
|
||||
throws Throwable {
|
||||
|
||||
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"
|
||||
+ " <third>three</third>\n" + " </sencond>\n"
|
||||
+ "</top>";
|
||||
}
|
||||
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"
|
||||
+ " <Connector value=\"第二个连接器\"> \n"
|
||||
+ " <open>开启服务</open> \n"
|
||||
@ -108,7 +111,8 @@ public class NacosConfigurationXmlJsonTest {
|
||||
|
||||
if ("test-name-dev.xml".equals(args[0])
|
||||
&& "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"
|
||||
+ " android:label=\"@string/app_name\" android:screenOrientation=\"landscape\">\n"
|
||||
+ " <intent-filter>\n"
|
||||
|
@ -44,7 +44,9 @@ import org.springframework.web.context.request.RequestContextHolder;
|
||||
*/
|
||||
public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(SeataHystrixConcurrencyStrategy.class);
|
||||
private final Logger logger = LoggerFactory
|
||||
.getLogger(SeataHystrixConcurrencyStrategy.class);
|
||||
|
||||
private HystrixConcurrencyStrategy delegate;
|
||||
|
||||
public SeataHystrixConcurrencyStrategy() {
|
||||
@ -53,45 +55,54 @@ public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy
|
||||
if (this.delegate instanceof SeataHystrixConcurrencyStrategy) {
|
||||
return;
|
||||
}
|
||||
HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance().getCommandExecutionHook();
|
||||
HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier();
|
||||
HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance().getMetricsPublisher();
|
||||
HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance().getPropertiesStrategy();
|
||||
logCurrentStateOfHystrixPlugins(eventNotifier, metricsPublisher, propertiesStrategy);
|
||||
HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins
|
||||
.getInstance().getCommandExecutionHook();
|
||||
HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance()
|
||||
.getEventNotifier();
|
||||
HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance()
|
||||
.getMetricsPublisher();
|
||||
HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance()
|
||||
.getPropertiesStrategy();
|
||||
logCurrentStateOfHystrixPlugins(eventNotifier, metricsPublisher,
|
||||
propertiesStrategy);
|
||||
HystrixPlugins.reset();
|
||||
HystrixPlugins.getInstance().registerConcurrencyStrategy(this);
|
||||
HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook);
|
||||
HystrixPlugins.getInstance()
|
||||
.registerCommandExecutionHook(commandExecutionHook);
|
||||
HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
|
||||
HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);
|
||||
HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
catch (Exception ex) {
|
||||
logger.error("Failed to register Seata Hystrix Concurrency Strategy", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void logCurrentStateOfHystrixPlugins(HystrixEventNotifier eventNotifier,
|
||||
HystrixMetricsPublisher metricsPublisher,
|
||||
HystrixPropertiesStrategy propertiesStrategy) {
|
||||
HystrixMetricsPublisher metricsPublisher,
|
||||
HystrixPropertiesStrategy propertiesStrategy) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Current Hystrix plugins configuration is [" + "concurrencyStrategy [" + this.delegate + "],"
|
||||
+ "eventNotifier [" + eventNotifier + "]," + "metricPublisher [" + metricsPublisher + "],"
|
||||
+ "propertiesStrategy [" + propertiesStrategy + "]," + "]");
|
||||
logger.debug("Current Hystrix plugins configuration is ["
|
||||
+ "concurrencyStrategy [" + this.delegate + "]," + "eventNotifier ["
|
||||
+ eventNotifier + "]," + "metricPublisher [" + metricsPublisher + "],"
|
||||
+ "propertiesStrategy [" + propertiesStrategy + "]," + "]");
|
||||
logger.debug("Registering Seata Hystrix Concurrency Strategy.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey, HystrixProperty<Integer> corePoolSize,
|
||||
HystrixProperty<Integer> maximumPoolSize,
|
||||
HystrixProperty<Integer> keepAliveTime, TimeUnit unit,
|
||||
BlockingQueue<Runnable> workQueue) {
|
||||
return this.delegate.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize, keepAliveTime, unit,
|
||||
workQueue);
|
||||
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
|
||||
HystrixProperty<Integer> corePoolSize,
|
||||
HystrixProperty<Integer> maximumPoolSize,
|
||||
HystrixProperty<Integer> keepAliveTime, TimeUnit unit,
|
||||
BlockingQueue<Runnable> workQueue) {
|
||||
return this.delegate.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize,
|
||||
keepAliveTime, unit, workQueue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
|
||||
HystrixThreadPoolProperties threadPoolProperties) {
|
||||
HystrixThreadPoolProperties threadPoolProperties) {
|
||||
return this.delegate.getThreadPool(threadPoolKey, threadPoolProperties);
|
||||
}
|
||||
|
||||
@ -101,7 +112,8 @@ public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> HystrixRequestVariable<T> getRequestVariable(HystrixRequestVariableLifecycle<T> rv) {
|
||||
public <T> HystrixRequestVariable<T> getRequestVariable(
|
||||
HystrixRequestVariableLifecycle<T> rv) {
|
||||
return this.delegate.getRequestVariable(rv);
|
||||
}
|
||||
|
||||
@ -114,14 +126,16 @@ public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy
|
||||
Callable<K> wrappedCallable;
|
||||
if (this.delegate != null) {
|
||||
wrappedCallable = this.delegate.wrapCallable(c);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
wrappedCallable = c;
|
||||
}
|
||||
if (wrappedCallable instanceof SeataContextCallable) {
|
||||
return wrappedCallable;
|
||||
}
|
||||
|
||||
return new SeataContextCallable<>(wrappedCallable, RequestContextHolder.getRequestAttributes());
|
||||
return new SeataContextCallable<>(wrappedCallable,
|
||||
RequestContextHolder.getRequestAttributes());
|
||||
}
|
||||
|
||||
private static class SeataContextCallable<K> implements Callable<K> {
|
||||
@ -144,7 +158,8 @@ public class SeataHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||
RootContext.bind(xid);
|
||||
return actual.call();
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
RootContext.unbind();
|
||||
RequestContextHolder.resetRequestAttributes();
|
||||
}
|
||||
|
@ -40,6 +40,7 @@ public abstract class AbstractHttpRequestMatcher implements HttpRequestMatcher {
|
||||
* <p>
|
||||
* For example {@code " || "} for URL patterns or {@code " && "} for param
|
||||
* expressions.
|
||||
* @return str
|
||||
*/
|
||||
protected abstract String getToStringInfix();
|
||||
|
||||
|
@ -169,8 +169,8 @@ public class RocketMQMessageChannelBinder extends
|
||||
rocketMQTemplate, destination.getName(), producerGroup,
|
||||
producerProperties.getExtension().getTransactional(),
|
||||
instrumentationManager, producerProperties,
|
||||
((AbstractMessageChannel) channel).getInterceptors().stream()
|
||||
.filter(channelInterceptor -> channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor)
|
||||
((AbstractMessageChannel) channel).getInterceptors().stream().filter(
|
||||
channelInterceptor -> channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor)
|
||||
.map(channelInterceptor -> ((MessageConverterConfigurer.PartitioningInterceptor) channelInterceptor))
|
||||
.findFirst().orElse(null));
|
||||
messageHandler.setBeanFactory(this.getApplicationContext().getBeanFactory());
|
||||
|
Loading…
x
Reference in New Issue
Block a user