我们前面使用xml配置了服务的提供方和消费方,其实Dubbo还提供了基于注解的配置。
我们下面把之前的服务改造成注解的方式。
分别创建服务提供者和服务消费者:
消费者:
添加的依赖和之前一样:
对于服务提供方:
Service接口:
package cn.com.dubbo.service;
public interface SortService {
//升序排序
int[] ascendingSort(int[] arrays);
}
Service实现中,添加Service暴露注解:
package cn.com.dubbo.service.impl;
import cn.com.dubbo.service.SortService;
import com.alibaba.dubbo.config.annotation.Service;
@Service(version="1.0.0")
public class SortServiceImpl implements SortService{
@Override
public int[] ascendingSort(int[] arrays) {
int temp=0;
//冒泡升序排序
for (int i = 0; i < arrays.length-1; i++) {
for (int j = i+1; j < arrays.length; j++) {
if(arrays[i]>arrays[j]){
temp=arrays[i];
arrays[i]=arrays[j];
arrays[j]=temp;
}
}
}
return arrays;
}
}
服务提供执行类Provider:
package cn.com.dubbo.run;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
//远程provider调用
/*ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"http://192.168.1.102/wiki/display/dubbo/provider.xml"});*/
context.start();
System.in.read(); // 按任意键退出
}
}
配置文件provider.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 提供方应用信息,也可以用dubbo.properties配置 -->
<dubbo:application name="annotation-arrays-sort-app" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
<dubbo:annotation package="cn.com.dubbo.service" />
</beans>
对于服务消费方:
Service接口与服务提供方一样。
编写一个SortAction:
package cn.com.dubbo.action;
import org.springframework.stereotype.Component;
import cn.com.dubbo.service.SortService;
import com.alibaba.dubbo.config.annotation.Reference;
@Component //与xml生命bean一样
public class SortAction {
@Reference(version="1.0.0") // 获取远程服务代理
private SortService sortService;
public void sort(){
int [] arrays = {234,1,45,22,123};
System.out.println("排序前:");
for (int i = 0; i < arrays.length; i++) {
System.out.println("arrays["+i+"]="+arrays[i]);
}
arrays = sortService.ascendingSort(arrays); // 执行远程方法
//显示调用结果
System.out.println("排序后:");
for (int i = 0; i < arrays.length; i++) {
System.out.println("arrays["+i+"]="+arrays[i]);
}
}
}
服务执行类Consumer:
package cn.com.dubbo.run;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Reference;
import cn.com.dubbo.action.SortAction;
import cn.com.dubbo.service.SortService;
public class Consumer {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
//远程consumer调用
/*ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"http://192.168.1.102/wiki/display/dubbo/consumer.xml"});*/
context.start();
SortAction sortAction=(SortAction)context.getBean("sortAction");
sortAction.sort();
}
}
配置文件consumer.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="annotation-consumer-of-arraysSort-app" />
<!-- 使用zookeeper注册中心暴露发现服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
<dubbo:annotation package="cn.com.dubbo.service" />
</beans>
然后我们启动Zookeeper:
运行服务提供端Provider类:
然后运行消费方Consumer类:
可以看到,我们的注解也实现了服务的暴露、注册与获取。