可以到我的github下载本demo:https://github.com/Feiyu123/DubboDemo.git
一.dubbo是什么:
Dubbo(注:HSF提供的是分布式服务开发框架,taobao内部使用较多)是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架
其核心部分包含:
1. 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
2. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
3. 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
二.dubbo有什么用
1.透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
2.软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
3. 服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。
三.Dubbox 实现原理
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Zookeeper: Registry服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。
四.dubbo项目搭建
下载zookeeper,zokeeper地址:http://zookeeper.apache.org/releases.html
如果下载不了请到我的百度云盘下载:https://pan.baidu.com/s/1tV1Pxx89fyQU6Ce5QsRN9Q
这里说明一下搭建目录:
Dubbo-parent为总工程,dubbo-provider,dubbo-consumer,dubbo-api都是子模块,dubbo-provider,dubbo-consumer又依赖于dubbo-api模块。
这里用idea作为开发IDE,构建Maven项目
1.先建Dubbo空项目(Empty Project)
2.建dubbo-parent总模块,File----New---Module,打包方式为pom
可以看一下它的pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>com.dubbo.parentgroupId> <artifactId>dubbo-parentartifactId> <version>1.0-SNAPSHOTversion> <modules> <module>../dubbo-providermodule> <module>../dubbo-consumermodule> <module>../dubbo-apimodule> <module>../dubbo-servicemodule> modules> <packaging>pompackaging> <properties> <maven.compiler.source>1.8maven.compiler.source> <maven.compiler.target>1.8maven.compiler.target> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <spring.version>4.2.5.RELEASEspring.version> <mybatis.version>3.2.8mybatis.version> <mysql-driver.version>5.1.29mysql-driver.version> <slf4j.version>1.7.18slf4j.version> <log4j.version>1.2.17log4j.version> properties> <dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>3.8.1version> <scope>testscope> dependency> <dependency> <groupId>jstlgroupId> <artifactId>jstlartifactId> <version>1.2version> dependency> <dependency> <groupId>javaxgroupId> <artifactId>javaee-apiartifactId> <version>7.0version> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.11version> <scope>testscope> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-coreartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-oxmartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-txartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-context-supportartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aopartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-testartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>${mybatis.version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>1.2.2version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>${mysql-driver.version}version> dependency> <dependency> <groupId>commons-dbcpgroupId> <artifactId>commons-dbcpartifactId> <version>1.2.2version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>fastjsonartifactId> <version>1.2.22version> dependency> <dependency> <groupId>log4jgroupId> <artifactId>log4jartifactId> <version>${log4j.version}version> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-apiartifactId> <version>${slf4j.version}version> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-log4j12artifactId> <version>${slf4j.version}version> dependency> <dependency> <groupId>org.codehaus.jacksongroupId> <artifactId>jackson-mapper-aslartifactId> <version>1.9.13version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-coreartifactId> <version>2.8.0version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> <version>2.8.0version> dependency> <dependency> <groupId>commons-fileuploadgroupId> <artifactId>commons-fileuploadartifactId> <version>1.3.1version> dependency> <dependency> <groupId>commons-iogroupId> <artifactId>commons-ioartifactId> <version>2.4version> dependency> <dependency> <groupId>commons-codecgroupId> <artifactId>commons-codecartifactId> <version>1.9version> dependency> <dependency> <groupId>org.quartz-schedulergroupId> <artifactId>quartzartifactId> <version>2.2.1version> dependency> <dependency> <groupId>org.apache.shirogroupId> <artifactId>shiro-coreartifactId> <version>1.3.2version> dependency> <dependency> <groupId>org.apache.shirogroupId> <artifactId>shiro-webartifactId> <version>1.3.2version> dependency> <dependency> <groupId>org.apache.shirogroupId> <artifactId>shiro-springartifactId> <version>1.3.2version> dependency> <dependency> <groupId>org.apache.shirogroupId> <artifactId>shiro-ehcacheartifactId> <version>1.3.2version> dependency> <dependency> <groupId>org.apache.zookeepergroupId> <artifactId>zookeeperartifactId> <version>3.4.9version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>dubboartifactId> <version>2.5.3version> <exclusions> <exclusion> <groupId>org.springframeworkgroupId> <artifactId>springartifactId> exclusion> exclusions> dependency> <dependency> <groupId>com.101tecgroupId> <artifactId>zkclientartifactId> <version>0.10version> dependency> dependencies> <distributionManagement> <site> <id>websiteid> <url>scp://webhost.company.com/www/websiteurl> site> distributionManagement> <build> <finalName>dubbo-parentfinalName> build> project>
3.构建dubbo-provider模块,File----New---Module,打包方式为war
看一下它的pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>dubbo-parentartifactId> <groupId>com.dubbo.parentgroupId> <version>1.0-SNAPSHOTversion> <relativePath>../dubbo-parent/pom.xmlrelativePath> parent> <modelVersion>4.0.0modelVersion> <groupId>com.dubbo.parentgroupId> <artifactId>dubbo-providerartifactId> <version>1.0-SNAPSHOTversion> <packaging>warpackaging> <name>dubbo-providername> <url>http://maven.apache.orgurl> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> properties> <dependencies> <dependency> <groupId>com.dubbo.apigroupId> <artifactId>dubbo-apiartifactId> <version>1.0-SNAPSHOTversion> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>3.8.1version> <scope>testscope> dependency> dependencies> <build> <finalName>dubbo-providerfinalName> build> project>
4.构建dubbo-api和构建dubbo-consumer模块,File----New---Module,打包方式为war
构建dubbo-api和构建dubbo-consumer和以上provider差不多,父工程是dubbo-parent,只不过dubbo-api是独立模块,
dubbo-consumer和dubbo-provider需要依赖dubbo-api
看一下dubbo-api的pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>dubbo-parentartifactId> <groupId>com.dubbo.parentgroupId> <version>1.0-SNAPSHOTversion> <relativePath>../dubbo-parent/pom.xmlrelativePath> parent> <modelVersion>4.0.0modelVersion> <groupId>com.dubbo.apigroupId> <artifactId>dubbo-apiartifactId> <version>1.0-SNAPSHOTversion> <packaging>warpackaging> <name>dubbo-apiname> <url>http://maven.apache.orgurl> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> properties> <dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>3.8.1version> <scope>testscope> dependency> dependencies> <build> <finalName>dubbo-apifinalName> build> project>
再看一下dubbo-consumer的pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>dubbo-parentartifactId> <groupId>com.dubbo.parentgroupId> <version>1.0-SNAPSHOTversion> <relativePath>../dubbo-parent/pom.xmlrelativePath> parent> <modelVersion>4.0.0modelVersion> <groupId>com.dubbo.consumergroupId> <artifactId>dubbo-consumerartifactId> <packaging>warpackaging> <name>dubbo-consumername> <url>http://maven.apache.orgurl> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> properties> <dependencies> <dependency> <groupId>com.dubbo.apigroupId> <artifactId>dubbo-apiartifactId> <version>1.0-SNAPSHOTversion> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>3.8.1version> <scope>testscope> dependency> dependencies> <build> <finalName>dubbo-consumerfinalName> build> project>
5.编写接口
在dubbo-api的src/main/java目录下加一个接口UserService
在dubbo-consumer的src/main/java目录下加一个Consumer类
在dubbo-consumer的src/main/resources目录下加一个consumer.xml,这里为配置zookeeper的客户端
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="demotest-consumer" owner="programmer" organization="dubbox"/> <dubbo:registry address="zookeeper://localhost:2181"/> <dubbo:reference id="permissionService" interface="com.dubbo.api.UserService"/> beans>
在dubbo-provider的src/main/java目录下加一个Provider类以及UserServiceImpl接口实现类
UserServiceImpl接口实现类 package com.dubbo.provider; import com.dubbo.api.UserService; import java.util.HashMap; import java.util.Map; public class UserServiceImpl implements UserService { @Override public MapgetUserInfo(int id) { Map map = new HashMap<>(); map.put("name","张三"); map.put("sex","男"); map.put("id",id); return map; } }
在dubbo-provider的src/main/resources目录下加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="demotest-provider" owner="programmer" organization="dubbox"/> <dubbo:registry address="zookeeper://localhost:2181"/> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service interface="com.dubbo.api.UserService" ref="userService" protocol="dubbo" /> <bean id="userService" class="com.dubbo.provider.UserServiceImpl"/> beans>
五.运行项目
1.找到zookeeper的bin目录下的zkServer.cmd,双击并运行
zookeeper服务端已经开启
接下来分别启动Provider和Consumer两个类
可以看到provider和consumer均正常启动,且consumer可以正常调用provider那边的接口实现类。provider实现api的接口并向zookeeper注册中心注册接口,然后consumer向zookeeper注册中心订阅接口。这里Provider和consumer都是用的zookeeper的客户端,如果需要进行调用,host和port必须一致。