mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
commit
829dc23550
@ -81,7 +81,7 @@ public class NacosConfigProperties {
|
|||||||
.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("${spring.cloud.nacos.server-addr:}");
|
.resolvePlaceholders("${spring.cloud.nacos.server-addr:localhost:8848}");
|
||||||
}
|
}
|
||||||
this.setServerAddr(serverAddr);
|
this.setServerAddr(serverAddr);
|
||||||
}
|
}
|
||||||
|
@ -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."
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -431,7 +431,7 @@ public class NacosDiscoveryProperties {
|
|||||||
.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("${spring.cloud.nacos.server-addr:}");
|
.resolvePlaceholders("${spring.cloud.nacos.server-addr:localhost:8848}");
|
||||||
}
|
}
|
||||||
this.setServerAddr(serverAddr);
|
this.setServerAddr(serverAddr);
|
||||||
}
|
}
|
||||||
|
@ -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>
|
||||||
|
@ -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,7 @@ 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", "localhost:8848");
|
||||||
if (StringUtils.isEmpty(serverAddr)) {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"NacosDataSource server-addr is empty");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +120,7 @@ public class SentinelSCGAutoConfiguration {
|
|||||||
"[Sentinel SpringCloudGateway] using AnonymousBlockRequestHandler, responseStatus: "
|
"[Sentinel SpringCloudGateway] using AnonymousBlockRequestHandler, responseStatus: "
|
||||||
+ fallbackProperties.getResponseStatus()
|
+ fallbackProperties.getResponseStatus()
|
||||||
+ ", responseBody: "
|
+ ", responseBody: "
|
||||||
+ fallbackProperties.getResponseStatus());
|
+ fallbackProperties.getResponseBody());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String redirectUrl = fallbackProperties.getRedirect();
|
String redirectUrl = fallbackProperties.getRedirect();
|
||||||
|
@ -24,6 +24,7 @@ import javax.annotation.PostConstruct;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.cloud.client.circuitbreaker.Customizer;
|
import org.springframework.cloud.client.circuitbreaker.Customizer;
|
||||||
import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory;
|
import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@ -35,6 +36,8 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass(
|
@ConditionalOnClass(
|
||||||
name = { "reactor.core.publisher.Mono", "reactor.core.publisher.Flux" })
|
name = { "reactor.core.publisher.Mono", "reactor.core.publisher.Flux" })
|
||||||
|
@ConditionalOnProperty(name = "spring.cloud.circuitbreaker.sentinel.enabled",
|
||||||
|
havingValue = "true", matchIfMissing = true)
|
||||||
public class ReactiveSentinelCircuitBreakerAutoConfiguration {
|
public class ReactiveSentinelCircuitBreakerAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -26,6 +26,7 @@ import com.alibaba.csp.sentinel.SphU;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
|
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
|
||||||
import org.springframework.cloud.client.circuitbreaker.Customizer;
|
import org.springframework.cloud.client.circuitbreaker.Customizer;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@ -38,6 +39,8 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass({ SphU.class })
|
@ConditionalOnClass({ SphU.class })
|
||||||
|
@ConditionalOnProperty(name = "spring.cloud.circuitbreaker.sentinel.enabled",
|
||||||
|
havingValue = "true", matchIfMissing = true)
|
||||||
public class SentinelCircuitBreakerAutoConfiguration {
|
public class SentinelCircuitBreakerAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
{"properties": [
|
||||||
|
{
|
||||||
|
"name": "spring.cloud.circuitbreaker.sentinel.enabled",
|
||||||
|
"type": "java.lang.Boolean",
|
||||||
|
"description": "enable sentinel circuitbreaker or not."
|
||||||
|
}
|
||||||
|
]}
|
Loading…
x
Reference in New Issue
Block a user