dubbo笔记
mvc三层架构:
所有代码仍然在同一台计算机中执行,并发能力有限
RPC: client server
远程调用,让一个项目部署在多个不同的计算机中,提高并发能力,但是此模式的ip+端口号比较分散,有一定的维护难度
dubbo: SOA面向服务架构
节点角色规范
节点 | 负责启动,加载和运行服务Provider。 |
---|---|
Provider | 提供者公开远程服务 |
Consumer | 消费者致电远程服务 |
Register | 注册表负责服务发现和配置 |
Monitor | 监视器计算服务调用的数量和耗时 |
Container | 容器管理服务的生命周期 |
官方提供的用法:
本地spring配置:
<bean id=“xxxService” class=“com.xxx.XxxServiceImpl” />
<bean id=“xxxAction” class=“com.xxx.XxxAction”>
<property name=“xxxService” ref=“xxxService” />
</bean>
远程服务的spring配置:
remote-provider.xml:
<!-- define remote service bean the same way as local service bean -->
<bean id=“xxxService” class=“com.xxx.XxxServiceImpl” />
<!-- expose the remote service -->
<dubbo:service interface=“com.xxx.XxxService” ref=“xxxService” />
remote-consumer.xml:
<!-- reference the remote service -->
<dubbo:reference id=“xxxService” interface=“com.xxx.XxxService” />
<!-- use remote service the same say as local service -->
<bean id=“xxxAction” class=“com.xxx.XxxAction”>
<property name=“xxxService” ref=“xxxService” />
</bean>
开发dubbo程序:
一、准备环境
linux中安装注册中心zookeeper
-zookeeper依赖于jdk 需要在linux中安装
1.Oracle官网下载
2.安装
安装根目录,通过pwd命令查看
3.配置环境变量
输入
在文件最后追加
4.刷新环境变量
5.下载zookeeper
复制到linux中 ,打开当前目录
6.解压zookeeper
打开解压后的文件夹
修改选中的文件名,可以直接修改也可以通过linux命令
7.查看配置文件 vi zoo.conf
Zookeeper的端口号
8.设置zookeeper存放数据的目录
默认存放目录:
在zookeeper文件夹中建立data目录
将dataDir指向新建的data目录
dataDir=/apps/zookeeper-3.4.12/data
Shift+zz保存
9.启动zookeeper
进入zookeeper中的bin目录
进入zookeeper根目录
启动
关闭
查看状态
bin/zkServer.sh Status
二、服务方提供服务
Pom.xml
给maven项目内置tomcat
三、集成spring
在web.xml中开启spring容器
applicationContext.xml
Zookeeper所在计算机的ip 192.168.2.128 端口号在zoo.conf中查看:2181
配置spring扫描包
四、消费方
1.引入各种依赖 pom.xml(与服务方一致,改端口号,并设置客户端自己的gav)
2.引入springmvc (通过springmvc来访问提供方),在web.xml中配置disparcherServlet
3.消费方代码
需要调用服务方提供的方法
添加注解,如果此时用Autowired是在此工程中找StudentServer对象,而此工程中只有接口无实现类,所以要用@Reference
@controller
4.Springmvc配置文件
记得要在控制器类上加入@Restcontroller注解 返回json格式
四、启动服务端和客户端
注意:客户端端口是8882
架构图:
dubbo:项目整合架构图