三个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(); } } }
通过运行结果即可明白
相关推荐
阿里巴巴分布式开源框架dubbo的demo,maven版
关于阿里巴巴的soa服务化治理框架dubbo的一个小demo,这里是我的工程源码,关于spring、dubbo的各项配置,均做了详细注释。 注:此工程为maven项目。
其前身是阿里巴巴公司开源的一个高性能、轻量级的开源Java RPC框架,可以和Spring框架无缝集成。 **什么是RPC?** RPC全称为remote procedure call,即**远程过程调用**。比如两台服务器A和B,A服务器上部署一个...
Dubbo是阿里巴巴开源的产品(2012),2018年捐献给了apache,现在是apache下的一个顶级开源项目,国内大量企业都在使用Dubbo框架。 (hsf taobao内部的dubbo) Dubbo的官方网站:http://dubbo.apache.org
Dubbo是 [1] 阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 [2] Spring框架无缝集成。 Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大...
Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点 这是我参照前人自己搭建的一个demo,详细过程可以参考 ...
阿里dubbo源码,版本2.5.8支持admin,十分好用,Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。 Dubbo[]是一个分布式...
Dubbo是阿里巴巴公司开源的一个高性能优秀的 服务框架,使得应用可通过高性能的 RPC 实现服务的输 出和输入功能,可以和 Spring框架无缝集成,dubbod的demo,上传供大家学习,自己也能收藏下来,希望审核通过,谢谢.....
demo使用java进行分模块方式,来展现dubbo的调用。...Dubbo是阿里巴巴开源的基于 Java 的高性能 RPC(一种远程调用) 分布式服务框架(SOA),致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
春云阿里巴巴 nacos,seata,sentinel,dubbo,mybatis | ibatis开箱即用 分布式微服务+本地服务 超动态自动装配消息系统(spring-stream-3,支持rocketMq,原则上同时支持kafuka和activeMq) 初步骨架生成器 前端...
Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用...
java二进制补码源码Dubbo是一个来自阿里巴巴的开源分布式服务框架,当当根据自身的需求,为Dubbo实现了一些新的功能,包括REST风格远程调用、Kryo/FST序列化等等,并将其命名为Dubbox。 Demo:类似范例: ##Dubbo...
国内方面,阿里巴巴、腾讯、360、京东、58同城等很多互联网公司都进行了微服务化实践。当前微服务的开发框架也非常多,比较著名的有Dubbo、SpringCloud、thrift、grpc等。虽然微服务现在如火如荼,但对其实践其实仍...
* 给前期已经使用项目的同学,可以使用【阿里巴巴Java开发规约插件p3c】进行修正,造成不便深表歉意 ###更新日期2018-10-08 * 最近学习了远程过程调用协议RPC(Remote Procedure Call Protocol),将本框架与dubbo...
Dubbo | 分布式服务框架 | [http://dubbo.io/](http://dubbo.io/) TBSchedule & elastic-job | 分布式调度框架 | [https://github.com/dangdangdotcom/elastic-job](https://github.com/dangdangdotcom/elastic-job)...