消费Dubbo服务

创建maven项目 dubbo-demo-consumer
加入maven依赖

<dependencies>
    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>dubboartifactId>
        <version>2.6.0version>
    dependency>
    <dependency>
        <groupId>com.101tecgroupId>
        <artifactId>zkclientartifactId>
        <version>0.10version>
    dependency>
    <dependency>
        <groupId>org.apache.curatorgroupId>
        <artifactId>curator-frameworkartifactId>
        <version>4.0.1version>
    dependency>
    <dependency>
        <groupId>com.alibabagroupId>
        <artifactId>fastjsonartifactId>
        <version>1.2.46version>
    dependency>
    <dependency>
        <groupId>log4jgroupId>
        <artifactId>log4jartifactId>
        <version>1.2.17version>
    dependency>
    <dependency>
        <groupId>org.slf4jgroupId>
        <artifactId>slf4j-apiartifactId>
        <version>1.7.25version>
    dependency>
    <dependency>
        <groupId>org.apache.commonsgroupId>
        <artifactId>commons-lang3artifactId>
        <version>3.4version>
    dependency>
    <dependency>
        <groupId>io.nettygroupId>
        <artifactId>netty-allartifactId>
        <version>4.0.35.Finalversion>
    dependency>
dependencies>

编写dubbo消费服务配置文件dubbo-demo-consumer.xml


<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" />
     
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    
    <dubbo:reference id="demoProviderService" check="false" interface="com.hbk.dubbo.DemoProviderService"/>


beans>

创建接口DemoProviderService.java

package com.hbk.dubbo;


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

编写消费服务测试类:

package com.hbk.dubbo;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConsumerTest {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "dubbo-demo-consumer.xml" });
        context.start();
        DemoProviderService demoProviderService = (DemoProviderService) context.getBean("demoProviderService");
        String result=demoProviderService.sayHello("你好");
        System.out.println("远程调用结果:"+result);
        try {
            System.in.read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        context.close();
    }

}

先运行zookeeper,然后把上一节的提供者服务跑起来,再运行消费者服务,运行结果 远程调用结果:服务001

说明远程调用成功

同时查看Dubbo Admin管理控制台

消费Dubbo服务_第1张图片

消费Dubbo服务_第2张图片

需要注意的是,消费服务接口DemoProviderService的包路径一定要跟提供服务的接口路径一直,要不然会报以下错误:

Exception in thread "main" com.alibaba.dubbo.rpc.RpcException: No provider available from registry 127.0.0.1:2181 for service com.hbk.dubbo.consumer.DemoProviderService on consumer 100.0.0.84 use dubbo version 2.6.0, may be providers disabled or not registered ?
    at com.alibaba.dubbo.registry.integration.RegistryDirectory.doList(RegistryDirectory.java:572)
    at com.alibaba.dubbo.rpc.cluster.directory.AbstractDirectory.list(AbstractDirectory.java:73)
    at com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker.list(AbstractClusterInvoker.java:265)
    at com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker.invoke(AbstractClusterInvoker.java:224)
    at com.alibaba.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker.invoke(MockClusterInvoker.java:70)
    at com.alibaba.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:51)
    at com.alibaba.dubbo.common.bytecode.proxy0.sayHello(proxy0.java)
    at com.hbk.dubbo.ConsumerTest.main(ConsumerTest.java:15)

专业墙纸贴纸厨房用具装饰出售,本人网店经营

博客对你有用记得访问下哦,增加下访问量,如有需要可以下单购买哦^_^。https://item.taobao.com/item.htm?id=569617707364

你可能感兴趣的:(dubbo)