dubbo快速入门

为什么要学习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



    
    
  
    
   
    
   
    
    
    
    

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


   com.share.dubbo
   service-api
   1.0-SNAPSHOT
 



   
    
    
    
    
    

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快速入门)