1.dubbo的架构
Registry:使用zookeeper
Provider:接口提供方
Consumer:接口调用方
1.2.启动zookeeper
1.3.查看服务器ip
2.新建providerweb工程
2.1,导入pom依赖
<!-- dubbo相关 -->
<
dependency
>
<
groupId
>com.alibaba</
groupId
>
<
artifactId
>dubbo</
artifactId
>
<
version
>2.5.3</
version
>
<
exclusions
>
<
exclusion
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring</
artifactId
>
</
exclusion
>
<
exclusion
>
<
groupId
>org.jboss.netty</
groupId
>
<
artifactId
>netty</
artifactId
>
</
exclusion
>
</
exclusions
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.zookeeper</
groupId
>
<
artifactId
>zookeeper</
artifactId
>
<
version
>3.4.7</
version
>
</
dependency
>
<
dependency
>
<
groupId
>com.github.sgroschupf</
groupId
>
<
artifactId
>zkclient</
artifactId
>
<
version
>0.1</
version
>
</
dependency
>
<!--4:spring依赖-->
<!--1)spring核心依赖-->
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-core</
artifactId
>
<
version
>4.1.7.RELEASE</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-beans</
artifactId
>
<
version
>4.1.7.RELEASE</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-context</
artifactId
>
<
version
>4.1.7.RELEASE</
version
>
</
dependency
>
<!--2)spring dao层依赖-->
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-jdbc</
artifactId
>
<
version
>4.1.7.RELEASE</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-tx</
artifactId
>
<
version
>4.1.7.RELEASE</
version
>
</
dependency
>
|
2,2 提供一个接口
2.3 spring 配置文件中配置声明发布的接口以及应用名称,端口zookeeper地址
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
"<a href="http://www.springframework.org/schema/beans" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/beans"
xmlns:context
=
"<a href="http://www.springframework.org/schema/context" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/context"
xmlns:p
=
"<a href="http://www.springframework.org/schema/p" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/p"
xmlns:aop
=
"<a href="http://www.springframework.org/schema/aop" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/aop"
xmlns:tx
=
"<a href="http://www.springframework.org/schema/tx" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/tx"
xmlns:dubbo
=
"<a href="http://code.alibabatech.com/schema/dubbo" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://code.alibabatech.com/schema/dubbo"
xmlns:xsi
=
"<a href="http://www.w3.org/2001/XMLSchema-instance" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util <a href="http://www.springframework.org/schema/util/spring-util-4.2.xsd" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 0, 0) !important;">http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<
context:component-scan
base-package
=
"com.niezheng.service.provider"
/>
<!-- 使用dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<
dubbo:application
name
=
"dubbo-provider"
/>
<
dubbo:registry
protocol
=
"zookeeper"
address
=
"192.168.56.100:2181"
/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<
dubbo:protocol
name
=
"dubbo"
port
=
"20880"
/>
<!-- 声明需要暴露的服务接口 -->
<
dubbo:service
interface
=
"com.niezheng.service.provider.IDubboProvider"
ref
=
"dubboProviderImpl"
/>
</
beans
>
|
2.4.启动工程
3.新建consumer工程
3.1导入pom依赖
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>
2.5
.
3
</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>
3.4
.
7
</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>
0.1
</version>
</dependency>
<!--
4
:spring依赖-->
<!--
1
)spring核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>
4.1
.
7
.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>
4.1
.
7
.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>
4.1
.
7
.RELEASE</version>
</dependency>
<!--
2
)spring dao层依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>
4.1
.
7
.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>
4.1
.
7
.RELEASE</version>
</dependency>
|
3.2 spring配置文件
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"<a href="http://www.springframework.org/schema/beans" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/beans"
xmlns:xsi=
"<a href="http://www.w3.org/2001/XMLSchema-instance" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.w3.org/2001/XMLSchema-instance"
xmlns:p=
"<a href="http://www.springframework.org/schema/p" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/p"
xmlns:context=
"<a href="http://www.springframework.org/schema/context" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/context"
xmlns:dubbo=
"<a href="http://code.alibabatech.com/schema/dubbo" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://code.alibabatech.com/schema/dubbo"
xmlns:mvc=
"<a href="http://www.springframework.org/schema/mvc" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 51, 102) !important;">http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http:
//www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http:
//www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http:
//www.springframework.org/schema/context <a href="http://www.springframework.org/schema/context/spring-context-4.2.xsd" "="" style="border-radius: 0px; background: 0px center; border: 0px; bottom: auto; float: none; height: auto; left: auto; line-height: 20px; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; min-height: inherit; color: rgb(0, 130, 0) !important;">http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 引用dubbo服务 -->
<dubbo:application name=
"dubbo-consumer"
/>
<dubbo:registry protocol=
"zookeeper"
address=
"192.168.56.100:2181"
/>
<dubbo:reference
interface
=
"com.niezheng.service.provider.IDubboProvider"
id=
"dubboProviderImpl"
></dubbo:reference>
</beans>
|
注:
<dubbo:reference interface="com.niezheng.service.provider.IDubboProvider" id="dubboProviderImpl"></dubbo:reference>
这里的接口名称要和dubbo提供方的要一致 ,消费方要有对应的接口如下
3.3 调用dubbo接口
3.4.访问调用接口
http://localhost:8080/test/dubbo
4.通过dubbo-admin查看提供者和调用者
4.1下载dbuuo-admin :http://dubbo.io/
4.2 将admin打包成war包
进入到dubbo-admin目录下 打开DOS窗口执行:mvn package -Dmaven.skip.test=true
4.3 使用xftp将war包部署到服务器
4.4.注意修改dubbo.properties
4.5 如果dubbo和zookeeper在同一台服务器上则不用修改 不在同一台机器上要修改zookeeper地址
4.6 启动tomcat
4.6 访问dubbo-admin
可以看见dubbo的消费者和提供者