fix:fix instance circuit breaker not working bug. (#893)

pull/901/head
Haotian Zhang 2 years ago committed by GitHub
parent 42cab51341
commit 1e7e63a875
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,3 +16,4 @@
- [feature:add polaris circuit breaker support.](https://github.com/Tencent/spring-cloud-tencent/pull/882) - [feature:add polaris circuit breaker support.](https://github.com/Tencent/spring-cloud-tencent/pull/882)
- [fix:fix NPE.](https://github.com/Tencent/spring-cloud-tencent/pull/886) - [fix:fix NPE.](https://github.com/Tencent/spring-cloud-tencent/pull/886)
- [refactor:optimize sct-all.](https://github.com/Tencent/spring-cloud-tencent/pull/888) - [refactor:optimize sct-all.](https://github.com/Tencent/spring-cloud-tencent/pull/888)
- [fix:fix instance circuit breaker not working bug.](https://github.com/Tencent/spring-cloud-tencent/pull/893)

@ -20,15 +20,22 @@ package com.tencent.cloud.polaris.loadbalancer;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import com.tencent.polaris.router.api.core.RouterAPI; import com.tencent.polaris.router.api.core.RouterAPI;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.ConditionalOnBlockingDiscoveryEnabled;
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled; import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer; import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
/** /**
@ -41,6 +48,11 @@ import org.springframework.core.env.Environment;
@ConditionalOnDiscoveryEnabled @ConditionalOnDiscoveryEnabled
public class PolarisLoadBalancerClientConfiguration { public class PolarisLoadBalancerClientConfiguration {
/**
* Order of reactive discovery service instance supplier.
*/
private static final int REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER = 193827465;
private final static String STRATEGY_WEIGHT = "polarisWeighted"; private final static String STRATEGY_WEIGHT = "polarisWeighted";
@Bean @Bean
@ -52,4 +64,35 @@ public class PolarisLoadBalancerClientConfiguration {
return new PolarisLoadBalancer(name, return new PolarisLoadBalancer(name,
loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), routerAPI); loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), routerAPI);
} }
@Configuration(proxyBeanMethods = false)
@ConditionalOnReactiveDiscoveryEnabled
@Order(REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER)
protected static class PolarisReactiveSupportConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(ReactiveDiscoveryClient.class)
public ServiceInstanceListSupplier polarisRouterDiscoveryClientServiceInstanceListSupplier(
ConfigurableApplicationContext context) {
return new PolarisServiceInstanceListSupplier(
ServiceInstanceListSupplier.builder().withDiscoveryClient().build(context));
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnBlockingDiscoveryEnabled
@Order(REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER + 1)
protected static class PolarisBlockingSupportConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(DiscoveryClient.class)
public ServiceInstanceListSupplier polarisRouterDiscoveryClientServiceInstanceListSupplier(
ConfigurableApplicationContext context) {
return new PolarisServiceInstanceListSupplier(
ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient().build(context));
}
}
} }

@ -0,0 +1,49 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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.tencent.cloud.polaris.loadbalancer;
import java.util.List;
import reactor.core.publisher.Flux;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.loadbalancer.core.DelegatingServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
/**
* Service instance list supplier of Polaris.
*
* @author Haotian Zhang
*/
public class PolarisServiceInstanceListSupplier extends DelegatingServiceInstanceListSupplier {
public PolarisServiceInstanceListSupplier(ServiceInstanceListSupplier delegate) {
super(delegate);
}
@Override
public Flux<List<ServiceInstance>> get() {
return getDelegate().get();
}
@Override
public Flux<List<ServiceInstance>> get(Request request) {
return super.get(request);
}
}

@ -41,6 +41,7 @@ import com.tencent.polaris.api.rpc.InstanceRegisterRequest;
import com.tencent.polaris.api.rpc.InstanceRegisterResponse; import com.tencent.polaris.api.rpc.InstanceRegisterResponse;
import com.tencent.polaris.api.rpc.InstancesResponse; import com.tencent.polaris.api.rpc.InstancesResponse;
import com.tencent.polaris.client.util.NamedThreadFactory; import com.tencent.polaris.client.util.NamedThreadFactory;
import com.tencent.polaris.factory.config.provider.ServiceConfigImpl;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -145,6 +146,11 @@ public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistrati
LOGGER.warn("Stat server started error, ", e); LOGGER.warn("Stat server started error, ", e);
} }
} }
ServiceConfigImpl serviceConfig = (ServiceConfigImpl) polarisDiscoveryHandler.getSdkContext().getConfig()
.getProvider().getService();
serviceConfig.setNamespace(polarisDiscoveryProperties.getNamespace());
serviceConfig.setName(serviceId);
} }
catch (Exception e) { catch (Exception e) {
LOGGER.error("polaris registry, {} register failed...{},", registration.getServiceId(), registration, e); LOGGER.error("polaris registry, {} register failed...{},", registration.getServiceId(), registration, e);

@ -73,7 +73,7 @@
<revision>1.10.0-2022.0.1-SNAPSHOT</revision> <revision>1.10.0-2022.0.1-SNAPSHOT</revision>
<!-- Dependencies --> <!-- Dependencies -->
<polaris.version>1.11.0</polaris.version> <polaris.version>1.11.1-SNAPSHOT</polaris.version>
<guava.version>31.1-jre</guava.version> <guava.version>31.1-jre</guava.version>
<mocktio.version>4.9.0</mocktio.version> <mocktio.version>4.9.0</mocktio.version>
<byte-buddy.version>1.12.19</byte-buddy.version> <byte-buddy.version>1.12.19</byte-buddy.version>

Loading…
Cancel
Save