API 配置
使用 API 配置您的 Dubbo 应用程序
通过 API 编码组装配置、启动 Dubbo、发布和订阅服务。此方法可以支持动态创建 ReferenceConfig/ServiceConfig,结合泛化调用满足 API 网关或测试平台的需求。
参考 API 示例
服务提供者
通过 ServiceConfig 暴露服务接口,并将服务接口发布到注册中心。
注意:为了更好地支持 Dubbo3 应用程序级服务发现,建议使用新的 DubboBootstrap API。
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ProviderConfig;
import org.apache.dubbo.config.ServiceConfig;
import com.xxx.DemoService;
import com.xxx.DemoServiceImpl;
public class DemoProvider {
public static void main(String[] args) {
// service implementation
DemoService demoService = new DemoServiceImpl();
// current application configuration
ApplicationConfig application = new ApplicationConfig();
application.setName("demo-provider");
// connect registry configuration
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://10.20.130.230:2181");
// service provider protocol configuration
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(12345);
protocol.setThreads(200);
// Note: ServiceConfig is a heavy object, which internally encapsulates the connection with the registration center and opens the service port
// The service provider exposes the service configuration
ServiceConfig<DemoService> service = new ServiceConfig<DemoService>(); // This instance is very heavy and encapsulates the connection with the registration center, please cache it yourself, otherwise it may cause memory and connection leaks
service. setApplication(application);
service.setRegistry(registry); // Multiple registries can use setRegistries()
service.setProtocol(protocol); // multiple protocols can use setProtocols()
service.setInterface(DemoService.class);
service.setRef(demoService);
service.setVersion("1.0.0");
// expose and register services
service. export();
// Suspend waiting (to prevent the process from exiting)
System.in.read();
}
}
服务消费者
通过 ReferenceConfig 引用远程服务,并从注册中心订阅服务接口。
注意:为了更好地支持 Dubbo3 应用程序级服务发现,建议使用新的 DubboBootstrap API。
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ConsumerConfig;
import org.apache.dubbo.config.ReferenceConfig;
import com.xxx.DemoService;
public class DemoConsumer {
public static void main(String[] args) {
// current application configuration
ApplicationConfig application = new ApplicationConfig();
application.setName("demo-consumer");
// connect registry configuration
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://10.20.130.230:2181");
// Note: ReferenceConfig is a heavy object, which internally encapsulates the connection with the registry and the connection with the service provider
// reference the remote service
ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>(); // This instance is very heavy and encapsulates the connection with the registry and the provider, please cache it yourself, otherwise it may cause memory and connection leaks
reference.setApplication(application);
reference.setRegistry(registry); // Multiple registries can use setRegistries()
reference.setInterface(DemoService.class);
reference.setVersion("1.0.0");
// use demoService like local bean
// Note: This proxy object internally encapsulates all communication details, the object is heavy, please cache and reuse
DemoService demoService = reference. get();
demoService.sayHello("Dubbo");
}
}
Bootstrap API
DubboBootstrap API 可以减少重复配置,更好地控制启动过程,支持批量发布/订阅服务接口,更好地支持 Dubbo3 的应用程序级服务发现。
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ProviderConfig;
import org.apache.dubbo.config.ServiceConfig;
import com.xxx.DemoService;
import com.xxx.DemoServiceImpl;
public class DemoProvider {
public static void main(String[] args) {
ConfigCenterConfig configCenter = new ConfigCenterConfig();
configCenter.setAddress("zookeeper://127.0.0.1:2181");
// service provider protocol configuration
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(12345);
protocol.setThreads(200);
// Note: ServiceConfig is a heavy object, which internally encapsulates the connection with the registration center and opens the service port
// The service provider exposes the service configuration
ServiceConfig<DemoService> demoServiceConfig = new ServiceConfig<>();
demoServiceConfig.setInterface(DemoService.class);
demoServiceConfig.setRef(new DemoServiceImpl());
demoServiceConfig.setVersion("1.0.0");
// Second service configuration
ServiceConfig<FooService> fooServiceConfig = new ServiceConfig<>();
fooServiceConfig.setInterface(FooService.class);
fooServiceConfig.setRef(new FooServiceImpl());
fooServiceConfig.setVersion("1.0.0");
...
// Use DubboBootstrap to simplify configuration assembly and control the startup process
DubboBootstrap. getInstance()
.application("demo-provider") // application configuration
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181")) // registry configuration
.protocol(protocol) // global default protocol configuration
.service(demoServiceConfig) // add ServiceConfig
.service(fooServiceConfig)
.start() // start Dubbo
.await(); // suspend waiting (to prevent the process from exiting)
}
}
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ProviderConfig;
import org.apache.dubbo.config.ServiceConfig;
import com.xxx.DemoService;
import com.xxx.DemoServiceImpl;
public class DemoConsumer {
public static void main(String[] args) {
// reference the remote service
ReferenceConfig<DemoService> demoServiceReference = new ReferenceConfig<DemoService>();
demoServiceReference.setInterface(DemoService.class);
demoServiceReference.setVersion("1.0.0");
ReferenceConfig<FooService> fooServiceReference = new ReferenceConfig<FooService>();
fooServiceReference.setInterface(FooService.class);
fooServiceReference.setVersion("1.0.0");
// Use DubboBootstrap to simplify configuration assembly and control the startup process
DubboBootstrap bootstrap = DubboBootstrap. getInstance();
bootstrap.application("demo-consumer") // application configuration
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181")) // registry configuration
.reference(demoServiceReference) // add ReferenceConfig
.service(fooServiceReference)
.start(); // Start Dubbo
...
// use demoService like local bean
// Obtain the remote service interface proxy through Interface, without relying on the ReferenceConfig object
DemoService demoService = DubboBootstrap.getInstance().getCache().get(DemoService.class);
demoService.sayHello("Dubbo");
FooService fooService = DubboBootstrap.getInstance().getCache().get(FooService.class);
fooService. greeting("Dubbo");
}
}
其他配置
- 基本配置
- 方法级配置
- 点对点直连
API 提供最灵活丰富的配置能力,以下是一些可配置组件的示例。
基本配置
全局基本配置可以在 DubboBootstrap 中设置,包括应用程序配置、协议配置、注册中心、配置中心、元数据中心、模块、监控、SSL、提供者配置、消费者配置等。
// registry
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://192.168.10.1:2181");
...
// service provider protocol configuration
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(12345);
protocol.setThreads(200);
...
// configuration center
ConfigCenterConfig configCenter = new ConfigCenterConfig();
configCenter.setAddress("zookeeper://192.168.10.2:2181");
...
// metadata center
MetadataReportConfig metadataReport = new MetadataReportConfig();
metadataReport.setAddress("zookeeper://192.168.10.3:2181");
...
// Metrics
MetricsConfig metrics = new MetricsConfig();
metrics.setProtocol("dubbo");
...
// SSL
SslConfig ssl = new SslConfig();
ssl.setServerKeyCertChainPath("/path/ssl/server-key-cert-chain");
ssl.setServerPrivateKeyPath("/path/ssl/server-private-key");
...
// Provider configuration (ServiceConfig default configuration)
ProviderConfig provider = new ProviderConfig();
provider.setGroup("demo");
provider.setVersion("1.0.0");
...
// Consumer configuration (ReferenceConfig default configuration)
ConsumerConfig consumer = new ConsumerConfig();
consumer.setGroup("demo");
consumer.setVersion("1.0.0");
consumer.setTimeout(2000);
...
DubboBootstrap. getInstance()
.application("demo-app")
.registry(registry)
.protocol(protocol)
.configCenter(configCenter)
.metadataReport(metadataReport)
.module(new ModuleConfig("module"))
.metrics(metrics)
.ssl (ssl)
.provider(provider)
.consumer(consumer)
...
.start();
方法级设置
...
// method-level configuration
List<MethodConfig> methods = new ArrayList<MethodConfig>();
MethodConfig method = new MethodConfig();
method. setName("sayHello");
method.setTimeout(10000);
method. setRetries(0);
methods. add(method);
// reference the remote service
ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>(); // This instance is very heavy and encapsulates the connection with the registry and the provider, please cache it yourself, otherwise it may cause memory and connection leaks
...
reference.setMethods(methods); // Set method-level configuration
...
点对点直连
...
// This instance is very heavy, it encapsulates the connection with the registry and the provider, please cache it yourself, otherwise it may cause memory and connection leaks
ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
// If point-to-point direct connection, you can use reference.setUrl() to specify the target address. After setting the url, the registration center will be bypassed.
// Among them, the protocol corresponds to the value of provider.setProtocol(), and the port corresponds to the value of provider.setPort().
// The path corresponds to the value of service.setPath(). If no path is set, the default path is the interface name
reference.setUrl("dubbo://10.20.130.230:20880/com.xxx.DemoService");
...
上次修改时间:2023 年 1 月 2 日:增强 en 文档 (#1798) (95a9f4f6c1c)