Dubbo快速入门

Dubbo是一个分布式服务框架,既然是服务,必然存在服务提供者与服务调用者。

接下来,我们先编写一个服务提供者。工程仍然使用上篇文章spring4入门中的工程。

 

我们在pom.xml中加入dubbo的依赖

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

编写如下类:

package com.mm.service;

public interface WeatherService {
    public String getMessage(String city);
}

 

package com.mm.service.impl;

import com.mm.service.WeatherService;

public class WeatherServiceImpl implements WeatherService{

    @Override
    public String getMessage(String city) {
        return city+"天气晴朗,局部有阵雨";
    }

}

 

新建一个spring配置文件(provider.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">
 
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="mm-weather"  />
 
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.mm.service.WeatherService" ref="weatherService" />
 
    <!-- 和本地bean一样实现服务 -->
    <bean id="weatherService" class="com.mm.service.impl.WeatherServiceImpl" />
 
</beans>

新建一个控制台程序,用于服务启动

package com.mm.main.dubbo;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DubboProvider {
    public static void main(String[] args) throws IOException{
         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"config/provider.xml"});
         context.start();
         System.in.read(); // 按任意键退出
    }
}

 然后,我们启动服务提供者,没有发现报错信息,服务应该是成功启动了。

接下来,我们来编写服务调用者,官方称为消费者。这里为了方便,我直接复制一份代码到新的工作空间,简单配置了一下maven和tomcat。并删除相应的实现类。代码结构如下:

consumer.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">
 
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="mm-weather-consumer"  />
 
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />
 
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="weatherService" interface="com.mm.service.WeatherService" />
 
</beans>

 

新建一个控制台程序,用于服务的调用

package com.mm.main.dubbo;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mm.service.WeatherService;

public class DubboConsumer {
    public static void main(String[] args) throws IOException{
         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"config/consumer.xml"});
         WeatherService  demoService = (WeatherService )context.getBean("weatherService"); // 获取远程服务代理
         String hello = demoService.getMessage("北京"); // 执行远程方法
         System.out.println( hello ); // 显示调用结果
    }
}

控制台输出如下:

 

同时我们在提供者的控制台也会发现如下信息:

 

 

额外补充:

在spring配置文件中使用dubbo标签时,会报错。实际应该不影响,估计是eclipse文件验证的问题。但是看得有点不爽,于是....

首先,在maven的本地仓库中找到dubbo-2.5.3.jar,解压发现xsd文件,如下:

打开eclipse的配置

 

将key的值改为:http://code.alibabatech.com/schema/dubbo/dubbo.xsd

接着,右击provider.xml后validate,经过漫长的等待,报错信息消失了。

转载于:https://www.cnblogs.com/guokeluren/p/7133126.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值