三个module:
service:定义接口
provider:定义接口实现
consumer:模拟接口调用
1:service
只是象征性的定义了一个接口:
package com.tch.test.dubbo.service;
public interface DemoService {
String sayHello(String name);
}
2:provider:
pom.xml:
<dependency> <groupId>com.tch.test.dubbo</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.8.4</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.7</version> </dependency>
还有一个接口的简单实现:
package com.tch.test.dubbo.provider.serviceimpl;
import com.tch.test.dubbo.service.DemoService;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
}
然后是spring配置文件applicationContext.xml,里面保护了dubbo的配置,我这里使用到了本地localhost的zookeeper,端口是默认的2181,如果不用zookeeper的话,直接将
<dubbo:registry address="zookeeper://localhost:2181" check="false"></dubbo:registry>
这句话注释掉,将
<dubbo:registry address="multicast://224.5.6.7:1234" />
放开即可。至于为什么建议zookeeper部署奇数台,是因为根据zookeeper的leader选举算法,由于zookeeper挂掉一半的机器集群就不可用,所以部署4台和3台的集群都是在挂掉2台后集群不可用,所以使用奇数台机器,不浪费资源
:
<?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" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <dubbo:registry address="zookeeper://localhost:2181" check="false"></dubbo:registry> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.tch.test.dubbo.service.DemoService" ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.tch.test.dubbo.provider.serviceimpl.DemoServiceImpl" /> </beans>
然后是provider的测试类:
package com.tch.test.dubbo.provider.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = null;
try {
context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
context.start();
context.registerShutdownHook();
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}finally{
context.close();
}
}
}
3:consumer:
pom.xml和provider一样:
<dependency> <groupId>com.tch.test.dubbo</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.8.4</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.7</version> </dependency>
然后是spring配置文件applicationContext.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="consumer-of-helloworld-app" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <dubbo:registry address="zookeeper://localhost:2181" check="false"></dubbo:registry> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.tch.test.dubbo.service.DemoService" /> </beans>
最后是consumer的测试类:
package com.tch.test.dubbo.consumer.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tch.test.dubbo.service.DemoService;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = null;
try {
context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
context.start();
context.registerShutdownHook();
DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理
String hello = demoService.sayHello("world"); // 执行远程方法
System.out.println(hello); // 显示调用结果
context.stop();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(context != null){
context.close();
}
}
}
}
接下来,先运行Provider.java,在运行Consumer.java即可看到调用结果(当前前提是zookeeper是启动的啦):
Hello world
添加依赖:
<dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.7</version> </dependency>
一方面对zookeeper进行修改等操作,一方面进行监测:
package com.tch.zookeeper.test;
import java.util.UUID;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;
public class ZookeeperOperator {
private ZkClient zkClient;
public ZkClient getZkClient() {
return zkClient;
}
public void setZkClient(ZkClient zkClient) {
this.zkClient = zkClient;
}
/**
* 函数入口
* @param args
*/
public static void main( String[] args ) {
ZookeeperOperator bootStrap=new ZookeeperOperator();
bootStrap.initialize();
try {
Thread.sleep(100000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 初始化zookeeper
*/
public void initialize() {
String connectionString="localhost:2181";
int connectionTimeout=50000;
zkClient=new ZkClient(connectionString, connectionTimeout);
if(!zkClient.exists("/root1")) {
zkClient.create("/root1", new Long(System.currentTimeMillis()), CreateMode.EPHEMERAL);
}
new Thread(new RootNodeChangeThread()).start();
}
/**
* 每20s改变一次 'root1'节点的数据
* @author yang
*
*/
private class RootNodeChangeThread implements Runnable{
public void run() {
while(true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
//ignore
}
String uuidStr=UUID.randomUUID().toString();
System.out.println(">>>>>>>>>> 产生随机的 uuid string,'uuidStr'===>"+uuidStr);
zkClient.writeData("/root1", uuidStr);
}
}
}
}
package com.tch.zookeeper.test.slave;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
public class ZookeeperMonitor {
private ZkClient zkClient;
public ZkClient getZkClient() {
return zkClient;
}
public void setZkClient(ZkClient zkClient) {
this.zkClient = zkClient;
}
/**
* 初始化zookeeper
*/
public void initialize() {
String connectionString="localhost:2181";
int connectionTimeout=500000;
zkClient=new ZkClient(connectionString, connectionTimeout);
new Thread(new Runnable() {
public void run() {
zkClient.subscribeDataChanges("/root1", new IZkDataListener() {
public void handleDataDeleted(String dataPath) throws Exception {
System.out.println("the node 'dataPath'===>");
}
public void handleDataChange(String dataPath, Object data) throws Exception {
System.out.println("the node 'dataPath'===>"+dataPath+", data has changed.it's data is "+String.valueOf(data));
}
});
}
}).start();
}
/**
* 函数入口
* @param args
*/
public static void main( String[] args ) {
ZookeeperMonitor bootStrap=new ZookeeperMonitor();
bootStrap.initialize();
try {
Thread.sleep(100000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
通过运行结果即可明白