之前用过EJB做分布式系统,前段时间跟人闲聊,发现还是Dubbo+ZK用的比较多,so,自己玩玩儿。
先安装一个zk作为服务注册中心,之后,建个maven工程,pom里面加入如下配置:
<dependencies>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.0.13</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
之后,加入服务提供方的配置文件:
<?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="hello-world-app" /> <!-- 配置zookeeper注册中心--> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.alibaba.dubbo.demo.DemoServiceImpl" /> </beans>
服务提供方的接口和类:
package com.alibaba.dubbo.demo;
/**
* 定义服务接口:该接口需要单独打包,在服务提供方和消费方共享
* @author LiuHuiChao
*
*/
public interface DemoService {
String sayHello(String name);
}
/** * 在服务提供方实现接口:对服务消费方隐藏实现 * @author LiuHuiChao * */ 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="consumer-of-helloworld-app" /> <!-- 配置zookeeper注册中心--> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" /> </beans>
消费者类:
package com.alibaba.dubbo.demo; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("consumer.xml"); context.start(); //获取bean DemoService demoService=(DemoService)context.getBean("demoService"); String hello=demoService.sayHello("world"); System.out.println(hello);//显示调用结果 } }
在测试的时候,先运行服务提供者,将服务注册到注册中心,之后再运行服务消费者进行调用。
原理性的东西会之后跟进。