Skip to content

Commit

Permalink
Merge pull request #57 from chuntaojun/dubbo-3.2.x
Browse files Browse the repository at this point in the history
fix:修复token的设置&调整metareport的逻辑
  • Loading branch information
andrewshan authored Mar 4, 2024
2 parents e8b4b2f + fdf00ba commit 78ea08d
Show file tree
Hide file tree
Showing 21 changed files with 207 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>dubbo-api-example</artifactId>
<version>0.2.0-3.2.7</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%style{%d{HH:mm:ss.SSS}}{Magenta} %style{|-}{White}%highlight{%-5p} [%t] %style{%40.40c}{Cyan}:%style{%-3L}{Blue} %style{-|}{White} %m%n%rEx{filters(jdk.internal.reflect,java.lang.reflect,sun.reflect)}" disableAnsi="false" charset="UTF-8"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
dubbo.application.name=dubbo-router-backend
dubbo.application.register-mode=all
dubbo.application.register-mode=instance
dubbo.application.metadata-type=remote
dubbo.application.qosEnable=false
dubbo.registry.address=polaris://${POLARIS_DISCOVERY_ADDR}
dubbo.config-center.address=polaris://${CONFIG_CENTER_ADDR}
dubbo.metadata-report.address=polaris://${METADATA_REPORT_ADDR}
dubbo.protocol.name=dubbo
dubbo.protocol.port=27000
dubbo.provider.filter=polaris_router
dubbo.protocol.port=${DUBBO_PORT}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<version>${revision}</version>
</dependency>

<!-- 引入 polaris 限流能力 -->
<!-- 引入 polaris 路由能力 -->
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>dubbo-router-polaris</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public class MiddleConsumer {
private MiddleService middleService;

public String sayHello(String name) {
RpcContext.getClientAttachment().setAttachment("name", name);
RpcContext.getClientAttachment().setAttachment("user", name);
String ret = middleService.sayHello(name);
return "[FrontService] sayHello, " + name + " -> " + ret;
}

public String sayHi(String name) {
RpcContext.getClientAttachment().setAttachment("name", name);
RpcContext.getClientAttachment().setAttachment("user", name);
String ret = middleService.sayHello(name);
return "[FrontService] sayHi, " + name + " -> " + ret;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%style{%d{HH:mm:ss.SSS}}{Magenta} %style{|-}{White}%highlight{%-5p} [%t] %style{%40.40c}{Cyan}:%style{%-3L}{Blue} %style{-|}{White} %m%n%rEx{filters(jdk.internal.reflect,java.lang.reflect,sun.reflect)}" disableAnsi="false" charset="UTF-8"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
dubbo.application.name=dubbo-router-front
dubbo.application.register-mode=all
dubbo.application.register-mode=instance
dubbo.application.metadata-type=remote
dubbo.application.qosEnable=false
dubbo.registry.address=polaris://${POLARIS_DISCOVERY_ADDR}
dubbo.config-center.address=polaris://${CONFIG_CENTER_ADDR}
dubbo.metadata-report.address=polaris://${METADATA_REPORT_ADDR}
dubbo.protocol.name=dubbo
dubbo.protocol.port=25000
dubbo.provider.filter=polaris_router
dubbo.protocol.port=${DUBBO_PORT}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<version>${revision}</version>
</dependency>

<!-- 引入 polaris 限流能力 -->
<!-- 引入 polaris 路由能力 -->
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>dubbo-router-polaris</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public class MiddleServiceImpl implements MiddleService {

@Override
public String sayHello(String name) {
RpcContext.getClientAttachment().setAttachment("name", name);
RpcContext.getClientAttachment().setAttachment("user", name);
String ret = backendService.sayHello(name);
return "[MiddleService-" + System.getenv("ENV") + "] sayHello, " + name + " -> " + ret;
}

@Override
public String sayHi(String name) {
RpcContext.getClientAttachment().setAttachment("name", name);
RpcContext.getClientAttachment().setAttachment("user", name);
String ret = backendService.sayHello(name);
return "[MiddleService-" + System.getenv("ENV") + "] sayHi, " + name + " -> " + ret;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%style{%d{HH:mm:ss.SSS}}{Magenta} %style{|-}{White}%highlight{%-5p} [%t] %style{%40.40c}{Cyan}:%style{%-3L}{Blue} %style{-|}{White} %m%n%rEx{filters(jdk.internal.reflect,java.lang.reflect,sun.reflect)}" disableAnsi="false" charset="UTF-8"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
dubbo.application.name=dubbo-middle-front
dubbo.application.register-mode=all
dubbo.application.name=dubbo-router-middle
dubbo.application.register-mode=instance
dubbo.application.metadata-type=remote
dubbo.application.environment=${ENV}
dubbo.application.qosEnable=false
dubbo.registry.address=polaris://${POLARIS_DISCOVERY_ADDR}
dubbo.config-center.address=polaris://${CONFIG_CENTER_ADDR}
dubbo.metadata-report.address=polaris://${METADATA_REPORT_ADDR}
dubbo.protocol.name=dubbo
dubbo.protocol.port=26000
dubbo.provider.filter=polaris_router
dubbo.protocol.port=${DUBBO_PORT}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public void addListener(String key, String group, ConfigurationListener listener
configurationListener.process(dubboEvent);
});
});
logger.info(String.format("add polaris config listener, key=%s, group=%s", key, group));
return new CopyOnWriteArraySet<>();
});

