Skip to content

Commit 36693e3

Browse files
authored
feat: register DubboService with optional version parameter (#88)
1 parent 78442b5 commit 36693e3

File tree

6 files changed

+42
-13
lines changed

6 files changed

+42
-13
lines changed

dubbo-examples/dubbo-discovery-example/dubbo-quick-consumer/src/main/java/com/tencent/polaris/dubbo/discovery/example/consumer/GreetingServiceConsumer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
@Component("annotatedConsumer")
2626
public class GreetingServiceConsumer {
2727

28-
@DubboReference(version = "1.0.0")
28+
@DubboReference(loadbalance = "roundrobin")
2929
private GreetingService greetingService;
3030

31-
@DubboReference(version = "1.0.0", providedBy = "dubbo-quickstart-provider")
31+
@DubboReference(version = "1.0.0")
3232
private EchoService echoService;
3333

3434
public String doSayHello(String name) {

dubbo-examples/dubbo-discovery-example/dubbo-quick-provider/src/main/java/com/tencent/polaris/dubbo/discovery/example/provider/AnnotatedGreetingService.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@
2020
import com.tencent.polaris.dubbo.example.api.GreetingService;
2121
import org.apache.dubbo.config.annotation.DubboService;
2222

23-
@DubboService(version = "1.0.0")
23+
@DubboService
2424
public class AnnotatedGreetingService implements GreetingService {
2525

2626
public String sayHello(String name) {
27-
return "hello, " + name;
27+
String port = System.getProperty("dubbo.protocol.port");
28+
return "hello, " + name + ", port: " + port;
2829
}
2930

3031
@Override
3132
public String sayHi(String name) {
32-
return "[provider by polaris] hi, " + name;
33+
String port = System.getProperty("dubbo.protocol.port");
34+
return "[provider by polaris] hi, " + name + ", port: " + port;
3335
}
3436

3537
}

dubbo-plugins/dubbo-metadatareport-polaris/src/main/java/com/tencent/polaris/dubbo/metadata/report/PolarisMetadataReport.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.tencent.polaris.common.registry.PolarisOperators;
3434
import com.tencent.polaris.common.utils.Consts;
3535
import com.tencent.polaris.specification.api.v1.service.manage.ServiceContractProto;
36+
import java.util.stream.Collectors;
3637
import org.apache.dubbo.common.URL;
3738
import org.apache.dubbo.common.config.configcenter.ConfigItem;
3839
import org.apache.dubbo.common.utils.ConcurrentHashSet;
@@ -82,6 +83,7 @@ public class PolarisMetadataReport extends AbstractMetadataReport {
8283
this.another = MultiReportUtil.buildAnother(applicationModel, url);
8384
}
8485

86+
// 保存应用的元数据
8587
@Override
8688
protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdentifier, String serviceDefinitions) {
8789
reportServiceContract(toDescriptor(providerMetadataIdentifier, serviceDefinitions));
@@ -255,22 +257,40 @@ private boolean reportServiceContract(ReportServiceContractRequest req) {
255257
*/
256258
@Override
257259
public boolean registerServiceAppMapping(String serviceKey, String application, URL url) {
258-
// TODO 顺序调整
260+
// 直接上报mapping数据会导致提供相同接口的其他应用的mapping数据被覆写。
261+
// 这里应该是追加写入应用-接口mapping数据。
262+
// 1. 先获取初始mapping,过滤掉当前serviceKey对应的mapping
259263
another.ifPresent(proxyReport -> proxyReport.getMetadataReport().registerServiceAppMapping(serviceKey, application, url));
260-
264+
GetServiceContractRequest getServiceContractRequest = new GetServiceContractRequest();
265+
getServiceContractRequest.setName(formatMappingName(serviceKey));
266+
getServiceContractRequest.setService("");
267+
getServiceContractRequest.setVersion("");
268+
Optional<ServiceContractProto.ServiceContract> result = getServiceContract(getServiceContractRequest);
269+
List<InterfaceDescriptor> descriptors = result
270+
.map(ServiceContractProto.ServiceContract::getInterfacesList)
271+
.orElse(Collections.emptyList())
272+
.stream()
273+
.filter(descriptor -> !descriptor.getName().equals(application))
274+
.map(descriptor -> {
275+
InterfaceDescriptor interfaceDescriptor = new InterfaceDescriptor();
276+
interfaceDescriptor.setName(descriptor.getName());
277+
interfaceDescriptor.setPath(descriptor.getPath());
278+
interfaceDescriptor.setMethod(descriptor.getMethod());
279+
interfaceDescriptor.setContent(descriptor.getContent());
280+
return interfaceDescriptor;
281+
})
282+
.collect(Collectors.toList());
283+
// 2. 再添加当前serviceKey对应的mapping
261284
ReportServiceContractRequest request = new ReportServiceContractRequest();
262285
request.setName(formatMappingName(serviceKey));
263286
request.setService("");
264287
request.setVersion("");
265-
266-
List<InterfaceDescriptor> descriptors = new ArrayList<>();
267288
InterfaceDescriptor descriptor = new InterfaceDescriptor();
268289
descriptor.setName(application);
269290
descriptor.setPath(application);
270291
descriptor.setContent(application);
271292
descriptor.setMethod("");
272293
descriptors.add(descriptor);
273-
274294
request.setInterfaceDescriptors(descriptors);
275295
return reportServiceContract(request);
276296
}

dubbo-plugins/dubbo-registry-polaris/src/main/java/com/tencent/polaris/dubbo/registry/PolarisRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void doRegister(URL url) {
7373
int port = url.getPort();
7474
if (port > 0) {
7575
int weight = url.getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
76-
String version = url.getParameter(CommonConstants.VERSION_KEY, "1.0.0");
76+
String version = url.getParameter(CommonConstants.VERSION_KEY);
7777
polarisOperator.register(url.getServiceInterface(), url.getHost(), port, url.getProtocol(), version, weight,
7878
metadata);
7979
registeredInstances.add(url);

dubbo-plugins/dubbo-registry-polaris/src/main/java/com/tencent/polaris/dubbo/registry/PolarisServiceDiscovery.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ protected void doRegister(ServiceInstance instance) throws RuntimeException {
7373
metadata = new HashMap<>();
7474
}
7575
metadata.replaceAll((s, s2) -> StringUtils.defaultString(s2));
76-
String version = instance.getMetadata(Consts.INSTANCE_VERSION, Consts.DEFAULT_VERSION);
77-
Context.saveToGlobal(Consts.INSTANCE_VERSION, version);
76+
String version = instance.getMetadata(Consts.INSTANCE_VERSION);
7877
operator.register(
7978
serviceName,
8079
instance.getHost(),
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
consumer:
2+
serviceRouter:
3+
plugin:
4+
recoverRouter:
5+
# 是否剔除被熔断的实例
6+
excludeCircuitBreakInstances: false
7+
# 关闭全死全活
8+
allRecoverEnable: false

0 commit comments

Comments
 (0)