快速启动
如果不想使用 Spring 配置,可以通过 API 的方式 进行调用。
完整安装步骤,请参见:
DemoService.java [1]:
在服务提供方实现接口
package com.alibaba.dubbo.demo.provider;
import com.alibaba.dubbo.demo.DemoService;
public String sayHello(String name) {
return "Hello " + name;
}
}
provider.xml:
加载 Spring 配置
Provider.java:
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/provider.xml"});
context.start();
System.in.read(); // 按任意键退出
}
}
服务消费者
完整安装步骤,请参见:示例消费者安装
加载Spring配置,并调用远程服务
Consumer.java [3]:
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/consumer.xml"});
context.start();
DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
String hello = demoService.sayHello("world"); // 执行远程方法
System.out.println( hello ); // 显示调用结果
}
该接口需单独打包,在服务提供方和消费方共享
对服务消费方隐藏实现 ↩︎