根据前边的helloWord案例我们不难看出使用xml方式进行Dubbo配置是比较繁琐的,其是在实际开发中大多遵循一个规则(服务的消费用xml配置,而服务的实现用注解配置)
这里我们先看一个注解实现的案例
依旧是两个工程DubboProvider和DubboConsumer
【dubboProvider】
接口dubbo.common.provider.CommonService.java
public interface CommonService {
public String common();
}
接口实现dubbo.common.provider.impl.CommonServiceImpl.java
import org.springframework.stereotype.Service;
import dubbo.common.provider.CommonService;
/**
*
* @author lvfang
*
* 这里注意两个service,一个spring的一个是alibaba
* @Service("commonServiceImpl")注入bean,注意xml进行包扫描
* @com.alibaba.dubbo.config.annotation.Service服务声明
* interfaceClass:服务接口类
* protocol:服务内容,这里是Dubbo服务
* retries:失败重启次数,一般为0
*/
@Service("commonServiceImpl")
@com.alibaba.dubbo.config.annotation.Service(
interfaceClass=CommonService.class,
protocol={"dubbo"},
retries=0)
public class CommonServiceImpl implements CommonService {
@Override
public String common() {
return "exec common !";
}
}
加载配 dubbo.common.test.Provider.java
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"common-provider.xml"});
context.start();
System.out.println("服务已启动");
System.in.read();
}
}
xml配置: common-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: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">
<!-- 具体实现bean 扫描代替(注意不能重复扫描)-->
<dubbo:annotation package="dubbo.common"/>
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="common-provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.22.128:2181" />
<!-- 用dubbo协议在20880端口暴露服务(服务端口要保证唯一性) -->
<dubbo:protocol name="dubbo" port="20883" />
</beans>
启动服务
【dubboConsumer】
接口:dubbo.common.provider.Common.java(接口与服务方保持一致)
public interface CommonService {
public String common();
}
服务调用测试类:dubbo.common.test.Provider.java
public class Provider {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"common-consumer.xml"});
CommonService commonService = (CommonService) context.getBean("commonService");
System.out.println(commonService.common());
}
}
消费方xml配置:common-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: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="common-consumer" />
<!-- 使用zookeeper注册中心暴露发现服务地址 -->
<dubbo:registry address="zookeeper://192.168.22.128:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="commonService" interface="dubbo.common.provider.CommonService" />
</beans>
服务调用