mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
0def289574
@ -33,6 +33,7 @@
|
|||||||
<aliyun.java.sdk.dysmsapi>1.1.0</aliyun.java.sdk.dysmsapi>
|
<aliyun.java.sdk.dysmsapi>1.1.0</aliyun.java.sdk.dysmsapi>
|
||||||
<aliyun.sdk.mns>1.1.8.6</aliyun.sdk.mns>
|
<aliyun.sdk.mns>1.1.8.6</aliyun.sdk.mns>
|
||||||
<aliyun.java.sdk.dyvmsapi>1.1.1</aliyun.java.sdk.dyvmsapi>
|
<aliyun.java.sdk.dyvmsapi>1.1.1</aliyun.java.sdk.dyvmsapi>
|
||||||
|
<spring.context.support.version>1.0.5</spring.context.support.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
@ -385,6 +386,11 @@
|
|||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.spring</groupId>
|
||||||
|
<artifactId>spring-context-support</artifactId>
|
||||||
|
<version>${spring.context.support.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Testing Dependencies -->
|
<!-- Testing Dependencies -->
|
||||||
|
|
||||||
@ -422,6 +428,13 @@
|
|||||||
<enabled>false</enabled>
|
<enabled>false</enabled>
|
||||||
</snapshots>
|
</snapshots>
|
||||||
</repository>
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>sonatype-nexus</id>
|
||||||
|
<url>https://oss.sonatype.org/content/repositories/releases</url>
|
||||||
|
<releases>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</releases>
|
||||||
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
<pluginRepositories>
|
<pluginRepositories>
|
||||||
<pluginRepository>
|
<pluginRepository>
|
||||||
|
@ -19,6 +19,11 @@
|
|||||||
<artifactId>nacos-client</artifactId>
|
<artifactId>nacos-client</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.spring</groupId>
|
||||||
|
<artifactId>spring-context-support</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--spring cloud-->
|
<!--spring cloud-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
@ -17,8 +17,11 @@
|
|||||||
package com.alibaba.cloud.nacos;
|
package com.alibaba.cloud.nacos;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
@ -26,11 +29,13 @@ import com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureExcept
|
|||||||
import com.alibaba.nacos.api.NacosFactory;
|
import com.alibaba.nacos.api.NacosFactory;
|
||||||
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 com.alibaba.spring.util.PropertySourcesUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@ -64,6 +69,8 @@ public class NacosConfigProperties {
|
|||||||
*/
|
*/
|
||||||
public static final String PREFIX = "spring.cloud.nacos.config";
|
public static final String PREFIX = "spring.cloud.nacos.config";
|
||||||
|
|
||||||
|
private static final Pattern PATTERN = Pattern.compile("-(\\w)");
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory
|
private static final Logger log = LoggerFactory
|
||||||
.getLogger(NacosConfigProperties.class);
|
.getLogger(NacosConfigProperties.class);
|
||||||
|
|
||||||
@ -80,8 +87,8 @@ public class NacosConfigProperties {
|
|||||||
String serverAddr = environment
|
String serverAddr = environment
|
||||||
.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}");
|
.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}");
|
||||||
if (StringUtils.isEmpty(serverAddr)) {
|
if (StringUtils.isEmpty(serverAddr)) {
|
||||||
serverAddr = environment
|
serverAddr = environment.resolvePlaceholders(
|
||||||
.resolvePlaceholders("${spring.cloud.nacos.server-addr:}");
|
"${spring.cloud.nacos.server-addr:localhost:8848}");
|
||||||
}
|
}
|
||||||
this.setServerAddr(serverAddr);
|
this.setServerAddr(serverAddr);
|
||||||
}
|
}
|
||||||
@ -357,6 +364,14 @@ public class NacosConfigProperties {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Environment getEnvironment() {
|
||||||
|
return environment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnvironment(Environment environment) {
|
||||||
|
this.environment = environment;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return ConfigService
|
* @return ConfigService
|
||||||
*/
|
*/
|
||||||
@ -399,9 +414,28 @@ public class NacosConfigProperties {
|
|||||||
else {
|
else {
|
||||||
properties.put(ENDPOINT, endpoint);
|
properties.put(ENDPOINT, endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enrichNacosConfigProperties(properties);
|
||||||
return properties;
|
return properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void enrichNacosConfigProperties(Properties nacosConfigProperties) {
|
||||||
|
Map<String, Object> properties = PropertySourcesUtils
|
||||||
|
.getSubProperties((ConfigurableEnvironment) environment, PREFIX);
|
||||||
|
properties.forEach((k, v) -> nacosConfigProperties.putIfAbsent(resolveKey(k),
|
||||||
|
String.valueOf(v)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveKey(String key) {
|
||||||
|
Matcher matcher = PATTERN.matcher(key);
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
while (matcher.find()) {
|
||||||
|
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
|
||||||
|
}
|
||||||
|
matcher.appendTail(sb);
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\''
|
return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\''
|
||||||
|
@ -62,7 +62,7 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PropertySource<?> locate(Environment env) {
|
public PropertySource<?> locate(Environment env) {
|
||||||
|
nacosConfigProperties.setEnvironment(env);
|
||||||
ConfigService configService = nacosConfigProperties.configServiceInstance();
|
ConfigService configService = nacosConfigProperties.configServiceInstance();
|
||||||
|
|
||||||
if (null == configService) {
|
if (null == configService) {
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
{
|
{
|
||||||
"name": "spring.cloud.nacos.server-addr",
|
"name": "spring.cloud.nacos.server-addr",
|
||||||
"type": "java.lang.String",
|
"type": "java.lang.String",
|
||||||
|
"defaultValue": "localhost:8848",
|
||||||
"description": "nacos server address."
|
"description": "nacos server address."
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -19,6 +19,11 @@
|
|||||||
<artifactId>nacos-client</artifactId>
|
<artifactId>nacos-client</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.spring</groupId>
|
||||||
|
<artifactId>spring-context-support</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-commons</artifactId>
|
<artifactId>spring-cloud-commons</artifactId>
|
||||||
|
@ -25,6 +25,8 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
@ -34,6 +36,7 @@ import com.alibaba.nacos.api.naming.NamingMaintainService;
|
|||||||
import com.alibaba.nacos.api.naming.NamingService;
|
import com.alibaba.nacos.api.naming.NamingService;
|
||||||
import com.alibaba.nacos.api.naming.PreservedMetadataKeys;
|
import com.alibaba.nacos.api.naming.PreservedMetadataKeys;
|
||||||
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
||||||
|
import com.alibaba.spring.util.PropertySourcesUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -41,6 +44,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.cloud.commons.util.InetUtils;
|
import org.springframework.cloud.commons.util.InetUtils;
|
||||||
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@ -66,6 +70,13 @@ public class NacosDiscoveryProperties {
|
|||||||
private static final Logger log = LoggerFactory
|
private static final Logger log = LoggerFactory
|
||||||
.getLogger(NacosDiscoveryProperties.class);
|
.getLogger(NacosDiscoveryProperties.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prefix of {@link NacosDiscoveryProperties}.
|
||||||
|
*/
|
||||||
|
public static final String PREFIX = "spring.cloud.nacos.discovery";
|
||||||
|
|
||||||
|
private static final Pattern PATTERN = Pattern.compile("-(\\w)");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* nacos discovery server address.
|
* nacos discovery server address.
|
||||||
*/
|
*/
|
||||||
@ -430,8 +441,8 @@ public class NacosDiscoveryProperties {
|
|||||||
String serverAddr = env
|
String serverAddr = env
|
||||||
.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}");
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}");
|
||||||
if (StringUtils.isEmpty(serverAddr)) {
|
if (StringUtils.isEmpty(serverAddr)) {
|
||||||
serverAddr = env
|
serverAddr = env.resolvePlaceholders(
|
||||||
.resolvePlaceholders("${spring.cloud.nacos.server-addr:}");
|
"${spring.cloud.nacos.server-addr:localhost:8848}");
|
||||||
}
|
}
|
||||||
this.setServerAddr(serverAddr);
|
this.setServerAddr(serverAddr);
|
||||||
}
|
}
|
||||||
@ -518,7 +529,26 @@ public class NacosDiscoveryProperties {
|
|||||||
properties.put(SECRET_KEY, secretKey);
|
properties.put(SECRET_KEY, secretKey);
|
||||||
properties.put(CLUSTER_NAME, clusterName);
|
properties.put(CLUSTER_NAME, clusterName);
|
||||||
properties.put(NAMING_LOAD_CACHE_AT_START, namingLoadCacheAtStart);
|
properties.put(NAMING_LOAD_CACHE_AT_START, namingLoadCacheAtStart);
|
||||||
|
|
||||||
|
enrichNacosDiscoveryProperties(properties);
|
||||||
return properties;
|
return properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void enrichNacosDiscoveryProperties(Properties nacosDiscoveryProperties) {
|
||||||
|
Map<String, Object> properties = PropertySourcesUtils
|
||||||
|
.getSubProperties((ConfigurableEnvironment) environment, PREFIX);
|
||||||
|
properties.forEach((k, v) -> nacosDiscoveryProperties.putIfAbsent(resolveKey(k),
|
||||||
|
String.valueOf(v)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveKey(String key) {
|
||||||
|
Matcher matcher = PATTERN.matcher(key);
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
while (matcher.find()) {
|
||||||
|
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
|
||||||
|
}
|
||||||
|
matcher.appendTail(sb);
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
{
|
{
|
||||||
"name": "spring.cloud.nacos.server-addr",
|
"name": "spring.cloud.nacos.server-addr",
|
||||||
"type": "java.lang.String",
|
"type": "java.lang.String",
|
||||||
|
"defaultValue": "localhost:8848",
|
||||||
"description": "nacos server address."
|
"description": "nacos server address."
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -38,6 +38,12 @@
|
|||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-loadbalancer</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
|
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
|
||||||
|
@ -22,7 +22,7 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
|
|||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
*/
|
*/
|
||||||
final class SeataBeanPostProcessor implements BeanPostProcessor {
|
public class SeataBeanPostProcessor implements BeanPostProcessor {
|
||||||
|
|
||||||
private final SeataFeignObjectWrapper seataFeignObjectWrapper;
|
private final SeataFeignObjectWrapper seataFeignObjectWrapper;
|
||||||
|
|
||||||
|
@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
* 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.seata.feign;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import feign.Client;
|
||||||
|
import feign.Request;
|
||||||
|
import feign.Response;
|
||||||
|
import io.seata.core.context.RootContext;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author yuhuangbin
|
||||||
|
*/
|
||||||
|
public class SeataFeignBlockingLoadBalancerClient implements Client {
|
||||||
|
|
||||||
|
private static final Log LOG = LogFactory
|
||||||
|
.getLog(SeataFeignBlockingLoadBalancerClient.class);
|
||||||
|
|
||||||
|
private final Client delegate;
|
||||||
|
|
||||||
|
private final BlockingLoadBalancerClient loadBalancerClient;
|
||||||
|
|
||||||
|
SeataFeignBlockingLoadBalancerClient(Client delegate,
|
||||||
|
BlockingLoadBalancerClient loadBalancerClient) {
|
||||||
|
this.delegate = delegate;
|
||||||
|
this.loadBalancerClient = loadBalancerClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Response execute(Request request, Request.Options options) throws IOException {
|
||||||
|
final URI originalUri = URI.create(request.url());
|
||||||
|
String serviceId = originalUri.getHost();
|
||||||
|
Assert.state(serviceId != null,
|
||||||
|
"Request URI does not contain a valid hostname: " + originalUri);
|
||||||
|
ServiceInstance instance = loadBalancerClient.choose(serviceId);
|
||||||
|
if (instance == null) {
|
||||||
|
String message = "Load balancer does not contain an instance for the service "
|
||||||
|
+ serviceId;
|
||||||
|
if (LOG.isWarnEnabled()) {
|
||||||
|
LOG.warn(message);
|
||||||
|
}
|
||||||
|
return Response.builder().request(request)
|
||||||
|
.status(HttpStatus.SERVICE_UNAVAILABLE.value())
|
||||||
|
.body(message, StandardCharsets.UTF_8).build();
|
||||||
|
}
|
||||||
|
String reconstructedUrl = loadBalancerClient.reconstructURI(instance, originalUri)
|
||||||
|
.toString();
|
||||||
|
Request newRequest = Request.create(request.httpMethod(), reconstructedUrl,
|
||||||
|
enrichRequstHeader(request.headers()), request.requestBody());
|
||||||
|
|
||||||
|
return delegate.execute(newRequest, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Collection<String>> enrichRequstHeader(
|
||||||
|
Map<String, Collection<String>> headers) {
|
||||||
|
String xid = RootContext.getXID();
|
||||||
|
if (!StringUtils.isEmpty(xid)) {
|
||||||
|
Map<String, Collection<String>> newHeaders = new HashMap<>();
|
||||||
|
newHeaders.putAll(headers);
|
||||||
|
newHeaders.put(RootContext.KEY_XID, Arrays.asList(xid));
|
||||||
|
return newHeaders;
|
||||||
|
}
|
||||||
|
return headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -16,9 +16,14 @@
|
|||||||
|
|
||||||
package com.alibaba.cloud.seata.feign;
|
package com.alibaba.cloud.seata.feign;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
import feign.Client;
|
import feign.Client;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
|
||||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||||
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
|
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
|
||||||
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
|
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
|
||||||
@ -28,12 +33,16 @@ import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
|
|||||||
*/
|
*/
|
||||||
public class SeataFeignObjectWrapper {
|
public class SeataFeignObjectWrapper {
|
||||||
|
|
||||||
|
private static final Log LOG = LogFactory.getLog(SeataFeignObjectWrapper.class);
|
||||||
|
|
||||||
private final BeanFactory beanFactory;
|
private final BeanFactory beanFactory;
|
||||||
|
|
||||||
private CachingSpringLoadBalancerFactory cachingSpringLoadBalancerFactory;
|
private CachingSpringLoadBalancerFactory cachingSpringLoadBalancerFactory;
|
||||||
|
|
||||||
private SpringClientFactory springClientFactory;
|
private SpringClientFactory springClientFactory;
|
||||||
|
|
||||||
|
private BlockingLoadBalancerClient loadBalancerClient;
|
||||||
|
|
||||||
SeataFeignObjectWrapper(BeanFactory beanFactory) {
|
SeataFeignObjectWrapper(BeanFactory beanFactory) {
|
||||||
this.beanFactory = beanFactory;
|
this.beanFactory = beanFactory;
|
||||||
}
|
}
|
||||||
@ -45,11 +54,41 @@ public class SeataFeignObjectWrapper {
|
|||||||
return new SeataLoadBalancerFeignClient(client.getDelegate(), factory(),
|
return new SeataLoadBalancerFeignClient(client.getDelegate(), factory(),
|
||||||
clientFactory(), this.beanFactory);
|
clientFactory(), this.beanFactory);
|
||||||
}
|
}
|
||||||
|
if (bean.getClass().getName().equals(
|
||||||
|
"org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient")) {
|
||||||
|
return new SeataFeignBlockingLoadBalancerClient(getClient(bean),
|
||||||
|
loadBalancerClient());
|
||||||
|
}
|
||||||
return new SeataFeignClient(this.beanFactory, (Client) bean);
|
return new SeataFeignClient(this.beanFactory, (Client) bean);
|
||||||
}
|
}
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Client getClient(Object bean) {
|
||||||
|
Field client = null;
|
||||||
|
boolean oldAccessible = false;
|
||||||
|
try {
|
||||||
|
client = bean.getClass().getDeclaredField("delegate");
|
||||||
|
oldAccessible = client.isAccessible();
|
||||||
|
client.setAccessible(true);
|
||||||
|
return (Client) client.get(bean);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
LOG.error("get delegate client error", e);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
client.setAccessible(oldAccessible);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private BlockingLoadBalancerClient loadBalancerClient() {
|
||||||
|
if (this.loadBalancerClient != null) {
|
||||||
|
return this.loadBalancerClient;
|
||||||
|
}
|
||||||
|
return beanFactory.getBean(BlockingLoadBalancerClient.class);
|
||||||
|
}
|
||||||
|
|
||||||
CachingSpringLoadBalancerFactory factory() {
|
CachingSpringLoadBalancerFactory factory() {
|
||||||
if (this.cachingSpringLoadBalancerFactory == null) {
|
if (this.cachingSpringLoadBalancerFactory == null) {
|
||||||
this.cachingSpringLoadBalancerFactory = this.beanFactory
|
this.cachingSpringLoadBalancerFactory = this.beanFactory
|
||||||
|
@ -54,11 +54,8 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
|
|||||||
public void preCheck(String dataSourceName) {
|
public void preCheck(String dataSourceName) {
|
||||||
if (StringUtils.isEmpty(serverAddr)) {
|
if (StringUtils.isEmpty(serverAddr)) {
|
||||||
serverAddr = this.getEnv().getProperty(
|
serverAddr = this.getEnv().getProperty(
|
||||||
"spring.cloud.sentinel.datasource.nacos.server-addr", "");
|
"spring.cloud.sentinel.datasource.nacos.server-addr",
|
||||||
if (StringUtils.isEmpty(serverAddr)) {
|
"localhost:8848");
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"NacosDataSource server-addr is empty");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user