一、Dubbo中Provider搭建
- 新建Maven Project, 里面只有接口(dubbo-service)
1.1 为什么这么做?
RPC框架,不希望Consumer知道具体实现.如果实现类和接口在同一个项目中,Consumer依赖这个项目时,就会知道实现类具体实现。 - 新建Maven Project, 写接口的实现类(dubbo-service-impl)
- 在duboo-service-impl中配置pom.xml
3.1 依赖接口
3.2 依赖dubbo,去掉老版本spring
3.3 依赖新版本spring
3.4 依赖zookeeper客户端工具zkClient<dependencies> <dependency> <groupId>com.bjsxt</groupId> <artifactId>dubbo-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- 访问zookeeper的客户端jar --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> </dependencies>
- 新建实现类,并实现接口方法.
- 新建配置文件applicationContext-dubbo.xml,并配置
5.1 <dubbo:application/> 给provider起名,在monitor或管理工具中区别是哪个provider
5.2 <dubbo:registry/> 配置注册中心
5.2.1 address:注册中心的ip和端口
5.2.2 protocol使用哪种注册中心
5.3 <dubbo:protocol/> 配置协议
5.3.1 name 使用什么协议
5.3.2 port: consumer invoke provider时的端口号
5.4 <dubbo:service/> 注册接口
5.4.1 ref 引用接口实现类<bean>的id值<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 给当前Provider自定义个名字 --> <dubbo:application name="dubbo-service"/> <!-- 配置注册中心 --> <dubbo:registry address="192.168.139.130:2181" protocol="zookeeper"></dubbo:registry> <!-- 配置端口 --> <dubbo:protocol name="dubbo" port="20888"></dubbo:protocol> <!-- 注册功能 --> <dubbo:service interface="com.bjsxt.service.DemoService" ref="demoServiceImpl"></dubbo:service> <bean id="demoServiceImpl" class="com.bjsxt.service.impl.DemoServiceImpl"></bean>
二、Admin管理界面
- 本质就是一个web项目
- 获取注册中心内Provider注册的信息.用页面呈现出来.
- 实现步骤
3.1 把dubbo-admin-2.5.3.war上传到服务器tomcat中.
3.2 启动tomcat完成后关闭tomcat,删除上传的dubbo-admin-2.5.3.war
3.2.1 为什么要删除:需要修改解压后的文件夹,如果不删除.war文件,下次重启tomcat会还原成未修改状态
3.3 进入dubbo-admin-2.5.3/WEB-INF/dubbo.properties,
3.3.1 修改第一行为zookeeper的ip和端口
3.3.2 第二行和第三行为管理界面的用户名和密码
3.4 启动tomcat, 在浏览器地址栏访问tomcat中dubbo项目
三、Consumer搭建过程
- 在pom.xml中除了ssm的依赖添加dubbo相关3个依赖(接口,dubbo.jar,zkClient)
- web.xml中修改<init-value>applicationContext-*.xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param>
- spring配置文件命名为applicationContext-spring.xml,配置dubbo的配置文件applicationContext-dubbo.xml
<!-- 给当前Provider自定义个名字 --> <dubbo:application name="dubbo-consumer"/> <!-- 配置注册中心 --> <dubbo:registry address="192.168.139.130:2181" protocol="zookeeper"></dubbo:registry> <!-- 配置注解扫描 --> <dubbo:annotation package="com.bjsxt.service.impl"/>
- 不需要编写mapper
-
除了ServiceImpl中引用Provider中接口对象改变,其他代码都一样.
@Service public class TestServiceImpl implements TestService { // @Resource // private xxMapper xxxMapper; @Reference private DemoService demoService;
四、Maven打包插件Assembly
- 在dubbo的provider项目(实现类项目)中pom.xml配置assembly插件信息
<!-- 指定项目的打包插件信息 --> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <!-- 指定打包描述文件的位置:相对项目根目录的路径 --> <!-- assembly打包的描述文件 --> <descriptor>assembly/assembly.xml</descriptor> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins>
- 在项目根目录下新建assembly文件夹
- 在assembly文件夹中新建assembly.xml
<?xml version='1.0' encoding='UTF-8'?> <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd"> <!-- 该字符会添加到最终tar.gz包的名称后面,作为后缀 --> <id>assembly</id> <!-- 指定打包的格式为tar.gz,该类型压缩包在linux中比较常见 --> <formats> <format>tar.gz</format> </formats> <!-- 在tar.gz压缩包中是否包含根文件夹,该根文件夹名称和tar.gz去掉id后缀一致 --> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <!-- 将项目根路径下assembly/bin路径中的内容打包到压缩包中的根目录下的bin目录中 --> <fileSet> <!-- 相对项目根路径的相对路径 --> <directory>assembly/bin</directory> <outputDirectory>bin</outputDirectory> <!-- 设置最终tar.gz中该文件夹下的权限,跟linux权限写法一致 --> <fileMode>0755</fileMode> </fileSet> <!-- 将项目根路径下assembly/conf路径中的内容打包到压缩包中的根目录下的conf目录中 --> <fileSet> <directory>assembly/conf</directory> <outputDirectory>conf</outputDirectory> <!-- 设置其linux权限 --> <fileMode>0644</fileMode> </fileSet> </fileSets> <!-- 将所有依赖的jar包打包到压缩包中的根目录下的lib目录中 --> <!-- 此lib目录中包含自己开发的项目jar包以及demo_service.jar,还有第三方的jar包 --> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> </dependencySet> </dependencySets> </assembly>
- 解压下载的dubbo-monitor-simple-2.5.3-assembly.tar.gz压缩包,把解压后的bin和conf粘贴到项目下assembly文件夹中.
4.1 清空conf/dubbo.properties中内容. - 右键项目--> maven install
5.1 在target下出现: 项目名-版本-assembly.tar.gz压缩包 - 把压缩包复制到window或linux中
6.1 window中使用start.bat启动,关闭命令窗口关闭服务.
6.2 linux中使用start.sh启动使用stop.sh关闭.