这段时间经历非常痛苦,因为在做 springboot dubbo集成时提供者能成功注册到zookeeper,但消费者使用@Reference一直为null,从yml配置到properties属性配置,再到xml配置,一直未解决。后来是因zookeeper版本引用错误导致,今天记录下来,把自己进入dubbo的足迹留下来。
环境 :sts + springboot 2.0.1.RELEASE + dubbo 2.6.1 + zookeeper 3.4.12 + zkclient 0.10 + curator-framework 4.0.1
采用 xml 配置方式
提供者(服务):
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="hello-world-app" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="20886" />
<dubbo:annotation package="mydubbo.app.dubbo" />
<!-- 如果设置了 dubbo:annotation 则下列配置不需要,因为包含了以下配置 -->
<!-- <dubbo:reference id="helloService" interface="mydubbo.app.dubbo.helloWorld" /> -->
<!-- <bean id="demoServiceRemote" class="mydubbo.app.dubbo.impl.helloWorldImpl"></bean>
<dubbo:service interface="mydubbo.app.dubbo.helloWorld" ref="helloService" /> -->
</beans>
接口:
package mydubbo.app.dubbo;
public interface helloWorld {
String getCityName(String name);
}
接口实现:
package mydubbo.app.dubbo.impl;
import com.alibaba.dubbo.config.annotation.Service;
import mydubbo.app.dubbo.helloWorld;
@Service(version="1.0.0")
public class helloWorldImpl implements helloWorld {
public String getCityName(String name) {
return "您选择的城市是:"+name;
}
}
应用:
@SpringBootApplication
@ImportResource(value = {"classpath:provider.xml"}) //加载配置
@DubboComponentScan(basePackages = "mydubbo.app.dubbo")
public class MydubboApplication {
public static void main(String[] args) {
SpringApplication.run(MydubboApplication.class, args);
}
}
消费者:
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"
xmlns:p="http://www.springframework.org/schema/p"
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="consumer" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:annotation package="mydubbo.app.dubbo" />
<dubbo:reference version="1.0.0" id="helloService" interface="mydubbo.app.dubbo.helloWorld" />
</beans>