开始接触spring boot2整合dubbo时,参考了好多文章,都不太完全,导致整合屡次失败。坚信降低门槛才会普及大众,否则技术再好束之高阁也白搭。
dubbo配置文件分好几种:xml类、properties类、api配置类、注解类。另外注册中心常用zookeeper和redis等。spring boot和dubbo版本不同。种种组合稍有出入就会导致失败,给初学者造成非常大困扰~
遇到的四处大坑和经验:
1、在注册中心上栽过坑,后来采用zookeeper作为中心解决。曾以redis试了好久没成功,改为zookeeper立马成功,看来dubbo推荐zookeeper是有道理的。(--追加说明,redis注册中心也配置好了,漏了用户和密码配置。使用jedis 2.9.0版本成功,2.9.1版本启动报错)
(正确的redies注册中心配置:<dubbo:registry address="redis://localhost:6379" username="root" password="123456"/>)
2、暴露dubbo服务上也栽过坑,在provider实现的类以@Service暴露成功(选的是阿里的import com.alibaba.dubbo.config.annotation.Service;)
3、加载xml文件也栽过坑,后来找到方法,在spring boot启动中加载@ImportResource({"classpath:dubbo/consumer.xml"}) 解决。
@SpringBootApplication
@ImportResource({"classpath:dubbo/consumer.xml"})
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
4、dubbo程序启动进行注册有时会报错,可以忽略,如下
2019-01-20 16:58:14.853 INFO 1036 --- [ main] c.a.d.r.zookeeper.ZookeeperRegistry : [DUBBO] Register: dubbo://192.168.0.106:20880/cc.itil.oms.api.DemoService?anyhost=true&application=myService&dubbo=2.6.2&generic=false&interface=cc.itil.oms.api.DemoService&methods=sayHello&pid=1036&side=provider&timeout=600000×tamp=1547974675270, dubbo version: 2.6.2, current host: 192.168.0.106
2019-01-20 16:58:19.923 ERROR 1036 --- [ main] org.apache.curator.ConnectionState : Connection timed out for connection string (localhost:2181) and timeout (5000) / elapsed (22147)
org.apache.curator.CuratorConnectionLossException: KeeperErrorCode = ConnectionLoss
at org.apache.curator.ConnectionState.checkTimeouts(ConnectionState.java:225) [curator-client-2.12.0.jar:na]
at org.apache.curator.ConnectionState.getZooKeeper(ConnectionState.java:94) [curator-client-2.12.0.jar:na]
at org.apache.curator.CuratorZookeeperClient.getZooKeeper(CuratorZookeeperClient.java:117) [curator-client-2.12.0.jar:na]
at org.apache.curator.framework.imps.CuratorFrameworkImpl.getZooKeeper(CuratorFrameworkImpl.java:489) [curator-framework-2.12.0.jar:na]
at org.apache.curator.framework.imps.ExistsBuilderImpl$3.call(ExistsBuilderImpl.java:237) [curator-framework-2.12.0.jar:na]
at org.apache.curator.framework.imps.ExistsBuilderImpl$3.call(ExistsBuilderImpl.java:226) [curator-framework-2.12.0.jar:na]
##############华丽丽的分割线##########
好了,接下来把全部文件粘贴出来,供大家参考,也加深自己的印象。
环境说明
1、Eclipse Jee Oxygen(win10)
2、zookeeper(zookeeper-3.4.13/centos7)
3、spring-boot-starter-parent(2.1.2.RELEASE)
4、jdk 1.8
5、dubbo-spring-boot-starter(0.2.0)
6、分两个项目(dubbo-provider和dubbo-consumer),两个项目中都存在接口类DemoService
相关截图如下:
1、eclipse目录