SpringMVC项目中同时有provider和consumer.其中地址为引用的配置文件属性。如下:
basedata.properties属性文件
dubbo.registry.address=zk.dev.xxx.com:2181
dubbo.registry.port=20880
dubbo.payload=16777216
dubbo.consumer.address=zk.test.yyy1.com:2181?backup=zk.test.yyy2.com:2181,zk.test.yyy3.com:2181
dubbo-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">
<!--提供方应用名-->
<dubbo:application name="app-name" />
<!--使用zookeeper注册中心暴露服务地址-->
<dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" client="zkclient"/>
<!--用dubbo协议在20880端口暴露服务,最大传输大小-->
<dubbo:protocol name="dubbo" port="${dubbo.registry.port}" payload="${dubbo.payload}"
<!--dubbo监控-->
<dubbo:monitor protocol="registry"/>
<!--对外提供接口, 其中aaaServiceImpl为暴露接口对应的@Service实现类-->
<dubbo:service version="1.0.0" interface="com.test.prom.facade.service.TestXXFacade" ref="aaaServiceImpl"/>
</beans>
dubbo-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">
<!--使用zookeeper注册中心暴露服务地址-->
<dubbo:registry protocol="zookeeper" address="${dubbo.consumer.address}" timeout="30000" id="xxxRegistry"/>
<!--定义全局consumer配置,是否检查,重试次数,超时时间-->
<dubbo:consumer check="false" retries="0" timeout="30000" registry="xxxRegistry" version="1.0.0"/>
<!--引用dubbo服务接口-->
<dubbo:reference id="xxxService" interface="com.test.prom.aaa.testService" version="1.0.0" check="false" timeout="1000"/>
</beans>
spring-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"
default-lazy-init="false">
<!--读入配置属性文件-->
<context:property-placeholder location="classpath:props/basedata.properties"/>
<!--引入服务提供者配置文件-->
<import resource="spring/spring-provider.xml"/>
<!--引入服务消费者配置文件-->
<import resource="spring/spring-consumer.xml"/>
</beans>
=========================================================================================================
若如上写时,会出现provider中读取配置文件成功, 但consumer中address="${dubbo.consumer.address}"
引用的地址无法解析成配置文件中对应的值。
原因:dubbo端口未配置,这时会默认使用9090端口,且地址无法解析
解决办法:加上端口配置
<dubbo:registry protocol="zookeeper" address="${dubbo.consumer.address}" port="${dubbo.registry.port}" timeout="30000" id="xxxRegistry"/>