1.代码实现
既然是通过API配置实现的,那么就是要创建纯Java项目或者Maven项目,我这里以Maven项目为例
(1).首先下载dubbo和zookeeper相关的的jar包
(2).创建服务提供者
public class Test { public static void main(String[] args) throws InterruptedException { // 服务实现 XxxService xxxService = new XxxServiceImpl(); // 当前应用配置 ApplicationConfig application = new ApplicationConfig(); application.setName(“xxx”); RegistryConfig registry = new RegistryConfig(); registry.setProtocol(“zookeeper”); registry.setAddress(“192.168.1.233:2181”); //这里填写zookeeper的地址 // 服务提供者协议配置 ProtocolConfig protocol = new ProtocolConfig(); protocol.setName(“dubbo”); protocol.setPort(12345); protocol.setThreads(200); // 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口 // 服务提供者暴露服务配置 ServiceConfig
(3).服务消费者
public class TestClient { public static void main(String[] args) { // 当前应用配置 ApplicationConfig application = new ApplicationConfig(); application.setName(“yyy”); // 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接 RegistryConfig registry = new RegistryConfig(); registry.setProtocol(“zookeeper”); registry.setAddress(“192.168.1.233:2181”); // 引用远程服务 ReferenceConfig
(4).运行结果
上边就是dubbo使用API配置实现的方式,这种配置还是有使用场景的,我就是遇到了使用spring配置文件配置方式解决不了的情况,使用API配置就可以解决
最后修改于 2019-07-11

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。