Dubbo(4)消费Dubbo服务

消费就是一个远程调用服务的过程;
1.项目结构:
 
 
2.项目依赖pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cy</groupId>
  <artifactId>dubbo-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.10</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>4.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.46</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.0.35.Final</version>
        </dependency>
    </dependencies>
</project>
View Code

 

3.需要调用的远程接口DemoProviderService也写到项目里:

package com.cy.service;

/**
 * 服务提供者接口
 * @author CY
 *
 */
public interface DemoProviderService {
    
    String sayHello(String name);
}

4.dubbo-consumer.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/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-2.5.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名称,用于计算依赖关系,不是匹配条件,不要与提供方名称一样  -->
    <dubbo:application name="demo-consumer"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- 生成远程服务代理,可以与本地bean一样使用  check属性,启动时候是否检查 一般设置成false 启动时候不检查 -->
    <dubbo:reference id="providerService" check="false" interface="com.cy.service.DemoProviderService"/>

</beans>

5.消费测试程序:ConsumerTest

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cy.service.DemoProviderService;


public class ConsumerTest {
    public static void main(String[] args) {
        
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-consumer.xml"});
        context.start();
        DemoProviderService demoProviderService = (DemoProviderService) context.getBean("providerService");    // get remote service proxy
        
        while (true) {
            try {
                Thread.sleep(1000);
                String result = demoProviderService.sayHello("world");     // call remote method
                System.out.println("远程调用的结果:" +  result);               // get result
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
}

6.测试:

启动zookeeper;

启动服务测试程序,run ProviderTest.java;

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
服务提供者向zookeeper注册中心注册服务成功(端口:20880)

 

最后启动消费测试程序 ConsumerTest:

console打印:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
远程调用的结果:Hello,world
远程调用的结果:Hello,world
远程调用的结果:Hello,world
远程调用的结果:Hello,world
远程调用的结果:Hello,world
远程调用的结果:Hello,world
远程调用的结果:Hello,world
远程调用的结果:Hello,world

此时查看dubbo-admin控制台页面:

提供者:

 

消费者:

 

 

posted on 2018-09-16 13:27  有点懒惰的大青年  阅读(494)  评论(0)    收藏  举报