在Spring项目中集成使用dubbo实现分布式服务
2017-08-09 11:30
691 查看
一、配置启动Zookeeper
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。下载
http://www-eu.apache.org/dist/zookeeper/配置
Zookeeper安装目录的conf文件夹下的zoo.cfg文件# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=C:\zookeeper # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1
启动
cd C:\zookeeper-3.4.9\bin zkServer.cmd
二、服务提供端
pom.xml
添加依赖jar包<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency>
java接口和实现类
接口public interface Test { String haha(String name); }
实现类
public class TestImpl implements Test { @Override public String haha(String name) { return "sucesss!"+name; } }
spring配置文件
dubbo-provider.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="dubbo-service" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.xyh.service.Test" ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" class="com.xyh.service.impl.TestImpl" /> </beans>
web.xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dubbo-provider.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
三、客户端
pom.xml
添加依赖jar包<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency>
spring配置文件
<?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="dubbo-service-consumer" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 声明需要暴露的服务接口 --> <dubbo:reference id="demoService" interface="com.xyh.service.Test"/> </beans>
java调用服务
public class Main { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("dubbo-consumer.xml"); Test test = (Test) applicationContext.getBean("demoService"); System.out.println(test.haha("xyh")); } }
四、服务提供端实现分布式
spring配置文件(服务端1)
<!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" />
实现类
public class TestImpl implements Test { @Override public String haha(String name) { return "sucesss1!"+name; } }
spring配置文件(服务端2)
<!-- 用dubbo协议在20881端口暴露服务 --> <dubbo:protocol name="dubbo" port="20881" />
实现类
public class TestImpl implements Test { @Override public String haha(String name) { return "sucesss2!"+name; } }
spring配置文件(服务端3)
<!-- 用dubbo协议在20882端口暴露服务 --> <dubbo:protocol name="dubbo" port="20882" />
实现类
public class TestImpl implements Test { @Override public String haha(String name) { return "sucesss3!"+name; } }
客户端多次调用会输出sucesss1、sucesss2、sucesss3等不一样的结果,这里能看出来zookeeper服务为我们实现了负载均衡,相比hession实现分布式需要自配负载均衡服务器,这是一大优点,省事。
相关文章推荐
- 【备忘】使用dubbo、spring-boot等技术实现互联网后台服务项目架构视频教程
- Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务
- 【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(二)
- Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- 【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(二)
- 【JEECG Dubbo专题】Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(二)
- Spring/Spring Boot微服务项目 集成Druid 实现监控功能
- (三)spring cloud微服务分布式云架构 - Spring Cloud集成项目简介
- spring集成blazeds,使用@RemoteDestination实现远程服务
- 【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(二)
- dubbo实例 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务
- 【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(二)
- 【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(二)
- 【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(二)
- AAA【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(二)
- 使用Spring来实现任务计划服务三:集成quartz任务调度框架
- Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务
- 【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(二)
- 如何使用Dubbo服务和集成Spring