feat:Support the individual use of spring-cloud-starter-tencent-polaris-router

pull/15/head
SkyeBeFreeman 3 years ago
parent e6f8ed316a
commit 3b6ba1f4a7

@ -71,7 +71,7 @@
<properties>
<!-- Project revision -->
<revision>1.0.2.Hoxton.SR9</revision>
<revision>1.1.0.Hoxton.SR9-SNAPSHOT</revision>
<!-- Spring Cloud -->
<spring.cloud.version>Hoxton.SR9</spring.cloud.version>
@ -231,4 +231,17 @@
</distributionManagement>
</profile>
</profiles>
<repositories>
<repository>
<id>nexus-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>

@ -62,8 +62,8 @@
</developers>
<properties>
<revision>1.0.2.Hoxton.SR9</revision>
<polaris.version>1.0.0</polaris.version>
<revision>1.1.0.Hoxton.SR9-SNAPSHOT</revision>
<polaris.version>1.1.0-SNAPSHOT</polaris.version>
<tomcat.version>10.0.0-M6</tomcat.version>
<powermock.version>2.0.0</powermock.version>
@ -83,6 +83,7 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>

@ -28,11 +28,6 @@
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-context</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>
</dependency>
<!-- Spring Cloud Tencent dependencies end -->
<!-- Polaris dependencies start -->

@ -40,7 +40,7 @@ public class PolarisProperties {
/**
* namespace, separation registry of different environments.
*/
@Value("${spring.cloud.polaris.discovery.namespace:#{'Production'}}")
@Value("${spring.cloud.polaris.discovery.namespace:#{'default'}}")
private String namespace;
/**

@ -27,9 +27,12 @@ import com.netflix.loadbalancer.ServerList;
import com.tencent.cloud.metadata.constant.MetadataConstant.SystemMetadataKey;
import com.tencent.cloud.metadata.context.MetadataContextHolder;
import com.tencent.cloud.polaris.pojo.PolarisServer;
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;
@ -63,8 +66,33 @@ public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Se
if (allServers.get(0) instanceof PolarisServer) {
serviceInstances = ((PolarisServer) allServers.get(0)).getServiceInstances();
} else {
// TODO serviceInstances = DefaultServiceInstancesImpl.createByServerList(allServers);
throw new IllegalStateException("PolarisRoutingLoadBalancer only support PolarisServer instances");
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);
@ -81,12 +109,12 @@ public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Se
processRoutersRequest.setSourceService(serviceInfo);
}
ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest);
ServiceInstances filteredInstances = processRoutersResponse.getServiceInstances();
List<Server> instances = new ArrayList<>();
for (Instance instance : filteredInstances.getInstances()) {
instances.add(new PolarisServer(serviceInstances, instance));
ServiceInstances filteredServiceInstances = processRoutersResponse.getServiceInstances();
List<Server> filteredInstances = new ArrayList<>();
for (Instance instance : filteredServiceInstances.getInstances()) {
filteredInstances.add(new PolarisServer(serviceInstances, instance));
}
return instances;
return filteredInstances;
}
@Override

@ -38,7 +38,7 @@ public class MetadataContextHolder {
private static MetadataLocalProperties metadataLocalProperties;
public static final String LOCAL_NAMESPACE = ApplicationContextAwareUtils.getProperties("spring.cloud"
+ ".polaris.discovery.namespace", "Production");
+ ".polaris.discovery.namespace", "default");
private static final String LOCAL_SPRING_APPLICATION_NAME =
ApplicationContextAwareUtils.getProperties("spring.application.name", null);

@ -61,7 +61,7 @@ public class PolarisContextProperties {
protected Configuration configuration() {
ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory
.defaultConfig(Configuration.DEFAULT_CONFIG_OPENSOURCE);
.defaultConfig(Configuration.DEFAULT_CONFIG);
String defaultHost = getHost();
configuration.getGlobal().getAPI().setBindIP(defaultHost);
Collection<PolarisConfigModifier> modifiers = modifierList;

Loading…
Cancel
Save