Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add polaris extensions support #184

Merged
merged 7 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions dubbo-cluster-extensions/dubbo-cluster-polaris-dubbo2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

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.
-->
<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>dubbo-cluster-extensions</artifactId>
<groupId>org.apache.dubbo.extensions</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dubbo-cluster-polaris-dubbo2</artifactId>
AlbumenJ marked this conversation as resolved.
Show resolved Hide resolved
<name>dubbo-cluster-polaris-dubbo2</name>
<version>1.0.0-SNAPSHOT</version>
<description>Dubbo2 cluster extension for PolarisMesh, support dynamic routing capability.</description>

<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.18</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-adapter-dubbo</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.dubbo.rpc.cluster.router;

import com.tencent.polaris.api.pojo.CircuitBreakerStatus;
import com.tencent.polaris.api.pojo.DefaultInstance;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.StatusDimension;
import com.tencent.polaris.common.registry.Consts;
import com.tencent.polaris.common.registry.ConvertUtils;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.cluster.Constants;

public class InstanceInvoker<T> implements Instance, Invoker<T> {

private static final Logger LOGGER = LoggerFactory.getLogger(InstanceInvoker.class);

private final Invoker<T> invoker;

private final DefaultInstance defaultInstance;

public InstanceInvoker(Invoker<T> invoker, String namespace) {
this.invoker = invoker;
defaultInstance = new DefaultInstance();
defaultInstance.setNamespace(namespace);
URL url = invoker.getUrl();
defaultInstance.setService(url.getServiceInterface());
defaultInstance.setHost(url.getHost());
defaultInstance.setPort(url.getPort());
defaultInstance.setId(url.getParameter(Consts.INSTANCE_KEY_ID));
defaultInstance.setHealthy(Boolean.parseBoolean(url.getParameter(Consts.INSTANCE_KEY_HEALTHY)));
defaultInstance.setIsolated(Boolean.parseBoolean(url.getParameter(Consts.INSTANCE_KEY_ISOLATED)));
defaultInstance.setVersion(url.getParameter(CommonConstants.VERSION_KEY));
defaultInstance.setWeight(url.getParameter(Constants.WEIGHT_KEY, 100));
String circuitBreakerStr = url.getParameter(Consts.INSTANCE_KEY_CIRCUIT_BREAKER);
Map<StatusDimension, CircuitBreakerStatus> statusDimensionCircuitBreakerStatusMap = ConvertUtils
.stringToCircuitBreakers(circuitBreakerStr);
defaultInstance.getCircuitBreakerStatuses().putAll(statusDimensionCircuitBreakerStatusMap);
defaultInstance.setMetadata(url.getParameters());
LOGGER.info(String.format("[POLARIS] construct instance from invoker, url %s, instance %s", url, defaultInstance));
}

@Override
public Class<T> getInterface() {
return invoker.getInterface();
}

@Override
public Result invoke(Invocation invocation) throws RpcException {
return invoker.invoke(invocation);
}

@Override
public URL getUrl() {
return invoker.getUrl();
}

@Override
public boolean isAvailable() {
return invoker.isAvailable();
}

@Override
public void destroy() {
invoker.destroy();
}

@Override
public String getNamespace() {
return defaultInstance.getNamespace();
}

@Override
public String getService() {
return defaultInstance.getService();
}

@Override
public String getRevision() {
return defaultInstance.getRevision();
}

@Override
public CircuitBreakerStatus getCircuitBreakerStatus() {
return defaultInstance.getCircuitBreakerStatus();
}

@Override
public Collection<StatusDimension> getStatusDimensions() {
return defaultInstance.getStatusDimensions();
}

@Override
public CircuitBreakerStatus getCircuitBreakerStatus(StatusDimension statusDimension) {
return defaultInstance.getCircuitBreakerStatus(statusDimension);
}

@Override
public boolean isHealthy() {
return defaultInstance.isHealthy();
}

@Override
public boolean isIsolated() {
return defaultInstance.isIsolated();
}

@Override
public String getProtocol() {
return defaultInstance.getProtocol();
}

@Override
public String getId() {
return defaultInstance.getId();
}

@Override
public String getHost() {
return defaultInstance.getHost();
}

@Override
public int getPort() {
return defaultInstance.getPort();
}

@Override
public String getVersion() {
return defaultInstance.getVersion();
}

@Override
public Map<String, String> getMetadata() {
return defaultInstance.getMetadata();
}

@Override
public boolean isEnableHealthCheck() {
return defaultInstance.isEnableHealthCheck();
}

@Override
public String getRegion() {
return defaultInstance.getRegion();
}

@Override
public String getZone() {
return defaultInstance.getZone();
}

@Override
public String getCampus() {
return defaultInstance.getCampus();
}

@Override
public int getPriority() {
return defaultInstance.getPriority();
}

@Override
public int getWeight() {
return defaultInstance.getWeight();
}

@Override
public String getLogicSet() {
return defaultInstance.getLogicSet();
}

@Override
public int compareTo(Instance o) {
return defaultInstance.compareTo(o);
}

private static final String SEP_CIRCUIT_BREAKER = ",";

private static final String SEP_CIRCUIT_BREAKER_VALUE = ":";

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof InstanceInvoker)) {
return false;
}
InstanceInvoker<?> that = (InstanceInvoker<?>) o;
return Objects.equals(defaultInstance, that.defaultInstance);
}

@Override
public int hashCode() {
return Objects.hash(defaultInstance);
}

public Invoker<T> getInvoker() {
return invoker;
}
}
Loading