一、配置服務提供方
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(); // 執行遠端方法
}
}