学习dubbo(1):安装zk和简单的提供者和消费者

《深入理解apache dubbo与实战》学习笔记

一:安装zk

很简单,官网下载:http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.6/

dubbo源码:https://github.com/apache/dubbo.git

我下载的是目前最新版,要下载apache-zookeeper-3.5.6-bin.tar.gz 的,没有bin的没用,这里一开始我下的是没有bin的,一直启动不了,不知道没有bin的包是干嘛用的,还没百度,待会百度查下

下载了解压,修改conf下的zoo.cfg文件,百度一堆怎么修改,然后启动就行。

在idea里新建一个项目,引入各种包,最重要的当然是dubbo了

 <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.3</version>
            </dependency>

先写提供者

提供者接口

public interface EchoService {

    /**
     *
     * @param message
     * @return
     */
    String echo (String message);
}

这个就是我们平时开发时定义的接口,要打包出去给别人调用的

实现类:

public class EchoServiceImpl implements EchoService {


    @Override
    public String echo(String message) {
        String now = new SimpleDateFormat("HH:mm:ss").format(new Date());
        String a = "["+now+"] hello"+message+", request from consumer:"+ RpcContext.getContext().getRemoteAddress();
        return a;
    }
}

实现类里具体做一些功能逻辑,很简单

然后需要配置一下,这里我们用的是xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       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="echo-provider"/>

    <!-- 使用本地zookeeper作为注册中心 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- 只用Dubbo协议并且指定监听端口 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 通过xml方式配置为bean, 让spring托管和实例化 -->
    <bean id="echoService" class="com.dubbo.study.echo.Impl.EchoServiceImpl"/>

    <!-- 声明服务暴露的接口,并暴露服务 -->
    <dubbo:service interface="com.dubbo.study.echo.api.EchoService" ref="echoService"/>


</beans>

配置文件里主要是定义你的服务名字,注册到哪个zk,然后用spring管理你的接口,就是bean,

这里踩的坑就是配置dubbo链接时按照书上的一直不对,这可能是版本问题,要用http://code.alibabatech.com/schema/dubbo这个

搞半天,

然后写消费者

public class EchoConsumer {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{"spring/echo-consumer.xml"});
        context.start();
        EchoService echoService = (EchoService) context.getBean("echoService");
        String message = echoService.echo("hello world");
        System.out.println("result:"+message);
    }
}

消费者也需要一个配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       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="echo-consumer"/>

    <!-- 使用本地zookeeper作为注册中心 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- 指定要消费的服务 -->
    <dubbo:reference id="echoService" check="false" interface="com.dubbo.study.echo.api.EchoService"/>

</beans>

跟提供者一样的功能

其实配置文件的话可以把里面的拆出来,比如,zk拆出来固定一个地方配置,暴露出去的接口用一个文件,引入接口用一个文件配置,

然后写一个服务提供者的main方法用于启动提供者,

public class EchoProvider {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/echo-provider.xml"});
        context.start();
        System.in.read();
    }
}

现在把提供者的这个main方法启动起来,你在控制台中就可以看见一堆日志,并且注册到了你本地的zk。

然后启动消费者的main方法,看控制台你会发现有打印出来你想要的东西。

学习代码:https://gitee.com/heqiang917/dubbo-study.git

以上是基于xml的,还有基于注解的和java的api形式的,在学习代码中有,其实不管啥形式,原理都是一样的,暴露服务,消费服务,通过zk来联系两个不同的服务,使其一个服务能调用到另外一个的服务的接口,不同的是dubbo是用rpc的,springCloud是用http的,各有优点,分布式与微服务分不开,都是致力于部署多个服务,分布式着重点是一个服务部署多台机器,微服务致力于一个大服务拆成多个小服务,各不受影响,其实现在公司都是分布式与微服务并存使用。

这就是一个简单的dubbo提供者消费者例子,我也初学,哈哈哈

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值