JavaEE-Dubbo的搭建

官网地址

zookeeper: apache zookeeper

dubbo: quick start
关于zookeeper在Linux系统上的搭建,请查看我的另一篇博客

Dubbo

1.框架概述

dubbo 框架图
dubbo的框架由4部分构成:
1. provider
  主要是项目的业务层用来提供service,此service主要是与db进行交互,然后将数据返回。当然也可以有其他的文件存储例如redis等
2.registry
  注册中心主要负责将编写好的provider进行链接存储,对consumer的请求进行分发,然后返回处理好的结果。注册中心有:

  1. zookeeper
  2. multicast
  3. nacos
  4. redis
  5. simple

  官方推荐:zookeeper
3.consumer
  主要是项目的前端ui层,需要获取数据进行请求registry,由registry返回数据。例如列表数据,订单详情等java实体数据
4.moniter
  非必须,监控中心用来统计一些调用次数等。

2.项目引用

2.1 公共配置

  1. maven配置
<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>dubbo</artifactId>
		<version>2.6.0</version>
</dependency>
<dependency>
		<groupId>org.apache.zookeeper</groupId>
		<artifactId>zookeeper</artifactId>
		<version>3.4.7}</version>
</dependency>
  1. 接口配置
      为了在不同模块可以调用到相应的provider service,需要将功能接口提取出来,变为一个接口模块。供其他provider和consumer进行依赖。
    公共接口模块

2.2 provider的搭建

  1. xml配置
      在provider module的resources中创建如下xml,文件名随便起,例如 spring-service.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"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc.xsd
                            http://code.alibabatech.com/schema/dubbo
                            http://code.alibabatech.com/schema/dubbo/dubbo.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 指定应用名称 -->
    <dubbo:application name="health_service_provider"/>
    <!--指定暴露服务的端口,如果不指定默认为20880-->
    <dubbo:protocol name="dubbo" port="20888"/>
    <!--指定服务注册中心地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!--批量扫描,发布服务-->
    <dubbo:annotation package="com.xxx.xxx"/>
</beans>

xml中有一下几点需要注意:

  1. application可以随便起,但是不能与其他的provider和consumer一样,这个涉及到在存储中心存储的目录。例如,A consumer和 A provider的application都是A,那么注册中心创建节点时会将这两个放在一个节点下,明显不合理。

  2. protocol中port,尽量不要和其他的provider和consumer一样,如果都部署在一个服务器会出现端口冲突的问题。protocol的name有其他的参数,默认使用dubbo就可以了,如果要用其他的,请参考官方文档。

  3. registry的地址是zookeeper的地址,如果zookeeper是在公网上,那么需要修改为公网地址,且端口号保持一致。最后需要注意:不能provider module运行在本地,而address写的是公网地址,会注册不上去,因为本地地址公网又连不到,他怎么把消息发给你。

  4. annotation 已经默认添加了包扫描,可以不需要写context:component-scan了

  5. 代码设置
    继承接口模块后进行功能实现,需要在实现的接口上进行@Service注解,并且填写注解参数interfaceClass。值得注意的是此Service注解并非Spring的@Service注解而是alibaba的@Servcie注解。
    在这里插入图片描述

2.3 consumer的搭建

  1. xml配置
      在consumer module的resources中创建如下xml,文件名随便起,例如 spring-dubbo.xml,也可以直接写到spring-mvc的配置中:
<?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"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc.xsd
                            http://code.alibabatech.com/schema/dubbo
                            http://code.alibabatech.com/schema/dubbo/dubbo.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 指定应用名称 -->
   	<dubbo:application name="health_backend" />
    <!--指定服务注册中心地址-->
	<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!--批量扫描-->
	<dubbo:annotation package="com.xxx.xxx" />
    <!--
        超时全局设置 10分钟
        check=false 不检查服务提供方,开发阶段建议设置为false
        check=true 启动时检查服务提供方,如果服务提供方没有启动则报错
    -->
	<dubbo:consumer timeout="600000" check="false"/>
</beans>
  1. 代码设置
      继承公共接口模块之后,可以通过@Reference进行直接注入,同样的使用alibaba的@Reference,然后调用接口即可。

项目运行失败的问题排查

1.dubbo 服务不可用

1. 登入zookeeper,执行./zkCli.sh 
2. ls / 查看是否有dubbo节点
3. 停止所有java服务(window需要停止一下,linux重启下zkServer.sh即可)
4. 删除dubbo目录
	rmr /dubbo
	ls /
5. 启动provider项目,然后查看节点信息
	ls /dubbo
	    com.xxx.xxx
	ls /dubbo/com.xxx.xxx

2. zookeeper 连接不上

  1. zookeeper connected no yet
    一般是防火墙的原因
  2. zookeeper 没有启动
    ./zkServer.sh start 请查看第一章节我的另一篇博客
  3. ip和端口不对
  4. 如果不是本地运行,请检查防火墙是否放行
  5. 云服务器,例如 腾讯云 阿里云 需要设置入站规则,开放相应端口 2181以及provider的端口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值