springboot + dubbo + zookeeper 搭建日记

这段时间经历非常痛苦,因为在做 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>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值