美文网首页
(2) dubbo的入门使用

(2) dubbo的入门使用

作者: Mrsunup | 来源:发表于2018-12-08 14:31 被阅读0次

1.dubbo的架构

  • 调用关系说明
    1.服务容器负责启动,加载,运行服务提供者。
    2.服务提供者在启动时,向注册中心注册自己提供的服务
    3.服务消费者在启动时,向注册中心订阅自己所需的服务
    4.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
    5.服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
    6.服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

接下里里介绍一下没有注册中心的直连方式以及有注册中心的连接方式以及多注册多协议多版本的配置

2.没有注册中心的直连方式

服务端的情况
  • 加入dubbo的依赖
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
  • 服务的接口
/**
 * IHello接口
 */
public interface IHello {
    String sayHello(String msg);
}

  • 服务的具体实现
/**
 * @Project: dubbo
 * @description: 具体的接口的实现
 * @author: sunkang
 * @create: 2018-06-24 16:27
 * @ModificationHistory who      when       What
 **/
@Service
public class HelloImpl  implements IHello  {
    @Override
    public String sayHello(String msg) {
        return "hello world "+ msg;
    }
}
  • 具体的服务端的xml配置dubbo-server-direct.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="dubbo-provider"  owner="sunkang"/>
    <!--不需要注册中心-->
    <dubbo:registry  address="N/A"/>
    <!--发布协议-->
    <dubbo:protocol  name="dubbo" port="20880"/>
    <!--启动过程中会有看到地址: dubbo://192.168.44.1:20880/com.sunkang.dubbo.IHello-->
    <dubbo:service interface="com.sunkang.dubbo.IHello" ref="helloService1" ></dubbo:service>

    <bean id="helloService1" class="com.sunkang.dubbo.HelloImpl"></bean>
</beans>
  • 服务发布的启动
/**
 * @Project: dubbo
 * @description:  服务端的启动
 * @author: sunkang
 * @create: 2018-06-24 16:34
 * @ModificationHistory who      when       What
 **/
public class bootStrap {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-server-direct.xml");
        context.start();
        //线程阻塞
        System.in.read();
    }
}
2018-12-08 13:31:31,751 INFO [com.alibaba.dubbo.config.AbstractConfig] -  [DUBBO] Export dubbo service com.sunkang.dubbo.IHello to url 
dubbo://192.168.44.1:20880/com.sunkang.dubbo.IHello?anyhost=true&application=dubbo-provider&dubbo=2.5.3&interface=com.sunkang.dubbo.IHello&methods=sayHello&owner=sunkang&pid=10144&side=provider&timestamp=1544247091312, dubbo version: 2.5.3, current host: 127.0.0.1
消费端的情况
  • 消费端的xml的配置dubbo-client-direct.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="dubbo-client" owner="sunkang"/>
    <!--通过直接连接的方式 需要通过url直接连接  -->
    <dubbo:reference id="helloImpl" interface = "com.sunkang.dubbo.IHello"
                     url="dubbo://192.168.44.1:20880/com.sunkang.dubbo.IHello"/>
</beans>
  • 消费端的启动进行调用
/**
 * @Project: dubbo
 * @description:  消费端启动消费
 * @author: sunkang
 * @create: 2018-12-08 13:38
 * @ModificationHistory who      when       What
 **/
public class ClientBootStrap {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-client-direct.xml");
        IHello hello = context.getBean("helloImpl",IHello.class);
        System.err.print(hello.sayHello("i am dubbo"));
    }
}
  • 输出结果如下
hello world i am dubbo

3.有注册中心的连接方式

  • 需要加入zookeeper的依赖,dubbo默认使用zkclient连接
        <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.10</version>
        </dependency>
  • 对应的服务端的xml调整

需要把服务端的xml的配置的标签为dubbo:registry改动如下

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

可以登录zookeeper观看服务地址列表的变化:dubbo的信息是基于url进行驱动的,管理信息可以在url地址看到,可以提供者列表只有一条信息,并且该url协议为dubbo而且地址为192.168.44.1而且接口名称为com.sunkang.dubbo.IHello

[zk: localhost:2181(CONNECTED) 3] ls /dubbo/com.sunkang.dubbo.IHello/providers
[dubbo%3A%2F%2F192.168.44.1%3A20880%2Fcom.sunkang.dubbo.IHello%3Fanyhost%3Dtrue%26application%3Ddubbo-provider%26dubbo%3D2.5.3%26interface%3Dcom.sunkang.dubbo.IHello%26methods%3DsayHello%26owner%3Dsunkang%26pid%3D14628%26side%3Dprovider%26timestamp%3D1544250185704]
  • 对应的消费端的xml调整
    dubbo:reference的引用不需要指定url,调用的服务地址会直接从注册中心获取
    <dubbo:reference id="helloImpl" interface = "com.sunkang.dubbo.IHello"/>

4.多注册中心和多协议和多版本的配置

这里不再讲述,可以参考官网的

多注册中心:https://dubbo.apache.org/zh-cn/docs/user/demos/multi-registry.html

  <?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world"  />
    <!-- 多注册中心配置 -->
    <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
    <!-- 向中文站注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="chinaRegistry" />
    <!-- 向国际站注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" registry="intlRegistry" />
</beans>

多协议:https://dubbo.apache.org/zh-cn/docs/user/demos/multi-protocols.html

<?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://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> 
    <dubbo:application name="world"  />
    <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />
    <!-- 多协议配置 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <dubbo:protocol name="rmi" port="1099" />
    <!-- 使用dubbo协议暴露服务 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" protocol="dubbo" />
    <!-- 使用rmi协议暴露服务 -->
    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" protocol="rmi" /> 
</beans>

多版本:https://dubbo.apache.org/zh-cn/docs/user/demos/multi-versions.html
这里的版本信息也会加入都url中,进行版本的识别

<dubbo:service interface="com.foo.BarService" version="1.0.0" />

相关文章

网友评论

      本文标题:(2) dubbo的入门使用

      本文链接:https://www.haomeiwen.com/subject/nrlqhqtx.html