分布式系统架构实战demo:SSM+Dubbo

对于传统的单一构架,也就是打成的war或者ear包部署在同一个Web容器里的构架,它虽然开发、测试、部署简单,但随着业务的不断发展,维护成本增加,可扩展性差,而且一台Tomcat的并发数在500左右,系统性能存在瓶颈。虽然互联网应用的系统性能瓶颈存在于每一个地方(数据库,网络等),先不考虑其他系统瓶颈,本文采用多Tomcat来解决一个Tomcat带来的瓶颈问题,那么多个Tomcat之间如何通信?答案是Dubbo。

        为什么要使用Dubbo?两台服务器A、B,分别部署不同的应用a,b。当A服务器想要调用B服务器上应用b提供的函数或方法的时候,由于不在一个内存空间,不能直接调用,需要通过网络来表达调用的语义传达调用的数据。--RPC。 Dubbo是基于RPC的高性能和透明化的RPC远程服务调用方案。

       Dubbo也是一个被国内互联网公司广泛使用的SOA(核心理念:对外提供服务)基础框架,说的直白点就是:一次开发出来的服务接口,相关服务都能调用。很像Java经典的特性:一次编译,到处运行。

开发环境:

Window 7 64;Eclipse-Jee-mars-2-win32-x86_64 ;Apache-Maven-3.3.9;Spring 4.1.3;Mybatis 3.2.7;Springmvc 4.1.3;Dubbo 2.5.3,开发工具和源码:http://pan.baidu.com/s/1pLdoe0r

源码博主也上传到了'码云'上:https://gitee.com/liugh_develop/ssm_dubbo

系统构架如下:

                  分布式系统架构实战demo:SSM+Dubbo_第1张图片

1.配置环境,创建项目工程

        配置开发环境不再这里做赘述,可以参考:Eclipse+Tomcat+MAVEN+SVN项目完整环境搭建

需要注意的是本文所用JDK是1.7,Tomcat是7.0。

      1.1  mysql安装

        数据库环境搭建吐血推荐,博主在踩了很多坑之后总结的高效安装方法。一分钟快速安装MySQL,超简单。

       Mysql链接工具Navicat安装后,打开Navicat,新建连接-->新建数据库Mysql-->双击进入数据库-->查询-->新建查询:

SET FOREIGN_KEY_CHECKS=0;


DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
  `book_id` varchar(100) NOT NULL COMMENT '图书ID',
  `book_name` varchar(100) DEFAULT NULL COMMENT '图书名称',
  `number` int(11) DEFAULT NULL COMMENT '馆藏数量',
  PRIMARY KEY (`book_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='图书表';


INSERT INTO `book` VALUES ('1000', 'Java程序设计', '10');
INSERT INTO `book` VALUES ('1001', '数据结构', '10');
INSERT INTO `book` VALUES ('1002', '设计模式', '10');
INSERT INTO `book` VALUES ('1003', '编译原理', '10');

      1.3  创建父工程和子工程,目录结构如下:

                                            分布式系统架构实战demo:SSM+Dubbo_第2张图片

    其中,parent是pom文件的父工程,common是jar包,里面存放interface,bean,dao等公用内容。

controller控制层和service业务层是war包,也就是各分为一个Tomcat。

创建父工程:

                 分布式系统架构实战demo:SSM+Dubbo_第3张图片

                      分布式系统架构实战demo:SSM+Dubbo_第4张图片

创建common工程,在Packaging时选择jar:

                分布式系统架构实战demo:SSM+Dubbo_第5张图片

创建controller和service的步骤和common一样,不过Packaging选择war,完成后会出现以下报错:

                      分布式系统架构实战demo:SSM+Dubbo_第6张图片

原因是webapp下没有WEB_INF文件夹和web.xml文件,自己创建web.xml,它内容在下文2.1中,复制粘贴即可。注意,此时的运行环境是jdk1.5,需要永久切换成jdk1.7(点buildPath切换1.7的运行环境,下回打开还是jdk1.5).所以在所有的pom文件中加入


	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.pluginsgroupId>
				<artifactId>maven-compiler-pluginartifactId>
				<version>3.2version>
				<configuration>
					<source>1.7source>
					<target>1.7target>
					<encoding>UTF-8encoding>
				configuration>
			plugin>
		plugins>
	build>

加入后会有以下报错,根据如下图片中步骤处理即可:

           分布式系统架构实战demo:SSM+Dubbo_第7张图片

      1.4  导入jar包。

parent项目pom.xml导入所有jar包的坐标,service和controller会依赖common,所以在common的pom文件中加入要引入的jar包。因为太长,此处省略源码,读者可以下载源码查看。

因为公用jar放在了common里,所以在controller和service的pom,xml文件中都加入common的依赖:

 
  <dependencies>
  	<dependency>
  		 <groupId>com.testgroupId>
    	<artifactId>test-commonartifactId>
    	<version>0.0.1-SNAPSHOTversion>
  	dependency>
  dependencies>

 

2.整合Spring+Mybatis

      2.1  配置Spring监听器

       一般会整合Spring+Mybatis放在业务逻辑层service。在service的web.xml中加入:

xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<context-param>
		<param-name>contextConfigLocationparam-name>
		<param-value>classpath:spring-context.xmlparam-value>
	context-param>
	
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
	listener>
web-app>

      2.2  创建上下文配置文件: 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">
		
       		
		<import resource="classpath:config/*.xml"/>

beans>

      2.3创建数据源和配置文件jdbc.xml:

      分布式系统架构实战demo:SSM+Dubbo_第8张图片

jdbc.xml中加入读取配置文件-->数据源-->事务管理-->Spring声明式事务管理配置:

<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.propertiesvalue>
			list>
		property>
	bean>
		
	  
	<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>
	 
	<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.test..*ServiceImpl.*(..))" />
		<aop:advisor pointcut-ref="transactionPointcut" advice-ref="txAdvice" />
	aop:config>
beans>

        此处的Spring声明式事务管理配置是上一篇具体应用,有兴趣的可以深入理解下AOP: Spring AOP是什么?你都拿它做什么?

      2.4  创建spring-common-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">

       
	<context:component-scan base-package="com.test.core.service"/>

beans>

      2.5  配置Mybatis

在config下创建mybatis.xml-->加入sqlSessionFactory 工厂-->接口的Mapper自动扫描方式:

<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.mybatis.spring.SqlSessionFactoryBean">
		
		<property name="dataSource" ref="dataSource"/>
		
		<property name="configLocation" value="classpath:mybatis-config.xml"/>
	bean>
	
	
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		
		<property name="basePackage" value="com.test.core.dao"/>
	bean>
	
beans>


      2.6 配置mybatis-config.xml

在src/main/resources下创建,并加入如下配置:

xml version="1.0" encoding="UTF-8" ?>


<configuration>
	
	<typeAliases>
		<package name="com.test.core.bean"/>
	typeAliases>
configuration>

接着创建mapper目录如下:

                 分布式系统架构实战demo:SSM+Dubbo_第9张图片

      2.7  测试spring+mybatis.

    创建JavaBean的TestBook.java:

package com.test.core.bean;

import java.io.Serializable;

public class TestBook implements Serializable{
	private static final long serialVersionUID = 1L;
	String bookId;
    String bookName;
    Integer number;
	public String getBookId() {
		return bookId;
	}
	public void setBookId(String bookId) {
		this.bookId = bookId;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public Integer getNumber() {
		return number;
	}
	public void setNumber(Integer number) {
		this.number = number;
	}
	@Override
	public String toString() {
		return "TestBean [bookId=" + bookId + ", bookName=" + bookName + ", number=" + number + "]";
	}
}

   Dao接口的TestBookDao:

package com.test.core.dao;

import com.test.core.bean.TestBook;

public interface TestBookDao {

	public TestBook findById(String id);
}

    Mapper文件:创建包-->加入TestBookDao.xml文件:

xml version="1.0" encoding="UTF-8" ?>  



<mapper namespace="com.test.core.dao.TestBookDao">

	
	<select id="findById" resultType="TestBook" parameterType="java.lang.String">
		select book_id bookId,book_name bookName,number from book where book_id= #{bookId}
	select>
	
mapper>

测试:

package com.test.core.testDemo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.test.core.bean.TestBook;
import com.test.core.dao.TestBookDao;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-context.xml"})
public class TestSpringMybatis {
	
	@Autowired
	TestBookDao testBookDao;
	@Test
	public void testFind(){
		TestBook findById = testBookDao.findById("1000");
		System.out.println(findById.toString());
	}
}

测试结果:

              分布式系统架构实战demo:SSM+Dubbo_第10张图片

3.整合springmvc

     整合SpringMVC一般放在controller层。

      3.1  配置前端控制器DispatcherServlet

在子项目controller中找到web.xml文件,加入:

xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
		
	<servlet>
		<servlet-name>controllerservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		<init-param>
			
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:springmvc-controller.xmlparam-value>
		init-param>
		<load-on-startup>1load-on-startup>
	servlet>
	
	<servlet-mapping>
		
		 <servlet-name>controllerservlet-name>
		 <url-pattern>/url-pattern>
	servlet-mapping>
web-app>

      3.2  配置扫描包,前端控制器,处理器映射器,处理器适配器,视图解析器

在classpath的SpringMVC-controller.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">
		
		
	<context:component-scan base-package="com.test.core.controller" />
	
	<mvc:annotation-driven/>
	
	
    <mvc:resources location="/WEB-INF/static/" mapping="/static/**" />
    
	
	<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/controller/"/>
		<property name="suffix" value=".jsp"/>
	bean>
	
beans>

      3.3 加入Log日志打印

在controller和service子项目的src/main/resources中创建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

      3.4  测试SpringMVC

创建Tomcat:

       分布式系统架构实战demo:SSM+Dubbo_第11张图片

配置service和controller的Tomcat:

      分布式系统架构实战demo:SSM+Dubbo_第12张图片

        注意两个Tomcat端口号不要重复,且启动时间设置长点:

          分布式系统架构实战demo:SSM+Dubbo_第13张图片

创建测试的controller和jsp:

package com.test.core.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestBookController {

	@RequestMapping(value="/index.do")
	public String index(){
		return "index";
	}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
	hello springmvc
body>
html>

在这里只用启动controller的Tomcat,并在浏览器中输入:http://localhost:8080/index.do

                           

4.整合Dubbo

                 分布式系统架构实战demo:SSM+Dubbo_第14张图片

     搭建Zookeeper本文不在赘述,为了方便,Dubbo将设置成直接从本地查找服务.

      4.1搭建Dubbo服务提供方

在test-service的config文件夹下创建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:application name="test-service"/>
	


     
	<dubbo:registry address="N/A"/>
	
	<dubbo:protocol port="20880" name="dubbo"/>
	
	<dubbo:service interface="com.test.core.service.TestBookService" ref="testBookService"/>

    <bean id="testBookService" class="com.test.core.service.TestBookServiceImpl" />
beans>

                    分布式系统架构实战demo:SSM+Dubbo_第15张图片

     Service的TestBookService接口(在common工程里创建)和TestBookServiceImpl实现类(这里只写实现类):

package com.test.core.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.test.core.bean.TestBook;
import com.test.core.dao.TestBookDao;

@Service("testBookService")
public class TestBookServiceImpl implements TestBookService{

	@Autowired
	TestBookDao testBookDao;
	
	public TestBook findById(String id) {
		return testBookDao.findById(id);
	}

}

      4.2  搭建服务消费方

在test-service项目的springmvc-controller.xml中加入:

<import resource="dubbo-consumer.xml"/>

src/main/resources下创建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">
		
	
	
	
	<dubbo:application name="test-controller"/>
	<dubbo:registry address="N/A" check="false"/>
	<dubbo:reference interface="com.test.core.service.TestBookService" id="testBookService"
		url="dubbo://127.0.0.1:20880" 
	/>
	
	<dubbo:consumer timeout="600000" check="false"/>
beans>

controller内容如下:

     分布式系统架构实战demo:SSM+Dubbo_第16张图片

      4.3 测试完整项目

在testDubbo.jsp的body中加入:${ requestScope.book}-->启动service的Tomcat-->启动controller的tomcat(这个必须在service启动之后才能启动,注意端口号不能重复)-->浏览器中输入http://localhost:8080/testDubbo.do?id=1000

              

5.项目中可能会遇到的问题:

      5.1  Spring注解@service无法取别名

分布式系统架构实战demo:SSM+Dubbo_第17张图片

      5.2  启动Tomcat时: cannot resolve reference to bean等找不到类的报错

      在parent上右键-->Run as-->Maven clean

      在parent上右键-->Maven-->Update Project

      5.3  Dubbo的xml文件里有红叉

分布式系统架构实战demo:SSM+Dubbo_第18张图片

ref:使用dubbo时applicationContext.xml报错解决办法

      5.4   jsp文件头报错

分布式系统架构实战demo:SSM+Dubbo_第19张图片

ref:新建的web项目的jsp页面报错的解决方法

你可能感兴趣的:(分布式与集群,Java学习)