dubbo学习笔记1 简单的Application形式 dubbo服务搭建 提供者

一.编写提供者

方法一:使用dubbo默认的main方法启动

1.新建Maven项目,编写接口和实现类

2.在pom中加入依赖

<dependencies>
        <dependency>
            <!--dubbo的依赖-->
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.4.10</version>
        </dependency>
        <!--zookeeper的依赖-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.4</version>
        </dependency>
        <!--spring的依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.8.RELEASE</version>
        </dependency>
    </dependencies>

3.编写接口和实现类SayHellow

4.在resource目录中新建META-INF文件夹,并在MATE-INF下新建spring文件夹,创建配置文件 spring-dubbo.xml(dubbo自带容器运行时读取配置文件的位置)
详细参考:http://www.cnblogs.com/linjiqin/p/5859153.html

<?xml version="1.0" encoding="UTF-8"?>
<!--dubbo默认main运行时查找的配置文件的位置-->
<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="tiglle-dubbo-provider" />

    <!--注册中心,如果出现不能注册的情况,请使用ip,
    address:zeekeeper所在服务器
    :2181:这里要与 zookeeper 配置文件的 clientPort 属性值一致,指zk监听注册
费者,以后消费者就知道了提供者的提供端口,直接连接提供者,不用再依赖zk
    -->
    <dubbo:registry id="zk" address="tianxiaolin.cn:2181" protocol="zookeeper"/>

    <!--用dubbo协议在20886端口暴露服务(此端口为dubbo项目运行所在服务器的端
    口),不同的服务暴露的端口不能一样-->
    <dubbo:protocol id="mydubbo" name="dubbo" port="20886"/>

    <!--对registry和protocol的配置-->
    <dubbo:provider registry="zk" protocol="mydubbo"/>

    <!--提供的服务 ref:实现类的id,service:类的接口路径-->
    <dubbo:service interface="com.tiglle.service.SayHellow" ref="sayHellow"/>
    <!-- 提供者:(dubbo:service interface="接口路径") 提供服务-->
    <!-- 消费者:(dubbo:reference interface="接口路径") 提供服务-->
    <!-- 两个interface的值相同-->

    <!--提供的 service的实现类-->
    <bean id="sayHellow" class="com.tiglle.service.impl.SayHellowImpl"/>

</beans>
详细配置:http://blog.csdn.net/huangjin0507/article/details/52234178

5.启动zookeeper

6.启动dubbo自带的main方法:com.alibaba.dubbo.container.Main

7.到zookeeper处查看注册信息,执行zookeeper/bin/zkCli.sh,然后
ls /
  [dubbo, zookeeper]
ls /dubbo
  [com.tiglle.service.SayHellow]
ls /dubbo/com.tiglle.service.SayHellow
  [configurators, providers]
ls /dubbo/com.tiglle.service.SayHellow/providers
  [dubbo%3A%2F%2F12.12.12.161%3A20886%2Fcom.tiglle.service.SayHellow%3Fanyhost%3Dtrue%26application%3Dtiglle-dubbo-provider%26dubbo%3D2.4.10%26interface%3Dcom.tiglle.service.SayHellow%26methods%3DsayHellow%26pid%3D7080%26side%3Dprovider%26timestamp%3D1490189940409]

服务注册成功

如果出现注册没有反应,将dubbo:registry 的address改为ip,不使用域名

方法二:自己编写main方法启动
1.新建maven项目,编写接口和实现类
2.在pom.xml加入dubbo和zookeeper和spring的依赖

<!--spring依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
        </dependency>

        <!--dubbo的依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.4.10</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--zookeeper的依赖-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.4</version>
        </dependency>
    </dependencies>

3.在resources目录新建spring配置文件dubbo.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="tiglle-dubbo-provider-myself" />

    <!--注册中心,如果出现不能注册的情况,请使用ip-->
    <dubbo:registry id="zk" address="tianxiaolin.cn:2181" protocol="zookeeper"/>

    <!--用dubbo协议在20886端口暴露服务,不同的服务暴露的端口不能一样-->
    <dubbo:protocol id="mydubbo" name="dubbo" port="20888"/>

    <!--对registry和protocol的配置-->
    <dubbo:provider registry="zk" protocol="mydubbo"/>

    <!--提供的服务 service类的接口路径-->
    <dubbo:service interface="com.tiglle.service.SayName" ref="sayName"/>

    <!--提供的 service的实现类-->
    <bean id="sayName" class="com.tiglle.service.impl.SayNameImpl"/>


</beans>

4.新建main方法,运行并启动服务,注册到zookeeper

public static void main( String[] args ) throws Exception {
        //加载spring配置文件
        /*
        此方法不支持dubbo的配置
        BeanFactory context = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
        */
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "dubbo.xml" });
        context.start();//启动容器
        System.in.read();// 为保证服务一直开着,利用输入流的阻塞来模拟
        System.out.println( "Hello World!" );
    }

5.到zookeeper查看服务注册信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值