一、配置服务提供方
1、首先配置spring配置
<?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:service inter ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
<dubbo:annotation package="com.alibaba.dubbo.annotation.service" />
</beans>
2、编写服务提供方的实现类
package com.alibaba.dubbo.annotation.service.impl;
import com.alibaba.dubbo.annotation.service.DemoService;
import com.alibaba.dubbo.config.annotation.Service;
@Service(version = "1.0.0")
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
// TODO Auto-generated method stub
System.out.print("annotation say"+name);
return name;
}
}
DemoService 接口类类同上一篇,封装在soa-common项目中
提供方的启动类不在赘述,参考作者上一篇文章
二、配置服务消费者
1、首先配置spring
<?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" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" inter />
<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
<dubbo:annotation package="com.alibaba.dubbo.action" />
</beans>
2、调用类如下;
package com.alibaba.dubbo.action;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.annotation.service.DemoService;
import com.alibaba.dubbo.config.annotation.Reference;
@Component
public class DemoAction {
@Reference(version = "1.0.0")
private DemoService demoService;
public void say() {
demoService.sayHello("hello");
}
}
3、然后测试类如下;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.action.DemoAction;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
context.start();
DemoAction demoService = (DemoAction)context.getBean("demoAction"); // 获取远程服务代理
demoService.say(); // 执行远程方法
}
}