Dubbo在SSM项目中的使用

Dubbo在SSM项目中的使用

案例源代码下载地址

https://download.csdn.net/download/pcbhyy/10764704

操作步骤

  1. 创建一个ssm集成的项目,及编写初始代码
  2. 把该项目拆分为两部分(表示层一部分、业务逻辑和数据访问层一部分)
  3. 通过dubbo在表示层中访问业务逻辑层中的组件(关键)

Dubbo简介

Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现
服务的输出和输入功能,可以和Spring框架无缝集成。

  1. Provider
    暴露服务方称之为“服务提供者”。
  2. Consumer
    调用远程服务方称之为“服务消费者”。
  3. Registry
    服务注册与发现的中心目录服务称之为“服务注册中心”。
  4. Monitor
    统计服务的调用次数和调用时间的日志服务称之为“服务监控中心”。

一、项目拆分

  1. 拆分内容:
    web.xml
    pom.xml
    springmvc.xml
    applicationContext.xml
    部分配置文件的删除
    代码的删减
  2. 表示层项目
    1)pom.xml(注意:添加了dubbo、zookeeper的依赖)
<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.yygroupId>
			<artifactId>ssm_dept2_vcartifactId>
			<version>0.0.1-SNAPSHOTversion>
			<packaging>warpackaging>
			<build>
				<resources>
					<resource>
						<directory>src/main/javadirectory>
						<excludes>
							<exclude>**/*.javaexclude>
						excludes>
					resource>
					<resource>
						<directory>src/main/resourcesdirectory>
					resource>
				resources>
				<plugins>
					
					<plugin>
						<groupId>org.apache.maven.pluginsgroupId>
						<artifactId>maven-resources-pluginartifactId>
						<version>2.7version>
						<configuration>
							<encoding>UTF-8encoding>
						configuration>
					plugin>
					
					<plugin>
						<groupId>org.apache.maven.pluginsgroupId>
						<artifactId>maven-compiler-pluginartifactId>
						<version>3.2version>
						<configuration>
							<source>1.8source>
							<target>1.8target>
							<encoding>UTF-8encoding>
						configuration>
					plugin>
					<plugin>
						<groupId>org.apache.tomcat.mavengroupId>
						<artifactId>tomcat7-maven-pluginartifactId>
						<version>2.2version>
						<configuration>
							
							<path>/path>
							
							<port>8090port>
							<uriEncoding>utf-8uriEncoding>
						configuration>
					plugin>
				plugins>
			build>
			<dependencies>
				<dependency>
					<groupId>junitgroupId>
					<artifactId>junitartifactId>
					<version>4.12version>
				dependency>
				<dependency>
					<groupId>log4jgroupId>
					<artifactId>log4jartifactId>
					<version>1.2.17version>
				dependency>
				<dependency>
					<groupId>org.springframeworkgroupId>
					<artifactId>spring-contextartifactId>
					<version>4.3.8.RELEASEversion>
				dependency>
				<dependency>
					<groupId>aopalliancegroupId>
					<artifactId>aopallianceartifactId>
					<version>1.0version>
				dependency>
				<dependency>
					<groupId>org.aspectjgroupId>
					<artifactId>aspectjweaverartifactId>
					<version>1.8.10version>
				dependency>
				<dependency>
					<groupId>org.springframeworkgroupId>
					<artifactId>spring-txartifactId>
					<version>4.3.8.RELEASEversion>
				dependency>
				<dependency>
					<groupId>org.springframeworkgroupId>
					<artifactId>spring-jdbcartifactId>
					<version>4.3.8.RELEASEversion>
				dependency>
				<dependency>
					<groupId>org.springframeworkgroupId>
					<artifactId>spring-ormartifactId>
					<version>4.3.8.RELEASEversion>
				dependency>


				<dependency>
					<groupId>org.mybatisgroupId>
					<artifactId>mybatis-springartifactId>
					<version>1.2.2version>
				dependency>
				<dependency>
					<groupId>jstlgroupId>
					<artifactId>jstlartifactId>
					<version>1.2version>
				dependency>
				
				<dependency>
					<groupId>javax.servletgroupId>
					<artifactId>javax.servlet-apiartifactId>
					<version>3.1.0version>
					<scope>providedscope>
				dependency>
				
				<dependency>
					<groupId>javax.servlet.jspgroupId>
					<artifactId>jsp-apiartifactId>
					<version>2.2version>
					<scope>providedscope>
				dependency>
				<dependency>
					<groupId>org.springframeworkgroupId>
					<artifactId>spring-webmvcartifactId>
					<version>4.3.8.RELEASEversion>
				dependency>

				
				<dependency>
					<groupId>org.slf4jgroupId>
					<artifactId>slf4j-log4j12artifactId>
					<version>1.7.25version>
				dependency>

				<dependency>
					<groupId>org.hibernategroupId>
					<artifactId>hibernate-validatorartifactId>
					<version>5.4.1.Finalversion>
				dependency>
				<dependency>
					<groupId>javax.validationgroupId>
					<artifactId>validation-apiartifactId>
					<version>1.1.0.Finalversion>
				dependency>
				
				<dependency>
					<groupId>org.jboss.logginggroupId>
					<artifactId>jboss-loggingartifactId>
					<version>3.3.1.Finalversion>
				dependency>
				
				<dependency>
					<groupId>com.fasterxmlgroupId>
					<artifactId>classmateartifactId>
					<version>1.3.3version>
				dependency>
				<dependency>
					<groupId>commons-fileuploadgroupId>
					<artifactId>commons-fileuploadartifactId>
					<version>1.3.2version>
				dependency>
				<dependency>
					<groupId>com.fasterxml.jackson.coregroupId>
					<artifactId>jackson-databindartifactId>
					<version>2.8.8.1version>
				dependency>
				<dependency>
					<groupId>com.github.pagehelpergroupId>
					<artifactId>pagehelperartifactId>
					<version>5.0.0version>
				dependency>
				<dependency>
					<groupId>com.alibabagroupId>
					<artifactId>dubboartifactId>
					<version>2.5.3version>
				dependency>
				<dependency>
					<groupId>org.apache.zookeepergroupId>
					<artifactId>zookeeperartifactId>
					<version>3.4.7version>
				dependency>
				<dependency>
					<groupId>com.github.sgroschupfgroupId>
					<artifactId>zkclientartifactId>
					<version>0.1version>
				dependency>
			dependencies>
		project>

2)web.xml(注意:去掉了监听器和上下文参数)


		<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
		  <display-name>spring_demodisplay-name>
		  <welcome-file-list>
			<welcome-file>index.jspwelcome-file>
		  welcome-file-list>
		  
		  <filter>
			<filter-name>CharacterEncodingFilterfilter-name>
			<filter-class>
				org.springframework.web.filter.CharacterEncodingFilter
			filter-class>
			<init-param>
				<param-name>encodingparam-name>
				<param-value>utf-8param-value>
			init-param>
		  filter>
		  
		  <filter-mapping>
			<filter-name>CharacterEncodingFilterfilter-name>
			<url-pattern>/*url-pattern>
		  filter-mapping>
		  
		  <servlet>
			<servlet-name>DispatcherServletservlet-name>
			<servlet-class>
				org.springframework.web.servlet.DispatcherServlet
			servlet-class>
			<init-param>
			  <param-name>contextConfigLocationparam-name>
			  <param-value>classpath:springmvc.xmlparam-value>
			init-param>
		  servlet>
		  <servlet-mapping>
			<servlet-name>DispatcherServletservlet-name>
			<url-pattern>/url-pattern>
		  servlet-mapping>  
		web-app>

3)去掉了applicationContext.xml、db.properties、SqlMapConfig.xml三个配置文件
4)删除了com.yy.mapper包和com.yy.service包中的实现类(接口留着)
3. 业务逻辑和数据访问层项目
1)pom.xml

<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.yygroupId>
			<artifactId>ssm_dept2_martifactId>
			<version>0.0.1-SNAPSHOTversion>
			<packaging>warpackaging>
			<build>
				<resources>
					<resource>
						<directory>src/main/javadirectory>
						<excludes>
							<exclude>**/*.javaexclude>
						excludes>
					resource>
					<resource>
						<directory>src/main/resourcesdirectory>
					resource>
				resources>
				<plugins>
					
					<plugin>
						<groupId>org.apache.maven.pluginsgroupId>
						<artifactId>maven-resources-pluginartifactId>
						<version>2.7version>
						<configuration>
							<encoding>UTF-8encoding>
						configuration>
					plugin>
					
					<plugin>
						<groupId>org.apache.maven.pluginsgroupId>
						<artifactId>maven-compiler-pluginartifactId>
						<version>3.2version>
						<configuration>
							<source>1.8source>
							<target>1.8target>
							<encoding>UTF-8encoding>
						configuration>
					plugin>
					<plugin>
						<groupId>org.apache.tomcat.mavengroupId>
						<artifactId>tomcat7-maven-pluginartifactId>
						<version>2.2version>
						<configuration>
							
							<path>/path>
							
							<port>8089port>

							<uriEncoding>utf-8uriEncoding>
						configuration>
					plugin>
				plugins>
			build>
			<dependencies>
				<dependency>
					<groupId>junitgroupId>
					<artifactId>junitartifactId>
					<version>4.12version>
				dependency>
				<dependency>
					<groupId>log4jgroupId>
					<artifactId>log4jartifactId>
					<version>1.2.17version>
				dependency>
				<dependency>
					<groupId>org.springframeworkgroupId>
					<artifactId>spring-contextartifactId>
					<version>4.3.8.RELEASEversion>
				dependency>
				<dependency>
					<groupId>aopalliancegroupId>
					<artifactId>aopallianceartifactId>
					<version>1.0version>
				dependency>
				<dependency>
					<groupId>org.aspectjgroupId>
					<artifactId>aspectjweaverartifactId>
					<version>1.8.10version>
				dependency>
				<dependency>
					<groupId>commons-dbcpgroupId>
					<artifactId>commons-dbcpartifactId>
					<version>1.4version>
				dependency>
				<dependency>
					<groupId>mysqlgroupId>
					<artifactId>mysql-connector-javaartifactId>
					<version>5.1.6version>
				dependency>
				<dependency>
					<groupId>org.springframeworkgroupId>
					<artifactId>spring-txartifactId>
					<version>4.3.8.RELEASEversion>
				dependency>
				<dependency>
					<groupId>org.springframeworkgroupId>
					<artifactId>spring-jdbcartifactId>
					<version>4.3.8.RELEASEversion>
				dependency>
				<dependency>
					<groupId>org.springframeworkgroupId>
					<artifactId>spring-ormartifactId>
					<version>4.3.8.RELEASEversion>
				dependency>
				<dependency>
					<groupId>org.mybatisgroupId>
					<artifactId>mybatisartifactId>
					<version>3.2.6version>
				dependency>
				<dependency>
					<groupId>cglibgroupId>
					<artifactId>cglibartifactId>
					<version>3.2.5version>
				dependency>
				<dependency>
					<groupId>org.mybatisgroupId>
					<artifactId>mybatis-springartifactId>
					<version>1.2.2version>
				dependency>

				<dependency>
					<groupId>org.springframeworkgroupId>
					<artifactId>spring-webmvcartifactId>
					<version>4.3.8.RELEASEversion>
				dependency>

				
				<dependency>
					<groupId>org.slf4jgroupId>
					<artifactId>slf4j-log4j12artifactId>
					<version>1.7.25version>
				dependency>
				<dependency>
					<groupId>org.hibernategroupId>
					<artifactId>hibernate-validatorartifactId>
					<version>5.4.1.Finalversion>
				dependency>
				<dependency>
					<groupId>javax.validationgroupId>
					<artifactId>validation-apiartifactId>
					<version>1.1.0.Finalversion>
				dependency>
				
				<dependency>
					<groupId>org.jboss.logginggroupId>
					<artifactId>jboss-loggingartifactId>
					<version>3.3.1.Finalversion>
				dependency>
				
				<dependency>
					<groupId>com.fasterxmlgroupId>
					<artifactId>classmateartifactId>
					<version>1.3.3version>
				dependency>
				<dependency>
					<groupId>com.github.pagehelpergroupId>
					<artifactId>pagehelperartifactId>
					<version>5.0.0version>
				dependency>
				<dependency>
					<groupId>com.alibabagroupId>
					<artifactId>dubboartifactId>
					<version>2.5.3version>
				dependency>
				<dependency>
					<groupId>org.apache.zookeepergroupId>
					<artifactId>zookeeperartifactId>
					<version>3.4.7version>
				dependency>
				<dependency>
					<groupId>com.github.sgroschupfgroupId>
					<artifactId>zkclientartifactId>
					<version>0.1version>
				dependency>
			dependencies>
		project>
  1. web.xml(删除了前端控制器Servlet的配置)

		<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
		  <display-name>spring_demodisplay-name>
		  <welcome-file-list>
			<welcome-file>index.jspwelcome-file>
		  welcome-file-list>
		  
		  <filter>
			<filter-name>CharacterEncodingFilterfilter-name>
			<filter-class>
				org.springframework.web.filter.CharacterEncodingFilter
			filter-class>
			<init-param>
				<param-name>encodingparam-name>
				<param-value>utf-8param-value>
			init-param>
		  filter>
		  
		  <filter-mapping>
			<filter-name>CharacterEncodingFilterfilter-name>
			<url-pattern>/*url-pattern>
		  filter-mapping> 
		  
		  <context-param>
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:applicationContext.xmlparam-value>
		  context-param>
		  <listener>
			<listener-class>
				org.springframework.web.context.ContextLoaderListener
			listener-class>
		  listener>
		web-app>

3)删除了springmvc.xml
4)删除了com.yy.controller包和所有jsp页面

二、在linux中安装zookeeper

1)测试java环境
java -version
2)SSH上传提示:encountered 1 errors during the transfer错误解决办法
上传的时候 windows 的路径不能存在括号“()”, 把括号去掉 重新上传就好了 或
把文件移到不带小括号的路径下就可以正常上传文件了。

3)zookeeper是java开发的可以运行在windows、linux环境。需要先安装jdk。
安装步骤:
第一步:安装jdk
第二步:把zookeeper的压缩包上传到linux系统。
第三步:解压缩压缩包

tar -zxvf zookeeper-3.4.6.tar.gz

第四步:进入zookeeper-3.4.6目录,创建data文件夹。

mkdir data

第五步:进入conf文件夹,把zoo_sample.cfg改名为zoo.cfg

[root@localhost conf]# mv zoo_sample.cfg zoo.cfg

第六步:编辑 zoo.cfg(vim zoo.cfg),修改data属性:dataDir=/root/zookeeper-3.4.6/data
第七步:启动zookeeper

[root@localhost bin]# ./zkServer.sh start

关闭:

[root@localhost bin]# ./zkServer.sh stop

查看状态:

[root@localhost bin]# ./zkServer.sh status
成功启动显示如下内容:

	JMX enabled by default
	Using config: /root/zookeeper-3.4.6/bin/../conf/zoo.cfg
	Mode: standalone

注意:需要关闭防火墙。

service iptables stop

永久关闭修改配置开机不启动防火墙:

chkconfig iptables off

如果不能成功启动zookeeper,需要删除data目录下的zookeeper_server.pid文件。

三、通过dubbo在表示层中访问业务逻辑层中的组件

  1. 业务逻辑项目中发布组件(在applicationContext.xml中添加)

<dubbo:application name="ssm_dept2_m"/>


<dubbo:registry protocol="zookeeper" address="192.168.220.129:2181">dubbo:registry>


<dubbo:protocol name="dubbo" port="20880" />


<dubbo:service interface="com.yy.service.DeptService" ref="deptServiceImpl">dubbo:service>
<dubbo:service interface="com.yy.service.EmpService" ref="empServiceImpl">dubbo:service>
  1. 在表示层项目中引用组件(在springmvc.xml中添加)


<dubbo:application name="ssm_dept2_vc"/>

<dubbo:registry protocol="zookeeper" address="192.168.220.129:2181">dubbo:registry>


<dubbo:reference interface="com.yy.service.DeptService" id="deptService">dubbo:reference>
<dubbo:reference interface="com.yy.service.EmpService" id="empService">dubbo:reference>
  1. 先启动业务逻辑项目,在启动表示层项目(注意:修改两个项目中tomcat的端口号不相同)
  2. 在浏览器中访问action,测试

四、说明

Dubbo是什么?

Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这

样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架(告别Web Service模式中的WSdl,以服务者与消费者的方式在dubbo上注册)
其核心部分包含:

  1. 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
  2. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
  3. 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

Dubbo能做什么?

  1. 透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
  2. 软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
  3. 服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。

你可能感兴趣的:(SSM框架)