dubbo入门-helloworld

    xiaoxiao2023-08-11  174

    在dubbo中,服务被注册在注册中心中,我们把提供服务的server成为服务提供方,调用服务的server称为服务调用方,两者通过RPC进行调用,并使用了dubbo协议(使用的协议可以经过配置进行修改)协调工作。为了demo的方便,这里把服务接口以及相关的一些依赖类复制放在了两个不同的工程中,在实际的开发中,我们需要提取一个API的工程,发布到MAVEN仓库中,由服务调用方和服务提供方引用依赖。

    准备工作

    这里使用zookeeper作为注册中心,所以需要准备zookeeper的可用环境。dubbo官网中推荐zookeeper作为注册中心,但据官网所述,阿里内部没有使用zookeeper,而是使用自己实现的一套注册中心方案。zookeeper可以参考https://my.oschina.net/thinwonton/blog?catalog=5607452

    1. 服务提供方

    一般是服务的实现。往往在架构层面抽象成服务层。

    1.1 工程结构

    在这里,dubbo-parent是其它实例工程的父工程,为子工程提供统一的版本号管理。

    DemoService.java

    public interface DemoService { /** * 对两个数求和 */ public int sum(Integer x, Integer y); /** * 对两个数做乘法运算 * * @param x * @param y * @return */ public int multi(Integer x, Integer y); }

    DemoServiceImpl.java 实现类

    public class DemoServiceImpl implements DemoService { public int sum(Integer x, Integer y) { return x + y; } public int multi(Integer x, Integer y) { return x * y; } }

    applicationContext.xmlspring的配置文件,dubbo框架运行在spring 容器中。

    <?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"> <!-- 引入服务提供者配置文件 --> <import resource="classpath:dubbo.xml" /> </beans>

    dubbo.xml服务的配置,dubbo通过xml文件扫描,在spring的启动阶段把服务注册到注册中心。这里的demoService是服务的实现者。当服务提供方获取到消费方的rpc请求后,将会调用该实现类的对应的方法运行,并把结果通过PRC返回给调用方。 是指定协议类型,我们的提供者可以选择dubbo或者RMI作为底层通信协议,或者两者都选择,由调用方选择其中一种和服务方进行通信。

    <?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"> <!-- 配置Bean --> <bean id="demoService" class="com.github.thinwonton.dubbo.sample.helloworld.service.DemoServiceImpl" /> <!-- 指定web服务名字 --> <dubbo:application name="helloworld-provider" /> <!-- 声明服务注册中心 --> <!-- 使用zookeeper作为注册中心 --> <dubbo:registry protocol="zookeeper" address="192.168.88.15:2181" /> <!-- 指定传输层通信协议 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 暴露服务地址,该服务的实现类是demoService的引用 --> <dubbo:service ref="demoService" interface="com.github.thinwonton.dubbo.sample.helloworld.service.DemoService" protocol="dubbo" /> </beans>

    主程序 ProviderMain.java

    public class ProviderMain { public static void main(String[] args) throws IOException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); System.out.println("服务启动..."); System.in.read(); System.out.println("服务结束..."); } }

    当启动提供方应用成功后,通过日志可以看到服务已经在服务中心进行注册,并获取到如下信息:

    暴露的服务IP和端口是192.168.42.2:20880服务所在的应用名是helloworld-provider所暴露的服务的接口和方法是com.github.thinwonton.dubbo.sample.helloworld.service.DemoService,multi & sum INFO 13:42:53,738 com.alibaba.dubbo.registry.support.AbstractRegistry: [DUBBO] Register: dubbo://192.168.42.2:20880/com.github.thinwonton.dubbo.sample.helloworld.service.DemoService?anyhost=true&application=helloworld-provider&dubbo=2.5.3&interface=com.github.thinwonton.dubbo.sample.helloworld.service.DemoService&methods=multi,sum&pid=8764&side=provider×tamp=1488087764234, dubbo version: 2.5.3, current host: 127.0.0.1 INFO 13:42:53,777 com.alibaba.dubbo.registry.support.AbstractRegistry: [DUBBO] Subscribe: provider://192.168.42.2:20880/com.github.thinwonton.dubbo.sample.helloworld.service.DemoService?anyhost=true&application=helloworld-provider&category=configurators&check=false&dubbo=2.5.3&interface=com.github.thinwonton.dubbo.sample.helloworld.service.DemoService&methods=multi,sum&pid=8764&side=provider×tamp=1488087764234, dubbo version: 2.5.3, current host: 127.0.0.1 INFO 13:42:53,804 com.alibaba.dubbo.registry.support.AbstractRegistry: [DUBBO] Notify urls for subscribe url provider://192.168.42.2:20880/com.github.thinwonton.dubbo.sample.helloworld.service.DemoService?anyhost=true&application=helloworld-provider&category=configurators&check=false&dubbo=2.5.3&interface=com.github.thinwonton.dubbo.sample.helloworld.service.DemoService&methods=multi,sum&pid=8764&side=provider×tamp=1488087764234, urls: [empty://192.168.42.2:20880/com.github.thinwonton.dubbo.sample.helloworld.service.DemoService?anyhost=true&application=helloworld-provider&category=configurators&check=false&dubbo=2.5.3&interface=com.github.thinwonton.dubbo.sample.helloworld.service.DemoService&methods=multi,sum&pid=8764&side=provider×tamp=1488087764234], dubbo version: 2.5.3, current host: 127.0.0.1

    通过zookeeper的客户端连接ZK的服务,查看树的ZNode情况

    [root@www zookeeper-3.4.9]# bin/zkCli.sh -server 192.168.88.15:2181 [zk: 192.168.88.15:2181(CONNECTED) 2] ls / [dubbo, zookeeper] [zk: 192.168.88.15:2181(CONNECTED) 3] ls /dubbo [com.github.thinwonton.dubbo.sample.helloworld.service.DemoService] [zk: 192.168.88.15:2181(CONNECTED) 4] ls /dubbo/com.github.thinwonton.dubbo.sample.helloworld.service.DemoService [consumers, routers, providers, configurators] [zk: 192.168.88.15:2181(CONNECTED) 5] ls /dubbo/com.github.thinwonton.dubbo.sample.helloworld.service.DemoService/providers [dubbo://192.168.42.2:20880/com.github.thinwonton.dubbo.sample.helloworld.service.DemoService?anyhost=true&application=helloworld-provider&dubbo=2.5.3&interface=com.github.thinwonton.dubbo.sample.helloworld.service.DemoService&methods=multi,sum&pid=7968&side=provider×tamp=1488095682955]

    zk树中,为dubbo的服务com.github.thinwonton.dubbo.sample.helloworld.service.DemoService建立了一个节点,并分别建立consumers, routers, providers, configurators四个节点。这四个节点分别记录了消费方,服务路由信息,提供方,配置的一些信息。我们查看providers可以看到192.168.42.2:20880这台机器再为我们服务。

    2. 服务调用方(消费方)

    一般集成在WEB工程,即表现层工程。

    2.1 工程结构

    消费方 dubbo.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"> <!-- 指定web应用名字 --> <dubbo:application name="helloworld-consumer" /> <!-- 声明服务注册中心 --> <dubbo:registry protocol="zookeeper" address="192.168.88.15:2181" /> <!-- 引用服务,demoService仅用于根据接口生成动态代理,默认使用javassist生成代理对象 --> <dubbo:reference id="demoService" interface="com.github.thinwonton.dubbo.sample.helloworld.service.DemoService" protocol="dubbo" /> </beans>

    ConsumerMain.java

    public class ConsumerMain { public static void main(String[] args) throws IOException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); DemoService demoService = (DemoService) applicationContext.getBean("demoService"); int sumResult = demoService.sum(1, 2); System.out.println("加法服务的结果是:" + sumResult); int multiResult = demoService.multi(3, 4); System.out.println("减法服务的结果是:" + multiResult); } }

    运行主程序后,即可看到相应的结果

    文章转载自 开源中国社区 [http://www.oschina.net]

    相关资源:敏捷开发V1.0.pptx
    最新回复(0)