Dubbo 学习(二)服务注册与发现

在上一篇中我们提到,dubbo官网上推荐使用ZooKeeper作为注册中心。那么今天我们就通过代码来实践一番,看看一个dubbo的服务消费者如果找到通过ZooKeeper暴露自己的dubbo服务提供者,并成功完成交互。

首先创建一个dubbo-test工程,并添加相关的依赖:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.6</version>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.10</version>
</dependency>
<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.5</version>
</dependency>
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.32.Final</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>2.8.0</version>
</dependency>

从依赖中也可以看到,dubbo的底层通信实际上是借助netty实现的。接下来我们在dubbo-test工程中创建一个名为dubbo-test-provider的module,并编写需要暴露的服务接口。

首先创建一个接口,ProviderService:

package provider.service;

public interface ProviderService {
String helloService(String name); }

接下来对这个接口进行实现:

package provider.impl;

import provider.service.ProviderService;

public class ProviderServiceImpl implements ProviderService {
    
    public String helloService(String name) {
        return "Hello " + name + "!";
    }
}

开始构建服务者使用的dubbo配置文件:

<?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 = "dubbo_provider" />

    <!--注册中心 -->
    <dubbo:registry address = "zookeeper://127.0.0.1:2181" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name = "dubbo" port = "20880" />

    <!--服务发布的配置,需要暴露的服务接口-->
    <dubbo:service interface = "provider.service.ProviderService" ref = "providerService" />

    <!--Bean-->
    <bean id = "providerService" class = "provider.impl.ProviderServiceImpl"/>

</beans>

这里补充说明一下,如果需要对接的是一个ZooKeeper集群的话,可以采用下方的写法:

<dubbo:registry protocol = "zookeeper" address = "192.168.11.129:2181,192.168.11.130:2181"/>

启动类中自然需要加载dubbo的配置文件:

public class DubboProvider {

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

此时的目录结构如下:

接下来继续添加一个module,起名为dubbo-test-consumer。需要注意的是,除非两个module加载同一份接口类,否则消费者项目中也需要有路径相同的接口声明,如下图所示:

由于我们的消费者仅仅用于调用服务提供者暴露的接口,无需暴露自己本身,因此dubbo配置文件会简单一些,如下所示:

<?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 = "dubbo_consumer" />

    <!--注册中心 -->
    <dubbo:registry address = "zookeeper://127.0.0.1:2181" />

    <dubbo:reference id = "providerService" interface = "provider.service.ProviderService"/>

</beans>

启动类中进行服务的调用并打印返回结果:

public class DubboConsumer {

    public static void main( String[] args ) throws IOException {

        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        ProviderService providerService = (ProviderService) context.getBean("providerService");
        String s = providerService.helloService("eddy");
        System.out.println(s);
        System.in.read();

    }
}

依次启动ZooKeeper、服务端以及消费端,效果如下:

 

 

参考资料:

https://blog.csdn.net/liyintaoliuyun/article/details/80066269

https://segmentfault.com/a/1190000019896723

https://blog.csdn.net/hjiacheng/article/details/55000570

posted on 2021-09-25 19:26  DluT_eDdy  阅读(443)  评论(0编辑  收藏  举报

导航