Skip to content

Commit

Permalink
Polish apache#4942 : Dubbo Cloud Native supports multiple protcols
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Aug 26, 2019
1 parent a15ea1a commit c9cb169
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public class NacosDubboServiceConsumerBootstrap {
public static void main(String[] args) throws Exception {

ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-nacos-consumer-demo");
applicationConfig.setMetadataType("remote");
// applicationConfig.setMetadataType("remote");
new DubboBootstrap()
.application(applicationConfig)
// Zookeeper
.registry("nacos", builder -> builder.address("nacos://127.0.0.1:8848?registry.type=service&subscribed.services=dubbo-nacos-provider-demo"))
.registry("nacos", builder -> builder.address("nacos://127.0.0.1:8848?registry-type=service&subscribed-services=dubbo-nacos-provider-demo"))
.metadataReport(new MetadataReportConfig("nacos://127.0.0.1:8848"))
// Nacos
// .registry("consul", builder -> builder.address("consul://127.0.0.1:8500?registry.type=service&subscribed.services=dubbo-provider-demo").group("namespace1"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public class NacosDubboServiceProviderBootstrap {

public static void main(String[] args) {
ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-nacos-provider-demo");
applicationConfig.setMetadataType("remote");
// applicationConfig.setMetadataType("remote");
new DubboBootstrap()
.application(applicationConfig)
// Zookeeper in service registry type
.registry("nacos", builder -> builder.address("nacos://127.0.0.1:8848?registry.type=service"))
.registry("nacos", builder -> builder.address("nacos://127.0.0.1:8848?registry-type=service"))
// Nacos
// .registry("nacos", builder -> builder.address("nacos://127.0.0.1:8848?registry.type=service"))
// .registry(RegistryBuilder.newBuilder().address("etcd3://127.0.0.1:2379?registry.type=service").build())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.bootstrap;

import org.apache.dubbo.bootstrap.rest.UserService;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.context.ConfigManager;

/**
* Dubbo Provider Bootstrap
*
* @since 2.7.4
*/
public class ZookeeperDubboServiceConsumerBootstrap {

public static void main(String[] args) throws Exception {

new DubboBootstrap()
.application("zookeeper-dubbo-consumer")
.registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service&subscribed-services=zookeeper-dubbo-provider"))
.reference("echo", builder -> builder.interfaceClass(EchoService.class).protocol("dubbo"))
.reference("user", builder -> builder.interfaceClass(UserService.class).protocol("rest"))
.start()
.await();

ConfigManager configManager = ConfigManager.getInstance();

ReferenceConfig<EchoService> referenceConfig = configManager.getReference("echo");

EchoService echoService = referenceConfig.get();

ReferenceConfig<UserService> referenceConfig2 = configManager.getReference("user");

UserService userService = referenceConfig2.get();

for (int i = 0; i < 500; i++) {
Thread.sleep(2000L);
System.out.println(echoService.echo("Hello,World"));
System.out.println(userService.getUser(i * 1L));
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.bootstrap;

import org.apache.dubbo.bootstrap.rest.UserService;
import org.apache.dubbo.bootstrap.rest.UserServiceImpl;

/**
* TODO
*/
public class ZookeeperDubboServiceProviderBootstrap {

public static void main(String[] args) {
new DubboBootstrap()
.application("zookeeper-dubbo-provider")
.registry(builder -> builder.address("zookeeper://127.0.0.1:2181?registry-type=service"))
.protocol("dubbo", builder -> builder.port(-1).name("dubbo"))
.protocol("rest", builder -> builder.port(8082).name("rest"))
.service("echo", builder -> builder.interfaceClass(EchoService.class).ref(new EchoServiceImpl()).protocolIds("dubbo"))
.service("user", builder -> builder.interfaceClass(UserService.class).ref(new UserServiceImpl()).protocolIds("rest"))
.start()
.await();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@
import static org.apache.dubbo.metadata.WritableMetadataService.DEFAULT_EXTENSION;
import static org.apache.dubbo.registry.client.ServiceDiscoveryFactory.getExtension;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getExportedServicesRevision;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataServiceURLsParams;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataStorageType;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getProviderHost;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getProviderPort;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getProtocolPort;

/**
* {@link ServiceDiscoveryRegistry} is the service-oriented {@link Registry} and dislike the traditional one that
Expand Down Expand Up @@ -419,33 +417,27 @@ private List<URL> cloneSubscribedURLs(URL subscribedURL, Collection<ServiceInsta

serviceInstances.forEach(serviceInstance -> {

List<URL> templateURLs = getTemplateURLs(subscribedURL, serviceInstance);
// The parameters of URLs that the MetadataService exported
Map<String, Map<String, Object>> serviceURLsParams = getMetadataServiceURLsParams(serviceInstance);

serviceURLsParams.forEach((protocol, parametersMap) -> {
templateURLs.stream()
.filter(templateURL -> isCompatibleProtocol(protocol, templateURL))
.map(templateURL -> templateURL.removeParameter(TIMESTAMP_KEY))
.map(templateURL -> templateURL.removeParameter(PID_KEY))
.map(templateURL -> {

String host = getProviderHost(parametersMap);
Integer port = getProviderPort(parametersMap);

if (Objects.equals(templateURL.getHost(), host)
&& Objects.equals(templateURL.getPort(), port)) { // use templateURL if equals
return templateURL;
}

URLBuilder clonedURLBuilder = from(templateURL) // remove the parameters from the template URL
.setHost(host) // reset the host
.setPort(port); // reset the port

return clonedURLBuilder.build();
})
.forEach(clonedURLs::add);
});
String host = serviceInstance.getHost();

getTemplateURLs(subscribedURL, serviceInstance)
.stream()
.map(templateURL -> templateURL.removeParameter(TIMESTAMP_KEY))
.map(templateURL -> templateURL.removeParameter(PID_KEY))
.map(templateURL -> {
String protocol = templateURL.getProtocol();
int port = getProtocolPort(serviceInstance, protocol);
if (Objects.equals(templateURL.getHost(), host)
&& Objects.equals(templateURL.getPort(), port)) { // use templateURL if equals
return templateURL;
}

URLBuilder clonedURLBuilder = from(templateURL) // remove the parameters from the template URL
.setHost(host) // reset the host
.setPort(port); // reset the port

return clonedURLBuilder.build();
})
.forEach(clonedURLs::add);
});
return clonedURLs;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.registry.client.metadata;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.metadata.WritableMetadataService;
import org.apache.dubbo.registry.client.ServiceInstance;
import org.apache.dubbo.registry.client.ServiceInstanceCustomizer;
import org.apache.dubbo.rpc.Protocol;

import static org.apache.dubbo.metadata.WritableMetadataService.getExtension;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataStorageType;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.setProtocolPort;

/**
* A Class to customize the ports of {@link Protocol protocols} into
* {@link ServiceInstance#getMetadata() the metadata of service instance}
*
* @since 2.7.4
*/
public class ProtocolPortsMetadataCustomizer implements ServiceInstanceCustomizer {

@Override
public void customize(ServiceInstance serviceInstance) {

String metadataStoredType = getMetadataStorageType(serviceInstance);

WritableMetadataService writableMetadataService = getExtension(metadataStoredType);

writableMetadataService.getExportedURLs()
.stream()
.map(URL::valueOf)
.forEach(url -> {
setProtocolPort(serviceInstance, url.getProtocol(), url.getPort());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.dubbo.metadata.MetadataService;
import org.apache.dubbo.metadata.WritableMetadataService;
import org.apache.dubbo.registry.client.ServiceInstance;
import org.apache.dubbo.rpc.Protocol;

import com.alibaba.fastjson.JSON;

Expand All @@ -28,7 +29,6 @@
import java.util.List;
import java.util.Map;

import static java.lang.String.valueOf;
import static java.util.Collections.emptyMap;
import static org.apache.dubbo.common.utils.StringUtils.isBlank;
import static org.apache.dubbo.metadata.WritableMetadataService.DEFAULT_METADATA_STORAGE_TYPE;
Expand All @@ -49,6 +49,16 @@ public class ServiceInstanceMetadataUtils {
*/
public static final String METADATA_SERVICE_PREFIX = "dubbo.metadata-service.";

/**
* The prefix of {@link Protocol} : "dubbo.protocols."
*/
public static final String DUBBO_PROTOCOLS_PREFIX = "dubbo.protocols.";

/**
* The suffix of port : ".port";
*/
public static final String DUBBO_PORT_SUFFIX = ".port";

/**
* The property name of metadata JSON of {@link MetadataService}'s {@link URL}
*/
Expand Down Expand Up @@ -76,16 +86,6 @@ public class ServiceInstanceMetadataUtils {
*/
public static String METADATA_STORAGE_TYPE_PROPERTY_NAME = "dubbo.metadata.storage-type";

/**
* The property name of {@link URL url's} parameter name of Dubbo Provider host
*/
public static final String PROVIDER_HOST_PROPERTY_NAME = "provider.host";

/**
* The {@link URL url's} parameter name of Dubbo Provider port
*/
public static final String PROVIDER_PORT_PROPERTY_NAME = "provider.port";

/**
* Get the multiple {@link URL urls'} parameters of {@link MetadataService MetadataService's} Metadata
*
Expand All @@ -110,23 +110,6 @@ public static Map<String, Object> getMetadataServiceURLParams(ServiceInstance se
return params.getOrDefault(protocol, emptyMap());
}

/**
* The provider port from {@link ServiceInstance the specified service instance}
*
* @param serviceInstance {@link ServiceInstance the specified service instance}
* @param protocol the protocol name
* @return The protocol port if found, or <code>null</code>
*/
public static Integer getProviderPort(ServiceInstance serviceInstance, String protocol) {
Map<String, Object> params = getMetadataServiceURLParams(serviceInstance, protocol);
return getProviderPort(params);
}

public static String getProviderHost(ServiceInstance serviceInstance, String protocol) {
Map<String, Object> params = getMetadataServiceURLParams(serviceInstance, protocol);
return getProviderHost(params);
}

public static String getMetadataServiceParameter(List<URL> urls) {
Map<String, Map<String, String>> params = new HashMap<>();

Expand All @@ -145,21 +128,9 @@ public static String getMetadataServiceParameter(List<URL> urls) {
private static Map<String, String> getParams(URL providerURL) {
Map<String, String> params = new LinkedHashMap<>();
setDefaultParams(params, providerURL);
// set provider host
setProviderHostParam(params, providerURL);
// set provider port
setProviderPortParam(params, providerURL);
return params;
}

public static String getProviderHost(Map<String, Object> params) {
return valueOf(params.get(PROVIDER_HOST_PROPERTY_NAME));
}

public static Integer getProviderPort(Map<String, Object> params) {
return Integer.valueOf(valueOf(params.get(PROVIDER_PORT_PROPERTY_NAME)));
}

/**
* The revision for all exported Dubbo services from the specified {@link ServiceInstance}.
*
Expand Down Expand Up @@ -228,12 +199,42 @@ public static boolean isDubboServiceInstance(ServiceInstance serviceInstance) {
|| metadata.containsKey(METADATA_SERVICE_URLS_PROPERTY_NAME);
}

private static void setProviderHostParam(Map<String, String> params, URL providerURL) {
params.put(PROVIDER_HOST_PROPERTY_NAME, providerURL.getHost());
/**
* Create the property name of Dubbo protocol port
*
* @param protocol the name of protocol, e.g, dubbo, rest, and so on
* @return e.g, "dubbo.protocols.dubbo.port"
*/
public static String createProtocolPortPropertyName(String protocol) {
return DUBBO_PROTOCOLS_PREFIX + protocol + DUBBO_PORT_SUFFIX;
}

private static void setProviderPortParam(Map<String, String> params, URL providerURL) {
params.put(PROVIDER_PORT_PROPERTY_NAME, valueOf(providerURL.getPort()));
/**
* Set the protocol port into {@link ServiceInstance#getMetadata() the metadata of service instance}
*
* @param serviceInstance {@link ServiceInstance service instance}
* @param protocol the name of protocol, e.g, dubbo, rest, and so on
* @param port the port of protocol
*/
public static void setProtocolPort(ServiceInstance serviceInstance, String protocol, int port) {
Map<String, String> metadata = serviceInstance.getMetadata();
String propertyName = createProtocolPortPropertyName(protocol);
metadata.put(propertyName, Integer.toString(port));
}

/**
* Get the property value of port by the specified {@link ServiceInstance#getMetadata() the metadata of
* service instance} and protocol
*
* @param serviceInstance {@link ServiceInstance service instance}
* @param protocol the name of protocol, e.g, dubbo, rest, and so on
* @return if not found, return <code>null</code>
*/
public static Integer getProtocolPort(ServiceInstance serviceInstance, String protocol) {
Map<String, String> metadata = serviceInstance.getMetadata();
String propertyName = createProtocolPortPropertyName(protocol);
String propertyValue = metadata.get(propertyName);
return propertyValue == null ? null : Integer.valueOf(propertyValue);
}

/**
Expand Down
Loading

0 comments on commit c9cb169

Please sign in to comment.