Expand All @@ -87,14 +88,34 @@ public void removeListener(String key, String group, ConfigurationListener liste

@Override
public String getConfig(String key, String group, long timeout) throws IllegalStateException {
ConfigFile configFile = fileQuerier.getConfigFile(polarisConfig.getNamespace(), group, key);
return configFile.getContent();
try {
ConfigFile configFile = fileQuerier.getConfigFile(polarisConfig.getNamespace(), group, key);
return configFile.getContent();
} catch (PolarisException e) {
logger.error(formatCode(
e.getCode()),
e.getMessage(),
String.format("key=%s, group=%s", key, group),
"get config from polaris fail",
e);
}
return null;
}

@Override
public Object getInternalProperty(String key) {
ConfigFile configFile = fileQuerier.getConfigFile(polarisConfig.getNamespace(), DEFAULT_GROUP, key);
return configFile.getContent();
try {
ConfigFile configFile = fileQuerier.getConfigFile(polarisConfig.getNamespace(), DEFAULT_GROUP, key);
return configFile.getContent();
} catch (PolarisException e) {
logger.error(formatCode(
e.getCode()),
e.getMessage(),
String.format("key=%s, group=%s", key, DEFAULT_GROUP),
"get config from polaris fail",
e);
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.tencent.polaris.api.pojo.ServiceRule;
import com.tencent.polaris.api.rpc.GetServiceContractRequest;
import com.tencent.polaris.api.rpc.ServiceRuleResponse;
import com.tencent.polaris.common.context.Context;
import com.tencent.polaris.common.registry.PolarisConfig;
import com.tencent.polaris.common.registry.PolarisOperator;
import com.tencent.polaris.common.registry.PolarisOperators;
Expand Down Expand Up @@ -78,11 +79,8 @@ public class PolarisMetadataReport extends AbstractMetadataReport {

private final ScheduledExecutorService fetchMappingExecutor = Executors.newScheduledThreadPool(4, new NamedThreadFactory("polaris-metadata-report"));

private final String polarisToken;

PolarisMetadataReport(URL url) {
super(url);
this.polarisToken = url.getParameter(Consts.KEY_TOKEN);
this.operator = PolarisOperators.loadOrStoreForMetaReport(url.getHost(), url.getPort(), url.getParameters());
this.config = operator.getPolarisConfig();
this.providerAPI = operator.getProviderAPI();
Expand Down Expand Up @@ -114,7 +112,7 @@ public String getServiceDefinition(MetadataIdentifier metadataIdentifier) {

List<ServiceContractProto.InterfaceDescriptor> descriptors = result.get().getInterfacesList();
for (ServiceContractProto.InterfaceDescriptor descriptor : descriptors) {
if (!Objects.equals(descriptor.getId(), metadataIdentifier.getIdentifierKey())) {
if (!Objects.equals(descriptor.getName(), metadataIdentifier.getIdentifierKey())) {
continue;
}
return descriptor.getContent();
Expand All @@ -127,15 +125,15 @@ public void publishAppMetadata(SubscriberMetadataIdentifier identifier, Metadata
ReportServiceContractRequest request = new ReportServiceContractRequest();
request.setName(formatAppMetaName(identifier));
request.setService(identifier.getApplication());
request.setRevision(identifier.getRevision());
request.setContent(metadataInfo.getContent());
// TODO 需要设置 version
request.setVersion(Context.getFromGlobal(Consts.INSTANCE_VERSION, Consts.DEFAULT_VERSION));
List<InterfaceDescriptor> descriptors = new ArrayList<>(metadataInfo.getServices().size());
metadataInfo.getServices().forEach((s, serviceInfo) -> {
InterfaceDescriptor descriptor = new InterfaceDescriptor();
descriptor.setId(s);
descriptor.setPath(serviceInfo.getPath());
descriptor.setPath(serviceInfo.getName());
descriptor.setMethod("");
descriptor.setName(serviceInfo.getName());
descriptor.setName(serviceInfo.getMatchKey());
descriptor.setContent(JsonUtils.toJson(serviceInfo));
descriptors.add(descriptor);
});
Expand All @@ -149,7 +147,7 @@ public MetadataInfo getAppMetadata(SubscriberMetadataIdentifier identifier, Map<
GetServiceContractRequest request = new GetServiceContractRequest();
request.setName(formatAppMetaName(identifier));
request.setService(identifier.getApplication());
request.setVersion("");
request.setVersion(Context.getFromGlobal(Consts.INSTANCE_VERSION, Consts.DEFAULT_VERSION));

Optional<ServiceContractProto.ServiceContract> result = getServiceContract(request);
if (!result.isPresent()) {
Expand Down Expand Up @@ -195,7 +193,6 @@ private ReportServiceContractRequest toDescriptor(MetadataIdentifier identifier,
private Optional<ServiceContractProto.ServiceContract> getServiceContract(GetServiceContractRequest req) {
req.setNamespace(config.getNamespace());
req.setProtocol(Consts.DUBBO_PROTOCOL);
req.setToken(polarisToken);
try {
ServiceRuleResponse response = consumerAPI.getServiceContract(req);
ServiceRule rule = response.getServiceRule();
Expand All @@ -208,8 +205,8 @@ private Optional<ServiceContractProto.ServiceContract> getServiceContract(GetSer
logger.error(
formatCode(e.getCode()),
e.getMessage(),
"",
"report service_contract fail"
req.toString(),
"get service_contract fail"
);
}
return Optional.empty();
Expand All @@ -220,19 +217,20 @@ private Optional<ServiceContractProto.ServiceContract> getServiceContract(GetSer
*
* @param req {@link ReportServiceContractRequest}
*/
private void reportServiceContract(ReportServiceContractRequest req) {
private boolean reportServiceContract(ReportServiceContractRequest req) {
req.setNamespace(config.getNamespace());
req.setProtocol(Consts.DUBBO_PROTOCOL);
req.setToken(polarisToken);
try {
providerAPI.reportServiceContract(req);
return true;
} catch (PolarisException e) {
logger.error(
formatCode(e.getCode()),
e.getMessage(),
"",
"report service_contract fail"
);
return false;
}
}

Expand Down Expand Up @@ -264,8 +262,7 @@ public boolean registerServiceAppMapping(String serviceKey, String application,
descriptors.add(descriptor);

request.setInterfaceDescriptors(descriptors);
reportServiceContract(request);
return true;
return reportServiceContract(request);
}

@Override
Expand Down Expand Up @@ -426,10 +423,10 @@ private static String formatAppMetaName(SubscriberMetadataIdentifier identifier)

private static String formatMetadataIdentifier(MetadataIdentifier identifier) {
String tmpl = "dubbo::metadata::";
if (StringUtils.isNotEmpty(identifier.getVersion())) {
if (StringUtils.isNotBlank(identifier.getVersion())) {
tmpl += identifier.getVersion() + "::";
}
if (StringUtils.isNotEmpty(identifier.getGroup())) {
if (StringUtils.isNotBlank(identifier.getGroup())) {
tmpl += identifier.getGroup() + "::";
}
tmpl += identifier.getSide() + "::";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.tencent.polaris.dubbo.registry;

import com.tencent.polaris.api.exception.ErrorCode;
import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.api.listener.ServiceListener;
import com.tencent.polaris.api.pojo.Instance;
Expand Down Expand Up @@ -85,6 +86,7 @@ public void doRegister(URL url) {
registeredInstances.add(url);
} else {
LOGGER.warn("[POLARIS] skip register url {} for zero port value", url);
throw new PolarisException(ErrorCode.INVALID_REQUEST, "zero port url: " + url);
}
}

Expand Down
Loading

0 comments on commit 78ea08d

Please sign in to comment.