service discovery demos

This commit is contained in:
ken.lj 2019-08-21 12:54:38 +08:00
parent ffa50bd8a7
commit 1d554d326f
40 changed files with 728 additions and 185 deletions

View File

@ -84,7 +84,6 @@ import static org.apache.dubbo.common.function.ThrowableAction.execute;
import static org.apache.dubbo.common.utils.StringUtils.isNotEmpty;
import static org.apache.dubbo.config.context.ConfigManager.getInstance;
import static org.apache.dubbo.metadata.WritableMetadataService.getExtension;
import static org.apache.dubbo.metadata.WritableMetadataService.getMetadataStorageType;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.setMetadataStorageType;
import static org.apache.dubbo.remoting.Constants.CLIENT_KEY;
@ -129,13 +128,6 @@ public class DubboBootstrap extends GenericEventListener implements Lifecycle {
private volatile boolean started = false;
/**
* Only Provider Register
*/
private volatile boolean onlyRegisterProvider = false;
private volatile boolean defaultMetadataStorageType = true;
private volatile ServiceInstance serviceInstance;
private volatile MetadataService metadataService;
@ -161,28 +153,13 @@ public class DubboBootstrap extends GenericEventListener implements Lifecycle {
});
}
/**
* Set only register provider or not
*
* @param onlyRegisterProvider if <code>true</code>, only register the provider and reduce the registries' load.
* @return {@link DubboBootstrap}
*/
public DubboBootstrap onlyRegisterProvider(boolean onlyRegisterProvider) {
this.onlyRegisterProvider = onlyRegisterProvider;
return this;
private boolean isOnlyRegisterProvider() {
Boolean registerConsumer = configManager.getApplicationOrElseThrow().getRegisterConsumer();
return registerConsumer == null || !registerConsumer;
}
public boolean isOnlyRegisterProvider() {
return onlyRegisterProvider;
}
public boolean isDefaultMetadataStorageType() {
return defaultMetadataStorageType;
}
public DubboBootstrap defaultMetadataStorageType(boolean defaultMetadataStorageType) {
this.defaultMetadataStorageType = defaultMetadataStorageType;
return this;
private String getMetadataType() {
return configManager.getApplicationOrElseThrow().getMetadataType();
}
public DubboBootstrap metadataReport(MetadataReportConfig metadataReportConfig) {
@ -456,8 +433,6 @@ public class DubboBootstrap extends GenericEventListener implements Lifecycle {
useRegistryAsConfigCenterIfNecessary();
initApplicationMetadata();
initMetadataService();
initMetadataServiceExporter();
@ -475,11 +450,6 @@ public class DubboBootstrap extends GenericEventListener implements Lifecycle {
return this;
}
private void initApplicationMetadata() {
String metadataStorageType = getMetadataStorageType(isDefaultMetadataStorageType());
getApplication().setMetadataStorageType(metadataStorageType);
}
private void startConfigCenter() {
Collection<ConfigCenterConfig> configCenters = configManager.getConfigCenters();
@ -499,7 +469,7 @@ public class DubboBootstrap extends GenericEventListener implements Lifecycle {
() -> new IllegalStateException("There's no ApplicationConfig specified.")
);
String metadataType = applicationConfig.getMetadataStorageType();
String metadataType = applicationConfig.getMetadataType();
// FIXME, multiple metadata config support.
Collection<MetadataReportConfig> metadataReportConfigs = configManager.getMetadataConfigs();
if (CollectionUtils.isEmpty(metadataReportConfigs)) {
@ -589,7 +559,7 @@ public class DubboBootstrap extends GenericEventListener implements Lifecycle {
* Initialize {@link MetadataService} from {@link WritableMetadataService}'s extension
*/
private void initMetadataService() {
this.metadataService = getExtension(isDefaultMetadataStorageType());
this.metadataService = getExtension(getMetadataType());
}
/**
@ -794,7 +764,12 @@ public class DubboBootstrap extends GenericEventListener implements Lifecycle {
if (cache == null) {
cache = ReferenceConfigCache.getCache();
}
configManager.getReferences().forEach(cache::get);
configManager.getReferences().forEach((rc) -> {
// check eager init or not.
if (rc.shouldInit()) {
cache.get(rc);
}
});
}
private void registerServiceInstance() {
@ -843,7 +818,7 @@ public class DubboBootstrap extends GenericEventListener implements Lifecycle {
private ServiceInstance createServiceInstance(String serviceName, String host, int port) {
this.serviceInstance = new DefaultServiceInstance(serviceName, host, port);
setMetadataStorageType(serviceInstance, isDefaultMetadataStorageType());
setMetadataStorageType(serviceInstance, getMetadataType());
return this.serviceInstance;
}

View File

@ -40,7 +40,6 @@ public class DubboServiceConsumerBootstrap {
// .registry("consul", builder -> builder.address("consul://127.0.0.1:8500?registry.type=service&subscribed.services=dubbo-provider-demo").group("namespace1"))
.reference("echo", builder -> builder.interfaceClass(EchoService.class).protocol("dubbo"))
.reference("user", builder -> builder.interfaceClass(UserService.class).protocol("rest"))
.onlyRegisterProvider(true)
.start()
.await();

View File

@ -63,7 +63,7 @@ public class DubboServiceProviderBootstrap {
// userService.setRegistries(Arrays.asList(interfaceRegistry, serviceRegistry));
ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-provider-demo");
applicationConfig.setMetadataStorageType("remote");
applicationConfig.setMetadataType("remote");
new DubboBootstrap()
.application(applicationConfig)
// Zookeeper in service registry type

View File

@ -32,7 +32,6 @@ public class EtcdDubboServiceConsumerBootstrap {
new DubboBootstrap()
.application("dubbo-consumer-demo")
.defaultMetadataStorageType(true)
// Zookeeper
.protocol(builder -> builder.port(20887).name("dubbo"))
.registry("zookeeper", builder -> builder.address("etcd3://127.0.0.1:2379?registry.type=service&subscribed.services=dubbo-provider-demo"))
@ -41,7 +40,6 @@ public class EtcdDubboServiceConsumerBootstrap {
// .registry("consul", builder -> builder.address("consul://127.0.0.1:8500?registry.type=service&subscribed.services=dubbo-provider-demo").group("namespace1"))
.reference("echo", builder -> builder.interfaceClass(EchoService.class).protocol("dubbo"))
.reference("user", builder -> builder.interfaceClass(UserService.class).protocol("rest"))
.onlyRegisterProvider(true)
.start()
.await();

View File

@ -63,10 +63,9 @@ public class EtcdDubboServiceProviderBootstrap {
// userService.setRegistries(Arrays.asList(interfaceRegistry, serviceRegistry));
ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-provider-demo");
applicationConfig.setMetadataStorageType("remote");
applicationConfig.setMetadataType("remote");
new DubboBootstrap()
.application(applicationConfig)
.defaultMetadataStorageType(true)
// Zookeeper in service registry type
// .registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181?registry.type=service"))
// Nacos

View File

@ -39,7 +39,6 @@ public class NacosDubboServiceConsumerBootstrap {
// .registry("consul", builder -> builder.address("consul://127.0.0.1:8500?registry.type=service&subscribed.services=dubbo-provider-demo").group("namespace1"))
.reference("echo", builder -> builder.interfaceClass(EchoService.class).protocol("dubbo"))
.reference("user", builder -> builder.interfaceClass(UserService.class).protocol("rest"))
.onlyRegisterProvider(true)
.start()
.await();

View File

@ -29,7 +29,6 @@ public class NacosDubboServiceProviderBootstrap {
public static void main(String[] args) {
new DubboBootstrap()
.defaultMetadataStorageType(false)
.application("dubbo-provider-demo")
// Zookeeper in service registry type
.registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181?registry.type=service"))

View File

@ -43,7 +43,6 @@ public class DubboInterfaceConsumerBootstrap {
// .registry("consul", builder -> builder.address("consul://127.0.0.1:8500?registry.type=service&subscribed.services=dubbo-provider-demo"))
.reference("echo", builder -> builder.interfaceClass(EchoService.class).protocol("dubbo"))
.reference("user", builder -> builder.interfaceClass(UserService.class).protocol("rest"))
.onlyRegisterProvider(true)
.start()
.await();

View File

@ -150,7 +150,9 @@ public class ApplicationConfig extends AbstractConfig {
/**
* Metadata type, local or remote, if choose remote, you need to further specify metadata center.
*/
private String metadataStorageType;
private String metadataType;
private Boolean registerConsumer;
public ApplicationConfig() {
}
@ -388,12 +390,20 @@ public class ApplicationConfig extends AbstractConfig {
return !StringUtils.isEmpty(name);
}
public String getMetadataStorageType() {
return metadataStorageType;
public String getMetadataType() {
return metadataType;
}
public void setMetadataStorageType(String metadataStorageType) {
this.metadataStorageType = metadataStorageType;
public void setMetadataType(String metadataType) {
this.metadataType = metadataType;
}
public Boolean getRegisterConsumer() {
return registerConsumer;
}
public void setRegisterConsumer(Boolean registerConsumer) {
this.registerConsumer = registerConsumer;
}
@Override

View File

@ -490,7 +490,7 @@ public class ReferenceConfig<T> extends AbstractReferenceConfig {
return shouldCheck;
}
protected boolean shouldInit() {
public boolean shouldInit() {
Boolean shouldInit = isInit();
if (shouldInit == null && getConsumer() != null) {
shouldInit = getConsumer().isInit();

View File

@ -160,6 +160,13 @@ public class RegistryConfig extends AbstractConfig {
*/
private Boolean useAsMetadataCenter;
/**
* list of rpc protocols accepted by this registry, for example, "dubbo,rest"
*/
private String accepts;
private Boolean preferred;
public RegistryConfig() {
}
@ -454,6 +461,22 @@ public class RegistryConfig extends AbstractConfig {
this.useAsMetadataCenter = useAsMetadataCenter;
}
public String getAccepts() {
return accepts;
}
public void setAccepts(String accepts) {
this.accepts = accepts;
}
public Boolean getPreferred() {
return preferred;
}
public void setPreferred(Boolean preferred) {
this.preferred = preferred;
}
@Override
public void refresh() {
super.refresh();

View File

@ -177,7 +177,7 @@ public class ApplicationBuilder extends AbstractBuilder<ApplicationConfig, Appli
super.build(config);
config.setName(name);
config.setMetadataStorageType(metadata);
config.setMetadataType(metadata);
config.setVersion(this.version);
config.setOwner(this.owner);
config.setOrganization(this.organization);

View File

@ -117,6 +117,10 @@ public class ConfigManager {
return ofNullable(getConfig(ApplicationConfig.class));
}
public ApplicationConfig getApplicationOrElseThrow() {
return getApplication().orElseThrow(() -> new IllegalStateException("There's no ApplicationConfig specified."));
}
// MonitorConfig correlative methods
public void setMonitor(MonitorConfig monitor) {

View File

@ -1,89 +0,0 @@
/*
* 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.config.spring;
import org.apache.dubbo.bootstrap.DubboBootstrap;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ConfigCenterConfig;
import org.apache.dubbo.config.ConsumerConfig;
import org.apache.dubbo.config.MetadataReportConfig;
import org.apache.dubbo.config.MetricsConfig;
import org.apache.dubbo.config.ModuleConfig;
import org.apache.dubbo.config.MonitorConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ProviderConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.spring.util.BeanFactoryUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import java.util.List;
public class ApplicationBean extends ApplicationConfig implements ApplicationListener<ApplicationEvent>, ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
List<RegistryConfig> registries = getBeans(RegistryConfig.class);
List<ProtocolConfig> protocols = getBeans(ProtocolConfig.class);
List<ConfigCenterConfig> configs = getBeans(ConfigCenterConfig.class);
List<MetadataReportConfig> metadatas = getBeans(MetadataReportConfig.class);
List<MonitorConfig> monitors = getBeans(MonitorConfig.class);
List<ProviderConfig> providers = getBeans(ProviderConfig.class);
List<ConsumerConfig> consumers = getBeans(ConsumerConfig.class);
List<ModuleConfig> modules = getBeans(ModuleConfig.class);
List<MetricsConfig> metrics = getBeans(MetricsConfig.class);
List<ServiceConfig> services = getBeans(ServiceConfig.class);
List<ReferenceConfig> references = getBeans(ReferenceConfig.class);
DubboBootstrap bootstrap = new DubboBootstrap();
bootstrap.application(this)
.monitor(CollectionUtils.isNotEmpty(monitors) ? monitors.get(0) : null)
.module(CollectionUtils.isNotEmpty(modules) ? modules.get(0) : null)
.metrics(CollectionUtils.isNotEmpty(metrics) ? metrics.get(0) : null)
.registries(registries)
.protocols(protocols)
.configCenters(configs)
.metadataReports(metadatas)
.providers(providers)
.consumers(consumers)
.services(services)
.references(references)
.start();
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
private <T> List<T> getBeans(Class<T> clazz) {
return BeanFactoryUtils.getBeans(applicationContext, new String[]{""}, clazz);
}
}

View File

@ -75,6 +75,7 @@ public class ReferenceBean<T> extends ReferenceConfig<T> implements FactoryBean,
BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ConfigCenterBean.class, false, false);
}
// eager init if necessary.
if (shouldInit()) {
getObject();
}

View File

@ -30,10 +30,6 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import static org.apache.dubbo.config.spring.util.BeanFactoryUtils.addApplicationListener;
/**
* ServiceFactoryBean
@ -41,7 +37,7 @@ import static org.apache.dubbo.config.spring.util.BeanFactoryUtils.addApplicatio
* @export
*/
public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean, DisposableBean,
ApplicationContextAware, ApplicationListener<ContextRefreshedEvent>, BeanNameAware,
ApplicationContextAware, BeanNameAware,
ApplicationEventPublisherAware {
@ -53,8 +49,6 @@ public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean
private transient String beanName;
private transient boolean supportedApplicationListener;
private ApplicationEventPublisher applicationEventPublisher;
public ServiceBean() {
@ -71,7 +65,6 @@ public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
SpringExtensionFactory.addApplicationContext(applicationContext);
supportedApplicationListener = addApplicationListener(applicationContext, this);
}
@Override
@ -88,16 +81,6 @@ public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean
return service;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (!isExported() && !isUnexported()) {
if (logger.isInfoEnabled()) {
logger.info("The service ready on spring started. service: " + getInterface());
}
export();
}
}
@Override
public void afterPropertiesSet() throws Exception {
if (StringUtils.isEmpty(getPath())) {
@ -107,9 +90,6 @@ public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean
setPath(beanName);
}
}
if (!supportedApplicationListener) {
export();
}
}
/**

View File

@ -397,11 +397,16 @@
<xsd:documentation><![CDATA[ Is default. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="metadata" type="xsd:string">
<xsd:attribute name="metadata-type" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The metadta type: local or remote. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="register-consumer" type="xsd:boolean">
<xsd:annotation>
<xsd:documentation><![CDATA[ Register consumer instance or not, default false. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
@ -590,6 +595,17 @@
<xsd:documentation><![CDATA[ work as metadata center or not. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="accepts" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
<![CDATA[ list of rpc protocols accepted by this registry, separated with ",". ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="preferred" type="xsd:boolean">
<xsd:annotation>
<xsd:documentation><![CDATA[ Is this registry the preferred one. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="metadataReportType">

View File

@ -392,11 +392,16 @@
<xsd:documentation><![CDATA[ Is default. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="metadata" type="xsd:string">
<xsd:attribute name="metadata-type" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The metadta type: local or remote. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="register-consumer" type="xsd:boolean">
<xsd:annotation>
<xsd:documentation><![CDATA[ Register consumer instance or not, default false. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="moduleType">
@ -584,6 +589,17 @@
<xsd:documentation><![CDATA[ work as metadata center or not. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="accepts" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
<![CDATA[ list of rpc protocols accepted by this registry, separated with ",". ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="preferred" type="xsd:boolean">
<xsd:annotation>
<xsd:documentation><![CDATA[ Is this registry the preferred one. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="metadataReportType">

View File

@ -0,0 +1,24 @@
/*
* 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.demo;
/**
*
*/
public interface GreetingService {
String hello();
}

View File

@ -0,0 +1,61 @@
<?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>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-demo</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<artifactId>dubbo-demo-servicediscovery-xml</artifactId>
<properties>
<skip_maven_deploy>true</skip_maven_deploy>
<spring-boot-maven-plugin.version>2.1.4.RELEASE</spring-boot-maven-plugin.version>
</properties>
<modules>
<module>servicediscovery-provider</module>
<module>servicediscovery-consumer</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,84 @@
<!--
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-demo-servicediscovery-xml</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>servicediscovery-consumer</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>The demo consumer module of dubbo project</description>
<properties>
<skip_maven_deploy>true</skip_maven_deploy>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-demo-interface</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-multicast</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-config-spring</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-remoting-netty4</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-hessian2</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,31 @@
/*
* 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.demo.consumer;
import org.apache.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
context.start();
DemoService demoService = context.getBean("demoService", DemoService.class);
String hello = demoService.sayHello("world");
System.out.println("result: " + hello);
}
}

View File

@ -0,0 +1 @@
dubbo.application.qos.port=33333

View File

@ -0,0 +1,7 @@
###set log levels###
log4j.rootLogger=info, stdout
###output to console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n

View File

@ -0,0 +1,30 @@
<?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.
-->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-consumer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181?registry.type=service"/>
<dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>
</beans>

View File

@ -0,0 +1,99 @@
<!--
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-demo-servicediscovery-xml</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>servicediscovery-provider</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>The demo provider module of dubbo project</description>
<properties>
<skip_maven_deploy>true</skip_maven_deploy>
<slf4j-log4j12.version>1.7.25</slf4j-log4j12.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-demo-interface</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-multicast</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-config-spring</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-remoting-netty4</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-hessian2</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-log4j12.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,27 @@
/*
* 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.demo.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml");
context.start();
System.in.read();
}
}

View File

@ -0,0 +1,34 @@
/*
* 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.demo.provider;
import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.rpc.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DemoServiceImpl implements DemoService {
private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
@Override
public String sayHello(String name) {
logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
}

View File

@ -0,0 +1 @@
dubbo.application.qos.port=22222

View File

@ -0,0 +1,7 @@
###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n

View File

@ -0,0 +1,34 @@
<?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.
-->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-provider"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181?registry.type=service"/>
<dubbo:protocol name="dubbo"/>
<bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
<dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/>
</beans>

View File

@ -35,7 +35,11 @@
<module>dubbo-demo-xml</module>
<module>dubbo-demo-annotation</module>
<module>dubbo-demo-api</module>
<module>dubbo-demo-servicediscovery-xml</module>
<!-- <module>servicediscovery-transfer</module>-->
<!-- <module>dubbo-call-sc</module>-->
<!-- <module>dubbo-call-sc&dubbo</module>-->
<!-- <module>sc-call-dubbo</module>-->
</modules>
<dependencyManagement>

View File

@ -0,0 +1,31 @@
/*
* 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.demo.provider;
import org.apache.dubbo.demo.GreetingService;
/**
*
*/
public class GreetingServiceImpl implements GreetingService {
@Override
public String hello() {
return "Greetings from server!";
}
}

View File

@ -0,0 +1,84 @@
<!--
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.dubbo</groupId>
<artifactId>servicediscovery-transfer</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>servicediscovery-consumer</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>The demo consumer module of dubbo project</description>
<properties>
<skip_maven_deploy>true</skip_maven_deploy>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-demo-interface</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-multicast</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-config-spring</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-remoting-netty4</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-hessian2</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,31 @@
/*
* 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.demo.consumer;
import org.apache.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
context.start();
DemoService demoService = context.getBean("demoService", DemoService.class);
String hello = demoService.sayHello("world");
System.out.println("result: " + hello);
}
}

View File

@ -0,0 +1 @@
dubbo.application.qos.port=33333

View File

@ -0,0 +1,7 @@
###set log levels###
log4j.rootLogger=info, stdout
###output to console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n

View File

@ -0,0 +1,30 @@
<?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.
-->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-consumer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181?registry.type=service"/>
<dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>
</beans>

View File

@ -106,20 +106,6 @@ public interface WritableMetadataService extends MetadataService {
return getExtensionLoader(WritableMetadataService.class).getDefaultExtension();
}
/**
* Get the metadata's storage type
*
* @param isDefaultStorageType is default storage type or not
* @return non-null, {@link #DEFAULT_METADATA_STORAGE_TYPE "default"} or {@link #REMOTE_METADATA_STORAGE_TYPE "remote"}
*/
public static String getMetadataStorageType(boolean isDefaultStorageType) {
return isDefaultStorageType ? DEFAULT_METADATA_STORAGE_TYPE : REMOTE_METADATA_STORAGE_TYPE;
}
static WritableMetadataService getExtension(boolean isDefaultStorageType) {
return getExtension(getMetadataStorageType(isDefaultStorageType));
}
static WritableMetadataService getExtension(String name) {
return getExtensionLoader(WritableMetadataService.class).getOrDefaultExtension(name);
}

View File

@ -215,7 +215,7 @@ public class ServiceInstanceMetadataUtils {
*/
public static String getDefaultMetadataStorageType() {
return ConfigManager.getInstance().getApplication()
.map(ApplicationConfig::getMetadataStorageType)
.map(ApplicationConfig::getMetadataType)
.orElse(DEFAULT_METADATA_STORAGE_TYPE);
}
@ -223,11 +223,11 @@ public class ServiceInstanceMetadataUtils {
* Set the metadata storage type in specified {@link ServiceInstance service instance}
*
* @param serviceInstance {@link ServiceInstance service instance}
* @param isDefaultStorageType is default storage type or not
* @param metadataType remote or local
*/
public static void setMetadataStorageType(ServiceInstance serviceInstance, boolean isDefaultStorageType) {
public static void setMetadataStorageType(ServiceInstance serviceInstance, String metadataType) {
Map<String, String> metadata = serviceInstance.getMetadata();
metadata.put(METADATA_STORAGE_TYPE_KEY, WritableMetadataService.getMetadataStorageType(isDefaultStorageType));
metadata.put(METADATA_STORAGE_TYPE_KEY, metadataType);
}
private static void setProviderHostParam(Map<String, String> params, URL providerURL) {