Skip to content

Commit

Permalink
feat: add polaris router lane examples (#1340)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuyuwei01 authored Jul 16, 2024
1 parent 3c83ec9 commit b6e0e0e
Show file tree
Hide file tree
Showing 24 changed files with 859 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
- [fix: fix RouterLabelRestTemplateInterceptor add response headers exception with httpclient5.](https://github.com/Tencent/spring-cloud-tencent/pull/1337)
- [feat: support lossless online/offline](https://github.com/Tencent/spring-cloud-tencent/pull/1338)
- [feat: support lane router](https://github.com/Tencent/spring-cloud-tencent/pull/1339)
- [feat: add lane router examples](https://github.com/Tencent/spring-cloud-tencent/pull/1340)
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@

import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties;
import com.tencent.polaris.api.config.consumer.ServiceRouterConfig;
import com.tencent.polaris.api.plugin.route.LocationLevel;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.plugins.router.healthy.RecoverRouterConfig;
import com.tencent.polaris.plugins.router.nearby.NearbyRouterConfig;
import org.apache.commons.lang.StringUtils;

/**
* RouterConfigModifier.
Expand All @@ -31,6 +35,12 @@
*/
public class RouterConfigModifier implements PolarisConfigModifier {

private final PolarisNearByRouterProperties polarisNearByRouterProperties;

public RouterConfigModifier(PolarisNearByRouterProperties polarisNearByRouterProperties) {
this.polarisNearByRouterProperties = polarisNearByRouterProperties;
}

@Override
public void modify(ConfigurationImpl configuration) {
// Set excludeCircuitBreakInstances to false
Expand All @@ -41,6 +51,16 @@ public void modify(ConfigurationImpl configuration) {
// Update modified config to source properties
configuration.getConsumer().getServiceRouter()
.setPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_RECOVER, recoverRouterConfig);

if (StringUtils.isNotBlank(polarisNearByRouterProperties.getMatchLevel())) {
LocationLevel locationLevel = LocationLevel.valueOf(polarisNearByRouterProperties.getMatchLevel());
NearbyRouterConfig nearbyRouterConfig = configuration.getConsumer().getServiceRouter().getPluginConfig(
ServiceRouterConfig.DEFAULT_ROUTER_NEARBY, NearbyRouterConfig.class);
nearbyRouterConfig.setMatchLevel(locationLevel);
configuration.getConsumer().getServiceRouter()
.setPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_NEARBY, nearbyRouterConfig);
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.tencent.cloud.polaris.router.config;

import com.tencent.cloud.polaris.router.RouterConfigModifier;
import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
Expand All @@ -35,8 +36,8 @@ public class RouterConfigModifierAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public RouterConfigModifier routerConfigModifier() {
return new RouterConfigModifier();
public RouterConfigModifier routerConfigModifier(PolarisNearByRouterProperties polarisNearByRouterProperties) {
return new RouterConfigModifier(polarisNearByRouterProperties);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class PolarisNearByRouterProperties {

private boolean enabled = true;

private String matchLevel;

public boolean isEnabled() {
return enabled;
}
Expand All @@ -37,10 +39,19 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public String getMatchLevel() {
return matchLevel;
}

public void setMatchLevel(String matchLevel) {
this.matchLevel = matchLevel;
}

@Override
public String toString() {
return "PolarisNearByRouterProperties{" +
"enabled=" + enabled +
", matchLevel='" + matchLevel + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
"name": "spring.cloud.polaris.router.nearby-router.enabled",
"type": "java.lang.Boolean",
"defaultValue": true,
"description": "the switch for near by router."
"description": "the switch for nearby router."
},
{
"name": "spring.cloud.polaris.router.nearby-router.matchLevel",
"type": "java.lang.String",
"defaultValue": "zone",
"description": "the match level for nearby router, options can be region/zone/campus."
},
{
"name": "spring.cloud.polaris.router.rule-router.enabled",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@

import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import com.tencent.cloud.polaris.router.RouterConfigModifier;
import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties;
import com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration;
import com.tencent.polaris.api.config.Configuration;
import com.tencent.polaris.api.config.consumer.ServiceRouterConfig;
import com.tencent.polaris.factory.ConfigAPIFactory;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.plugins.router.nearby.NearbyRouterConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
Expand All @@ -38,15 +45,23 @@ public class RouterBootstrapAutoConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PolarisContextAutoConfiguration.class,
PolarisNearByRouterProperties.class,
RpcEnhancementAutoConfiguration.class,
RouterBootstrapAutoConfiguration.class))
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.cloud.polaris.router.nearby-router.matchLevel=campus")
.withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true");

@Test
public void testDefaultInitialization() {
this.contextRunner.run(context -> {
assertThat(context).hasSingleBean(RouterConfigModifier.class);
RouterConfigModifier routerConfigModifier = (RouterConfigModifier) context.getBean("routerConfigModifier");
Configuration configuration = ConfigAPIFactory.defaultConfig();
routerConfigModifier.modify((ConfigurationImpl) configuration);
NearbyRouterConfig nearbyRouterConfig = configuration.getConsumer().getServiceRouter().getPluginConfig(
ServiceRouterConfig.DEFAULT_ROUTER_NEARBY, NearbyRouterConfig.class);
Assertions.assertEquals("campus", nearbyRouterConfig.getMatchLevel().name());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public void setEnabled() {
@Test
public void testToString() {
assertThat(properties.toString())
.isEqualTo("PolarisNearByRouterProperties{enabled=true}");
.isEqualTo("PolarisNearByRouterProperties{enabled=true, matchLevel='null'}");
}
}
2 changes: 1 addition & 1 deletion spring-cloud-tencent-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<revision>1.14.0-2021.0.9-SNAPSHOT</revision>

<!-- Polaris SDK version -->
<polaris.version>1.15.3</polaris.version>
<polaris.version>1.15.4-SNAPSHOT</polaris.version>

<!-- Dependencies -->
<guava.version>32.0.1-jre</guava.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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>spring-cloud-tencent-examples</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modules>
<module>router-grayrelease-lane-gateway</module>
<module>router-grayrelease-lane-caller-service</module>
<module>router-grayrelease-lane-callee-service</module>
</modules>
<modelVersion>4.0.0</modelVersion>

<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-router-grayrelease-lane-example</artifactId>

<packaging>pom</packaging>
<name>Spring Cloud Tencent Polaris Router Lane Example</name>
<description>Example of Spring Cloud Tencent Polaris Router Lane</description>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-router-grayrelease-lane-example</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>route-grayrelease-lane-callee-service</artifactId>

<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-starter-tencent-polaris-router</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.lane.callee;

import java.util.HashMap;
import java.util.Map;

import com.tencent.cloud.common.spi.InstanceMetadataProvider;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* Custom metadata for instance.
*
* @author Haotian Zhang
*/
@Component
public class CustomMetadata implements InstanceMetadataProvider {

@Value("${service.lane:base}")
private String lane;

@Override
public Map<String, String> getMetadata() {
Map<String, String> metadata = new HashMap<>();
if (!"base".equals(lane)) {
metadata.put("lane", lane);
}
return metadata;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.lane.callee;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LaneRouterCalleeApplication {

public static void main(String[] args) {
SpringApplication.run(LaneRouterCalleeApplication.class, args);
}
}
Loading

0 comments on commit b6e0e0e

Please sign in to comment.