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{
@overridepublic 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 ">
<!-- 提供方应用信息,用于计算依赖关系 -->
<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 ">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<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的值改为:
接着,右击provider.xml后validate,经过漫长的等待,报错信息消失了。
以上就是一个分布式服务框架--dubbo实例的详细内容。