dubbo初体验(一)HelloWorld远程服务调用

简介

本篇主要是基于dubbo2.5.3版本的远程服务调用的简单demo,包括服务端接口和实现以及客户端服务调用,简单的配置了注册中心。

服务端

创建过程

  1. 创建maven项目dubbo-server
  2. 创建module
    a. server-api(服务接口)
    b. server-provider(服务实现)

依赖

server-provider

<dependency>
    <groupId>com.study</groupId>
    <artifactId>server-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.3</version>
</dependency>

代码实现

server-api

package com.study;

/**
 * @author future
 * @date 2019-09-04 07:26
 */
public interface IHello {

    String sayHello(String msg);
}
server-provider
package com.study;

/**
 * @author future
 * @date 2019-09-04 07:28
 */
public class HelloImpl implements IHello {
    @Override
    public String sayHello(String msg) {
        return "Hello " + msg;
    }
}

配置文件

dubbo-server.xml(整合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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--应用名-->
    <dubbo:application name="dubbo-server" owner="future"/>
    <!--协议-->
    <dubbo:protocol name="dubbo"/>
    <!--服务接口-->
    <dubbo:service interface="com.study.IHello" ref="helloService"/>

    <!--注册中心-->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!--服务实现-->
    <bean id="helloService" name="hello" class="com.study.HelloImpl"/>
</beans>

配置文件位置

在这里插入图片描述

加载配置文件

package com.study;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * @author future
 * @date 2019-09-04 22:41
 */
public class BootStrap {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-server.xml");
        context.start();
        System.in.read();
    }
}

客户端

创建

  • 创建dubbo-client工程

maven依赖

<dependency>
  <groupId>com.study</groupId>
    <artifactId>server-api</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

 <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>dubbo</artifactId>
     <version>2.5.3</version>
 </dependency>

服务调用

package com.study;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-client.xml");
//        context.start();
        IHello hello = (IHello) context.getBean("helloService");
        System.out.println( hello.sayHello("World") );
    }
}

配置文件

<?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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--应用名-->
    <dubbo:application name="dubbo-client" owner="future"/>
    <!--协议-->
    <dubbo:protocol name="dubbo"/>
    <!--注册中心-->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!---->
    <!--<dubbo:reference interface="com.study.IHello" id="helloService" url="dubbo://192.168.3.32:20880/com.study.IHello"/>-->
    <dubbo:reference interface="com.study.IHello" id="helloService"/>

</beans>

客户端启动报错

  • 找不到发布的服务

警告:  [DUBBO] Ignore empty notify urls for subscribe url consumer://192.168.3.32/com.study.IHello?application=dubbo-client&category=providers,configurators,routers&dubbo=2.5.3&interface=com.study.IHello&methods=sayHello&owner=future&pid=17744&revision=1.0-SNAPSHOT&side=consumer&timestamp=1567783039716, dubbo version: 2.5.3, current host: 192.168.3.32
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloService': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.study.IHello. No provider available for the service com.study.IHello from the url multicast://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?application=dubbo-client&dubbo=2.5.3&interface=com.study.IHello&methods=sayHello&owner=future&pid=17744&revision=1.0-SNAPSHOT&side=consumer&timestamp=1567783039716 to the consumer 192.168.3.32 use dubbo version 2.5.3
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport$1.run(FactoryBeanRegistrySupport.java:127)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:116)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:91)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1288)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:217)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:880)
	at com.study.App.main(App.java:15)
Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.study.IHello. No provider available for the service com.study.IHello from the url multicast://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?application=dubbo-client&dubbo=2.5.3&interface=com.study.IHello&methods=sayHello&owner=future&pid=17744&revision=1.0-SNAPSHOT&side=consumer&timestamp=1567783039716 to the consumer 192.168.3.32 use dubbo version 2.5.3
	at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:420)
	at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:300)
	at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:138)
	at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:65)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport$1.run(FactoryBeanRegistrySupport.java:121)
	... 9 more
  • 解决:

  • 在reference上加url,指定服务路径
    • 这种方式是点对点直连服务提供地址,而不是使用注册中心的方式
<dubbo:reference interface="com.study.IHello" id="helloService" url="dubbo://192.168.3.32:20880/com.study.IHello"/>
  • 改为使用Zookeeper作为注册中心
<dubbo:registry address="zookeeper://192.168.3.111:2181"/>
Zookeeper依赖
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.3.3</version>
</dependency>
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

future_1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值