一个简单的SSM+dubbo分布式应用Demo

1. 概述

        本文指导在eclipse下快速搭建一个SSM+dubbo分布式应用Demo,作为后续分布式技术学习基础。

        主要两个工程,web层为dubbo_web,control和dao层为dubbo_service

        dubbo_web工程github地址: https://github.com/WeedOutWorld/dubboDemo_web/tree/dev

        dubbo_service工程github地址: https://github.com/WeedOutWorld/dubboDemo_service/tree/dev



2. 环境准备

        eclispe、mysql、maven、jdk1.7

        eclipse maven插件安装:  https://www.cnblogs.com/cynchanpin/p/6848452.html


3. dubboDemo_web项目实现

        3.1 从git上导入项目dubboDemo_web至eclipse

            


        3.2 配置pom.xml (采用jetty-maven插件作为服务器

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.ych</groupId>
  <artifactId>dubboDemo_web</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>dubboDemo_web Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <dependencies>

		<!-- jetty -->
		<dependency>
			<groupId>org.eclipse.jetty.aggregate</groupId>
			<artifactId>jetty-webapp</artifactId>
			<version>7.6.14.v20131031</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-jsp</artifactId>
			<version>7.6.14.v20131031</version>
			<scope>test</scope>
		</dependency>
		<!-- TEST end -->
		
		<!--common start -->
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>18.0</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.7.5</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.5</version>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.3.2</version>
		</dependency>

		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>

		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.1</version>
		</dependency>

		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>1.8.5</version>
		</dependency>

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

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
			<version>4.2.0.Final</version>
		</dependency>

		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5</version>
		</dependency>

		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.7</version>
		</dependency>
		<!--common end -->

		<!--active mq start-->
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-core</artifactId>
			<version>5.7.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
			<version>5.13.3</version>
		</dependency>
		<!--active mq end-->

		<!--dubbo start -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
			<exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>   
                    <groupId>org.springframework</groupId>  
                </exclusion>
                <!-- <exclusion>
                    <artifactId>javassist</artifactId>   
                    <groupId>org.javassist</groupId>  
                </exclusion>
                <exclusion>
                    <artifactId>netty</artifactId>   
                    <groupId>io.netty</groupId>  
                </exclusion> -->
            </exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
		</dependency>
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.8</version>
		</dependency>
		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.18.1-GA</version>
		</dependency>
		<!--dubbo end -->

		<!--spring start -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.apache.xbean</groupId>
			<artifactId>xbean-spring</artifactId>
			<version>4.2</version>
		</dependency>
		<!--spring end -->

		<!--springmvc 控制层 start -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>

		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.7.4</version>
		</dependency>
		<!--springmvc end -->

		<!--mybatis 数据访问层 start -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.38</version>
			<scope>runtime</scope>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.20</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.0</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.0</version>
		</dependency>

		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>1.6.4.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>com.dyuproject.protostuff</groupId>
			<artifactId>protostuff-core</artifactId>
			<version>1.0.8</version>
		</dependency>

		<dependency>
			<groupId>com.dyuproject.protostuff</groupId>
			<artifactId>protostuff-runtime</artifactId>
			<version>1.0.8</version>
		</dependency>
	</dependencies>
  
  
  <build>
    <finalName>dubboDemo_web</finalName>
    
    <plugins>
			<!-- compiler插件, 设定JDK版本 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<showWarnings>true</showWarnings>
				</configuration>
			</plugin>

			<!-- war打包插件, 设定war包名称不带版本号 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<warName>${project.artifactId}</warName>
				</configuration>
			</plugin>

			<!-- jetty插件  -->
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>7.6.14.v20131031</version>
				<configuration>
					<systemProperties>
						<systemProperty>
							<name>spring.profiles.active</name>
							<value>development</value>
						</systemProperty>
					</systemProperties>
					<useTestClasspath>true</useTestClasspath>

					<webAppConfig>
						<contextPath>/${project.artifactId}</contextPath>
					</webAppConfig>
		            <connectors>
		                <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
		                    <port>8082</port>
		                </connector>
		                <!--  <connector implementation="org.eclipse.jetty.server.ssl.SslSelectChannelConnector">
	                        <port>9443</port>
	                        <password>changeit</password>
	                    </connector> -->
		            </connectors>
				</configuration>
			</plugin>

			<!-- resources插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.6</version>
			</plugin>

			<!-- install插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-install-plugin</artifactId>
				<version>2.5.1</version>
			</plugin>

			<!-- ant插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<version>1.7</version>
			</plugin>
		</plugins>  
  </build>
</project>

        3.3 配置web.xml

            配置监听以及mvc的servlet

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

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <welcome-file-list>
  	<welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
	<!-- 监听 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-all.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!--配置DispatcherServlet -->
	<servlet>
		<servlet-name>spring-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/spring-web.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	
  
</web-app>


        3.4 配置src/main/resources

            在spring-all.xml中配置所有需要经由spring加载的配置文件(现在各类组件基本上都经由spring加载

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	
    <!--作为消费者导入远程接口-->
    <import resource="classpath:dubbo/dubbo-consumer.xml"/>
    <!--springmvc-->
    <import resource="classpath:spring/spring-web.xml"/>
</beans>

    dubbo-consumer.xml文件,默认以zookeeper为注册中心,如果采用直连,将dubbo:registry与dubbo:reference注释进行替换

<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	   http://code.alibabatech.com/schema/dubbo
	   http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


	<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
	<dubbo:application name="dubboDemo_web"  owner="ych"/>

	<!-- 使用zookeeper注册中心获取服务地址 -->
	<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
	<!-- <dubbo:registry address="N/A" /> -->
	
	<!-- 用户服务接口 -->
	<dubbo:reference interface="org.ych.dubboDemo.core.BooksService" id="booksService"/>
	<!-- 
	<dubbo:reference interface="org.ych.dubboDemo.core.BooksService" id="booksService" url="dubbo://127.0.0.1:20880"/>
	 -->
</beans>  

spring-web.xml文件,mvc配置

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven />

    <mvc:default-servlet-handler/>

	<!-- 扫描相关注解包 -->
    <context:component-scan base-package="org.ych.dubboDemo"/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>


</beans>

        3.5 配置src/main/java

            dubbo服务接口BooksService.java,注入任意需要的类中使用
,        


4. dubboDemo_service项目实现

        4.1 从git上导入项目dubboDemo_web至eclipse

            

        4.2 配置pom.xml

            同上

        4.3 配置web.xml

            同上,只需要配置监听即可

        4.4 配置src/main/resources

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <import resource="classpath:dubbo/dubbo-provider.xml"/>
    
    <import resource="classpath:spring/spring-dao.xml"/>
	
	<!-- 扫描beans包 -->
    <context:component-scan base-package="org.ych.dubboDemo"/>
    
</beans>

            dubbo-provider.xml文件,默认以zookeeper为配置中心,如果希望直连,取消注释覆盖dubbo:registry

<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	   http://code.alibabatech.com/schema/dubbo
	   http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="dubboDemo_service" owner="ych"/>

	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
	<!-- <dubbo:registry  address="N/A" /> -->

	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />

	<!-- 当ProtocolConfig和ServiceConfig某属性没有配置时,采用此缺省值 -->
	<dubbo:provider timeout="10000" threadpool="fixed" threads="100" accepts="1000" />

	<!-- 暴露服务接口 一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心-->
	<dubbo:service interface="org.ych.dubboDemo.core.BooksService" ref="booksService" />

    <!--具体实现该接口的 bean-->
    <!-- <bean id="booksService" class="org.ych.techDemo_service.service.impl.BooksServiceImpl"/>
 -->
</beans>  

        4.5 配置src/main/java

           编写dubbo服务接口实现类BooksServiceImpl.java


注意:如果未使用数据库,注释掉如下两个地方即可




展开阅读全文

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