美文网首页
dubbo快速入门

dubbo快速入门

作者: 烟雾袅绕 | 来源:发表于2018-08-07 13:21 被阅读32次

    为什么要学习dubbo 这里不多做说明,有需要可自行百度

    说明:基于zookeeper 作为注册服务中心,

    1:新建两个maven 项目,一个作为服务提供者,一个作为服务消费者,如下

    dubbo-client  -- 服务消费者
    dubbo-provider -- 服务提供者
    

    2:服务端接口代码如下(服务接口作为服务提供者的一个子模块打包成jar包以供调用)

    public interface SayHello {
    
        public  String sayHello(String name);
    }
    

    3:接口实现代码如下

    public class SayHelloImpl implements SayHello {
        @Override
        public String sayHello(String name) {
    
            return "hello version 1.0.0"+name;
        }
    
        public static void main(String[] args) throws  Exception{
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-server.xml");
            context.start();
           System.in.read();
        }
    }
    

    4:版本升级处理,可通过配置文件指定version,然后在客户端指定调用的版本

    public class SayHelloImpl2 implements SayHello {
        @Override
        public String sayHello(String name) {
            return "hello version 2.0.0"+name;
        }
    
        public static void main(String[] args) throws  Exception{
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-server.xml");
            context.start();
           System.in.read();
        }
    }
    
    

    5:服务提供配置xml

    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/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-4.3.xsd
           http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!-- 提供方应用信息,用于计算依赖关系,dubbo-provider这个是项目名 -->
        <dubbo:application name="dubbo-provider"/>
      <!-- 这里使用的注册中心是zookeeper -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/>
       <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
       <!-- 将该接口暴露到dubbo中 ,在做服务升级的时候,为了兼容老版本,可通过 version 可以指定调用的版本-->
        <dubbo:service interface="com.share.dubbo.SayHello" ref="sayHelloImpl" version="1.0.0"/>
        <dubbo:service interface="com.share.dubbo.SayHello" ref="sayHelloImpl2" version="2.0.0"/>
        <bean id="sayHelloImpl" class="com.share.dubbo.SayHelloImpl" />
        <bean id="sayHelloImpl2" class="com.share.dubbo.SayHelloImpl2" />
    </beans>
    

    6:客户端调用(引入服务接口jar包)

    <dependency>
       <groupId>com.share.dubbo</groupId>
       <artifactId>service-api</artifactId>
       <version>1.0-SNAPSHOT</version>
     </dependency>
    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/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-4.3.xsd
           http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
       <!-- 提供方应用信息,用于计算依赖关系,dubbo-provider这个是项目名 -->
        <dubbo:application name="dubbo-client" />
        <!-- 这里使用的注册中心是zookeeper -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/>
        <!--通过version 指定调用的版本-->
        <dubbo:reference id="sayHelloImpl" interface="com.share.dubbo.SayHello" version="2.0.0"/>
    </beans>
    

    7:客服端调用服务

    ublic class SayHelloClient {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                    new String[] { "dubbo-client.xml" });
            context.start();
    
           SayHello hello = (SayHello)context.getBean("sayHelloImpl");
         String name =  hello.sayHello("我是来测试的");
            System.out.println(name);
        }
    }
    

    测试:
    启动zookeeper 后,在启动服务端,然后会在zookeeper 中看到新注册的服务中心地址,然后在启动客户端检验服务调用是否成功

    ls /dubbo/com.share.dubbo.SayHello/providers
    [dubbo%3A%2F%2F169.254.72.117%3A20880%2Fcom.share.dubbo.SayHello%3Fanyhost%3Dtrue%26application%3Ddubbo-provider%26dubbo%3D2.5.6%26generic%3Dfalse%26interface%3Dcom.share.dubbo.SayHello%26methods%3DsayHello%26pid%3D48779%26revision%3D1.0.0%26side%3Dprovider%26timestamp%3D1533543075021%26version%3D1.0.0, dubbo%3A%2F%2F169.254.72.117%3A20880%2Fcom.share.dubbo.SayHello2%3Fanyhost%3Dtrue%26application%3Ddubbo-provider%26dubbo%3D2.5.6%26generic%3Dfalse%26interface%3Dcom.share.dubbo.SayHello%26methods%3DsayHello%26pid%3D48779%26revision%3D2.0.0%26side%3Dprovider%26timestamp%3D1533543090459%26version%3D2.0.0]
    
    

    以上完成了 dubbo 简单的服务调用完成,事例代码稍后会上传github上以供下载

    相关文章

      网友评论

          本文标题:dubbo快速入门

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