parent
9c99474e84
commit
8c9da70750
@ -1,38 +0,0 @@
|
||||
/*
|
||||
* 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.circuitbreaker.feign;
|
||||
|
||||
import feign.Client;
|
||||
|
||||
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
|
||||
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
|
||||
|
||||
/**
|
||||
* Wrap for {@link FeignBlockingLoadBalancerClient}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisFeignBlockingLoadBalancerClient
|
||||
extends FeignBlockingLoadBalancerClient {
|
||||
|
||||
public PolarisFeignBlockingLoadBalancerClient(Client delegate,
|
||||
BlockingLoadBalancerClient loadBalancerClient) {
|
||||
super(delegate, loadBalancerClient);
|
||||
}
|
||||
|
||||
}
|
@ -1,88 +0,0 @@
|
||||
/*
|
||||
* 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.discovery.reactive;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.tencent.cloud.polaris.discovery.PolarisServiceDiscovery;
|
||||
import com.tencent.polaris.api.exception.PolarisException;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
|
||||
|
||||
/**
|
||||
* Reactive Discovery Client for Polaris.
|
||||
*
|
||||
* @author Haotian Zhang, Andrew Shan, Jie Cheng
|
||||
*/
|
||||
public class PolarisReactiveDiscoveryClient implements ReactiveDiscoveryClient {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(PolarisReactiveDiscoveryClient.class);
|
||||
|
||||
private PolarisServiceDiscovery polarisServiceDiscovery;
|
||||
|
||||
public PolarisReactiveDiscoveryClient(
|
||||
PolarisServiceDiscovery polarisServiceDiscovery) {
|
||||
this.polarisServiceDiscovery = polarisServiceDiscovery;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "Spring Cloud Polaris Reactive Discovery Client";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<ServiceInstance> getInstances(String serviceId) {
|
||||
|
||||
return Mono.justOrEmpty(serviceId).flatMapMany(loadInstancesFromPolaris())
|
||||
.subscribeOn(Schedulers.boundedElastic());
|
||||
}
|
||||
|
||||
private Function<String, Publisher<ServiceInstance>> loadInstancesFromPolaris() {
|
||||
return serviceId -> {
|
||||
try {
|
||||
return Flux.fromIterable(polarisServiceDiscovery.getInstances(serviceId));
|
||||
}
|
||||
catch (PolarisException e) {
|
||||
log.error("get service instance[{}] from polaris error!", serviceId, e);
|
||||
return Flux.empty();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<String> getServices() {
|
||||
return Flux.defer(() -> {
|
||||
try {
|
||||
return Flux.fromIterable(polarisServiceDiscovery.getServices());
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("get services from polaris server fail,", e);
|
||||
return Flux.empty();
|
||||
}
|
||||
}).subscribeOn(Schedulers.boundedElastic());
|
||||
}
|
||||
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
/*
|
||||
* 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.discovery.reactive;
|
||||
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisServiceDiscovery;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled;
|
||||
import org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration;
|
||||
import org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClientAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Reactive Discovery Client Configuration for Polaris.
|
||||
*
|
||||
* @author Haotian Zhang, Andrew Shan, Jie Cheng
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnReactiveDiscoveryEnabled
|
||||
@AutoConfigureAfter({ PolarisDiscoveryAutoConfiguration.class,
|
||||
ReactiveCompositeDiscoveryClientAutoConfiguration.class })
|
||||
@AutoConfigureBefore({ ReactiveCommonsClientAutoConfiguration.class })
|
||||
public class PolarisReactiveDiscoveryClientConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public PolarisReactiveDiscoveryClient polarisReactiveDiscoveryClient(
|
||||
PolarisServiceDiscovery polarisServiceDiscovery) {
|
||||
return new PolarisReactiveDiscoveryClient(polarisServiceDiscovery);
|
||||
}
|
||||
|
||||
}
|
@ -1,80 +0,0 @@
|
||||
/*
|
||||
* 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.discovery.reactive;
|
||||
|
||||
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
|
||||
import com.tencent.polaris.test.mock.discovery.NamingServer;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.PORT;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisReactiveDiscoveryClientConfiguration}
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisReactiveDiscoveryClientConfigurationTest {
|
||||
|
||||
private static NamingServer namingServer;
|
||||
|
||||
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class,
|
||||
PolarisReactiveDiscoveryClientConfiguration.class,
|
||||
PolarisDiscoveryClientConfiguration.class,
|
||||
PolarisContextConfiguration.class))
|
||||
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
|
||||
.withPropertyValues("server.port=" + PORT)
|
||||
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081");
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
namingServer = NamingServer.startNamingServer(10081);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
if (null != namingServer) {
|
||||
namingServer.terminate();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultInitialization() {
|
||||
this.contextRunner.run(context -> assertThat(context)
|
||||
.hasSingleBean(PolarisReactiveDiscoveryClient.class));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@EnableDiscoveryClient
|
||||
static class PolarisReactiveDiscoveryClientConfiguration {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
/*
|
||||
* 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.discovery.reactive;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.tencent.cloud.polaris.discovery.PolarisServiceDiscovery;
|
||||
import com.tencent.polaris.api.exception.PolarisException;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisReactiveDiscoveryClient}
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
public class PolarisReactiveDiscoveryClientTest {
|
||||
|
||||
@Mock
|
||||
private PolarisServiceDiscovery serviceDiscovery;
|
||||
|
||||
@Mock
|
||||
private ServiceInstance serviceInstance;
|
||||
|
||||
@InjectMocks
|
||||
private PolarisReactiveDiscoveryClient client;
|
||||
|
||||
@Test
|
||||
public void testGetInstances() throws PolarisException {
|
||||
|
||||
when(serviceDiscovery.getInstances(SERVICE_PROVIDER))
|
||||
.thenReturn(singletonList(serviceInstance));
|
||||
|
||||
Flux<ServiceInstance> instances = this.client.getInstances(SERVICE_PROVIDER);
|
||||
|
||||
StepVerifier.create(instances).expectNextCount(1).expectComplete().verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetServices() throws PolarisException {
|
||||
|
||||
when(serviceDiscovery.getServices())
|
||||
.thenReturn(Arrays.asList(SERVICE_PROVIDER + 1, SERVICE_PROVIDER + 2));
|
||||
|
||||
Flux<String> services = this.client.getServices();
|
||||
|
||||
StepVerifier.create(services)
|
||||
.expectNext(SERVICE_PROVIDER + 1, SERVICE_PROVIDER + 2).expectComplete()
|
||||
.verify();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.common.metadata.aop;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import com.tencent.cloud.common.constant.MetadataConstant;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import feign.Request;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
|
||||
/**
|
||||
* Aspect for getting service name of peer-service in Feign of Greenwich.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@Aspect
|
||||
public class MetadataFeignAspect {
|
||||
|
||||
@Pointcut("execution(* feign.Client.execute(..))")
|
||||
public void execute() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get service name before execution of Feign client.
|
||||
*
|
||||
* @param joinPoint join point
|
||||
*/
|
||||
@Before("execute()")
|
||||
public void doBefore(JoinPoint joinPoint) {
|
||||
Request request = (Request) joinPoint.getArgs()[0];
|
||||
MetadataContextHolder.get().putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE,
|
||||
URI.create(request.url()).getAuthority());
|
||||
}
|
||||
}
|
Loading…
Reference in new issue