今天创建dubbo客户端遇到些问题,下面时需要注意和问题的解决方案
dubbo客户端创建步骤
1.pom文件加载jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
<type>jar</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
注意:这里需要将服务端的接口jar以及model和需要的相关jar进行导入,dubbo服务端实现类的jar不用导入
2.cosumer.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:component-scan base-package="com.yanghaun.test" />
<dubbo:application name="order-service-consumer"/>
<!--指定注册中心的地址 -->
<dubbo:registry protocol="zookeeper" address="10.159.59.8:2181,10.159.59.9:2181,10.159.59.12:2181"/>
<!--声明需要调用的远程服务接口,生成远程代理 -->
<dubbo:reference interface="com.yanghuan.dubbotest.DeviceEventService" id="deviceEventService" check="false"/>
<dubbo:reference interface="com.yanghuan.dubbotest.DeviceFaultService" id="deviceFaultService" check="false"/>
<dubbo:reference interface="com.yanghuan.dubbotest.DeviceOfflineCauseService" id="deviceOfflineCauseService" check="false"/>
<dubbo:reference interface="com.yanghuan.dubbotest.DeviceStatusService" id="deviceStatusService" check="false"/>
<dubbo:reference interface="com.yanghuan.dubbotest.DeviceWifiInfoService" id="deviceWifiInfoService" check="false"/>
<dubbo:reference interface="com.yanghuan.dubbotest.AmsAppService" id="appInfoService" check="false"/>
<dubbo:reference interface="com.yanghuan.dubbotest.AmsSystemService" id="systemInfoService" check="false"/>
</beans>
注意: xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
如果写错运行main时会出现Unable to locate Spring NamespaceHandler for XML schema的错误
如果遇到Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd问题,参照https://www.cnblogs.com/chenmingjun/p/9934384.html解决
3.main方法中调用
public class MainApplication {
public static void main(String[] args) throws Exception {
BasicConfigurator.configure(); //自动快速地使用缺省Log4j环境
//ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("consumer.xml");
@SuppressWarnings("resource")
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
ac.start();
AmsSystemService amsSystemService=(AmsSystemService)ac.getBean("systemInfoService");
SystemInfo systemInfo=amsSystemService.get("001");
System.out.println("systemInfo:"+systemInfo.toString());
System.out.println("调用完成.....");
System.in.read();
}
}
4.注意红线圈出的部分要设置为None,然后remove
dubbo服务端创建步骤相同,provider.xml与consumer.xml内容不同而已