《Apache Dubbo微服务开发从入门到精通》——配置手册——二、 API配置(上) https://developer.aliyun.com/article/1225171
3. 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"); // 服务提供者协议配置 ProtocolConfig protocol = new ProtocolConfig(); protocol.setName("dubbo"); protocol.setPort(12345); protocol.setThreads(200); // 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口 // 服务提供者暴露服务配置 ServiceConfig<DemoService> demoServiceConfig = new ServiceConfig<>(); demoServiceConfig.setInterface(DemoService.class); demoServiceConfig.setRef(new DemoServiceImpl()); demoServiceConfig.setVersion("1.0.0"); // 第二个服务配置 ServiceConfig<FooService> fooServiceConfig = new ServiceConfig<>(); fooServiceConfig.setInterface(FooService.class); fooServiceConfig.setRef(new FooServiceImpl()); fooServiceConfig.setVersion("1.0.0"); ... // 通过DubboBootstrap简化配置组装,控制启动过程 DubboBootstrap.getInstance() .application("demo-provider") // 应用配置 .registry(new RegistryConfig("zookeeper://127.0.0.1:2181")) // 注册中心配置 .protocol(protocol) // 全局默认协议配置 .service(demoServiceConfig) // 添加ServiceConfig .service(fooServiceConfig) .start() // 启动Dubbo .await(); // 挂起等待(防止进程退出) } } 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) { // 引用远程服务 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"); // 通过DubboBootstrap简化配置组装,控制启动过程 DubboBootstrap bootstrap = DubboBootstrap.getInstance(); bootstrap.application("demo-consumer") // 应用配置 .registry(new RegistryConfig("zookeeper://127.0.0.1:2181")) // 注册中心配置 .reference(demoServiceReference) // 添加ReferenceConfig .service(fooServiceReference) .start(); // 启动Dubbo ... // 和本地bean一样使用demoService // 通过Interface获取远程服务接口代理,不需要依赖ReferenceConfig对象 DemoService demoService = DubboBootstrap.getInstance().getCache().get(DemoService.class); demoService.sayHello("Dubbo"); FooService fooService = DubboBootstrap.getInstance().getCache().get(FooService.class); fooService.greeting("Dubbo"); } }
《Apache Dubbo微服务开发从入门到精通》——配置手册——二、 API配置(下) https://developer.aliyun.com/article/1225168