RPC远程调用

项目结构

dubbo - 图1

示例1

直接调用服务提供方的接口实现类对象

接口

  1. public interface DemoService {
  2. String sayHello(String name);
  3. }

服务提供方

接口实现类

  1. public class DemoServiceImpl implements DemoService {
  2. @Override
  3. public String sayHello(String name) {
  4. return "你好" + name;
  5. }
  6. }

配置文件

  1. <!--服务提供者名称-->
  2. <dubbo:application name="provider"/>
  3. <!--接口实现类对象-->
  4. <bean id="demoService" class="com.cskaoyan.service.impl.DemoServiceImpl"/>
  5. <!--定义服务-->
  6. <dubbo:service interface="com.cskaoyan.api.DemoService" ref="demoService" registry="N/A"/>
  7. <!--对外暴露的协议-->
  8. <dubbo:protocol name="dubbo" port="20880"/>

主程序

  1. public class Main {
  2. public static void main(String[] args) throws IOException {
  3. // 初始化Spring容器
  4. ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
  5. // DemoService demoService = (DemoService) app.getBean("demoService");
  6. // System.out.println(demoService.sayHello("中国"));
  7. // 阻塞
  8. System.in.read();
  9. }
  10. }

调用者

主程序

  1. public class Main {
  2. public static void main(String[] args) {
  3. // 获取Spring容器
  4. ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
  5. // 获取接口实现对象
  6. DemoService demoService = (DemoService) app.getBean("demoService");
  7. String res = demoService.sayHello("中国");
  8. System.out.println(res);
  9. }
  10. }

配置文件

  1. <!--调用者名称-->
  2. <dubbo:application name="comsumer"/>
  3. <!--服务接口-->
  4. <dubbo:reference interface="com.cskaoyan.api.DemoService" id="demoService" url="dubbo://localhost:20880"/>

示例2

基于注册中心的RPC远程调用

只需要启动注册中心服务

服务提供方配置文件如下

  1. <!--服务提供者名称-->
  2. <dubbo:application name="provider"/>
  3. <!--接口实现类对象-->
  4. <bean id="demoService" class="com.cskaoyan.service.impl.DemoServiceImpl"/>
  5. <!--定义服务-->
  6. <dubbo:service interface="com.cskaoyan.api.DemoService" ref="demoService"/>
  7. <!--对外暴露的协议-->
  8. <dubbo:protocol name="dubbo" port="20880"/>
  9. <!--注册中心-->
  10. <dubbo:registry address="zookeeper://localhost:2181" />

调用者提供方配置文件如下

  1. <!--调用者名称-->
  2. <dubbo:application name="comsumer"/>
  3. <!--服务接口-->
  4. <dubbo:reference interface="com.cskaoyan.api.DemoService" id="demoService"/>
  5. <!--注册中心-->
  6. <dubbo:registry address="zookeeper://localhost:2181" />

示例3

基于SpringBoot框架的RPC远程调用

接口

  1. public interface DemoService {
  2. String sayHello(String name);
  3. }

服务方

接口实现类

  1. @Service
  2. public class DemoServiceImpl implements DemoService {
  3. @Override
  4. public String sayHello(String name) {
  5. return "你好" + name;
  6. }
  7. }

主程序

  1. @SpringBootApplication
  2. public class ProviderTest {
  3. public static void main(String[] args) {
  4. SpringApplication.run(ProviderTest.class, args);
  5. }
  6. }

配置文件

  1. dubbo:
  2. application:
  3. name: provider
  4. protocol:
  5. name: dubbo
  6. port: 20880
  7. scan:
  8. base-packages: com.cskaoyan.service.impl
  9. registry:
  10. address: zookeeper://localhost:2181

调用者

  1. @Component
  2. public class ConsumerService {
  3. @Reference(interfaceClass = DemoService.class)
  4. DemoService demoService;
  5. public String sayHello(String name){
  6. return demoService.sayHello(name);
  7. }
  8. }

主程序

  1. @SpringBootApplication
  2. @ComponentScan(basePackages = "com.cskaoyan.service")
  3. public class ConsumerTest {
  4. public static void main(String[] args) {
  5. // 获取Spring容器
  6. ApplicationContext app = SpringApplication.run(ConsumerTest.class, args);
  7. ConsumerService consumerService = (ConsumerService) app.getBean("consumerService");
  8. String res = consumerService.sayHello("中国");
  9. System.out.println(res);
  10. }
  11. }

配置文件

  1. dubbo:
  2. application:
  3. name: consumer
  4. registry:
  5. address: zookeeper://localhost:2181