1、接口定义
package com.dubbo.inter;
public interface UserApi {
String sayHello(String name);
}
2、接口实现类定义
package com.dubbo.inter;
public class UserApiImpl implements UserApi{
@Override
public String sayHello(String name) {
return "用户名:" + name;
}
}
3、提供者服务启动测试类
package com.rmispring.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext-dubbo.xml"});
context.start();
System.in.read(); //阻塞
}
}
4、消费者
package com.rmispring.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dubbo.inter.UserApi;
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext-dubbo-client.xml" });
context.start();
UserApi demoService = (UserApi) context.getBean("userApi");
System.out.println(demoService.sayHello("消费者"));
}
}
5、提供者配置文件applicationContext-dubbo.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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 服务提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="server_provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry id="bb" address="zookeeper://127.0.0.1:2181" group="test" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 interface -->
<bean id="userApiImpl" class="com.dubbo.inter.UserApiImpl" />
<!-- 下面的编号对应 applicationContext-dubbo-client.xml中相应编号-->
<!--编号001 测试成功 推荐使用-->
<dubbo:service interface="com.dubbo.inter.UserApi" ref="userApiImpl" registry="bb" group="test"/>
<!--编号002 测试失败(没有分组而服务端定义了分组,没有提供者)-->
<!-- <dubbo:service interface="com.dubbo.inter.UserApi" ref="userApiImpl" registry="bb" group="test"/> -->
<!--编号003 测试失败( No provider available for the service com.dubbo.inter.UserApi)-->
<!-- <dubbo:service interface="com.dubbo.inter.UserApi" ref="userApiImpl" registry="bb" group="test"/> -->
<!--编号004测试成功 -->
<!-- <dubbo:service interface="com.dubbo.inter.UserApi" ref="userApiImpl"/> -->
<!--编号005测试成功 -->
<!-- <dubbo:service interface="com.dubbo.inter.UserApi" ref="userApiImpl"/> -->
<!--编号006测试成功 (No provider available for the service test/com.dubbo.inter.UserApi)-->
<!-- <dubbo:service interface="com.dubbo.inter.UserApi" ref="userApiImpl"/> -->
<!--编号007测试成功 -->
<!-- <dubbo:service interface="com.dubbo.inter.UserApi" ref="userApiImpl" registry="bb"/> -->
<!--编号008测试成功 -->
<!-- <dubbo:service interface="com.dubbo.inter.UserApi" ref="userApiImpl" registry="bb"/> -->
<!--编号009测试失败-->
<!-- <dubbo:service interface="com.dubbo.inter.UserApi" ref="userApiImpl" registry="bb"/> -->
</beans>
6、消费者配置文件applicationContext-dubbo-client.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="wlclient_consumer" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry id="aa" address="zookeeper://127.0.0.1:2181" group="test" />
<!-- 下面的编号对应 applicationContext-dubbo.xml中相应编号-->
<!--编号001 测试成功 推荐使用-->
<dubbo:reference id="userApi" interface="com.dubbo.inter.UserApi" registry="aa" group="test"/>
<!--编号002 测试失败(没有分组而服务端定义了分组,没有提供者)-->
<!-- <dubbo:reference id="userApi" interface="com.dubbo.inter.UserApi" registry="aa"/> -->
<!--编号003 测试失败( No provider available for the service com.dubbo.inter.UserApi)-->
<!-- <dubbo:reference id="userApi" interface="com.dubbo.inter.UserApi"/> -->
<!--编号004测试成功 -->
<!-- <dubbo:reference id="userApi" interface="com.dubbo.inter.UserApi" registry="aa"/> -->
<!--编号005测试成功 -->
<!-- <dubbo:reference id="userApi" interface="com.dubbo.inter.UserApi"/> -->
<!--编号006测试失败(No provider available for the service test/com.dubbo.inter.UserApi)-->
<!-- <dubbo:reference id="userApi" interface="com.dubbo.inter.UserApi" registry="aa" group="test"/> -->
<!--编号007测试成功 -->
<!-- <dubbo:reference id="userApi" interface="com.dubbo.inter.UserApi"/> -->
<!--编号008测试成功 -->
<!-- <dubbo:reference id="userApi" interface="com.dubbo.inter.UserApi" registry="aa"/> -->
<!--编号009测试失败(No provider available for the service test/com.dubbo.inter.UserApi)-->
<!-- <dubbo:reference id="userApi" interface="com.dubbo.inter.UserApi" registry="aa" group="test"/> -->
</beans>
备注:
zookeeper保存了服务提供方和服务消费方的的URI(dubbo自定义的一种URI),服务消费方找到zookeeper,向zookeeper要到服务提供方的URI,然后就找到提供方,并调用提供方的服务。
解耦,分布式,failover。