1.Hello World

服务者者的Spring配置文件applicationProvider.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-app"  />
  
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234"  />
  
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 用dubbo协议在20880端口暴露服务 -->

    <dubbo:service interface="com.apartsman.provider.IProcessData" ref="demoService" />
    <bean id="demoService" class="com.apartsman.provider.ProcessDataImpl" />
</beans>

新建一个包叫做com.apartsman.provider,这是服务的提供方:

public interface IProcessData {
    public String hello(String name);
}

public class ProcessDataImpl  implements IProcessData,Serializable {

    public String hello(String name) {
    
        return name;
    }

}

发布服务:

public class Publish {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationProvider.xml" });
        context.start();
        System.out.println("按任意键退出");
        System.in.read();
    }
}

消费者的Spring配置文件applicationConsumer.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://code.alibabatech.com/schema/dubbo 
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <dubbo:application name="consumer-of-helloworld-app"  />
    <dubbo:registry address="multicast://224.5.6.7:1234"  check="false" />
        <dubbo:consumer timeout="1200000"/>
    <dubbo:reference id="demoService" interface="com.apartsman.provider.IProcessData" />
</beans>

新建一个包com.apartsman.custom,这是服务的消费方:

public class AppTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });
        context.start();
        IProcessData demoService = (IProcessData) context.getBean("demoService");// 获取远程服务代理
        System.out.println(demoService.hello(" hello world!"));    
    }
}

要注意的三个问题:

  • multicast地址不能配成127.0.0.1,也不能配成机器的IP地址,必须是D段广播地址,也就是:224.0.0.0239.255.255.255之间的任意地址,这里直接采用dubbo官方的地址

  • timeout以毫秒为准。如果设置的太小会超时。报出超时异常

  • dubbo:registrycheck要设置为"false"。因为客户端的配置文件并没有对demoService实现,Spring会做检查。如果Spring发现没有这样id对应的实例,就会报出异常。


happyfish
2.4k 声望66 粉丝

[链接]


« 上一篇
JavaIO