资源:
dubbo 官网
定义服务接口,由于服务提供端、服务消费端都依赖于该接口,官方强烈推荐
将这个接口放在一个单独的模块中,以便在两端共享。
package com.alibaba.dubbo.demo;
public interface DemoService {
String sayHello(String name);
}
在我经手的一个项目中,采取的方式是将接口放在服务提供端,然后将接口类打包添加到服务消费端的系统
实现服务提供者
package com.alibaba.dubbo.demo.provider;
import com.alibaba.dubbo.demo.DemoService;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
}
配置服务提供者
<?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="demo-provider"/>
<!-- 配置使用的注册中心地址,服务提供端、服务消费端都需要配置 -->
<dubbo:registry address="zookeeper://10.25.158.19:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 声明需要暴露的服务接口,ref指定该接口的实现者 -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
<!-- 使用spring 管理对象 -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
</beans>
配置服务消费者
<?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="demo-consumer"/>
<!-- 配置注册中心地址 -->
<dubbo:registry address="zookeeper://10.25.158.19:2181"/>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>
在我经手的一个项目中,服务消费端也配置了<dubbo:protocol name="dubbo" port="20881"/>
,也就是在实际项目中,服务提供端也可以同时作为服务消费端。
2018-09-12 单点直连
在项目开发过程中大家都连的是同一台zookeeper
服务器,导致测试接口时有时会调用到同事的电脑上,使用单点直连
方式可使接口调用的是本地服务
<dubbo:reference id="cityExternalService"
interface="com.iss.wy.hmis.system.service.CityExternalService"
url="dubbo://127.0.0.1:20880"
/>
使用url
属性指定要连接的服务端地址,端口号为服务端暴露的端口号
2019-01-6 服务分组
<!-- Dubbo服务配置 -->
<dubbo:application name="${dubbo.application.name}" />
<!-- 使用zookeeper注册中心 -->
<dubbo:registry protocol="zookeeper" address="${dubbo.registry.zookeeper.address}" />
<dubbo:protocol name="dubbo" port="${dubbo.protocol.port}" />
<!-- 全局 提供者配置, 超 时时间是30秒, 重试次数为0 -->
<dubbo:provider timeout="30000" retries="0" group="${dubbo.registry.group}" filter="dubboAuthFilter"></dubbo:provider>
<!-- 全局 消费者配置 , 超 时时间是25秒, 重试次数为0 -->
<dubbo:consumer timeout="25000" group="${dubbo.registry.group}" retries="0"></dubbo:consumer>
由于开发环境和测试环境共用一个zookeeper注册中心,故使用分组的方式进行区分
2020-2-29 补充关于一个接口多个实现类的配置
【Bug】同一接口多实现类下dubbo调用服务错乱
2021-01-15 补充关于注册中心的配置
<dubbo:registry protocol="redis" address="127.0.0.1:6379" timeout="30000"/>
<dubbo:registry address="redis://127.0.0.1:6379" timeout="30000"/>