快速启动

    如果不想使用 Spring 配置,可以通过 进行调用。

    完整安装步骤,请参见:示例提供者安装

    DemoService.java

    在服务提供方实现接口

    1. package com.alibaba.dubbo.demo.provider;
    2. import com.alibaba.dubbo.demo.DemoService;
    3. public String sayHello(String name) {
    4. return "Hello " + name;
    5. }
    6. }

    provider.xml:

    加载 Spring 配置

    Provider.java:

    1. public class Provider {
    2. public static void main(String[] args) throws Exception {
    3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/provider.xml"});
    4. context.start();
    5. System.in.read(); // 按任意键退出
    6. }
    7. }

    服务消费者

    完整安装步骤,请参见:示例消费者安装

    加载Spring配置,并调用远程服务

    Consumer.java

    1. public class Consumer {
    2. public static void main(String[] args) throws Exception {
    3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"http://10.20.160.198/wiki/display/dubbo/consumer.xml"});
    4. context.start();
    5. DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
    6. String hello = demoService.sayHello("world"); // 执行远程方法
    7. System.out.println( hello ); // 显示调用结果
    8. }

    • 该接口需单独打包,在服务提供方和消费方共享 ↩︎

    • 对服务消费方隐藏实现