学习记录: SpringBoot+Dubbo+zookeeper整合实现,以及遇到的坑

学习记录: SpringBoot+Dubbo+zookeeper整合

  • 学习记录
    • 下面开始
      • zookeeper启动注意事项
      • dubbo-admin下载下来发布到Tomcat下就可以
    • 遇到的坑
        • 使用spring mvc也是可以的 但是自己学习,想先看看效果,所以就怎么快怎么来
    • 此次学习结束

学习记录

第一次写博客,如有不当请联系后删除,本片记录为自我学习,借鉴了CSDN中许多大神的博客,运行通过后记录下来以便于以后可能会用到,本片主要借鉴于【那些离我而去的时光】写的文章《SpringBoot+Dubbo+zookeeper整合》

下面开始

我主要开发软件为eclipse,new 一个maven project -->> dubbo-parent:
学习记录: SpringBoot+Dubbo+zookeeper整合实现,以及遇到的坑_第1张图片
parent pom修改


pom


    UTF-8
    1.8
    1.0.0




    org.springframework.boot
    spring-boot-starter-parent
    1.5.2.RELEASE



    dubbo-consumer
    dubbo-provider
    dubbo-api




    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter-test

 

    io.dubbo.springboot
    spring-boot-starter-dubbo
    ${dubbo-spring-boot}

新建三个maven module (dubbo-api,dubbo-provider,dubbo-consumer)

根据我的理解,api即服务接口,provider服务提供者,主要是对api接口的实现,consumer消费者没什么,就是调接口的操作

1).以api module为例,parent project需要选择dubbo-parent,下一步下一步直接完成就好了
学习记录: SpringBoot+Dubbo+zookeeper整合实现,以及遇到的坑_第2张图片
2).新建interface,DubboService,随便写一个方法

public interface DubboService {
    public void sayHello(String name);
}

3).在新建的dubbo-provider与dubbo-consumer的pom中添加dubbo-api依赖

 
    ******
    dubbo-api
    0.0.1-SNAPSHOT

4).provider中重写api方法。

import ******.service.DubboService;
import com.alibaba.dubbo.config.annotation.Service;

@Service(version="1.0.0")
public class DubboServiceImpl implements DubboService {

    @Override
    public void sayHello(String name) {
	    System.out.println(name);
	    System.out.println("---dubbo provider 发布服务---");
    }

}

5).provider添加springboot启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProviderApplication {
    public static void main(String[] args){
        SpringApplication.run(ProviderApplication.class,args);
    }
}

6).resource下添加配置文件application.properties

#spring.application.name=dubbo-provider
server.port=7001
#应用名称
spring.dubbo.application.name=dubbo-provider
#注册中心地址
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
#暴露服务方式
spring.dubbo.protocol.name=dubbo
#暴露服务端口
spring.dubbo.protocol.port=20880
spring.dubbo.scan=******.service.impl

添加配置文件dubbo-provider.xml




    
    
    
    
    

7).consumer中添加访问接口controller DubboController

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ******.service.DubboService;
import com.alibaba.dubbo.config.annotation.Reference;

@RestController
@RequestMapping("/dubbo")
public class DubboController {

    @Reference(version="1.0.0")
    DubboService dubboService;

    @RequestMapping("sayHello")
    public void sayHello() {
        System.out.println("dubbo开始调用");
        dubboService.sayHello("consumer 请求服务");
    }
}

8).添加启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args){
        SpringApplication.run(ConsumerApplication.class,args);
    }
}

9).resource下添加配置文件application.properties

#spring.application.name=dubbo-consumer
server.port=7002
spring.dubbo.application.name=dubbo-consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=******

添加配置文件dubbo-consumer.xml




    
    
    

10).先启动服务提供者,在启动消费者,(这里默认启动zookeeper)
访问消费者接口 http://localhost:7002/dubbo/sayHello

可以看到消费者控制台打印输出
学习记录: SpringBoot+Dubbo+zookeeper整合实现,以及遇到的坑_第3张图片
提供者控制台输出
学习记录: SpringBoot+Dubbo+zookeeper整合实现,以及遇到的坑_第4张图片

可以启动dubbo-admin看下我们是否成功发布了服务
学习记录: SpringBoot+Dubbo+zookeeper整合实现,以及遇到的坑_第5张图片
学习记录: SpringBoot+Dubbo+zookeeper整合实现,以及遇到的坑_第6张图片

(我觉得应该这就算是完成了吧,但是dubbo+zookeeper实际上的用途还没仔细的看过,之后会实践)

zookeeper启动注意事项

下载好zookeeper,需要修改修改sample配置文件为zoo.cfg,然后启动bin/zkServer.cmd
这是我的配置,我好像就增加了
dataDir=D:\software\zookeeper-3.4.8\data
dataLogDir=D:\software\zookeeper-3.4.8\log
这两个记录数据与日志的文件夹

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=5
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=2
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=D:\\software\\zookeeper-3.4.8\\data
dataLogDir=D:\\software\\zookeeper-3.4.8\\log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

dubbo-admin下载下来发布到Tomcat下就可以

遇到的坑

java.lang.IllegalStateException: Failed to check the status of the service ******.service.DubboService. No provider available for the service ******.service.DubboService:1.0.0 from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=dubbo-consumer&dubbo=2.5.3&interface=******.service.DubboService&methods=sayHello&pid=16276&revision=1.0.0&side=consumer×tamp=1552284818363&version=1.0.0 to the consumer 192.168.8.31 use dubbo version 2.5.3
	at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:420) ~[dubbo-2.5.3.jar:2.5.3]
	at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:300) ~[dubbo-2.5.3.jar:2.5.3]
	at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:138) ~[dubbo-2.5.3.jar:2.5.3]
	at com.alibaba.dubbo.config.spring.AnnotationBean.refer(AnnotationBean.java:302) ~[dubbo-2.5.3.jar:2.5.3]
	at com.alibaba.dubbo.config.spring.AnnotationBean.postProcessBeforeInitialization(AnnotationBean.java:233) ~[dubbo-2.5.3.jar:2.5.3]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) [spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) [spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) [spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
	at ******.ConsumerApplication.main(ConsumerApplication.java:9) [classes/:na]

  1. 第一次学习的时候总会出现这种错误
    我是因为在还没启动提供者的时候先启动了消费者,当时不理解,现在想想,也应该是先有DubboService这个服务提供出来,才能去消费它,所以一定要先启动提供者后启动消费者,这都是我自己的理解。如果可以不先启动提供者就可以,希望能有人告诉我2333.
    这个错误会导致第一,提供者启动不起来,第二dubboService无法实例化,会出现空指针异常

  2. 第二次做的时候,发现这个错误也出现了,后来发现是因为第一次复制代码代码没有什么错误,第二次手动敲的时候在实现api接口的时候忘记写了关于版本号的注解@Service(version=“1.0.0”),这样dubboService也是无法实例化的。目前就遇到这两个问题,

使用spring mvc也是可以的 但是自己学习,想先看看效果,所以就怎么快怎么来

最后感谢博主【那些离我而去的时光】的文章。

此次学习结束

version 2.0 文中所有******代表Group id或者包名

你可能感兴趣的:(学习记录: SpringBoot+Dubbo+zookeeper整合实现,以及遇到的坑)