上一遍已经讲到dubbo项目划分,现在基于项目划分实现服务提供者
1.dubbo包的导入(maven)
(首先把问题提出来一下:本人现在使用的是jdk1.8,tomcat8可能运行项目时会出现component-scan包扫描错误提示了要用jdk1.7一下,原因是dubbo的com.alibaba包里面spring版本是2.0的4.x以上才能兼容jdk1.8所以可以吧jdk换成1.7在运行
)
2.provider配置
<?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: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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 注解包扫描位置 -->
<context:component-scan base-package="com.provider.impl" />
<!-- 接入dubbo的应用程序名称 -->
<dubbo:application name="demo-provider"/>
<!-- 注册创库地址: zookeeper组件,192.168.61.128:2181 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 声明需要暴露的服务接口
请注意ref属性中指定的HelloService接口实现类,它并没有在xml文件中定义,而是使用注解的方式在class中定义 -->
<dubbo:service interface="com.provider.DemoService" ref="helloServiceImpl"></dubbo:service>
</beans>
3.添加一个测试接口
4.编写测试类用main方法运行测试
package com.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ProviderApp {
public static void main(String[] args) throws Exception {
//读取配置文件
new ClassPathXmlApplicationContext(new String[]{"dubbo-provider.xml"});
System.out.println("provider服务已注册");
//使线程阻塞
System.in.read();
}
}