使用dubbo-api完成纯粹的RPC调用

1次阅读

共计 2678 个字符,预计需要花费 7 分钟才能阅读完成。

有的时候我们不想和 spring 集成,也不想使用 netty,可以使用 dubbo api 的方式手动构建服务提供者,服务消费者。

一、依赖

  <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-context</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-beans</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-web</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>netty</artifactId>
                    <groupId>org.jboss.netty</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

可以看到我已经把所有 spring 依赖和 netty 依赖全部排除了。引入 zkclient 是因为 dubbo 这个版本本身不带连接 zkclient 的 jar。

二、服务

public interface HelloService {String hello();
}
public class HelloServiceImpl implements HelloService {public String hello() {return "hello";}
}

三、构建 dubbo 服务提供者

public class Provider {public static void main(String[] args) throws IOException {HelloServiceImpl helloService = new HelloServiceImpl();

    // 当前应用配置
    ApplicationConfig application = new ApplicationConfig();
    application.setName("hello");

    // 连接注册中心配置
    RegistryConfig registry = new RegistryConfig();
    registry.setAddress("zookeeper://127.0.0.1:2181");
    // registry.setUsername("aaa");
    //registry.setPassword("bbb");

    // 服务提供者协议配置
    ProtocolConfig protocol = new ProtocolConfig();
    protocol.setName("rmi"); // 使用 jdk 的 RMI,替代 netty
    protocol.setPort(8888);
    protocol.setThreads(10);

    // 注意:ServiceConfig 为重对象,内部封装了与注册中心的连接,以及开启服务端口

// 服务提供者暴露服务配置
    ServiceConfig<HelloService> service = new ServiceConfig<HelloService>(); // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
    service.setApplication(application);
    service.setRegistry(registry); // 多个注册中心可以用 setRegistries()
    service.setProtocol(protocol); // 多个协议可以用 setProtocols()
    service.setInterface(HelloService.class);
    service.setRef(helloService);
    service.setVersion("1.0.0");

// 暴露及注册服务
    service.export();
    System.out.println("启动成功");
  }
}

四、构建 dubbo 服务消费者

public class Consumer {public static void main(String[] args) {
    // 当前应用配置
    ApplicationConfig application = new ApplicationConfig();
    application.setName("consumerA");

    // 连接注册中心配置
    RegistryConfig registry = new RegistryConfig();
    registry.setAddress("zookeeper://127.0.0.1:2181");

    // 注意:ReferenceConfig 为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接

    // 引用远程服务
    ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
    reference.setApplication(application);
    reference.setRegistry(registry); // 多个注册中心可以用 setRegistries()
    reference.setInterface(HelloService.class);
    reference.setVersion("1.0.0");

    // 和本地 bean 一样使用 xxxService
    HelloService helloService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
    String str = helloService.hello();
    System.out.println(str);
  }
}

输出结果:
hello

正文完
 0