ssm Dubbo分布式系统架构实战入门,良心详解之作二

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_30764991/article/details/81221878

前言:Dubbo (https://github.com/alibaba/dubbo)是阿里巴巴开源的分布式服务化治理框架(微服务框架),历经阿里巴巴电商平台的大规模复杂业务的高并发考验,到目前为止Dubbo仍然是开源界中体系最完善的服务化治理框架,因此Dubbo被国内大量的的互联网公司和专统企业使用,国内使用Dubbo的企业有:阿里巴巴、京东、电信运营商等。

当当对Dubbo的评价:Dubbo是一个被国内很多互联网公司广泛使用的开源分布式服务框架,即使从国际视野来看应该也是一个非常全面的SOA基础框架。作为一个重要的技术研究课题,在当当网我们根据自身的需求,为Dubbo实现了一些新的功能,并将其命名为Dubbox(即Dubbo eXtensions)。京东、当当、去哪儿等企业还组建了自己的中间件团队按自己的业务需求对Dubbo进行框架定制,目前当当网已开源了自己的Dubbo定制版Dubbox :http://www.oschina.net/p/dubbox

开发环境:这个根据个人的喜欢,没有强制的。喜欢用什么版本就用什么版本。

Window 7 64;Eclipse ;Apache-Maven-3.3.9;Spring ;Mybatis;Springmvc;Dubbo 2.5.3,

源码已经上传:https://download.csdn.net/download/qq_30764991/10568372

开发思路:

  • 一般开发是先设计数据库:那么这里也很简单的创建一个用户表。
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `user_id` varchar(100) NOT NULL COMMENT '用户ID',
  `user_name` varchar(100) DEFAULT NULL COMMENT '用户名称',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';


INSERT INTO `user` VALUES ('1', 'zhangsan', '18');
INSERT INTO `user` VALUES ('2', 'lisi', '19');
INSERT INTO `user` VALUES ('3', 'wangwu', '20');
INSERT INTO `user` VALUES ('4', 'zhaoliu', '21');

本文采用聚合工程模式:创建父工程和子工程,目录结构如下:

  • 依赖关系说明:
  • 1:ssm-dubbo-parent是pom文件的父工程,父目录工程的主要作用是统一大家所使用的jar包依赖。
  • 2:ssm-dubbbo-common与ssm-dubbbo-controller与ssm-dubbbo-service首先都是依赖于父目录工程。
  • 3:ssm-dubbbo-common是jar包,里面存放interface,bean,dao等公用内容。
  • 4:ssm-dubbbo-controller控制层和ssm-dubbbo-service业务层是war包,也就是各分为一个Tomcat。
  • 5:ssm-dubbbo-service依赖于ssm-dubbbo-common,ssm-dubbbo-controller依赖于ssm-dubbbo-service

父工程目录如下:

父目录管理的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.0</modelVersion>
  <groupId>com.demo</groupId>
  <artifactId>ssm-dubbo-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <description>这是一个ssm与dubbo的整合父目录工程</description>
  <!-- 设置统一控制版本 -->
	<properties>
		<java-version>1.7</java-version>
		<junit-version>4.12</junit-version>
		<spring.version>4.1.3.RELEASE</spring.version>
		<jackson.version>2.4.2</jackson.version>
		<mysql-connector-java-version>5.1.8</mysql-connector-java-version>
		<org.mybatis-version>3.2.7</org.mybatis-version>
		<org.mybatis-spring-version>1.2.2</org.mybatis-spring-version>
		<opensymphony-version>2.4.2</opensymphony-version>
		<freemarker-version>2.3.18</freemarker-version>
		<druid.version>1.0.9</druid.version>
		<commons-collections-version>1.0</commons-collections-version>
		<commons-fileupload-version>1.2.2</commons-fileupload-version>
		<org.apache.commons-version>3.1</org.apache.commons-version>
		<commons-codec-version>1.6</commons-codec-version>
		<dom4j-version>1.6.1</dom4j-version>
		<javax.servlet-version>1.2</javax.servlet-version>
		<aspectjweaver-version>1.6.6</aspectjweaver-version>
		<slf4j-log4j12-version>1.6.6</slf4j-log4j12-version>
		<log4j-version>1.2.16</log4j-version>
		<javax.servlet-jsp-version>2.0</javax.servlet-jsp-version>
		<cglib-version>2.2.2</cglib-version>
		<slf4j-api-version>1.6.6</slf4j-api-version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	
	<!-- 依赖管理 版本号 -->
	<!-- 设置统一依赖管理、不强行依赖 -->
	<dependencyManagement>
		<dependencies>
			<!-- with junit4.8.2 -->
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>${junit-version}</version>
				<type>jar</type>
			</dependency>
			<!-- Spring -->
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-jdbc</artifactId>
				<version>${spring.version}</version>
			</dependency>
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aspects</artifactId>
				<version>${spring.version}</version>
			</dependency>

			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-test</artifactId>
				<version>${spring.version}</version>
			</dependency>

			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context-support</artifactId>
				<version>${spring.version}</version>
			</dependency>

			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-jms</artifactId>
				<version>${spring.version}</version>
			</dependency>


			<!-- Jackson Json处理工具包 -->
			<dependency>
				<groupId>com.fasterxml.jackson.core</groupId>
				<artifactId>jackson-databind</artifactId>
				<version>${jackson.version}</version>
			</dependency>

			<dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>fastjson</artifactId>
				<version>1.1.41</version>
			</dependency>

			<!-- with mybatis-spring -->
			<dependency>
				<groupId>org.mybatis</groupId>
				<artifactId>mybatis</artifactId>
				<version>${org.mybatis-version}</version>
			</dependency>
			<dependency>
				<groupId>org.mybatis</groupId>
				<artifactId>mybatis-spring</artifactId>
				<version>${org.mybatis-spring-version}</version>
			</dependency>
			<!-- jdbc driver -->
			<dependency>
				<groupId>mysql</groupId>
				<artifactId>mysql-connector-java</artifactId>
				<version>${mysql-connector-java-version}</version>
			</dependency>
			<!-- 连接池 -->
			<dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>druid</artifactId>
				<version>${druid.version}</version>
			</dependency>
			<!-- apache commons jar -->
			<dependency>
				<groupId>commons-collections</groupId>
				<artifactId>commons-collections</artifactId>
				<version>${commons-collections-version}</version>
			</dependency>

			<dependency>
				<groupId>commons-fileupload</groupId>
				<artifactId>commons-fileupload</artifactId>
				<version>${commons-fileupload-version}</version>
			</dependency>

			<dependency>
				<groupId>org.apache.commons</groupId>
				<artifactId>commons-lang3</artifactId>
				<version>${org.apache.commons-version}</version>
			</dependency>

			<dependency>
				<groupId>commons-codec</groupId>
				<artifactId>commons-codec</artifactId>
				<version>${commons-codec-version}</version>
			</dependency>
			<!-- analyze xml use dom4j -->
			<dependency>
				<groupId>dom4j</groupId>
				<artifactId>dom4j</artifactId>
				<version>${dom4j-version}</version>
			</dependency>

			<!-- jstl标签 -->
			<dependency>
				<groupId>jstl</groupId>
				<artifactId>jstl</artifactId>
				<version>1.2</version>
			</dependency>
			<dependency>
				<groupId>taglibs</groupId>
				<artifactId>standard</artifactId>
				<version>1.1.2</version>
			</dependency>

			<dependency>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-log4j12</artifactId>
				<version>1.6.6</version>
			</dependency>
			<dependency>
				<groupId>log4j</groupId>
				<artifactId>log4j</artifactId>
				<version>1.2.17</version>
			</dependency>

			<dependency>
				<groupId>org.slf4j</groupId>
				<artifactId>slf4j-api</artifactId>
				<version>1.6.6</version>
			</dependency>
			<dependency>
				<groupId>javax.activation</groupId>
				<artifactId>activation</artifactId>
				<version>1.1.1</version>
			</dependency>

			<dependency>
				<groupId>cglib</groupId>
				<artifactId>cglib</artifactId>
				<version>${cglib-version}</version>
			</dependency>
			<dependency>
				<groupId>org.json</groupId>
				<artifactId>json</artifactId>
				<version>20131018</version>
			</dependency>
			<dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>dubbo</artifactId>
				<version>2.5.3</version>
			</dependency>
			<!-- Zookeeper 用于分布式服务管理 -->
			<dependency>
				<groupId>com.github.sgroschupf</groupId>
				<artifactId>zkclient</artifactId>
				<version>0.1</version>
			</dependency>
			<dependency>
				<groupId>org.apache.zookeeper</groupId>
				<artifactId>zookeeper</artifactId>
				<version>3.4.6</version>
			</dependency>
		</dependencies>
	</dependencyManagement>
			<!-- Tomcat7 servlet-api -->
		<dependencies>
			<dependency>
				<groupId>org.apache.tomcat</groupId>
				<artifactId>tomcat-jsp-api</artifactId>
				<version>7.0.47</version>
				<scope>provided</scope>
			</dependency>
		</dependencies>
		 <!-- jdk1.7 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

ssm-dubbo-common具体如下:

common项目这里存放的是interface,bean,dao等公用内容,其所对应的相应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.0</modelVersion>
	<parent>
		<groupId>com.demo</groupId>
		<artifactId>ssm-dubbo-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.demo</groupId>
	<artifactId>ssm-dubbo-common</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<description>这里存放的是interface,bean,dao等公用内容</description>
	<!-- jdk1.7 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<!-- 依赖管理之后台 -->
	<dependencies>
		<!-- with junit4.12 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
		</dependency>
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
		</dependency>
		<!-- Jackson Json处理工具包 -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
		</dependency>
		<!-- with mybatis-spring -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
		</dependency>
		<!-- jdbc driver -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- 连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
		</dependency>
		<!-- apache commons jar -->
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
		</dependency>

		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
		</dependency>
		<!-- analyze xml use dom4j -->
		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
		</dependency>

		<!-- jstl标签 -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
		</dependency>
		<dependency>
			<groupId>javax.activation</groupId>
			<artifactId>activation</artifactId>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
		</dependency>
		<dependency>
			<groupId>org.json</groupId>
			<artifactId>json</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<exclusions>
				<exclusion>
					<artifactId>spring</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
		</dependency>
		<!-- Zookeeper 用于分布式服务管理 -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
		</dependency>

		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
		</dependency>
	</dependencies>
</project>

ssm-dubbo-service项目工程如下:

具体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.0</modelVersion>
	<parent>
		<groupId>com.demo</groupId>
		<artifactId>ssm-dubbo-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.demo</groupId>
	<artifactId>ssm-dubbo-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<description>这里是service项目工程</description>
	<!-- jdk1.7 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- 配置tomcat插件 ,这样配置之后修改端口号就很方便-->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<!-- 端口 -->
					<port>8080</port>
					<!-- /表示工程名 -->
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<!-- 依赖关系管理,这里的service需要依赖dao,而dao在common中 -->
	<dependencies>
		<dependency>
			<groupId>com.demo</groupId>
			<artifactId>ssm-dubbo-common</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</project>

ssm-dubbo-controller如下:

具体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.0</modelVersion>
	<parent>
		<groupId>com.demo</groupId>
		<artifactId>ssm-dubbo-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.demo</groupId>
	<artifactId>ssm-dubbo-controller</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<description>这个是controller表现层的项目工程</description>
	<!-- jdk1.7 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- 配置tomcat插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<!-- 端口 -->
					<port>8081</port>
					<!-- /表示工程名 -->
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<!-- 依赖关系管理,controller依赖的serivce -->
	<dependencies>
		<dependency>
			<groupId>com.demo</groupId>
			<artifactId>ssm-dubbo-service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</project>

至此:聚合工程就搭建好了,接下是ssm的相应整合,相信网上已经有很多的分享。理解就比较简单了。

那么主要是配置ssm-dubbo-service与ssm-dubbo-controller.

ssm-dubbo-service下的配置结构目录具体如下:

spring.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/task
   		http://www.springframework.org/schema/task/spring-task-4.0.xsd
		http://code.alibabatech.com/schema/dubbo        
		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	
	<!-- 读取配置文件,可以读取多个 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>
		
	  <!-- 数据源  阿里巴巴出品  druid   -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="initialSize" value="${jdbc.initialSize}"></property>
		<property name="maxActive" value="${jdbc.maxActive}"></property>
		<property name="minIdle" value="1"></property>
		<property name="maxWait" value="30000"></property>
		<property name="timeBetweenEvictionRunsMillis" value="60000"></property>
		<property name="minEvictableIdleTimeMillis" value="300000"></property>
		<property name="validationQuery" value="SELECT 1 "></property>
		<property name="testWhileIdle" value="true"></property>
		<property name="poolPreparedStatements" value="true"></property>
		<property name="maxPoolPreparedStatementPerConnectionSize" value="20"></property>
		<property name="filters" value="stat,slf4j" />
		<property name="proxyFilters">
			<list>
				<ref bean="stat-filter" />
				<ref bean="log-filter" />
			</list>
		</property>
	</bean>
	
	<bean id="log-filter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter">
		<property name="resultSetLogEnabled" value="true" />
		<property name="statementExecutableSqlLogEnable" value="true" />
	</bean>

	<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
		<property name="slowSqlMillis" value="30000" />
		<property name="logSlowSql" value="true" />
		<property name="mergeSql" value="true" />
	</bean>
	<!-- 事务管理-->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	 <!-- 拦截器方式配置事物,方法命名记得要规范,这里是使用的是xml的方式配置事物 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="select*" read-only="true" propagation="REQUIRED"/>
			<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
			<tx:method name="query*" read-only="true" propagation="REQUIRED"/>
			<tx:method name="get*" read-only="true" propagation="REQUIRED"/>
			<tx:method name="search*" read-only="true" propagation="REQUIRED"/>
			<tx:method name="is*" read-only="true" propagation="REQUIRED"/>
			<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="create*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="execute*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="remove*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="put*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="import*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="submit*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="cancel*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="send*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="edit*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="handle*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="close*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
			<tx:method name="export*" propagation="REQUIRED" rollback-for="Exception,RuntimeException"/>
		</tx:attributes>
	</tx:advice>
	<!-- 事务切面-->
	<aop:config>
		<aop:pointcut id="transactionPointcut"
			expression="execution(* com.demo..*ServiceImpl.*(..))" />
		<aop:advisor pointcut-ref="transactionPointcut" advice-ref="txAdvice" />
	</aop:config>
</beans>

spring-mybatis.xml的配置代码具体如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/task
   		http://www.springframework.org/schema/task/spring-task-4.0.xsd
		http://code.alibabatech.com/schema/dubbo        
		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	
	<!-- sqlSessionFactory 工厂 -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource"/>
		<!-- 配置Mybatis核心配置文件所在位置 -->
		<property name="configLocation" value="classpath:mybatis-config.xml"/>
	</bean>
	
	<!-- 接口   Mapper 扫描方式 自动 -->
	<!-- MapperScannerConfigurer会自动找sqlSessionFactory,所以不用注入了.自己干预也可以 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 扫描的包 -->
		<property name="basePackage" value="com.demo.core.dao"/>
	</bean>
	
</beans>

spring-service.xml的代码具体如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/task
   		http://www.springframework.org/schema/task/spring-task-4.0.xsd
		http://code.alibabatech.com/schema/dubbo        
		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

       <!-- 扫描service -->
	<context:component-scan base-package="com.demo.core.service"/>
</beans>

spring-dubbo-provider.xml配置代码如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/task
   		http://www.springframework.org/schema/task/spring-task-4.0.xsd
		http://code.alibabatech.com/schema/dubbo        
		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
		
	<!-- Dubbo提供方 -->
	<!-- 1:名称  计算机 区别 唯一 -->
	<dubbo:application name="demo-service"/>
	<!-- 2:设置注册中心  地址  zookeeper在哪里  redis ... -->
<!-- 	<dubbo:registry address="192.168.0.150:2181,192.168.0.151:2181,192.168.0.152:2181" protocol="zookeeper"/> -->
<!-- 	<dubbo:registry address="192.168.0.150:2181" protocol="zookeeper"/> -->
     <!-- 设置不注册 -->
	<dubbo:registry address="N/A"/>
	<!-- 3:dubbo://127.0.0.1:20880  默认  设置提供服务的端口号 -->
	<dubbo:protocol port="20880" name="dubbo"/>
	
	<!-- 4: 暴露实现类-->
	<dubbo:service interface="com.demo.core.service.DemoUserService" ref="testUserService"/>
	
	<bean id="testUserService" class="com.demo.core.service.TestUserServiceImpl" />
</beans>

jdbc.properties配置如下:

jdbc.url=jdbc:mysql://localhost:3306/test2?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
jdbc.initialSize=1
jdbc.maxActive=20

log4j.properties配置如下:

log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

mybatis-config.xml配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <!-- 别名 -->
    <typeAliases>
        <package name="com.demo.core.bean"/>
    </typeAliases>
</configuration>

spring-context.xml配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/task
   		http://www.springframework.org/schema/task/spring-task-4.0.xsd
		http://code.alibabatech.com/schema/dubbo        
		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
		<!-- 数据源  、事务 、扫描、MYbatis、ActiveMQ、Freemarker、Redis、Solr。。。。。 -->		
		<import resource="classpath:config/*.xml"/>
</beans>

ssm-dubbo-service的web.xml配置如下,主要是配置spring的整合具体代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" version="3.0">
  <display-name>ssm-dubbo-service</display-name>
 <!-- 上下文配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-context.xml</param-value>
	</context-param>
	
	<!-- 监听器 加载配置上下文配置文件-->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

接下配置:ssm-dubbo-controller,结构目录如下

dubbo-consumer.xml的 配置如下

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/task
   		http://www.springframework.org/schema/task/spring-task-4.0.xsd
		http://code.alibabatech.com/schema/dubbo        
		http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
		
	<!-- 扫描 @Contr -->	
	<context:component-scan base-package="com.demo.core.controller" />
	<!-- 处理器 映射器  适配器 -->
	<mvc:annotation-driven/>
	
	<!-- 静态资源访问(不拦截此目录下的东西的访问) -->
    <mvc:resources location="/WEB-INF/static/" mapping="/static/**" />
    
	<!-- 视图解释器 jsp -->
	<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	<import resource="dubbo-consumer.xml"/>
	
</beans>

springmvc.xml配置代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" version="3.0">
  <display-name>ssm-dubbo-controller</display-name>
  	<!--springmvc 前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<!-- 默认找 WEB-INF/[servlet的名称]-servlet.xml -->
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<!-- /  :拦截所有请求 (但不包括.jsp)   .js .png .css   (配置对静态资源放行)
			 /* :拦截所有请求   真全拦截   (基本上不用)   
			 *.do  : .do 
		 -->
		 <servlet-name>springmvc</servlet-name>
		 <url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

log4j.properties配置如下:

log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

ssm-dubbo-controller的web.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" version="3.0">
  <display-name>ssm-dubbo-controller</display-name>
  	<!--springmvc 前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<!-- 默认找 WEB-INF/[servlet的名称]-servlet.xml -->
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<!-- /  :拦截所有请求 (但不包括.jsp)   .js .png .css   (配置对静态资源放行)
			 /* :拦截所有请求   真全拦截   (基本上不用)   
			 *.do  : .do 
		 -->
		 <servlet-name>springmvc</servlet-name>
		 <url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

到这,相应的配置就完成了,接下来就是简单的编写代码测试:

这里简单的编写公用的的具体结构如下:

控制表现层代码很简单的,如下:

启动项目:先启动ssm-dubbo-service,然后再启动项目ssm-dubbo-controller..浏览器输入:http://localhost:8081/user?userId=2

结果如图

到此,所有的全都弄好了。

哎,累死我了,编码不易,继续加油!!!项目中你可能会遇到一此错误,那么不断的调试一定会成功的!!!这里小编也把项目源码上传了,希望对于你有帮助。

源码已经上传:https://download.csdn.net/download/qq_30764991/10568372

展开阅读全文

没有更多推荐了,返回首页