parent
d6d663f07e
commit
085ae91f77
@ -1,38 +0,0 @@
|
||||
<?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"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>polaris-gateway-example</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>gateway-zuul-service</artifactId>
|
||||
<name>Spring Cloud Starter Tencent Polaris Gateway Zuul Example</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-polaris-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,20 +0,0 @@
|
||||
server:
|
||||
session-timeout: 1800
|
||||
port: 48082
|
||||
spring:
|
||||
application:
|
||||
name: GatewayZuulService
|
||||
cloud:
|
||||
polaris:
|
||||
address: grpc://127.0.0.1:8091
|
||||
tencent:
|
||||
metadata:
|
||||
content:
|
||||
a: 1
|
||||
transitive:
|
||||
- a
|
||||
zuul:
|
||||
routes:
|
||||
GatewayCalleeService:
|
||||
serviceId: GatewayCalleeService
|
||||
path: /GatewayCalleeService/**
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.polaris.loadbalancer;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.polaris.enabled", havingValue = "true")
|
||||
public @interface ConditionalOnLoadBalancerPolaris {
|
||||
|
||||
}
|
14
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisDiscoveryRibbonAutoConfiguration.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerPolarisAutoConfiguration.java
14
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisDiscoveryRibbonAutoConfiguration.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerPolarisAutoConfiguration.java
19
spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/java/com/tencent/cloud/polaris/gateway/example/zuul/GatewayZuulApplication.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerClientConfiguration.java
19
spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/java/com/tencent/cloud/polaris/gateway/example/zuul/GatewayZuulApplication.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerClientConfiguration.java
@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package com.tencent.cloud.polaris.loadbalancer;
|
||||
|
||||
import com.tencent.cloud.polaris.PolarisProperties;
|
||||
import java.util.List;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.DefaultResponse;
|
||||
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
|
||||
import org.springframework.cloud.client.loadbalancer.Request;
|
||||
import org.springframework.cloud.client.loadbalancer.Response;
|
||||
import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
|
||||
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
|
||||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
||||
*/
|
||||
public class PolarisLoadbalancer implements ReactorServiceInstanceLoadBalancer {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(PolarisLoadbalancer.class);
|
||||
|
||||
private final String serviceId;
|
||||
|
||||
private ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider;
|
||||
|
||||
private final PolarisProperties discoveryProperties;
|
||||
|
||||
public PolarisLoadbalancer(String serviceId,
|
||||
ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider,
|
||||
PolarisProperties discoveryProperties) {
|
||||
this.serviceId = serviceId;
|
||||
this.supplierObjectProvider = supplierObjectProvider;
|
||||
this.discoveryProperties = discoveryProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Response<ServiceInstance>> choose(Request request) {
|
||||
ServiceInstanceListSupplier supplier = supplierObjectProvider.getIfAvailable(NoopServiceInstanceListSupplier::new);
|
||||
return supplier.get().next().map(this::getInstanceResponse);
|
||||
}
|
||||
|
||||
private Response<ServiceInstance> getInstanceResponse(List<ServiceInstance> serviceInstances) {
|
||||
if (serviceInstances.isEmpty()) {
|
||||
log.warn("No servers available for service: " + this.serviceId);
|
||||
return new EmptyResponse();
|
||||
}
|
||||
|
||||
try {
|
||||
ServiceInstance instance = NacosBalancer.getHostByRandomWeight3(serviceInstances);
|
||||
return new DefaultResponse(instance);
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.warn("NacosLoadBalancer error", e);
|
||||
return new EmptyResponse();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.polaris.ribbon;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.AbstractServerList;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.tencent.cloud.polaris.pojo.PolarisServer;
|
||||
import com.tencent.polaris.api.pojo.Instance;
|
||||
import com.tencent.polaris.api.pojo.ServiceInstances;
|
||||
import com.tencent.polaris.api.rpc.InstancesResponse;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Haotian Zhang, Andrew Shan, Jie Cheng
|
||||
*/
|
||||
public class PolarisServerList extends AbstractServerList<Server> {
|
||||
|
||||
private String serviceId;
|
||||
|
||||
private PolarisDiscoveryHandler polarisDiscoveryHandler;
|
||||
|
||||
public PolarisServerList(PolarisDiscoveryHandler polarisDiscoveryHandler) {
|
||||
this.polarisDiscoveryHandler = polarisDiscoveryHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Server> getInitialListOfServers() {
|
||||
return getServers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Server> getUpdatedListOfServers() {
|
||||
return getServers();
|
||||
}
|
||||
|
||||
private List<Server> getServers() {
|
||||
InstancesResponse filteredInstances = polarisDiscoveryHandler.getInstances(serviceId);
|
||||
ServiceInstances serviceInstances = filteredInstances.toServiceInstances();
|
||||
List<Server> polarisServers = new ArrayList<>();
|
||||
for (Instance instance : serviceInstances.getInstances()) {
|
||||
polarisServers.add(new PolarisServer(serviceInstances, instance));
|
||||
}
|
||||
return polarisServers;
|
||||
}
|
||||
|
||||
public String getServiceId() {
|
||||
return serviceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initWithNiwsConfig(IClientConfig iClientConfig) {
|
||||
this.serviceId = iClientConfig.getClientName();
|
||||
}
|
||||
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.polaris.router.config;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
import com.netflix.loadbalancer.IPing;
|
||||
import com.netflix.loadbalancer.IRule;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.ServerList;
|
||||
import com.tencent.cloud.polaris.router.PolarisRoutingLoadBalancer;
|
||||
import com.tencent.cloud.polaris.router.rule.PolarisLoadBalanceRule;
|
||||
import com.tencent.cloud.polaris.router.rule.PolarisWeightedRandomRule;
|
||||
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@Configuration
|
||||
public class PolarisRibbonClientConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public IRule polarisRibbonRule(PolarisRibbonProperties polarisRibbonProperties) {
|
||||
switch (PolarisLoadBalanceRule.fromStrategy(polarisRibbonProperties.getPolicy())) {
|
||||
case WEIGHTED_RANDOM_RULE:
|
||||
default:
|
||||
return new PolarisWeightedRandomRule();
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ILoadBalancer polarisRoutingLoadBalancer(IClientConfig iClientConfig, IRule iRule, IPing iPing,
|
||||
ServerList<Server> serverList, RouterAPI polarisRouter) {
|
||||
return new PolarisRoutingLoadBalancer(iClientConfig, iRule, iPing, serverList, polarisRouter);
|
||||
}
|
||||
}
|
13
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRouterAutoConfiguration.java
13
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRouterAutoConfiguration.java
21
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfiguration.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRouterClientConfiguration.java
21
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfiguration.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRouterClientConfiguration.java
2
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonProperties.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRouterProperties.java
2
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonProperties.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRouterProperties.java
@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.polaris.router.rule;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.tencent.cloud.polaris.pojo.PolarisServer;
|
||||
import com.tencent.polaris.api.config.consumer.LoadBalanceConfig;
|
||||
import com.tencent.polaris.api.pojo.Instance;
|
||||
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;
|
||||
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisWeightedRandomRule extends AbstractLoadBalancerRule {
|
||||
|
||||
private static final String POLICY = LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM;
|
||||
|
||||
@Autowired
|
||||
private RouterAPI polarisRouter;
|
||||
|
||||
@Override
|
||||
public void initWithNiwsConfig(IClientConfig clientConfig) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Server choose(Object key) {
|
||||
List<Server> allServers = getLoadBalancer().getReachableServers();
|
||||
if (CollectionUtils.isEmpty(allServers)) {
|
||||
return null;
|
||||
}
|
||||
Server server = allServers.get(0);
|
||||
if (!(server instanceof PolarisServer)) {
|
||||
throw new IllegalStateException("PolarisDiscoveryRule only support PolarisServer instances");
|
||||
}
|
||||
PolarisServer polarisServer = (PolarisServer) server;
|
||||
ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
|
||||
request.setDstInstances(polarisServer.getServiceInstances());
|
||||
request.setLbPolicy(POLICY);
|
||||
ProcessLoadBalanceResponse processLoadBalanceResponse = polarisRouter.processLoadBalance(request);
|
||||
Instance targetInstance = processLoadBalanceResponse.getTargetInstance();
|
||||
return new PolarisServer(polarisServer.getServiceInstances(), targetInstance);
|
||||
}
|
||||
}
|
@ -1,2 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.tencent.cloud.polaris.router.config.PolarisRibbonAutoConfiguration
|
||||
com.tencent.cloud.polaris.router.config.PolarisRouterAutoConfiguration
|
||||
|
@ -1,109 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.polaris.pojo;
|
||||
|
||||
import com.google.common.base.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;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 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.equal(instance, that.instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(super.hashCode(), instance);
|
||||
}
|
||||
|
||||
public ServiceInstances getServiceInstances() {
|
||||
return serviceInstances;
|
||||
}
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.polaris.gateway.core.zuul.filter;
|
||||
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.tencent.cloud.metadata.constant.MetadataConstant;
|
||||
import com.tencent.cloud.metadata.context.MetadataContext;
|
||||
import com.tencent.cloud.metadata.context.MetadataContextHolder;
|
||||
import com.tencent.cloud.metadata.util.JacksonUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.RIBBON_ROUTING_FILTER_ORDER;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.ROUTE_TYPE;
|
||||
|
||||
/**
|
||||
* Zuul filter used for writing metadata in HTTP request header.
|
||||
*
|
||||
* @author skyehtzhang
|
||||
*/
|
||||
public class Metadata2HeaderZuulFilter extends ZuulFilter {
|
||||
|
||||
@Override
|
||||
public String filterType() {
|
||||
return ROUTE_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return RIBBON_ROUTING_FILTER_ORDER - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object run() {
|
||||
// get request context
|
||||
RequestContext requestContext = RequestContext.getCurrentContext();
|
||||
|
||||
// get metadata of current thread
|
||||
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||
|
||||
// add new metadata and cover old
|
||||
Map<String, String> customMetadata = metadataContext.getAllTransitiveCustomMetadata();
|
||||
if (!CollectionUtils.isEmpty(customMetadata)) {
|
||||
String metadataStr = JacksonUtils.serialize2Json(customMetadata);
|
||||
try {
|
||||
requestContext.addZuulRequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA,
|
||||
URLEncoder.encode(metadataStr, "UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
requestContext.addZuulRequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
package com.tencent.cloud.polaris.gateway.core.zuul.filter;
|
||||
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.tencent.cloud.metadata.constant.MetadataConstant;
|
||||
import com.tencent.cloud.metadata.context.MetadataContextHolder;
|
||||
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.REQUEST_URI_KEY;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SERVICE_ID_KEY;
|
||||
|
||||
/**
|
||||
* Zuul filter used for setting peer info in context.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class MetadataFirstZuulFilter extends ZuulFilter {
|
||||
@Override
|
||||
public String filterType() {
|
||||
return PRE_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return PRE_DECORATION_FILTER_ORDER + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object run() {
|
||||
// get request context
|
||||
RequestContext requestContext = RequestContext.getCurrentContext();
|
||||
|
||||
// TODO 对端命名空间暂时与本地命名空间相同
|
||||
MetadataContextHolder.get().putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
|
||||
MetadataContextHolder.get().getSystemMetadata(MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE));
|
||||
MetadataContextHolder.get().putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE,
|
||||
(String) requestContext.get(SERVICE_ID_KEY));
|
||||
MetadataContextHolder.get().putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH,
|
||||
(String) requestContext.get(REQUEST_URI_KEY));
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue