Dubbo使用

DUBBO使用

服务提供者

首先,我们先把服务端的接口写好,因为其实 dubbo 的作用简单来说就是给消费端提供接口。

实现服务接口

复制代码
 1 /**
 2  * 要暴露的服务接口
 3  */
 4 
 5 
 6 public interface ProviderService {
 7 
 8     String SayHello(String word);
 9 }
10 
11 
12 /**
13  * x要暴露的服务接口实现类
14  */
15 public class ProviderServiceImpl implements ProviderService{
16 
17     public String SayHello(String word) {
18         return word;
19     }
20 }
复制代码

导入 maven 依赖

复制代码
   <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
        <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 的版本是 2.6.6 ,需要注意的是,如果你只导入 dubbo 的包的时候是会报错的,找不到 netty 和 curator 的依赖,所以,在这里我们需要把这两个的依赖加上,就不会报错了。

另外,这里我们使用 zookeeper 作为注册中心。

到目前为止,dubbo 需要的环境就已经可以了,下面,我们就把上面刚刚定义的接口暴露出去。

暴露接口(xml 配置方法)

首先,我们在我们项目的 resource 目录下创建 META-INF.spring 包,然后再创建dubbo-spring.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="provider" owner="sihai">
        <dubbo:parameter key="qos.enable" value="true"/>
        <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
        <dubbo:parameter key="qos.port" value="55555"/>
    </dubbo:application>

    <dubbo:monitor protocol="registry"/>

    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <!--<dubbo:registry address="zookeeper:127.0.0.1:2181"/>   配置zk注册中心 -->
    <dubbo:registry address="N/A" />

    <!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
    <dubbo:protocol name="dubbo" port="20880"/>

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

    <!--Bean bean定义-->
    <bean id="providerService" class="com.sihai.dubbo.provider.service.ProviderServiceImpl"/>

</beans>
复制代码

1.上面的文件其实就是类似 spring 的配置文件,而且,dubbo 底层就是 spring。
2.节点:dubbo:application
就是整个项目在分布式架构中的唯一名称,可以在 name 属性中配置,另外还可以配置 owner 字段,表示属于谁。
下面的参数是可以不配置的,这里配置是因为出现了端口的冲突,所以配置。
3..节点:dubbo:monitor
监控中心配置, 用于配置连接监控中心相关信息,可以不配置,不是必须的参数。
4.节点:dubbo:registry
配置注册中心的信息,比如,这里我们可以配置 zookeeper 作为我们的注册中心。address 是注册中心的地址,这里我们配置的是 N/A 表示由 dubbo 自动分配地址。或者说是一种直连的方式,不通过注册中心。
5.节点:dubbo:protocol
服务发布的时候 dubbo 依赖什么协议,可以配置 dubbo、webserovice、Thrift、Hessain、http等协议。

不同协议的请求方式如下:

dubbo: //ip: 20880/org.apache.dubbo.demo.DemoService
http: //ip: 80/org . apache.dubbo.dem.DemoService

6.节点:dubbo:service
这个节点就是我们的重点了,当我们服务发布的时候,我们就是通过这个配置将我们的服务发布出去的。interface 是接口的包路径,ref 是第 7点配置的接口的 bean。

retries=2属性,服务调用失败后重试次数2;(容错集群时使用

相当于DUBBO的@Service,可以设置timeout属性,代表执行时间超过XXMS,会抛出warn。

7.最后,我们需要像配置 spring 的接口一样,配置接口的 bean。

 

到这一步,关于服务端的配置就完成了,下面我们通过 main 方法将接口发布出去。 直连方式

复制代码
 1 /**
 2  * xml方式启动
 3  *
 4  */
 5 public class App 
 6 {
 7     public static void main( String[] args ) throws IOException {
 8         //加载xml配置文件启动
 9         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/provider.xml");
10         context.start();
11         System.in.read(); // 按任意键退出
12     }
13 }
复制代码

发布接口非常简单,因为 dubbo 底层就是依赖 spring 的,所以,我们只需要通过 ClassPathXmlApplicationContext 拿到我们刚刚配置好的 xml ,然后调用 context.start() 方法就启动了。

看到下面的截图,就算是启动成功了,接口也就发布出去了。

你以为到这里就结束了了,并不是的,我们拿到 dubbo 暴露出去的 url分析分析。

dubbo 暴露的 url

dubbo://192.168.234.1:20880/com.sihai.dubbo.provider.service.ProviderService?anyhost=true
&application=provider&bean.name=com.sihai.dubbo.provider.service.ProviderService&bind.ip=192.168.234.1
&bind.port=20880&dubbo=2.0.2&generic=false&interface=com.sihai.dubbo.provider.service.ProviderService
&methods=SayHello&owner=sihai&pid=8412&qos.accept.foreign.ip=false&qos.enable=true&qos.port=55555&side=provider&timestamp=1562077289380

分析

首先,在形式上我们发现,其实这么牛逼的 dubbo 也是用类似于 http 的协议发布自己的服务的,只是这里我们用的是 dubbo 协议。
dubbo://192.168.234.1:20880/com.sihai.dubbo.provider.service.ProviderService
上面这段链接就是 ? 之前的链接,构成:协议://ip:端口/接口。发现是不是也没有什么神秘的。
anyhost=true&application=provider&bean.name=com.sihai.dubbo.provider.service.ProviderService&bind.ip=192.168.234.1&bind.port=20880&dubbo=2.0.2&generic=false&interface=com.sihai.dubbo.provider.service.ProviderService&methods=SayHello&owner=sihai&pid=8412&qos.accept.foreign.ip=false&qos.enable=true&qos.port=55555&side=provider&timestamp=1562077289380
? 之后的字符串,分析后你发现,这些都是刚刚在 dubbo-spring.xml  中配置的字段,然后通过 & 拼接而成的,闻到了 http 的香味了吗?

服务消费者

在服务端提供的只是点对点的方式提供服务,并没有使用注册中心,所以,下面的配置也是会有一些不一样的。

消费端环境配置

首先,我们在消费端的 resource 下建立配置文件 dubbo-spring.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="consumer" owner="sihai"/>

    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <!--点对点的方式-->
    <dubbo:registry address="N/A" />
    <!--<dubbo:registry address="zookeeper://localhost:2181" check="false"/>-->

    <!--生成一个远程服务的调用代理-->
    <!--点对点方式-->
    <dubbo:reference id="providerService"
                     interface="com.sihai.dubbo.provider.service.ProviderService"
                     url="dubbo://192.168.234.1:20880/com.zcy.dubbo.provider.service.ProviderService"/>

    <!--<dubbo:reference id="providerService"
                     interface="com.zcy.dubbo.provider.service.ProviderService"/>-->

</beans>
复制代码

分析

① 发现这里的 dubbo:application 和 dubbo:registry 是一致的。
② dubbo:reference :我们这里采用点对点的方式,所以,需要配置在服务端暴露的 url 。此配置会获取远程Bean对象。进行对象的属性填充阶段。(同@Reference注解)

@Reference可以设置timeout属性,代表调用时间超过XXMS,会报错,不再接受返回结果。

 

 

maven 依赖

和服务端一样

复制代码
 1 /**
 2  * xml的方式调用
 3  *
 4  */
 5 public class App 
 6 {
 7     public static void main( String[] args ) throws IOException {
 8 
 9         ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("consumer.xml");
10         context.start();
11         ProviderService providerService = (ProviderService) context.getBean("providerService");
12         String str = providerService.SayHello("hello");
13         System.out.println(str);
14         System.in.read();
15 
16     }
17 }
复制代码

 

加入 zookeeper 作为注册中心

 

 

在前面的案例中,我们没有使用任何的注册中心,而是用一种直连的方式进行的。但是,实际上很多时候,我们都是使用 dubbo + zookeeper 的方式,使用 zookeeper 作为注册中心,这里,我们就介绍一下 zookeeper 作为注册中心的使用方法。

这里,我们在前面的入门实例中进行改造。

 

 

 服务提供者

在dubbo-spring.xml配置文件中修改注册中心

    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <!--<dubbo:registry address="N/A"/>-->
    <dubbo:registry address="zookeeper://localhost:2181" check="false"/>



 <!--如果是 zookeeper 集群的话,使用下面的方式。-->
<dubbo:registry protocol="zookeeper" address="192.168.11.129:2181,192.168.11.137:2181,192.168.11.138:2181"/>

 

服务消费者

复制代码
    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <!--点对点的方式-->
    <!--<dubbo:registry address="N/A" />-->
    <dubbo:registry address="zookeeper://localhost:2181" check="false"/>

    <!--生成一个远程服务的调用代理-->
    <!--点对点方式-->
    <!--<dubbo:reference id="providerService"
                     interface="com.sihai.dubbo.provider.service.ProviderService"
                     url="dubbo://192.168.234.1:20880/com.sihai.dubbo.provider.service.ProviderService"/>-->

    <dubbo:reference id="providerService"
                     interface="com.sihai.dubbo.provider.service.ProviderService"/>
复制代码

1.注册中心配置跟服务端一样。

<dubbo:registry address="zookeeper://localhost:2181"/>

2.dubbo:reference
由于我们这里使用 zookeeper 作为注册中心,所以,跟点对点的方式是不一样的,这里不再需要 dubbo 服务端提供的 url 了,只需要直接引用服务端提供的接口即可。

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

好了,消费端也配置好了,这样就可以使用修改的入门案例,重新启动运行了。

将 dubbo 发布的 url 注册到了 zookeeper,消费端从 zookeeper 消费,zookeeper 相当于一个中介,给消费者提供服务。

官方推荐 xml 的配置dubbo方法。此外还有注解配置方式

posted @   堤苏白  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示