Dubbo配置

转载连接 – Dubbo的四种配置

1.XML方式 (本文只写了XML配置方式)

2.注解方式

3.API方式

4.属性配置方式(properties)

4.1接口之类的公共类单独写成一个类,通过maven依赖引入provide和consumer中
4.2 注意:先启动zookeeper
4.3.dubbo调用和maven依赖的区别:
4.4.dubbo:跨系统通信。比如:两个系统,一个系统A作客户端,一个系统B作服务器, 服务器B把自己的接口定义提供给客户端A,客户端A将接口定义在spring中的bean。

客户端A可直接使用这个bean,就好像这些接口的实现(即服务器B的代码)也是在自己的代码里一样。
客户端A和服务器B在启动的时候都会把自己的机器IP注册到zookeeper上,客户端A会把zk上的服务端ip拉到磁盘上,并记录哪些ip提供哪些服务(服务端启动时暴露给zk),然后客户端根据ip调用服务端的服务。
dubbo需要将服务器B(提供方)的接口类打成包,服务器B(提供方)去实现,客户端A(消费方)去调用。

4.5.maven依赖:在一个多module的maven项目中,maven子模块间提供依赖实现调用。比

如,模块A调用模块B,将模块B打包成jar,引入到模块A中(相当于模块A拥有了模块B),实则模块A和模块B是在同一项目中运行。
而dubbo的提供者和消费者是两个独立的服务(A只是调用B,并未拥有B)。

4.6 provider启动类写上这个注解@ImportResource(locations = “classpath:provider.xml”)
4.7 provider 的 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:cache="http://www.springframework.org/schema/cache"
       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://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="provider"/>
    <!-- zookeeper地址 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    <!-- 用dubbo协议在28888端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="28888"/>
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service  interface="org.example.service.IUserService" ref="UserServiceImpl"  />
    <!-- 接口实现类路径 -->
    <bean id="UserServiceImpl" class="com.example.dubbo.provider.service.impl.UserServiceImpl"/>
</beans>

4.8 consumer启动类写上这个注解@ImportResource(locations = “classpath:consumer.xml”)
4.9 consumer的service引用接口

@Resource
private IUserService iUserService;

4.10 consumer 的 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:cache="http://www.springframework.org/schema/cache"
       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://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">

    <!-- 消费方应用信息,用于计算依赖关系 -->
    <dubbo:application name="consumer"/>
    <!-- zookeeper地址 -->
    <dubbo:registry protocol="zookeeper"
                    address="127.0.0.1:2181"/>
    <!-- 要调用的服务方接口和实现类 -->
    <dubbo:reference  interface="org.example.service.IUserService"
                      id="UserServiceImpl"  />
</beans>

5. Dubbo依赖

<!-- **************************** Dubbo 依赖 **************************** -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.3</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>

        </dependency>
        <!-- **************************** /Dubbo 依赖 **************************** -->

6 invoke指令

6.1 使用telnet命令进入控制台
  1. 命令:telnet ip 端口 ( 提供者(provider)用dubbo协议的暴露的 17886端口,不是项目的端口)
6.2 使用invoke命令注入
  1. 如果注入的是json
    那就直接传入json串就ok了,如果是基础数据类型,也可以分别对应参数直接传
    invoke XxxService.xxxMethod({“prop”: “value”}, 1, “1”)
  2. 如果注入的是对象(com.cn.Test。具体的方法名:test(List tList))
    对象,我们也是按照json的格式传,只不过在每个对象后都需要指定具体的这个对象具体所属的类型。
    invoke XxxService.xxxMethod([{“key”:“11”,“merchantId”:“888”,“skuId”:“111”,“class”:“com.cn.Test”}])
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值