Merge pull request #56 from SkyeBeFreeman/main
feat:optimize project structure and checkstylepull/59/head
commit
5d3a3aa45f
@ -0,0 +1,28 @@
|
||||
root = true
|
||||
|
||||
[*.java]
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
continuation_indent_size = 8
|
||||
|
||||
[*.groovy]
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
continuation_indent_size = 8
|
||||
|
||||
[*.xml]
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
continuation_indent_size = 8
|
||||
|
||||
[*.yml]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[*.yaml]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[*.sh]
|
||||
indent_style = space
|
||||
indent_size = 4
|
@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-cloud-tencent</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId>
|
||||
<name>Spring Cloud Starter Tencent Polaris Circuitbreaker</name>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring Cloud Tencent dependencies start -->
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-commons</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-metadata</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-polaris-context</artifactId>
|
||||
</dependency>
|
||||
<!-- Spring Cloud Tencent dependencies end -->
|
||||
|
||||
<!-- Polaris dependencies start -->
|
||||
<dependency>
|
||||
<groupId>com.tencent.polaris</groupId>
|
||||
<artifactId>polaris-discovery-factory</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencent.polaris</groupId>
|
||||
<artifactId>polaris-circuitbreaker-factory</artifactId>
|
||||
</dependency>
|
||||
<!-- Polaris dependencies end -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-openfeign-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
23
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfiguration.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfiguration.java
23
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfiguration.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfiguration.java
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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 com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import feign.Client;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
|
||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
|
||||
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
|
||||
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
|
||||
|
||||
/**
|
||||
* Wrap Spring Bean and decorating proxy for Feign Client.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisFeignBeanPostProcessor
|
||||
implements BeanPostProcessor, BeanFactoryAware {
|
||||
|
||||
private final ConsumerAPI consumerAPI;
|
||||
|
||||
private BeanFactory factory;
|
||||
|
||||
public PolarisFeignBeanPostProcessor(ConsumerAPI consumerAPI) {
|
||||
this.consumerAPI = consumerAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
return wrapper(bean);
|
||||
}
|
||||
|
||||
private Object wrapper(Object bean) {
|
||||
if (isNeedWrap(bean)) {
|
||||
if (bean instanceof LoadBalancerFeignClient) {
|
||||
LoadBalancerFeignClient client = ((LoadBalancerFeignClient) bean);
|
||||
return new PolarisLoadBalancerFeignClient(
|
||||
createPolarisFeignClient(client.getDelegate()), factory(),
|
||||
clientFactory());
|
||||
}
|
||||
if (bean instanceof FeignBlockingLoadBalancerClient) {
|
||||
FeignBlockingLoadBalancerClient client = (FeignBlockingLoadBalancerClient) bean;
|
||||
return new PolarisFeignBlockingLoadBalancerClient(
|
||||
createPolarisFeignClient(client.getDelegate()),
|
||||
factory.getBean(BlockingLoadBalancerClient.class));
|
||||
}
|
||||
return createPolarisFeignClient((Client) bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
private boolean isNeedWrap(Object bean) {
|
||||
return bean instanceof Client && !(bean instanceof PolarisFeignClient)
|
||||
&& !(bean instanceof PolarisFeignBlockingLoadBalancerClient)
|
||||
&& !(bean instanceof PolarisLoadBalancerFeignClient);
|
||||
}
|
||||
|
||||
private PolarisFeignClient createPolarisFeignClient(Client delegate) {
|
||||
return new PolarisFeignClient(delegate, consumerAPI);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
this.factory = beanFactory;
|
||||
}
|
||||
|
||||
CachingSpringLoadBalancerFactory factory() {
|
||||
return this.factory.getBean(CachingSpringLoadBalancerFactory.class);
|
||||
}
|
||||
|
||||
SpringClientFactory clientFactory() {
|
||||
return this.factory.getBean(SpringClientFactory.class);
|
||||
}
|
||||
|
||||
}
|
6
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBlockingLoadBalancerClient.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBlockingLoadBalancerClient.java
6
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBlockingLoadBalancerClient.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBlockingLoadBalancerClient.java
@ -0,0 +1,104 @@
|
||||
/*
|
||||
* 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 java.io.IOException;
|
||||
import java.net.URI;
|
||||
|
||||
import com.tencent.cloud.metadata.constant.MetadataConstant.SystemMetadataKey;
|
||||
import com.tencent.cloud.metadata.context.MetadataContext;
|
||||
import com.tencent.cloud.metadata.context.MetadataContextHolder;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.api.pojo.RetStatus;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.api.rpc.ServiceCallResult;
|
||||
import feign.Client;
|
||||
import feign.Request;
|
||||
import feign.Request.Options;
|
||||
import feign.Response;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import static feign.Util.checkNotNull;
|
||||
|
||||
/**
|
||||
* Wrap for {@link Client}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisFeignClient implements Client {
|
||||
|
||||
private final Client delegate;
|
||||
|
||||
private final ConsumerAPI consumerAPI;
|
||||
|
||||
public PolarisFeignClient(Client target, ConsumerAPI consumerAPI) {
|
||||
this.delegate = checkNotNull(target, "target");
|
||||
this.consumerAPI = checkNotNull(consumerAPI, "CircuitBreakAPI");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response execute(Request request, Options options) throws IOException {
|
||||
final ServiceCallResult resultRequest = createServiceCallResult(request);
|
||||
try {
|
||||
Response response = delegate.execute(request, options);
|
||||
// HTTP code greater than 400 is an exception
|
||||
if (response.status() >= 400) {
|
||||
resultRequest.setRetStatus(RetStatus.RetFail);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
catch (IOException origin) {
|
||||
resultRequest.setRetStatus(RetStatus.RetFail);
|
||||
throw origin;
|
||||
}
|
||||
finally {
|
||||
consumerAPI.updateServiceCallResult(resultRequest);
|
||||
}
|
||||
}
|
||||
|
||||
private ServiceCallResult createServiceCallResult(final Request request) {
|
||||
ServiceCallResult resultRequest = new ServiceCallResult();
|
||||
|
||||
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||
String namespace = metadataContext
|
||||
.getSystemMetadata(SystemMetadataKey.PEER_NAMESPACE);
|
||||
resultRequest.setNamespace(namespace);
|
||||
String serviceName = metadataContext
|
||||
.getSystemMetadata(SystemMetadataKey.PEER_SERVICE);
|
||||
resultRequest.setService(serviceName);
|
||||
String method = metadataContext.getSystemMetadata(SystemMetadataKey.PEER_PATH);
|
||||
resultRequest.setMethod(method);
|
||||
resultRequest.setRetStatus(RetStatus.RetSuccess);
|
||||
String sourceNamespace = metadataContext
|
||||
.getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
|
||||
String sourceService = metadataContext
|
||||
.getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
|
||||
if (StringUtils.isNotBlank(sourceNamespace)
|
||||
&& StringUtils.isNotBlank(sourceService)) {
|
||||
resultRequest
|
||||
.setCallerService(new ServiceKey(sourceNamespace, sourceService));
|
||||
}
|
||||
|
||||
URI uri = URI.create(request.url());
|
||||
resultRequest.setHost(uri.getHost());
|
||||
resultRequest.setPort(uri.getPort());
|
||||
|
||||
return resultRequest;
|
||||
}
|
||||
|
||||
}
|
4
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisLoadBalancerFeignClient.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisLoadBalancerFeignClient.java
4
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisLoadBalancerFeignClient.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisLoadBalancerFeignClient.java
7
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClientTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClientTest.java
7
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClientTest.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClientTest.java
13
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/TestPolarisFeignApp.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/TestPolarisFeignApp.java
13
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/TestPolarisFeignApp.java → spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/TestPolarisFeignApp.java
@ -1,9 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-cloud-tencent-starters</artifactId>
|
||||
<artifactId>spring-cloud-tencent</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
@ -0,0 +1,231 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
* Properties for Polaris.
|
||||
*
|
||||
* @author Haotian Zhang, Andrew Shan, Jie Cheng
|
||||
*/
|
||||
@ConfigurationProperties("spring.cloud.polaris.discovery")
|
||||
public class PolarisProperties {
|
||||
|
||||
/**
|
||||
* The polaris authentication token.
|
||||
*/
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* Namespace, separation registry of different environments.
|
||||
*/
|
||||
@Value("${spring.cloud.polaris.discovery.namespace:#{'default'}}")
|
||||
private String namespace;
|
||||
|
||||
/**
|
||||
* Service name to registry.
|
||||
*/
|
||||
@Value("${spring.cloud.polaris.discovery.service:${spring.application.name:}}")
|
||||
private String service;
|
||||
|
||||
/**
|
||||
* Load balance weight.
|
||||
*/
|
||||
@Value("${spring.cloud.polaris.discovery.weight:#{100}}")
|
||||
private float weight;
|
||||
|
||||
/**
|
||||
* Version number.
|
||||
*/
|
||||
private String version;
|
||||
|
||||
/**
|
||||
* Protocol name such as http, https.
|
||||
*/
|
||||
@Value("${spring.cloud.polaris.discovery.protocol:http}")
|
||||
private String protocol;
|
||||
|
||||
/**
|
||||
* Port of instance.
|
||||
*/
|
||||
@Value("${server.port:}")
|
||||
private int port;
|
||||
|
||||
/**
|
||||
* Ip address to be registered.
|
||||
*/
|
||||
private String ipAddress;
|
||||
|
||||
/**
|
||||
* If instance registered.
|
||||
*/
|
||||
@Value("${spring.cloud.polaris.discovery.register.enabled:#{true}}")
|
||||
private Boolean registerEnabled;
|
||||
|
||||
/**
|
||||
* If heartbeat enabled.
|
||||
*/
|
||||
@Value("${spring.cloud.polaris.discovery.heartbeat.enabled:#{true}}")
|
||||
private Boolean heartbeatEnabled = true;
|
||||
|
||||
/**
|
||||
* Custom health check url to override default.
|
||||
*/
|
||||
@Value("${spring.cloud.polaris.discovery.health-check-url:}")
|
||||
private String healthCheckUrl;
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
public PolarisProperties(InetUtils inetUtils) {
|
||||
if (inetUtils != null) {
|
||||
this.ipAddress = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Init properties.
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
if (StringUtils.isEmpty(this.getNamespace())) {
|
||||
this.setNamespace(environment
|
||||
.resolvePlaceholders("${spring.cloud.polaris.discovery.namespace:}"));
|
||||
}
|
||||
if (StringUtils.isEmpty(this.getService())) {
|
||||
this.setService(environment
|
||||
.resolvePlaceholders("${spring.cloud.polaris.discovery.service:}"));
|
||||
}
|
||||
if (StringUtils.isEmpty(this.getToken())) {
|
||||
this.setToken(environment
|
||||
.resolvePlaceholders("${spring.cloud.polaris.discovery.token:}"));
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isHeartbeatEnabled() {
|
||||
if (null == heartbeatEnabled) {
|
||||
return false;
|
||||
}
|
||||
return heartbeatEnabled;
|
||||
}
|
||||
|
||||
public void setHeartbeatEnabled(Boolean heartbeatEnabled) {
|
||||
this.heartbeatEnabled = heartbeatEnabled;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
public void setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
public float getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(float weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public String getService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
public void setService(String service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public boolean isRegisterEnabled() {
|
||||
return registerEnabled;
|
||||
}
|
||||
|
||||
public void setRegisterEnabled(boolean registerEnabled) {
|
||||
this.registerEnabled = registerEnabled;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(String version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getProtocol() {
|
||||
return protocol;
|
||||
}
|
||||
|
||||
public void setProtocol(String protocol) {
|
||||
this.protocol = protocol;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getIpAddress() {
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
public void setIpAddress(String ipAddress) {
|
||||
this.ipAddress = ipAddress;
|
||||
}
|
||||
|
||||
public String getHealthCheckUrl() {
|
||||
return healthCheckUrl;
|
||||
}
|
||||
|
||||
public void setHealthCheckUrl(String healthCheckUrl) {
|
||||
this.healthCheckUrl = healthCheckUrl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PolarisProperties{" + "token='" + token + '\'' + ", namespace='"
|
||||
+ namespace + '\'' + ", service='" + service + '\'' + ", weight=" + weight
|
||||
+ ", version='" + version + '\'' + ", protocol='" + protocol + '\''
|
||||
+ ", port=" + port + ", ipAddress='" + ipAddress + '\''
|
||||
+ ", registerEnabled=" + registerEnabled + ", heartbeatEnabled="
|
||||
+ heartbeatEnabled + ", healthCheckUrl=" + healthCheckUrl
|
||||
+ ", environment=" + environment + '}';
|
||||
}
|
||||
|
||||
}
|
6
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/ConditionalOnPolarisDiscoveryEnabled.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/ConditionalOnPolarisDiscoveryEnabled.java
6
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/ConditionalOnPolarisDiscoveryEnabled.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/ConditionalOnPolarisDiscoveryEnabled.java
14
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfiguration.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfiguration.java
14
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfiguration.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfiguration.java
7
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfiguration.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfiguration.java
7
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfiguration.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfiguration.java
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.metadata.constant.MetadataConstant.SystemMetadataKey;
|
||||
import com.tencent.cloud.metadata.context.MetadataContextHolder;
|
||||
import com.tencent.cloud.polaris.PolarisProperties;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import com.tencent.polaris.api.core.ProviderAPI;
|
||||
import com.tencent.polaris.api.pojo.ServiceInfo;
|
||||
import com.tencent.polaris.api.rpc.GetAllInstancesRequest;
|
||||
import com.tencent.polaris.api.rpc.GetInstancesRequest;
|
||||
import com.tencent.polaris.api.rpc.GetServicesRequest;
|
||||
import com.tencent.polaris.api.rpc.InstancesResponse;
|
||||
import com.tencent.polaris.api.rpc.ServicesResponse;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Discovery Handler for Polaris.
|
||||
*
|
||||
* @author Haotian Zhang, Andrew Shan, Jie Cheng
|
||||
*/
|
||||
@Component
|
||||
public class PolarisDiscoveryHandler {
|
||||
|
||||
@Autowired
|
||||
private PolarisProperties polarisProperties;
|
||||
|
||||
@Autowired
|
||||
private ProviderAPI providerAPI;
|
||||
|
||||
@Autowired
|
||||
private ConsumerAPI polarisConsumer;
|
||||
|
||||
/**
|
||||
* Get a list of instances after service routing.
|
||||
* @param service service name
|
||||
* @return list of instances
|
||||
*/
|
||||
public InstancesResponse getFilteredInstances(String service) {
|
||||
String namespace = polarisProperties.getNamespace();
|
||||
GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
|
||||
getInstancesRequest.setNamespace(namespace);
|
||||
getInstancesRequest.setService(service);
|
||||
String method = MetadataContextHolder.get()
|
||||
.getSystemMetadata(SystemMetadataKey.PEER_PATH);
|
||||
getInstancesRequest.setMethod(method);
|
||||
String localNamespace = MetadataContextHolder.get()
|
||||
.getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
|
||||
String localService = MetadataContextHolder.get()
|
||||
.getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
|
||||
Map<String, String> allTransitiveCustomMetadata = MetadataContextHolder.get()
|
||||
.getAllTransitiveCustomMetadata();
|
||||
if (StringUtils.isNotBlank(localNamespace) || StringUtils.isNotBlank(localService)
|
||||
|| null != allTransitiveCustomMetadata) {
|
||||
ServiceInfo sourceService = new ServiceInfo();
|
||||
sourceService.setNamespace(localNamespace);
|
||||
sourceService.setService(localService);
|
||||
sourceService.setMetadata(allTransitiveCustomMetadata);
|
||||
getInstancesRequest.setServiceInfo(sourceService);
|
||||
}
|
||||
return polarisConsumer.getInstances(getInstancesRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all instances for the given service.
|
||||
* @param service serviceName
|
||||
* @return list of instances
|
||||
*/
|
||||
public InstancesResponse getInstances(String service) {
|
||||
String namespace = polarisProperties.getNamespace();
|
||||
GetAllInstancesRequest request = new GetAllInstancesRequest();
|
||||
request.setNamespace(namespace);
|
||||
request.setService(service);
|
||||
return polarisConsumer.getAllInstance(request);
|
||||
}
|
||||
|
||||
public ProviderAPI getProviderAPI() {
|
||||
return providerAPI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all service for given namespace.
|
||||
* @return service list
|
||||
*/
|
||||
public ServicesResponse GetServices() {
|
||||
String namespace = polarisProperties.getNamespace();
|
||||
GetServicesRequest request = new GetServicesRequest();
|
||||
request.setNamespace(namespace);
|
||||
return polarisConsumer.getServices(request);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
|
||||
import com.tencent.polaris.api.exception.PolarisException;
|
||||
import com.tencent.polaris.api.pojo.Instance;
|
||||
import com.tencent.polaris.api.pojo.ServiceInfo;
|
||||
import com.tencent.polaris.api.pojo.ServiceInstances;
|
||||
import com.tencent.polaris.api.rpc.InstancesResponse;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
|
||||
/**
|
||||
* @author Haotian Zhang, Andrew Shan, Jie Cheng
|
||||
*/
|
||||
public class PolarisServiceDiscovery {
|
||||
|
||||
private final PolarisDiscoveryHandler polarisDiscoveryHandler;
|
||||
|
||||
public PolarisServiceDiscovery(PolarisDiscoveryHandler polarisDiscoveryHandler) {
|
||||
this.polarisDiscoveryHandler = polarisDiscoveryHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all instances for the given service.
|
||||
* @param serviceId id of service
|
||||
* @return list of instances
|
||||
* @throws PolarisException polarisException
|
||||
*/
|
||||
public List<ServiceInstance> getInstances(String serviceId) throws PolarisException {
|
||||
List<ServiceInstance> instances = new ArrayList<>();
|
||||
InstancesResponse filteredInstances = polarisDiscoveryHandler
|
||||
.getFilteredInstances(serviceId);
|
||||
ServiceInstances serviceInstances = filteredInstances.toServiceInstances();
|
||||
for (Instance instance : serviceInstances.getInstances()) {
|
||||
instances.add(new PolarisServiceInstance(instance));
|
||||
}
|
||||
return instances;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the names of all services.
|
||||
* @return list of service names
|
||||
* @throws PolarisException polarisException
|
||||
*/
|
||||
public List<String> getServices() throws PolarisException {
|
||||
return polarisDiscoveryHandler.GetServices().getServices().stream()
|
||||
.map(ServiceInfo::getService).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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());
|
||||
}
|
||||
|
||||
}
|
7
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfiguration.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfiguration.java
7
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfiguration.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfiguration.java
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.registry;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Haotian Zhang, Andrew Shan, Jie Cheng
|
||||
*/
|
||||
public class PolarisAutoServiceRegistration
|
||||
extends AbstractAutoServiceRegistration<Registration> {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(PolarisAutoServiceRegistration.class);
|
||||
|
||||
private final PolarisRegistration registration;
|
||||
|
||||
public PolarisAutoServiceRegistration(ServiceRegistry<Registration> serviceRegistry,
|
||||
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
|
||||
PolarisRegistration registration) {
|
||||
super(serviceRegistry, autoServiceRegistrationProperties);
|
||||
this.registration = registration;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PolarisRegistration getRegistration() {
|
||||
if (this.registration.getPort() <= 0) {
|
||||
this.registration.setPort(this.getPort().get());
|
||||
}
|
||||
return this.registration;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PolarisRegistration getManagementRegistration() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void register() {
|
||||
if (!this.registration.getPolarisProperties().isRegisterEnabled()) {
|
||||
log.debug("Registration disabled.");
|
||||
return;
|
||||
}
|
||||
if (this.registration.getPort() <= 0) {
|
||||
this.registration.setPort(getPort().get());
|
||||
}
|
||||
super.register();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void registerManagement() {
|
||||
if (!this.registration.getPolarisProperties().isRegisterEnabled()) {
|
||||
return;
|
||||
}
|
||||
super.registerManagement();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object getConfiguration() {
|
||||
return this.registration.getPolarisProperties();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isEnabled() {
|
||||
return this.registration.getPolarisProperties().isRegisterEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
protected String getAppName() {
|
||||
String appName = registration.getPolarisProperties().getService();
|
||||
return StringUtils.isEmpty(appName) ? super.getAppName() : appName;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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.registry;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.metadata.context.MetadataContextHolder;
|
||||
import com.tencent.cloud.polaris.PolarisProperties;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
|
||||
/**
|
||||
* @author Haotian Zhang, Andrew Shan, Jie Cheng
|
||||
*/
|
||||
public class PolarisRegistration implements Registration, ServiceInstance {
|
||||
|
||||
private final PolarisProperties polarisProperties;
|
||||
|
||||
private final SDKContext polarisContext;
|
||||
|
||||
public PolarisRegistration(PolarisProperties polarisProperties, SDKContext context) {
|
||||
this.polarisProperties = polarisProperties;
|
||||
this.polarisContext = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return polarisProperties.getService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
if (StringUtils.isNotBlank(polarisProperties.getIpAddress())) {
|
||||
return polarisProperties.getIpAddress();
|
||||
}
|
||||
return polarisContext.getConfig().getGlobal().getAPI().getBindIP();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return polarisProperties.getPort();
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.polarisProperties.setPort(port);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSecure() {
|
||||
return StringUtils.equalsIgnoreCase(polarisProperties.getProtocol(), "https");
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
return DefaultServiceInstance.getUri(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getMetadata() {
|
||||
return MetadataContextHolder.get().getAllSystemMetadata();
|
||||
}
|
||||
|
||||
public PolarisProperties getPolarisProperties() {
|
||||
return polarisProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PolarisRegistration{" + "polarisProperties=" + polarisProperties
|
||||
+ ", polarisContext=" + polarisContext + '}';
|
||||
}
|
||||
|
||||
}
|
22
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java
22
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java
1
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisDiscoveryRibbonAutoConfiguration.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisDiscoveryRibbonAutoConfiguration.java
1
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisDiscoveryRibbonAutoConfiguration.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisDiscoveryRibbonAutoConfiguration.java
5
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfiguration.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfiguration.java
5
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfiguration.java → spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfiguration.java
17
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfigurationTest.java
17
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfigurationTest.java
28
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfigurationTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfigurationTest.java
28
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfigurationTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfigurationTest.java
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
|
||||
import com.tencent.polaris.api.exception.PolarisException;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.test.mock.discovery.NamingServer;
|
||||
import com.tencent.polaris.test.mock.discovery.NamingService;
|
||||
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.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
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 PolarisServiceDiscovery}
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisServiceDiscoveryTest {
|
||||
|
||||
private static NamingServer namingServer;
|
||||
|
||||
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class,
|
||||
PolarisServiceDiscoveryTest.PolarisPropertiesConfiguration.class,
|
||||
PolarisDiscoveryClientConfiguration.class,
|
||||
PolarisDiscoveryAutoConfiguration.class))
|
||||
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
|
||||
.withPropertyValues("server.port=" + PORT)
|
||||
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
|
||||
.withPropertyValues(
|
||||
"spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST)
|
||||
.withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx");
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
namingServer = NamingServer.startNamingServer(10081);
|
||||
|
||||
// add service with 3 instances
|
||||
NamingService.InstanceParameter instanceParameter = new NamingService.InstanceParameter();
|
||||
instanceParameter.setHealthy(true);
|
||||
instanceParameter.setIsolated(false);
|
||||
instanceParameter.setWeight(100);
|
||||
ServiceKey serviceKey = new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER);
|
||||
namingServer.getNamingService().batchAddInstances(serviceKey, PORT, 3,
|
||||
instanceParameter);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
if (null != namingServer) {
|
||||
namingServer.terminate();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetInstances() {
|
||||
this.contextRunner.run(context -> {
|
||||
PolarisServiceDiscovery polarisServiceDiscovery = context
|
||||
.getBean(PolarisServiceDiscovery.class);
|
||||
List<ServiceInstance> serviceInstances = polarisServiceDiscovery
|
||||
.getInstances(SERVICE_PROVIDER);
|
||||
assertThat(serviceInstances.isEmpty()).isFalse();
|
||||
assertThat(serviceInstances).hasSize(3);
|
||||
assertThat(serviceInstances.get(0).getPort()).isEqualTo(PORT);
|
||||
assertThat(serviceInstances.get(1).getPort()).isEqualTo(PORT + 1);
|
||||
assertThat(serviceInstances.get(2).getPort()).isEqualTo(PORT + 2);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetServices() throws PolarisException {
|
||||
this.contextRunner.run(context -> {
|
||||
PolarisServiceDiscovery polarisServiceDiscovery = context
|
||||
.getBean(PolarisServiceDiscovery.class);
|
||||
List<String> services = polarisServiceDiscovery.getServices();
|
||||
assertThat(services.size()).isEqualTo(1);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@EnableDiscoveryClient
|
||||
static class PolarisPropertiesConfiguration {
|
||||
|
||||
}
|
||||
|
||||
}
|
20
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfigurationTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfigurationTest.java
20
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfigurationTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfigurationTest.java
32
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientTest.java
32
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientTest.java
21
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfigurationTest.java
21
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfigurationTest.java
@ -0,0 +1,120 @@
|
||||
/*
|
||||
* 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.registry;
|
||||
|
||||
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.test.mock.discovery.NamingServer;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
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.NAMESPACE_TEST;
|
||||
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;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisServiceRegistry}
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisServiceRegistryTest {
|
||||
|
||||
private static NamingServer namingServer;
|
||||
|
||||
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class,
|
||||
PolarisPropertiesConfiguration.class,
|
||||
PolarisDiscoveryClientConfiguration.class,
|
||||
PolarisDiscoveryAutoConfiguration.class))
|
||||
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
|
||||
.withPropertyValues("server.port=" + PORT)
|
||||
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
|
||||
.withPropertyValues(
|
||||
"spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST)
|
||||
.withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx");
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
namingServer = NamingServer.startNamingServer(10081);
|
||||
|
||||
// add service
|
||||
namingServer.getNamingService()
|
||||
.addService(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
if (null != namingServer) {
|
||||
namingServer.terminate();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegister() {
|
||||
this.contextRunner.run(context -> {
|
||||
PolarisServiceRegistry registry = context
|
||||
.getBean(PolarisServiceRegistry.class);
|
||||
PolarisRegistration registration = Mockito.mock(PolarisRegistration.class);
|
||||
when(registration.getHost()).thenReturn("127.0.0.1");
|
||||
when(registration.getPort()).thenReturn(PORT);
|
||||
when(registration.getServiceId()).thenReturn(SERVICE_PROVIDER);
|
||||
|
||||
try {
|
||||
registry.register(registration);
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail();
|
||||
}
|
||||
|
||||
try {
|
||||
assertThat(registry.getStatus(registration)).isEqualTo("DOWN");
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail();
|
||||
}
|
||||
|
||||
try {
|
||||
registry.deregister(registration);
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@EnableDiscoveryClient
|
||||
static class PolarisPropertiesConfiguration {
|
||||
|
||||
}
|
||||
|
||||
}
|
27
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfigurationTest.java
27
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfigurationTest.java
@ -0,0 +1,145 @@
|
||||
/*
|
||||
* 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.ribbon;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.test.mock.discovery.NamingServer;
|
||||
import com.tencent.polaris.test.mock.discovery.NamingService;
|
||||
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.NAMESPACE_TEST;
|
||||
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;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisServerList}
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisServerListTest {
|
||||
|
||||
private static NamingServer namingServer;
|
||||
|
||||
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class,
|
||||
PolarisServerListTest.PolarisPropertiesConfiguration.class,
|
||||
PolarisDiscoveryClientConfiguration.class,
|
||||
PolarisDiscoveryAutoConfiguration.class))
|
||||
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
|
||||
.withPropertyValues("server.port=" + PORT)
|
||||
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
|
||||
.withPropertyValues(
|
||||
"spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST)
|
||||
.withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx");
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
namingServer = NamingServer.startNamingServer(10081);
|
||||
|
||||
// add service
|
||||
namingServer.getNamingService()
|
||||
.addService(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
if (null != namingServer) {
|
||||
namingServer.terminate();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test {@link PolarisServerList#getInitialListOfServers()} with empty server list.
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void test1() {
|
||||
this.contextRunner.run(context -> {
|
||||
// mock
|
||||
IClientConfig iClientConfig = mock(IClientConfig.class);
|
||||
when(iClientConfig.getClientName()).thenReturn(SERVICE_PROVIDER);
|
||||
PolarisDiscoveryHandler polarisDiscoveryHandler = context
|
||||
.getBean(PolarisDiscoveryHandler.class);
|
||||
PolarisServerList serverList = new PolarisServerList(polarisDiscoveryHandler);
|
||||
serverList.initWithNiwsConfig(iClientConfig);
|
||||
|
||||
List<Server> servers = serverList.getInitialListOfServers();
|
||||
assertThat(servers).isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Test {@link PolarisServerList#getUpdatedListOfServers()} with server list of size
|
||||
* 3.
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void test2() throws Exception {
|
||||
this.contextRunner.run(context -> {
|
||||
// mock
|
||||
IClientConfig iClientConfig = mock(IClientConfig.class);
|
||||
when(iClientConfig.getClientName()).thenReturn(SERVICE_PROVIDER);
|
||||
PolarisDiscoveryHandler polarisDiscoveryHandler = context
|
||||
.getBean(PolarisDiscoveryHandler.class);
|
||||
PolarisServerList serverList = new PolarisServerList(polarisDiscoveryHandler);
|
||||
serverList.initWithNiwsConfig(iClientConfig);
|
||||
|
||||
// add service with 3 instances
|
||||
NamingService.InstanceParameter instanceParameter = new NamingService.InstanceParameter();
|
||||
instanceParameter.setHealthy(true);
|
||||
instanceParameter.setIsolated(false);
|
||||
instanceParameter.setWeight(100);
|
||||
ServiceKey serviceKey = new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER);
|
||||
namingServer.getNamingService().batchAddInstances(serviceKey, PORT, 3,
|
||||
instanceParameter);
|
||||
|
||||
List<Server> servers = serverList.getUpdatedListOfServers();
|
||||
assertThat(servers).hasSize(3);
|
||||
assertThat(servers.get(0).getPort()).isEqualTo(PORT);
|
||||
assertThat(servers.get(1).getPort()).isEqualTo(PORT + 1);
|
||||
assertThat(servers.get(2).getPort()).isEqualTo(PORT + 2);
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@EnableDiscoveryClient
|
||||
static class PolarisPropertiesConfiguration {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-cloud-tencent</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-starter-tencent-polaris-ratelimit</artifactId>
|
||||
<name>Spring Cloud Starter Tencent Polaris Ratelimit</name>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring Cloud Tencent dependencies start -->
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-commons</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-polaris-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-commons</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-metadata</artifactId>
|
||||
</dependency>
|
||||
<!-- Spring Cloud Tencent dependencies end -->
|
||||
|
||||
<!-- Polaris dependencies start -->
|
||||
<dependency>
|
||||
<groupId>com.tencent.polaris</groupId>
|
||||
<artifactId>polaris-ratelimit-factory</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencent.polaris</groupId>
|
||||
<artifactId>polaris-test-common</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencent.polaris</groupId>
|
||||
<artifactId>polaris-test-mock-discovery</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- Polaris dependencies end -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.tencent.cloud.polaris.ratelimit.config.RateLimitConfiguration
|
@ -0,0 +1,150 @@
|
||||
/*
|
||||
* 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.ratelimit.controller;
|
||||
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.ratelimit.api.core.LimitAPI;
|
||||
import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse;
|
||||
import com.tencent.polaris.ratelimit.api.rpc.QuotaResultCode;
|
||||
import com.tencent.polaris.test.mock.discovery.NamingServer;
|
||||
import com.tencent.polaris.test.mock.discovery.NamingService;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.client.HttpClientErrorException.TooManyRequests;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
|
||||
import static com.tencent.polaris.test.common.Consts.PORT;
|
||||
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Test for rate-limit.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
classes = { CalleeControllerTests.Config.class, TestController.class },
|
||||
properties = { "spring.application.name=java_provider_test",
|
||||
"spring.cloud.polaris.discovery.namespace=Test",
|
||||
"spring.cloud.polaris.address=grpc://127.0.0.1:10081" })
|
||||
public class CalleeControllerTests {
|
||||
|
||||
private static NamingServer namingServer;
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@MockBean
|
||||
private LimitAPI limitAPI;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
namingServer = NamingServer.startNamingServer(10081);
|
||||
|
||||
// add service with 3 instances
|
||||
NamingService.InstanceParameter instanceParameter = new NamingService.InstanceParameter();
|
||||
instanceParameter.setHealthy(true);
|
||||
instanceParameter.setIsolated(false);
|
||||
instanceParameter.setWeight(100);
|
||||
ServiceKey serviceKey = new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER);
|
||||
namingServer.getNamingService().batchAddInstances(serviceKey, PORT, 3,
|
||||
instanceParameter);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
if (null != namingServer) {
|
||||
namingServer.terminate();
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
QuotaResponse quotaResponse = mock(QuotaResponse.class);
|
||||
when(quotaResponse.getCode()).thenReturn(QuotaResultCode.QuotaResultOk);
|
||||
when(limitAPI.getQuota(any())).thenReturn(quotaResponse);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1() {
|
||||
String url = "http://localhost:" + port + "/test/info";
|
||||
|
||||
boolean hasPassed = false;
|
||||
boolean hasLimited = false;
|
||||
for (int i = 0; i < 30; i++) {
|
||||
try {
|
||||
if (i > 9) {
|
||||
QuotaResponse quotaResponse = mock(QuotaResponse.class);
|
||||
when(quotaResponse.getCode())
|
||||
.thenReturn(QuotaResultCode.QuotaResultLimited);
|
||||
when(quotaResponse.getInfo())
|
||||
.thenReturn("Testing rate limit after 10 times success.");
|
||||
when(limitAPI.getQuota(any())).thenReturn(quotaResponse);
|
||||
}
|
||||
String result = restTemplate.getForObject(url, String.class);
|
||||
System.out.println(result + " [" + i + "]");
|
||||
hasPassed = true;
|
||||
}
|
||||
catch (RestClientException e) {
|
||||
if (e instanceof TooManyRequests) {
|
||||
System.out.println(((TooManyRequests) e).getResponseBodyAsString());
|
||||
hasLimited = true;
|
||||
}
|
||||
else {
|
||||
e.printStackTrace();
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Assert.assertTrue(hasPassed);
|
||||
Assert.assertTrue(hasLimited);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
public static class Config {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-cloud-tencent-starters</artifactId>
|
||||
<artifactId>spring-cloud-tencent</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
@ -0,0 +1,138 @@
|
||||
/*
|
||||
* 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.router;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.DynamicServerListLoadBalancer;
|
||||
import com.netflix.loadbalancer.IPing;
|
||||
import com.netflix.loadbalancer.IRule;
|
||||
import com.netflix.loadbalancer.PollingServerListUpdater;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.ServerList;
|
||||
import com.tencent.cloud.common.pojo.PolarisServer;
|
||||
import com.tencent.cloud.metadata.constant.MetadataConstant.SystemMetadataKey;
|
||||
import com.tencent.cloud.metadata.context.MetadataContextHolder;
|
||||
import com.tencent.polaris.api.pojo.DefaultInstance;
|
||||
import com.tencent.polaris.api.pojo.DefaultServiceInstances;
|
||||
import com.tencent.polaris.api.pojo.Instance;
|
||||
import com.tencent.polaris.api.pojo.ServiceInfo;
|
||||
import com.tencent.polaris.api.pojo.ServiceInstances;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||
import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest;
|
||||
import com.tencent.polaris.router.api.rpc.ProcessRoutersResponse;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Routing load balancer of polaris.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Server> {
|
||||
|
||||
private final RouterAPI routerAPI;
|
||||
|
||||
public PolarisRoutingLoadBalancer(IClientConfig config, IRule rule, IPing ping,
|
||||
ServerList<Server> serverList, RouterAPI routerAPI) {
|
||||
super(config, rule, ping, serverList, null, new PollingServerListUpdater());
|
||||
this.routerAPI = routerAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Server> getReachableServers() {
|
||||
List<Server> allServers = super.getAllServers();
|
||||
if (CollectionUtils.isEmpty(allServers)) {
|
||||
return allServers;
|
||||
}
|
||||
ServiceInstances serviceInstances = null;
|
||||
if (allServers.get(0) instanceof PolarisServer) {
|
||||
serviceInstances = ((PolarisServer) allServers.get(0)).getServiceInstances();
|
||||
}
|
||||
else {
|
||||
String serviceName;
|
||||
// notice the difference between different service registries
|
||||
if (StringUtils.isNotBlank(
|
||||
allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) {
|
||||
serviceName = allServers.get(0).getMetaInfo().getServiceIdForDiscovery();
|
||||
}
|
||||
else {
|
||||
serviceName = allServers.get(0).getMetaInfo().getAppName();
|
||||
}
|
||||
if (StringUtils.isBlank(serviceName)) {
|
||||
throw new IllegalStateException(
|
||||
"PolarisRoutingLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute");
|
||||
}
|
||||
ServiceKey serviceKey = new ServiceKey(MetadataContextHolder.LOCAL_NAMESPACE,
|
||||
serviceName);
|
||||
List<Instance> instances = new ArrayList<>(8);
|
||||
for (Server server : allServers) {
|
||||
DefaultInstance instance = new DefaultInstance();
|
||||
instance.setNamespace(MetadataContextHolder.LOCAL_NAMESPACE);
|
||||
instance.setService(serviceName);
|
||||
instance.setHealthy(server.isAlive());
|
||||
instance.setProtocol(server.getScheme());
|
||||
instance.setId(server.getId());
|
||||
instance.setHost(server.getHost());
|
||||
instance.setPort(server.getPort());
|
||||
instance.setZone(server.getZone());
|
||||
instance.setWeight(100);
|
||||
instances.add(instance);
|
||||
}
|
||||
serviceInstances = new DefaultServiceInstances(serviceKey, instances);
|
||||
}
|
||||
ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
|
||||
processRoutersRequest.setDstInstances(serviceInstances);
|
||||
String srcNamespace = MetadataContextHolder.get()
|
||||
.getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
|
||||
String srcService = MetadataContextHolder.get()
|
||||
.getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
|
||||
Map<String, String> transitiveCustomMetadata = MetadataContextHolder.get()
|
||||
.getAllTransitiveCustomMetadata();
|
||||
String method = MetadataContextHolder.get()
|
||||
.getSystemMetadata(SystemMetadataKey.PEER_PATH);
|
||||
processRoutersRequest.setMethod(method);
|
||||
if (StringUtils.isNotBlank(srcNamespace) && StringUtils.isNotBlank(srcService)) {
|
||||
ServiceInfo serviceInfo = new ServiceInfo();
|
||||
serviceInfo.setNamespace(srcNamespace);
|
||||
serviceInfo.setService(srcService);
|
||||
serviceInfo.setMetadata(transitiveCustomMetadata);
|
||||
processRoutersRequest.setSourceService(serviceInfo);
|
||||
}
|
||||
ProcessRoutersResponse processRoutersResponse = routerAPI
|
||||
.processRouters(processRoutersRequest);
|
||||
ServiceInstances filteredServiceInstances = processRoutersResponse
|
||||
.getServiceInstances();
|
||||
List<Server> filteredInstances = new ArrayList<>();
|
||||
for (Instance instance : filteredServiceInstances.getInstances()) {
|
||||
filteredInstances.add(new PolarisServer(serviceInstances, instance));
|
||||
}
|
||||
return filteredInstances;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Server> getAllServers() {
|
||||
return getReachableServers();
|
||||
}
|
||||
|
||||
}
|
5
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java → spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java
5
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java → spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java
13
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonClientConfiguration.java → spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonClientConfiguration.java
13
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonClientConfiguration.java → spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonClientConfiguration.java
9
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java
9
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java → spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java
@ -0,0 +1,79 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-cloud-tencent</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-tencent-commons</artifactId>
|
||||
<name>Spring Cloud Tencent Commons</name>
|
||||
|
||||
<properties>
|
||||
<commons.collections.version>3.2.2</commons.collections.version>
|
||||
<commons.lang.version>2.5</commons.lang.version>
|
||||
<commons.io.version>2.7</commons.io.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Polaris dependencies start -->
|
||||
<dependency>
|
||||
<groupId>com.tencent.polaris</groupId>
|
||||
<artifactId>polaris-model</artifactId>
|
||||
</dependency>
|
||||
<!-- Polaris dependencies end -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-json</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.netflix.ribbon</groupId>
|
||||
<artifactId>ribbon-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-collections</groupId>
|
||||
<artifactId>commons-collections</artifactId>
|
||||
<version>${commons.collections.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>${commons.lang.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons.io.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* 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.pojo;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.tencent.polaris.api.pojo.Instance;
|
||||
import com.tencent.polaris.api.pojo.ServiceInstances;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
/**
|
||||
* Polaris' implementation of {@link Server}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisServer extends Server {
|
||||
|
||||
private final ServiceInstances serviceInstances;
|
||||
|
||||
private final Instance instance;
|
||||
|
||||
private final MetaInfo metaInfo;
|
||||
|
||||
public PolarisServer(ServiceInstances serviceInstances, Instance instance) {
|
||||
super(instance.getHost(), instance.getPort());
|
||||
if (StringUtils.equalsIgnoreCase(instance.getProtocol(), "https")) {
|
||||
setSchemea("https");
|
||||
}
|
||||
else {
|
||||
setSchemea("http");
|
||||
}
|
||||
this.serviceInstances = serviceInstances;
|
||||
this.instance = instance;
|
||||
this.metaInfo = new MetaInfo() {
|
||||
@Override
|
||||
public String getAppName() {
|
||||
return instance.getService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServerGroup() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceIdForDiscovery() {
|
||||
return instance.getService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return instance.getId();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Instance getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MetaInfo getMetaInfo() {
|
||||
return metaInfo;
|
||||
}
|
||||
|
||||
public Map<String, String> getMetadata() {
|
||||
return instance.getMetadata();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
if (!super.equals(o)) {
|
||||
return false;
|
||||
}
|
||||
PolarisServer that = (PolarisServer) o;
|
||||
return Objects.equals(instance, that.instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), instance);
|
||||
}
|
||||
|
||||
public ServiceInstances getServiceInstances() {
|
||||
return serviceInstances;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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.pojo;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.polaris.api.pojo.Instance;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
|
||||
/**
|
||||
* Polaris's implementation of {@link ServiceInstance}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisServiceInstance implements ServiceInstance {
|
||||
|
||||
private final Instance instance;
|
||||
|
||||
private final boolean isSecure;
|
||||
|
||||
private final String scheme;
|
||||
|
||||
public PolarisServiceInstance(Instance instance) {
|
||||
this.instance = instance;
|
||||
this.isSecure = StringUtils.equalsIgnoreCase(instance.getProtocol(), "https");
|
||||
if (isSecure) {
|
||||
scheme = "https";
|
||||
}
|
||||
else {
|
||||
scheme = "http";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return ServiceInstance.super.getInstanceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceId() {
|
||||
return instance.getService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
return instance.getHost();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return instance.getPort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSecure() {
|
||||
return this.isSecure;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
return DefaultServiceInstance.getUri(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getMetadata() {
|
||||
return instance.getMetadata();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getScheme() {
|
||||
return this.scheme;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Utils for Jackson.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public final class JacksonUtils {
|
||||
|
||||
/**
|
||||
* Object Mapper.
|
||||
*/
|
||||
public static final ObjectMapper OM = new ObjectMapper();
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JacksonUtils.class);
|
||||
|
||||
private JacksonUtils() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Object to Json.
|
||||
* @param object object to be serialized
|
||||
* @param <T> type of object
|
||||
* @return Json String
|
||||
*/
|
||||
public static <T> String serialize2Json(T object) {
|
||||
try {
|
||||
return OM.writeValueAsString(object);
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
LOG.error("Object to Json failed. {}", object, e);
|
||||
throw new RuntimeException("Object to Json failed.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Json to Map.
|
||||
* @param jsonStr Json String
|
||||
* @return Map
|
||||
*/
|
||||
public static Map<String, String> deserialize2Map(String jsonStr) {
|
||||
try {
|
||||
if (StringUtils.hasText(jsonStr)) {
|
||||
return OM.readValue(jsonStr, Map.class);
|
||||
}
|
||||
return new HashMap<>();
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
LOG.error(
|
||||
"Json to map failed. check if the format of the json string[{}] is correct.",
|
||||
jsonStr, e);
|
||||
throw new RuntimeException("Json to map failed.", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,3 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.tencent.cloud.common.util.ApplicationContextAwareUtils
|
||||
|
@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>spring-cloud-tencent</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-tencent-metadata</artifactId>
|
||||
<name>Spring Cloud Tencent Metadata</name>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring Cloud Tencent dependencies start -->
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-commons</artifactId>
|
||||
</dependency>
|
||||
<!-- Spring Cloud Tencent dependencies end -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- powermock-module-junit4 -->
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- powermock-api-mockito -->
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue