【汇智学堂】通过XML实现Dubbo第一个程序(Client端)_spring


配置文件echo-consumer.xml

<?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.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 服务消费方应用名称, 方便用于依赖跟踪 -->
    <dubbo:application name="echo-consumer"/>

    <!-- 使用本地zookeeper作为注册中心 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- 指定要消费的服务 -->
    <dubbo:reference id="echoService" check="false" interface="com.alibaba.dubbo.samples.echo.api.EchoService"/>

</beans>

这些依赖配置主要告诉框架使用ZooKeeper作为注册中心,并且基于XML配置消费的服务,消费的服务定义会被Spring托管。和服务提供方类似,我们需要动手编写服务消费启动代码。

package com.alibaba.dubbo.samples.echo;

import com.alibaba.dubbo.samples.echo.api.EchoService;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class EchoConsumer {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext(new String[]{"spring/echo-consumer.xml"});
        context.start();
        EchoService echoService = (EchoService) context.getBean("echoService"); // get remote service proxy

        String status = echoService.echo("Hello world!");
        System.out.println("echo result: " + status);
    }
}

run.

first,run provider

【汇智学堂】通过XML实现Dubbo第一个程序(Client端)_zookeeper_02


second,run consumer

【汇智学堂】通过XML实现Dubbo第一个程序(Client端)_dubbo_03


see ,in provider,find “Hello Hello world!”

【汇智学堂】通过XML实现Dubbo第一个程序(Client端)_apache_04